private static void copy(final File file, final File lib) throws IOException {
   final File dest = new File(lib, file.getName());
   if (dest.exists()) {
     return;
   }
   IO.copy(file, dest);
 }
  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;
  }
Exemple #3
0
 private static void doCopyDirectory(
     final File srcDir, final File destDir, final List<String> exclusionList) throws IOException {
   final File[] files = srcDir.listFiles();
   if (files == null) { // null if security restricted
     throw new IOException("Failed to list contents of " + srcDir);
   }
   if (destDir.exists()) {
     if (!destDir.isDirectory()) {
       throw new IOException("Destination '" + destDir + "' exists but is not a directory");
     }
   } else {
     if (!destDir.mkdirs()) {
       throw new IOException("Destination '" + destDir + "' directory cannot be created");
     }
   }
   if (!destDir.canWrite()) {
     throw new IOException("Destination '" + destDir + "' cannot be written to");
   }
   for (final File file : files) {
     final File copiedFile = new File(destDir, file.getName());
     if (exclusionList == null || !exclusionList.contains(file.getCanonicalPath())) {
       if (file.isDirectory()) {
         doCopyDirectory(file, copiedFile, exclusionList);
       } else {
         copy(file, copiedFile);
       }
     }
   }
 }
Exemple #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);
   }
 }
Exemple #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);
   }
 }
Exemple #6
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);
   }
 }
  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();
  }
Exemple #8
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();
  }
  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();
  }
Exemple #10
0
 public static void copy(final byte[] from, final OutputStream to) throws IOException {
   copy(new ByteArrayInputStream(from), to);
 }
Exemple #11
0
 public static String slurp(final InputStream in) throws IOException {
   final ByteArrayOutputStream out = new ByteArrayOutputStream();
   copy(in, out);
   return new String(out.toByteArray());
 }