private static String getManifestKey(JarFile jarFile, String key) { try { Manifest manifest = jarFile.getManifest(); if (manifest != null) { Attributes attributes = manifest.getMainAttributes(); return attributes.getValue(key); } } catch (IOException e) { LOG.error("Exception while trying to read key {} from manifest of {}", key, jarFile.getName(), e); } return null; }
private static Optional<String> getPropertiesPath(final JarFile jarFile) throws IOException { // Lookup the resource path in the JAR manifest. final String value = jarFile.getManifest().getMainAttributes().getValue(MANIFEST_ATTRIBUTE); if (isNullOrEmpty(value)) { LOG.debug("No value found for attribute <{}> in JAR manifest of file <{}>", MANIFEST_ATTRIBUTE, jarFile.getName()); return Optional.empty(); } else { return Optional.of(value + "/" + GRAYLOG_PLUGIN_PROPERTIES); } }
protected void findResourcesInJarFiles(List<URL> result, String resource) throws MalformedURLException { for (JarFile jarFile : jarFiles) { JarEntry entry = jarFile.getJarEntry(resource); if (entry != null) { result.add(new URL("jar", "", String.format("file:%s!%s", jarFile.getName(), resource))); } } }
private boolean isResourcesJar(JarFile jar) throws IOException { try { return jar.getName().endsWith(".jar") && (jar.getJarEntry("META-INF/resources") != null); } finally { jar.close(); } }
private File getRootJarFile(JarFile jarFile) { String name = jarFile.getName(); int separator = name.indexOf("!/"); if (separator > 0) { name = name.substring(0, separator); } return new File(name); }
public InputStream openClassfile(String classname) throws NotFoundException { try { String jarname = classname.replace('.', '/') + ".class"; JarEntry je = jarfile.getJarEntry(jarname); if (je != null) return jarfile.getInputStream(je); else return null; // not found } catch (IOException e) {} throw new NotFoundException("broken jar file?: " + jarfile.getName()); }
/** * Unload any JAR files that have been cached by this plugin */ public void unloadJarFiles() { for (JarURLConnection url : cachedJarFiles) { try { Log.info("Unloading plugin JAR file " + url.getJarFile().getName()); url.getJarFile().close(); } catch (Exception e) { Log.error("Failed to unload JAR file", e); } } }
public InputStream openClassfile(String classname) throws NotFoundException { try { String jarname = classname.replace('.', '/') + ".class"; JarEntry je = jarfile.getJarEntry(jarname); if (je != null) return jarfile.getInputStream(je); else return null; // not found } catch (IOException e) {} throw new NotFoundException("broken jar file?: " + jarfile.getName()); }
@SuppressWarnings("ResultOfMethodCallIgnored") private void copyFromJarToLocation(final JarFile jarFile, final JarEntry entry, final String fromRoot, final File toRoot) throws IOException { String destinationName = entry.getName().replaceFirst(fromRoot, ""); File newFile = new File(toRoot, destinationName); log.debug("Copying resource {} from JAR file {}", fromRoot, jarFile.getName()); if (!entry.isDirectory()) { // Create parent directories Path parent = newFile.getAbsoluteFile().toPath().getParent(); parent.toFile().mkdirs(); newFile.deleteOnExit(); try (InputStream is = jarFile.getInputStream(entry)) { Files.copy(is, newFile.toPath()); } catch (IOException e) { log.error("Failed to extract classpath resource " + entry.getName() + " from JAR file " + jarFile.getName(), e); throw e; } } }
@Override protected void scanJarFile(ClassLoader loader, JarFile file) throws IOException { this.found = new File(file.getName()); throw new StopScanningException(); }
/** * @param errs Errors collection. * @param rsrcPath Path. * @return {@code True} if library was found and loaded. */ private static boolean extractAndLoad(Collection<Throwable> errs, JarFile jar, File tmpDir, String rsrcPath) { ZipEntry rsrc = jar.getEntry(rsrcPath); if (rsrc != null) return extract(errs, rsrc, jar, new File(tmpDir, mapLibraryName(LIB_NAME))); else { errs.add(new IllegalStateException("Failed to find resource within specified jar file " + "[rsrc=" + rsrcPath + ", jar=" + jar.getName() + ']')); return false; } }
File getCodeSourceArchive(CodeSource codeSource) { try { URL location = (codeSource != null) ? codeSource.getLocation() : null; if (location == null) { return null; } String path; URLConnection connection = location.openConnection(); if (connection instanceof JarURLConnection) { path = ((JarURLConnection) connection).getJarFile().getName(); } else { path = location.toURI().getPath(); } int index = path.indexOf("!/"); if (index != -1) { path = path.substring(0, index); } return new File(path); } catch (Exception ex) { return null; } }
/** * Retrieves the specified POM from a jar. * * @param path the path to the pom.xml file within the jar file * @param jar the jar file to extract the pom from * @return returns the POM file * @throws AnalysisException is thrown if there is an exception extracting * the file */ private File extractPom(String path, JarFile jar) throws AnalysisException { final File tmpDir = getNextTempDirectory(); final File file = new File(tmpDir, "pom.xml"); final ZipEntry entry = jar.getEntry(path); if (entry == null) { throw new AnalysisException(String.format("Pom (%s) does not exist in %s", path, jar.getName())); } try (InputStream input = jar.getInputStream(entry); FileOutputStream fos = new FileOutputStream(file)) { IOUtils.copy(input, fos); } catch (IOException ex) { LOGGER.warn("An error occurred reading '{}' from '{}'.", path, jar.getName()); LOGGER.error("", ex); } return file; }
model = parser.parse(jar.getInputStream(entry)); if (model == null) { throw new AnalysisException(String.format("Unable to parse pom '%s/%s'", jar.getName(), path)); LOGGER.warn("Unable to parse pom '{}' in jar '{}'; invalid signature", path, jar.getName()); LOGGER.debug("", ex); throw new AnalysisException(ex); } catch (IOException ex) { LOGGER.warn("Unable to parse pom '{}' in jar '{}' (IO Exception)", path, jar.getName()); LOGGER.debug("", ex); throw new AnalysisException(ex); } catch (Throwable ex) { LOGGER.warn("Unexpected error during parsing of the pom '{}' in jar '{}'", path, jar.getName()); LOGGER.debug("", ex); throw new AnalysisException(ex);
private InputStream getTemporaryInputStream(JarFile jf, JarEntry je, boolean forceRecreate) throws IOException { String filePath = jf.getName(); String entryPath = je.getName(); StringBuffer jarCacheFolder = new StringBuffer("jarfscache"); //NOI18N
@Override public Binding doBind(Binder binder) { final InputStream stream; try { stream = jarFile.getInputStream( jarFileEntry ); } catch (IOException e) { throw new MappingException( String.format( "Unable to open InputStream for jar file entry [%s : %s]", jarFile.getName(), jarFileEntry.getName() ), e, getOrigin() ); } return InputStreamXmlSource.doBind( binder, stream, getOrigin(), true ); } }
private ExtractedJarEntry extractJarEntry(URL url) { try { JarURLConnection jarUrlConnection = (JarURLConnection) url.openConnection(); JarFile jarFile = jarUrlConnection.getJarFile(); JarEntry jarEntry = jarUrlConnection.getJarEntry(); if (jarEntry.isDirectory()) { return new ExtractedJarEntry(jarEntry.getName()); // a directory } Instant lastModified = getLastModified(jarFile.getName()); // Extract JAR entry to file try (InputStream is = jarFile.getInputStream(jarEntry)) { Path tempFile = Files.createTempFile("ws", ".je"); Files.copy(is, tempFile, StandardCopyOption.REPLACE_EXISTING); return new ExtractedJarEntry(tempFile, lastModified, jarEntry.getName()); } finally { if (!jarUrlConnection.getUseCaches()) { jarFile.close(); } } } catch (IOException ioe) { throw new HttpException("Cannot load JAR file!", Http.Status.INTERNAL_SERVER_ERROR_500, ioe); } }
String.format( "Unable to access stream from jar file [%s] for entry [%s]", jarFile.getName(), zipEntry.getName()
@Test void checkClassFromExternalJar() { final JarFile actual = ClassUtil.jarFileOf(StringUtils.class); // asserts assertNotNull(actual); assertTrue(actual.getName().contains("junit-platform-commons")); }
@Test @DisabledOnJava(value = 9, description = "rt.jar does not exists in Java 9 anymore") void checkWithClassFromJRE() { final JarFile actual = ClassUtil.jarFileOf(Object.class); // asserts assertNotNull(actual); assertTrue(actual.getName().contains("rt.jar")); } }