Search in sources :

Example 56 with JarInputStream

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

the class BundlesInstaller method getBundleSymbolicName.

public String getBundleSymbolicName(File bundleFile) throws IOException {
    String name = null;
    final JarInputStream jis = new JarInputStream(new FileInputStream(bundleFile));
    try {
        final Manifest m = jis.getManifest();
        if (m == null) {
            fail("Manifest is null in " + bundleFile.getAbsolutePath());
        }
        name = m.getMainAttributes().getValue(Constants.BUNDLE_SYMBOLICNAME);
    } finally {
        jis.close();
    }
    return name;
}
Also used : JarInputStream(java.util.jar.JarInputStream) Manifest(java.util.jar.Manifest) FileInputStream(java.io.FileInputStream)

Example 57 with JarInputStream

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

the class WebAppContextDashboard method scanJarForFiles.

private List<String> scanJarForFiles(JarResource jarResource, String[] suffixes) {
    String jarUrl = jarResource.toString();
    int pos = jarUrl.indexOf("!/");
    if (pos == -1)
        return Collections.EMPTY_LIST;
    List<String> result = new ArrayList<String>();
    String jarFileUrl = jarUrl.substring(4, pos);
    JarInputStream jarIn = null;
    try {
        jarIn = new JarInputStream(new URL(jarFileUrl).openStream());
        ZipEntry e;
        while ((e = jarIn.getNextEntry()) != null) {
            String name = e.getName();
            if (filenameEndsWithSuffix(name, suffixes)) {
                if (name.startsWith("Templates/"))
                    name = name.substring(9);
                else
                    name = "/" + name;
                result.add(name);
            }
        }
    } catch (Exception e) {
        Log.error("Unable to scan '" + jarFileUrl + "' for files", e);
    } finally {
        FileUtils.safelyClose(jarIn);
    }
    return result;
}
Also used : JarInputStream(java.util.jar.JarInputStream) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) URL(java.net.URL) ServletException(javax.servlet.ServletException) PrivilegedActionException(java.security.PrivilegedActionException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 58 with JarInputStream

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

the class TemplateLoader method searchJarForTemplates.

private static JarSearchResult searchJarForTemplates(DashHierarchy templates, String jarURL, DataRepository data) {
    boolean foundTemplates = false;
    try {
        debug("searching for templates in " + jarURL);
        URL jarFileUrl = new URL(jarURL);
        JarInputStream jarFile = new JarInputStream((jarFileUrl).openStream());
        ZipEntry file;
        String filename;
        while ((file = jarFile.getNextEntry()) != null) {
            filename = file.getName().toLowerCase();
            if (filename.equals(MCF_PROCESS_XML)) {
                String baseURL = "jar:" + jarURL + "!/";
                InputStream mcfXmlData = MCFManager.getInstance().registerMcf(baseURL, jarFile, null, true);
                if (mcfXmlData != null) {
                    String n = MCF_PROCESS_XML + " (in " + jarURL + ")";
                    loadXMLProcessTemplate(templates, data, n, null, file.getTime(), mcfXmlData, true);
                    jarFile.close();
                    return JarSearchResult.Mcf;
                }
            }
            if (startsWithIgnoreCase(filename, TEMPLATE_DIR)) {
                if (filename.lastIndexOf('/') != 9)
                    continue;
            } else if (startsWithIgnoreCase(filename, WEB_INF_DIR)) {
                if (!filename.equalsIgnoreCase(WEB_INF_XML_FILE))
                    continue;
            } else {
                continue;
            }
            if (filename.endsWith(XML_TEMPLATE_SUFFIX) || filename.endsWith(XML_TEMPLATE_FILE)) {
                debug("loading template: " + filename);
                String n = file.getName() + " (in " + jarURL + ")";
                loadXMLProcessTemplate(templates, data, n, jarFileUrl, file.getTime(), jarFile, false);
                foundTemplates = true;
            } else if (filename.endsWith(TEMPLATE_SUFFIX)) {
                debug("loading template: " + filename);
                loadProcessTemplate(templates, jarFile, false);
                foundTemplates = true;
            } else if (filename.endsWith(DATAFILE_SUFFIX)) {
                try {
                    debug("loading data: " + filename);
                    data.addGlobalDefinitions(jarFile, false);
                } catch (Exception e) {
                    logger.severe("unable to load global process data from " + file.getName() + " in " + jarURL + ": " + e);
                }
            }
        }
        jarFile.close();
    } catch (IOException ioe) {
        logger.severe("error looking for templates in " + jarURL);
        ioe.printStackTrace(System.out);
    }
    return foundTemplates ? JarSearchResult.Template : JarSearchResult.None;
}
Also used : JarInputStream(java.util.jar.JarInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) NonclosingInputStream(net.sourceforge.processdash.util.NonclosingInputStream) JarInputStream(java.util.jar.JarInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) URL(java.net.URL) SAXException(org.xml.sax.SAXException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 59 with JarInputStream

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

the class bnd method _grep.

@Description("Grep the manifest of bundles/jar files. ")
public void _grep(grepOptions opts) throws Exception {
    List<String> args = opts._arguments();
    String s = args.remove(0);
    Pattern pattern = Glob.toPattern(s);
    if (pattern == null) {
        messages.InvalidGlobPattern_(s);
        return;
    }
    if (args.isEmpty()) {
        args = new ExtList<String>(getBase().list(new FilenameFilter() {

            public boolean accept(File dir, String name) {
                return name.endsWith(".jar");
            }
        }));
    }
    Set<String> headers = opts.headers();
    if (headers == null)
        headers = new TreeSet<String>();
    if (opts.exports())
        headers.add(Constants.EXPORT_PACKAGE);
    if (opts.bsn())
        headers.add(Constants.BUNDLE_SYMBOLICNAME);
    if (opts.imports())
        headers.add(Constants.IMPORT_PACKAGE);
    Instructions instructions = new Instructions(headers);
    for (String fileName : args) {
        File file = getFile(fileName);
        if (!file.isFile()) {
            messages.NoSuchFile_(file);
            continue;
        }
        try (JarInputStream in = new JarInputStream(IO.stream(file))) {
            Manifest m = in.getManifest();
            for (Object header : m.getMainAttributes().keySet()) {
                Attributes.Name name = (Name) header;
                if (instructions.isEmpty() || instructions.matches(name.toString())) {
                    String h = m.getMainAttributes().getValue(name);
                    QuotedTokenizer qt = new QuotedTokenizer(h, ",;=");
                    for (String value : qt.getTokenSet()) {
                        Matcher matcher = pattern.matcher(value);
                        while (matcher.find()) {
                            int start = matcher.start() - 8;
                            if (start < 0)
                                start = 0;
                            int end = matcher.end() + 8;
                            if (end > value.length())
                                end = value.length();
                            out.printf("%40s : %20s ...%s[%s]%s...\n", fileName, name, value.substring(start, matcher.start()), value.substring(matcher.start(), matcher.end()), value.substring(matcher.end(), end));
                        }
                    }
                }
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) QuotedTokenizer(aQute.libg.qtokens.QuotedTokenizer) JarInputStream(java.util.jar.JarInputStream) Matcher(java.util.regex.Matcher) Attributes(java.util.jar.Attributes) Instructions(aQute.bnd.osgi.Instructions) Name(java.util.jar.Attributes.Name) Manifest(java.util.jar.Manifest) PomFromManifest(aQute.bnd.maven.PomFromManifest) Name(java.util.jar.Attributes.Name) FilenameFilter(java.io.FilenameFilter) TreeSet(java.util.TreeSet) File(java.io.File) Description(aQute.lib.getopt.Description)

Example 60 with JarInputStream

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

the class DeploymentPackageInstaller method getManifest.

/**
     * Read the manifest from supplied input stream, which is closed before return.
     */
private Manifest getManifest(final InputStream ins) throws IOException {
    Manifest result = null;
    if (ins != null) {
        JarInputStream jis = null;
        try {
            jis = new JarInputStream(ins);
            result = jis.getManifest();
        } finally {
            // since closing the jar stream closes the input stream
            if (jis != null) {
                try {
                    jis.close();
                } catch (final IOException ignore) {
                }
            } else {
                try {
                    ins.close();
                } catch (final IOException ignore) {
                }
            }
        }
    }
    return result;
}
Also used : JarInputStream(java.util.jar.JarInputStream) IOException(java.io.IOException) Manifest(java.util.jar.Manifest)

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