use of java.util.jar.JarInputStream in project jersey by jersey.
the class OsgiRegistry method getPackageResources.
/**
* Get URLs of resources from a given package.
*
* @param packagePath package.
* @param classLoader resource class loader.
* @param recursive whether the given package path should be scanned recursively by OSGi
* @return URLs of the located resources.
*/
@SuppressWarnings("unchecked")
public Enumeration<URL> getPackageResources(final String packagePath, final ClassLoader classLoader, final boolean recursive) {
final List<URL> result = new LinkedList<URL>();
for (final Bundle bundle : bundleContext.getBundles()) {
// Look for resources at the given <packagePath> and at WEB-INF/classes/<packagePath> in case a WAR is being examined.
for (final String bundlePackagePath : new String[] { packagePath, WEB_INF_CLASSES + packagePath }) {
final Enumeration<URL> enumeration = findEntries(bundle, bundlePackagePath, "*.class", recursive);
if (enumeration != null) {
while (enumeration.hasMoreElements()) {
final URL url = enumeration.nextElement();
final String path = url.getPath();
classToBundleMapping.put(bundleEntryPathToClassName(packagePath, path), bundle);
result.add(url);
}
}
}
// Now interested only in .jar provided by current bundle.
final Enumeration<URL> jars = findEntries(bundle, "/", "*.jar", true);
if (jars != null) {
while (jars.hasMoreElements()) {
final URL jar = jars.nextElement();
final InputStream inputStream = classLoader.getResourceAsStream(jar.getPath());
if (inputStream == null) {
LOGGER.config(LocalizationMessages.OSGI_REGISTRY_ERROR_OPENING_RESOURCE_STREAM(jar));
continue;
}
final JarInputStream jarInputStream;
try {
jarInputStream = new JarInputStream(inputStream);
} catch (final IOException ex) {
LOGGER.log(Level.CONFIG, LocalizationMessages.OSGI_REGISTRY_ERROR_PROCESSING_RESOURCE_STREAM(jar), ex);
try {
inputStream.close();
} catch (final IOException e) {
// ignored
}
continue;
}
try {
JarEntry jarEntry;
while ((jarEntry = jarInputStream.getNextJarEntry()) != null) {
final String jarEntryName = jarEntry.getName();
final String jarEntryNameLeadingSlash = jarEntryName.startsWith("/") ? jarEntryName : "/" + jarEntryName;
if (jarEntryName.endsWith(".class") && // explicitly instructs us to do so somehow (not implemented)
jarEntryNameLeadingSlash.contains("/" + normalizedPackagePath(packagePath))) {
if (!recursive && !isPackageLevelEntry(packagePath, jarEntryName)) {
continue;
}
classToBundleMapping.put(jarEntryName.replace(".class", "").replace('/', '.'), bundle);
result.add(bundle.getResource(jarEntryName));
}
}
} catch (final Exception ex) {
LOGGER.log(Level.CONFIG, LocalizationMessages.OSGI_REGISTRY_ERROR_PROCESSING_RESOURCE_STREAM(jar), ex);
} finally {
try {
jarInputStream.close();
} catch (final IOException e) {
// ignored
}
}
}
}
}
return Collections.enumeration(result);
}
use of java.util.jar.JarInputStream in project liquibase by liquibase.
the class DefaultPackageScanClassResolver method loadImplementationsInJar.
/**
* Finds matching classes within a jar files that contains a folder
* structure matching the package structure. If the File is not a JarFile or
* does not exist a warning will be logged, but no error will be raised.
*
* Any nested JAR files found inside this JAR will be assumed to also be
* on the classpath and will be recursively examined for classes in `parentPackage`.
* @param parentPackage the parent package under which classes must be in order to
* be considered
* @param parentFileStream the inputstream of the jar file to be examined for classes
* @param loader a classloader which can load classes contained within the JAR file
* @param parentFileName a unique name for the parentFileStream, to be used for caching.
* This is the URL of the parentFileStream, if it comes from a URL,
* or a composite ID if we are currently examining a nested JAR.
*/
protected void loadImplementationsInJar(String parentPackage, InputStream parentFileStream, ClassLoader loader, String parentFileName, String grandparentFileName) throws IOException {
Set<String> classFiles = classFilesByLocation.get(parentFileName);
if (classFiles == null) {
classFiles = new HashSet<String>();
classFilesByLocation.put(parentFileName, classFiles);
Set<String> grandparentClassFiles = classFilesByLocation.get(grandparentFileName);
if (grandparentClassFiles == null) {
grandparentClassFiles = new HashSet<String>();
classFilesByLocation.put(grandparentFileName, grandparentClassFiles);
}
JarInputStream jarStream;
if (parentFileStream instanceof JarInputStream) {
jarStream = (JarInputStream) parentFileStream;
} else {
jarStream = new JarInputStream(parentFileStream);
}
JarEntry entry;
while ((entry = jarStream.getNextJarEntry()) != null) {
String name = entry.getName();
if (name != null) {
if (name.endsWith(".jar")) {
//in a nested jar
log.debug("Found nested jar " + name);
// To avoid needing to unzip 'parentFile' in its entirety, as that
// may take a very long time (see CORE-2115) or not even be possible
// (see CORE-2595), we load the nested JAR from the classloader and
// read it as a zip.
//
// It is safe to assume that the nested JAR is readable by the classloader
// as a resource stream, because we have reached this point by scanning
// through packages located from `classloader` by using `getResource`.
// If loading this nested JAR as a resource fails, then certainly loading
// classes from inside it with `classloader` would fail and we are safe
// to exclude it form the PackageScan.
InputStream nestedJarResourceStream = loader.getResourceAsStream(name);
if (nestedJarResourceStream != null) {
JarInputStream nestedJarStream = new JarInputStream(nestedJarResourceStream);
try {
loadImplementationsInJar(parentPackage, nestedJarStream, loader, parentFileName + "!" + name, parentFileName);
} finally {
nestedJarStream.close();
}
}
} else if (!entry.isDirectory() && name.endsWith(".class")) {
classFiles.add(name.trim());
grandparentClassFiles.add(name.trim());
}
}
}
}
for (String name : classFiles) {
if (name.contains(parentPackage)) {
loadClass(name, loader);
}
}
}
use of java.util.jar.JarInputStream in project hadoop by apache.
the class TestJarFinder method testExistingManifest.
@Test
public void testExistingManifest() throws Exception {
File dir = GenericTestUtils.getTestDir(TestJarFinder.class.getName() + "-testExistingManifest");
delete(dir);
dir.mkdirs();
File metaInfDir = new File(dir, "META-INF");
metaInfDir.mkdirs();
File manifestFile = new File(metaInfDir, "MANIFEST.MF");
Manifest manifest = new Manifest();
OutputStream os = new FileOutputStream(manifestFile);
manifest.write(os);
os.close();
File propsFile = new File(dir, "props.properties");
Writer writer = new FileWriter(propsFile);
new Properties().store(writer, "");
writer.close();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JarOutputStream zos = new JarOutputStream(baos);
JarFinder.jarDir(dir, "", zos);
JarInputStream jis = new JarInputStream(new ByteArrayInputStream(baos.toByteArray()));
Assert.assertNotNull(jis.getManifest());
jis.close();
}
use of java.util.jar.JarInputStream in project hadoop by apache.
the class TestJarFinder method testNoManifest.
@Test
public void testNoManifest() throws Exception {
File dir = GenericTestUtils.getTestDir(TestJarFinder.class.getName() + "-testNoManifest");
delete(dir);
dir.mkdirs();
File propsFile = new File(dir, "props.properties");
Writer writer = new FileWriter(propsFile);
new Properties().store(writer, "");
writer.close();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JarOutputStream zos = new JarOutputStream(baos);
JarFinder.jarDir(dir, "", zos);
JarInputStream jis = new JarInputStream(new ByteArrayInputStream(baos.toByteArray()));
Assert.assertNotNull(jis.getManifest());
jis.close();
}
use of java.util.jar.JarInputStream in project flink by apache.
the class JarFileCreatorTest method validate.
private boolean validate(Set<String> expected, File out) throws IOException {
int count = expected.size();
try (JarInputStream jis = new JarInputStream(new FileInputStream(out))) {
ZipEntry ze;
while ((ze = jis.getNextEntry()) != null) {
count--;
expected.remove(ze.getName());
}
}
return count == 0 && expected.size() == 0;
}
Aggregations