Search in sources :

Example 66 with JarInputStream

use of java.util.jar.JarInputStream in project bnd by bndtools.

the class JarTest method testNewLine.

public static void testNewLine() throws Exception {
    Jar jar = new Jar("dot");
    Manifest manifest = new Manifest();
    jar.setManifest(manifest);
    String value = "Test\nTest\nTest\nTest";
    String expectedValue = "Test Test Test Test";
    manifest.getMainAttributes().putValue(Constants.BUNDLE_DESCRIPTION, value);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    jar.write(bout);
    JarInputStream jin = new JarInputStream(new ByteArrayInputStream(bout.toByteArray()));
    Manifest m = jin.getManifest();
    assertNotNull(m);
    String parsedValue = m.getMainAttributes().getValue(Constants.BUNDLE_DESCRIPTION);
    assertEquals(expectedValue, parsedValue);
}
Also used : JarInputStream(java.util.jar.JarInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Jar(aQute.bnd.osgi.Jar) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Manifest(java.util.jar.Manifest)

Example 67 with JarInputStream

use of java.util.jar.JarInputStream in project bndtools by bndtools.

the class BndContainerSourceManager method getSourceBundle.

private static File getSourceBundle(IPath path, Map<String, String> props) {
    Workspace bndWorkspace;
    try {
        bndWorkspace = Central.getWorkspace();
        if (bndWorkspace == null) {
            return null;
        }
    } catch (final Exception e) {
        return null;
    }
    IPath bundlePath = path;
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IWorkspaceRoot root = workspace.getRoot();
    IResource resource = root.findMember(path);
    if (resource != null) {
        bundlePath = resource.getLocation();
    }
    try (JarInputStream jarStream = new JarInputStream(IO.stream(bundlePath.toFile()), false)) {
        Manifest manifest = jarStream.getManifest();
        if (manifest == null) {
            return null;
        }
        Domain domain = Domain.domain(manifest);
        Entry<String, Attrs> bsnAttrs = domain.getBundleSymbolicName();
        if (bsnAttrs == null) {
            return null;
        }
        String bsn = bsnAttrs.getKey();
        String version = domain.getBundleVersion();
        if (version == null) {
            version = props.get("version");
        }
        for (RepositoryPlugin repo : RepositoryUtils.listRepositories(true)) {
            if (repo == null) {
                continue;
            }
            if (repo instanceof WorkspaceRepository) {
                continue;
            }
            File sourceBundle = repo.get(bsn + ".source", new Version(version), props);
            if (sourceBundle != null) {
                return sourceBundle;
            }
        }
    } catch (final Exception e) {
    // Ignore, something went wrong, or we could not find the source bundle
    }
    return null;
}
Also used : IPath(org.eclipse.core.runtime.IPath) JarInputStream(java.util.jar.JarInputStream) Attrs(aQute.bnd.header.Attrs) RepositoryPlugin(aQute.bnd.service.RepositoryPlugin) Manifest(java.util.jar.Manifest) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) Version(aQute.bnd.version.Version) WorkspaceRepository(aQute.bnd.build.WorkspaceRepository) IWorkspace(org.eclipse.core.resources.IWorkspace) Domain(aQute.bnd.osgi.Domain) File(java.io.File) IResource(org.eclipse.core.resources.IResource) IWorkspace(org.eclipse.core.resources.IWorkspace) Workspace(aQute.bnd.build.Workspace)

Example 68 with JarInputStream

use of java.util.jar.JarInputStream in project geode by apache.

the class DeployedJar method registerFunctions.

/**
   * Scan the JAR file and attempt to register any function classes found.
   */
