Beispiel #1
0
 public static void writeString(final File file, final String string) throws IOException {
   final FileWriter out = new FileWriter(file);
   try {
     final BufferedWriter bufferedWriter = new BufferedWriter(out);
     try {
       bufferedWriter.write(string);
       bufferedWriter.newLine();
     } finally {
       close(bufferedWriter);
     }
   } finally {
     close(out);
   }
 }
Beispiel #2
0
  private static String fallback(final String rawLocation) {
    if (rawLocation.startsWith(MVN_PREFIX)) {
      try {
        final String repo1Url =
            quickMvnUrl(rawLocation.substring(MVN_PREFIX.length()).replace(":", "/"));
        return realLocation(repo1Url);
      } catch (MalformedURLException e1) {
        Logger.getLogger(ProvisioningUtil.class.getName()).severe("Can't find " + rawLocation);
      }
    } else { // try url
      try {
        final File file = cacheFile(lastPart(rawLocation));
        final URL url = new URL(rawLocation);
        InputStream is = null;
        try {
          is = new BufferedInputStream(url.openStream());
          IO.copy(is, file);
          return file.getAbsolutePath();
        } finally {
          IO.close(is);
        }
      } catch (final Exception e1) {
        // no-op
      }
    }

    // if it was not an url that's just a file path
    return rawLocation;
  }
Beispiel #3
0
  public static String readFileAsString(final URI uri) throws IOException {
    final StringBuilder builder = new StringBuilder("");
    for (final Proxy proxy : ProxySelector.getDefault().select(uri)) {
      final InputStream is;

      try {
        final URLConnection urlConnection = uri.toURL().openConnection(proxy);
        urlConnection.setConnectTimeout(MAX_TIMEOUT);
        is = urlConnection.getInputStream();
      } catch (IOException e) {
        continue;
      }

      String line;
      try {
        final BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        while ((line = reader.readLine()) != null) {
          builder.append(line);
        }
      } finally {
        close(is);
      }
    }

    return builder.toString();
  }
Beispiel #4
0
 public static void copy(final InputStream from, final File to) throws IOException {
   final OutputStream write = write(to);
   try {
     copy(from, write);
   } finally {
     close(write);
   }
 }
Beispiel #5
0
 public static void copy(final URL from, final OutputStream to) throws IOException {
   final InputStream read = read(from);
   try {
     copy(read, to);
   } finally {
     close(read);
   }
 }
Beispiel #6
0
 public static String readString(final File file) throws IOException {
   final FileReader in = new FileReader(file);
   try {
     final BufferedReader reader = new BufferedReader(in);
     return reader.readLine();
   } finally {
     close(in);
   }
 }
Beispiel #7
0
 public static String readString(final URL url) throws IOException {
   final InputStream in = url.openStream();
   try {
     final BufferedReader reader = new BufferedReader(new InputStreamReader(in));
     return reader.readLine();
   } finally {
     close(in);
   }
 }
Beispiel #8
0
 /**
  * Reads and closes the input stream
  *
  * @param in InputStream
  * @param properties Properties
  * @return Properties
  * @throws IOException
  */
 public static Properties readProperties(final InputStream in, final Properties properties)
     throws IOException {
   if (in == null) throw new NullPointerException("InputStream is null");
   if (properties == null) throw new NullPointerException("Properties is null");
   try {
     properties.load(in);
   } finally {
     close(in);
   }
   return properties;
 }
Beispiel #9
0
 public static void copy(final File from, final File to) throws IOException {
   if (!from.isDirectory()) {
     final FileOutputStream fos = new FileOutputStream(to);
     try {
       copy(from, fos);
     } finally {
       close(fos);
     }
   } else {
     copyDirectory(from, to);
   }
 }
Beispiel #10
0
  public static String copyTryingProxies(final URI source, final File destination)
      throws Exception {
    final InputStream is = inputStreamTryingProxies(source);
    if (is == null) {
      return null;
    }

    try {
      IO.copy(is, destination);
    } finally {
      IO.close(is);
    }
    return destination.getAbsolutePath();
  }
Beispiel #11
0
  public static String hash(final Set<URL> urls, final String algo) {
    final Collection<File> files = new ArrayList<File>();

    for (final URL u : urls) {
      final File file = toFile(u);
      if (!file.isDirectory()) {
        files.add(file);
      } else {
        files.addAll(Files.collect(file, TrueFilter.INSTANCE));
      }
    }

    MessageDigest digest = DIGESTS.get(algo);
    if (digest == null) {
      try {
        digest = MessageDigest.getInstance(algo);
      } catch (final NoSuchAlgorithmException e) {
        throw new LoaderRuntimeException(e);
      }
      DIGESTS.put(algo, digest);
    }

    for (final File file : files) {
      if (!file.exists()) {
        continue;
      }

      DigestInputStream is = null;
      try {
        is = new DigestInputStream(new FileInputStream(file), digest);
        IO.copy(is, new NoopOutputStream()); // read the stream
      } catch (final IOException e) {
        // no-op: shouldn't occur here
      } finally {
        IO.close(is);
      }
    }

    final byte[] hash = digest.digest();
    digest.reset();

    final StringBuilder sb = new StringBuilder("");
    for (final byte b : hash) { // hex convertion
      sb.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
    }

    return sb.toString();
  }
Beispiel #12
0
  private static String mvnArtifactPath(final String toParse, final String snapshotBase)
      throws MalformedURLException {
    final StringBuilder builder = new StringBuilder();
    final String[] segments = toParse.split("/");
    if (segments.length < 3) {
      throw new MalformedURLException("Invalid path. " + toParse);
    }

    final String group = segments[0];
    if (group.trim().isEmpty()) {
      throw new MalformedURLException("Invalid groupId. " + toParse);
    }
    builder.append(group.replace('.', '/')).append("/");

    final String artifact = segments[1];
    if (artifact.trim().isEmpty()) {
      throw new MalformedURLException("Invalid artifactId. " + toParse);
    }
    builder.append(artifact).append("/");

    final String version = segments[2];
    if (version.trim().isEmpty()) {
      throw new MalformedURLException("Invalid artifactId. " + toParse);
    }

    builder.append(version).append("/");

    String artifactVersion;
    if (snapshotBase != null
        && snapshotBase.startsWith(HTTP_PREFIX)
        && version.endsWith(SNAPSHOT_SUFFIX)) {
      final String meta =
          new StringBuilder(snapshotBase)
              .append(builder.toString())
              .append("maven-metadata.xml")
              .toString();
      final URL url = new URL(meta);
      final ByteArrayOutputStream out = new ByteArrayOutputStream();
      InputStream is = null;
      try {
        is = inputStreamTryingProxies(url.toURI());
        IO.copy(is, out);
        artifactVersion =
            extractLastSnapshotVersion(version, new ByteArrayInputStream(out.toByteArray()));
      } catch (final Exception e) {
        artifactVersion = version;
      } finally {
        IO.close(is);
      }
    } else {
      artifactVersion = version;
    }

    String type = "jar";
    if (segments.length >= 4 && segments[3].trim().length() > 0) {
      type = segments[3];
    }

    String fullClassifier = null;
    if (segments.length >= 5 && segments[4].trim().length() > 0) {
      fullClassifier = "-" + segments[4];
    }

    builder.append(artifact).append("-").append(artifactVersion);

    if (fullClassifier != null) {
      builder.append(fullClassifier);
    }

    return builder.append(".").append(type).toString();
  }