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; }
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(); }
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 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 void addSystemProperties(final File file) { if (!file.exists()) { return; } final Properties systemProperties; try { systemProperties = IO.readProperties(file); } catch (IOException e) { return; } system.getProperties().putAll(systemProperties); }
public static void addAdditionalLibraries() throws IOException { final File conf = SystemInstance.get().getConf(ADDITIONAL_LIB_CONFIG); if (conf == null || !conf.exists()) { return; } final Properties additionalLibProperties = IO.readProperties(conf); final List<String> libToCopy = new ArrayList<String>(); final String toCopy = additionalLibProperties.getProperty(JAR_KEY); if (toCopy != null) { for (final String lib : toCopy.split(",")) { libToCopy.add(realLocation(lib.trim())); } } final String toExtract = additionalLibProperties.getProperty(ZIP_KEY); if (toExtract != null) { for (final String zip : toExtract.split(",")) { libToCopy.addAll(extract(realLocation(zip))); } } final File destination; if (additionalLibProperties.containsKey(DESTINATION_KEY)) { destination = new File(additionalLibProperties.getProperty(DESTINATION_KEY)); } else { destination = new File(SystemInstance.get().getBase().getDirectory(), Embedder.ADDITIONAL_LIB_FOLDER); } if (!destination.exists()) { Files.mkdirs(destination); } for (final String lib : libToCopy) { copy(new File(lib), destination); } }
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(); }
@Test public void run() throws Exception { assertTrue( IO.slurp(new File(catalinaBase, "conf/server.xml")) .contains("<Connector port=\"" + tomeeHttpPort + "\"")); }