Search in sources :

Example 61 with JarEntry

use of java.util.jar.JarEntry in project jetty.project by eclipse.

the class WarBundleManifestGenerator method getJarsInWebInfLib.

private static List<String> getJarsInWebInfLib(JarFile jarFile) {
    List<String> res = new ArrayList<String>();
    Enumeration<JarEntry> en = jarFile.entries();
    while (en.hasMoreElements()) {
        JarEntry e = en.nextElement();
        if (e.getName().startsWith("WEB-INF/lib/") && e.getName().endsWith(".jar")) {
            res.add(e.getName());
        }
    }
    return res;
}
Also used : ArrayList(java.util.ArrayList) JarEntry(java.util.jar.JarEntry)

Example 62 with JarEntry

use of java.util.jar.JarEntry in project jetty.project by eclipse.

the class AnnotationParser method parse.

/**
     * Parse classes in the supplied classloader. 
     * Only class files in jar files will be scanned.
     * 
     * @param handlers the handlers to look for classes in 
     * @param loader the classloader for the classes
     * @param visitParents if true, visit parent classloaders too
     * @param nullInclusive if true, an empty pattern means all names match, if false, none match
     * @throws Exception if unable to parse
     */
public void parse(final Set<? extends Handler> handlers, ClassLoader loader, boolean visitParents, boolean nullInclusive) throws Exception {
    if (loader == null)
        return;
    if (!(loader instanceof URLClassLoader))
        //can't extract classes?
        return;
    final MultiException me = new MultiException();
    JarScanner scanner = new JarScanner() {

        @Override
        public void processEntry(URI jarUri, JarEntry entry) {
            try {
                parseJarEntry(handlers, Resource.newResource(jarUri), entry);
            } catch (Exception e) {
                me.add(new RuntimeException("Error parsing entry " + entry.getName() + " from jar " + jarUri, e));
            }
        }
    };
    scanner.scan(null, loader, nullInclusive, visitParents);
    me.ifExceptionThrow();
}
Also used : URLClassLoader(java.net.URLClassLoader) JarEntry(java.util.jar.JarEntry) MultiException(org.eclipse.jetty.util.MultiException) JarScanner(org.eclipse.jetty.webapp.JarScanner) URI(java.net.URI) MultiException(org.eclipse.jetty.util.MultiException) IOException(java.io.IOException)

Example 63 with JarEntry

use of java.util.jar.JarEntry in project jetty.project by eclipse.

the class JarFileResource method exists.

/* ------------------------------------------------------------ */
/**
     * Returns true if the represented resource exists.
     */
@Override
public boolean exists() {
    if (_exists)
        return true;
    if (_urlString.endsWith("!/")) {
        String file_url = _urlString.substring(4, _urlString.length() - 2);
        try {
            return newResource(file_url).exists();
        } catch (Exception e) {
            LOG.ignore(e);
            return false;
        }
    }
    boolean check = checkConnection();
    // Is this a root URL?
    if (_jarUrl != null && _path == null) {
        // Then if it exists it is a directory
        _directory = check;
        return true;
    } else {
        // Can we find a file for it?
        boolean close_jar_file = false;
        JarFile jar_file = null;
        if (check)
            // Yes
            jar_file = _jarFile;
        else {
            // No - so lets look if the root entry exists.
            try {
                JarURLConnection c = (JarURLConnection) ((new URL(_jarUrl)).openConnection());
                c.setUseCaches(getUseCaches());
                jar_file = c.getJarFile();
                close_jar_file = !getUseCaches();
            } catch (Exception e) {
                LOG.ignore(e);
            }
        }
        // Do we need to look more closely?
        if (jar_file != null && _entry == null && !_directory) {
            // OK - we have a JarFile, lets look for the entry
            JarEntry entry = jar_file.getJarEntry(_path);
            if (entry == null) {
                // the entry does not exist
                _exists = false;
            } else if (entry.isDirectory()) {
                _directory = true;
                _entry = entry;
            } else {
                // Let's confirm is a file
                JarEntry directory = jar_file.getJarEntry(_path + '/');
                if (directory != null) {
                    _directory = true;
                    _entry = directory;
                } else {
                    // OK is a file
                    _directory = false;
                    _entry = entry;
                }
            }
        }
        if (close_jar_file && jar_file != null) {
            try {
                jar_file.close();
            } catch (IOException ioe) {
                LOG.ignore(ioe);
            }
        }
    }
    _exists = (_directory || _entry != null);
    return _exists;
}
Also used : JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) URL(java.net.URL)

Example 64 with JarEntry

use of java.util.jar.JarEntry in project jetty.project by eclipse.

the class JarResource method copyTo.