public synchronized void registerFunctions() throws ClassNotFoundException {
    final boolean isDebugEnabled = logger.isDebugEnabled();
    if (isDebugEnabled) {
        logger.debug("Registering functions with DeployedJar: {}", this);
    }
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(this.getJarContent());
    JarInputStream jarInputStream = null;
    try {
        List<String> functionClasses = findFunctionsInThisJar();
        jarInputStream = new JarInputStream(byteArrayInputStream);
        JarEntry jarEntry = jarInputStream.getNextJarEntry();
        while (jarEntry != null) {
            if (jarEntry.getName().endsWith(".class")) {
                final String className = PATTERN_SLASH.matcher(jarEntry.getName()).replaceAll("\\.").substring(0, jarEntry.getName().length() - 6);
                if (functionClasses.contains(className)) {
                    if (isDebugEnabled) {
                        logger.debug("Attempting to load class: {}, from JAR file: {}", jarEntry.getName(), this.file.getAbsolutePath());
                    }
                    try {
                        Class<?> clazz = ClassPathLoader.getLatest().forName(className);
                        Collection<Function> registerableFunctions = getRegisterableFunctionsFromClass(clazz);
                        for (Function function : registerableFunctions) {
                            FunctionService.registerFunction(function);
                            if (isDebugEnabled) {
                                logger.debug("Registering function class: {}, from JAR file: {}", className, this.file.getAbsolutePath());
                            }
                            this.registeredFunctions.add(function);
                        }
                    } catch (ClassNotFoundException | NoClassDefFoundError cnfex) {
                        logger.error("Unable to load all classes from JAR file: {}", this.file.getAbsolutePath(), cnfex);
                        throw cnfex;
                    }
                } else {
                    if (isDebugEnabled) {
                        logger.debug("No functions found in class: {}, from JAR file: {}", jarEntry.getName(), this.file.getAbsolutePath());
                    }
                }
            }
            jarEntry = jarInputStream.getNextJarEntry();
        }
    } catch (IOException ioex) {
        logger.error("Exception when trying to read class from ByteArrayInputStream", ioex);
    } finally {
        if (jarInputStream != null) {
            try {
                jarInputStream.close();
            } catch (IOException ioex) {
                logger.error("Exception attempting to close JAR input stream", ioex);
            }
        }
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) Function(org.apache.geode.cache.execute.Function) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 69 with JarInputStream

use of java.util.jar.JarInputStream in project geode by apache.

the class DeployedJar method hasValidJarContent.

/**
   * Peek into the JAR data and make sure that it is valid JAR content.
   * 
   * @param inputStream InputStream containing data to be validated.
   * @return True if the data has JAR content, false otherwise
   */
private static boolean hasValidJarContent(final InputStream inputStream) {
    JarInputStream jarInputStream = null;
    boolean valid = false;
    try {
        jarInputStream = new JarInputStream(inputStream);
        valid = jarInputStream.getNextJarEntry() != null;
    } catch (IOException ignore) {
    // Ignore this exception and just return false
    } finally {
        try {
            jarInputStream.close();
        } catch (IOException ignored) {
        // Ignore this exception and just return result
        }
    }
    return valid;
}
Also used : JarInputStream(java.util.jar.JarInputStream) IOException(java.io.IOException)

Example 70 with JarInputStream

use of java.util.jar.JarInputStream in project processdash by dtuma.

the class CustomProcessPublisher method openStartingJar.

private JarInputStream openStartingJar(String scriptStartingJar) throws IOException {
    if (scriptStartingJar == null || scriptStartingJar.length() == 0)
        return null;
    byte[] contents = getRawFileBytes(scriptStartingJar);
    if (contents == null)
        return null;
    ByteArrayInputStream bytesIn = new ByteArrayInputStream(contents);
    return new JarInputStream(bytesIn);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) JarInputStream(java.util.jar.JarInputStream)

Aggregations

JarInputStream (java.util.jar.JarInputStream)185 JarEntry (java.util.jar.JarEntry)82 IOException (java.io.IOException)73 FileInputStream (java.io.FileInputStream)66 Manifest (java.util.jar.Manifest)56 File (java.io.File)48 InputStream (java.io.InputStream)45 ZipEntry (java.util.zip.ZipEntry)34 JarOutputStream (java.util.jar.JarOutputStream)29 Test (org.junit.Test)29 FileOutputStream (java.io.FileOutputStream)26 ByteArrayInputStream (java.io.ByteArrayInputStream)24 URL (java.net.URL)20 ArrayList (java.util.ArrayList)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 OutputStream (java.io.OutputStream)14 JarFile (java.util.jar.JarFile)14 BufferedInputStream (java.io.BufferedInputStream)11 Attributes (java.util.jar.Attributes)11 HashSet (java.util.HashSet)9