/* ------------------------------------------------------------ */
@Override
public void copyTo(File directory) throws IOException {
    if (!exists())
        return;
    if (LOG.isDebugEnabled())
        LOG.debug("Extract " + this + " to " + directory);
    String urlString = this.getURL().toExternalForm().trim();
    int endOfJarUrl = urlString.indexOf("!/");
    int startOfJarUrl = (endOfJarUrl >= 0 ? 4 : 0);
    if (endOfJarUrl < 0)
        throw new IOException("Not a valid jar url: " + urlString);
    URL jarFileURL = new URL(urlString.substring(startOfJarUrl, endOfJarUrl));
    String subEntryName = (endOfJarUrl + 2 < urlString.length() ? urlString.substring(endOfJarUrl + 2) : null);
    boolean subEntryIsDir = (subEntryName != null && subEntryName.endsWith("/") ? true : false);
    if (LOG.isDebugEnabled())
        LOG.debug("Extracting entry = " + subEntryName + " from jar " + jarFileURL);
    URLConnection c = jarFileURL.openConnection();
    c.setUseCaches(false);
    try (InputStream is = c.getInputStream();
        JarInputStream jin = new JarInputStream(is)) {
        JarEntry entry;
        boolean shouldExtract;
        while ((entry = jin.getNextJarEntry()) != null) {
            String entryName = entry.getName();
            if ((subEntryName != null) && (entryName.startsWith(subEntryName))) {
                // is the subentry really a dir?
                if (!subEntryIsDir && subEntryName.length() + 1 == entryName.length() && entryName.endsWith("/"))
                    subEntryIsDir = true;
                //extract it.
                if (subEntryIsDir) {
                    //if it is a subdirectory we are looking for, then we
                    //are looking to extract its contents into the target
                    //directory. Remove the name of the subdirectory so
                    //that we don't wind up creating it too.
                    entryName = entryName.substring(subEntryName.length());
                    if (!entryName.equals("")) {
                        //the entry is
                        shouldExtract = true;
                    } else
                        shouldExtract = false;
                } else
                    shouldExtract = true;
            } else if ((subEntryName != null) && (!entryName.startsWith(subEntryName))) {
                //there is a particular entry we are looking for, and this one
                //isn't it
                shouldExtract = false;
            } else {
                //we are extracting everything
                shouldExtract = true;
            }
            if (!shouldExtract) {
                if (LOG.isDebugEnabled())
                    LOG.debug("Skipping entry: " + entryName);
                continue;
            }
            String dotCheck = entryName.replace('\\', '/');
            dotCheck = URIUtil.canonicalPath(dotCheck);
            if (dotCheck == null) {
                if (LOG.isDebugEnabled())
                    LOG.debug("Invalid entry: " + entryName);
                continue;
            }
            File file = new File(directory, entryName);
            if (entry.isDirectory()) {
                // Make directory
                if (!file.exists())
                    file.mkdirs();
            } else {
                // make directory (some jars don't list dirs)
                File dir = new File(file.getParent());
                if (!dir.exists())
                    dir.mkdirs();
                // Make file
                try (OutputStream fout = new FileOutputStream(file)) {
                    IO.copy(jin, fout);
                }
                // touch the file.
                if (entry.getTime() >= 0)
                    file.setLastModified(entry.getTime());
            }
        }
        if ((subEntryName == null) || (subEntryName != null && subEntryName.equalsIgnoreCase("META-INF/MANIFEST.MF"))) {
            Manifest manifest = jin.getManifest();
            if (manifest != null) {
                File metaInf = new File(directory, "META-INF");
                metaInf.mkdir();
                File f = new File(metaInf, "MANIFEST.MF");
                try (OutputStream fout = new FileOutputStream(f)) {
                    manifest.write(fout);
                }
            }
        }
    }
}
Also used : JarInputStream(java.util.jar.JarInputStream) FilterInputStream(java.io.FilterInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) Manifest(java.util.jar.Manifest) URL(java.net.URL) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 65 with JarEntry

use of java.util.jar.JarEntry in project tomcat by apache.

the class JspCompilationContext method getLastModified.

public Long getLastModified(String resource, Jar tagJar) {
    long result = -1;
    URLConnection uc = null;
    try {
        if (tagJar != null) {
            if (resource.startsWith("/")) {
                resource = resource.substring(1);
            }
            result = tagJar.getLastModified(resource);
        } else {
            URL jspUrl = getResource(resource);
            if (jspUrl == null) {
                incrementRemoved();
                return Long.valueOf(result);
            }
            uc = jspUrl.openConnection();
            if (uc instanceof JarURLConnection) {
                JarEntry jarEntry = ((JarURLConnection) uc).getJarEntry();
                if (jarEntry != null) {
                    result = jarEntry.getTime();
                } else {
                    result = uc.getLastModified();
                }
            } else {
                result = uc.getLastModified();
            }
        }
    } catch (IOException e) {
        if (log.isDebugEnabled()) {
            log.debug(Localizer.getMessage("jsp.error.lastModified", getJspFile()), e);
        }
        result = -1;
    } finally {
        if (uc != null) {
            try {
                uc.getInputStream().close();
            } catch (IOException e) {
                if (log.isDebugEnabled()) {
                    log.debug(Localizer.getMessage("jsp.error.lastModified", getJspFile()), e);
                }
                result = -1;
            }
        }
    }
    return Long.valueOf(result);
}
Also used : JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection) URL(java.net.URL)

Aggregations

JarEntry (java.util.jar.JarEntry)594 JarFile (java.util.jar.JarFile)290 File (java.io.File)217 IOException (java.io.IOException)187 InputStream (java.io.InputStream)134 JarOutputStream (java.util.jar.JarOutputStream)112 FileOutputStream (java.io.FileOutputStream)109 FileInputStream (java.io.FileInputStream)92 URL (java.net.URL)87 JarInputStream (java.util.jar.JarInputStream)87 ArrayList (java.util.ArrayList)67 Manifest (java.util.jar.Manifest)58 JarURLConnection (java.net.JarURLConnection)53 Test (org.junit.Test)39 HashSet (java.util.HashSet)31 ZipEntry (java.util.zip.ZipEntry)31 ZipFile (java.util.zip.ZipFile)30 OutputStream (java.io.OutputStream)29 BufferedInputStream (java.io.BufferedInputStream)26 Enumeration (java.util.Enumeration)26