Search in sources :

Example 71 with JarURLConnection

use of java.net.JarURLConnection in project tomcat70 by apache.

the class StandardJarScanner method process.

/*
     * Scan a URL for JARs with the optional extensions to look at all files
     * and all directories.
     */
protected void process(JarScannerCallback callback, URL url) throws IOException {
    if (log.isTraceEnabled()) {
        log.trace(sm.getString("jarScan.jarUrlStart", url));
    }
    URLConnection conn = url.openConnection();
    if (conn instanceof JarURLConnection) {
        callback.scan((JarURLConnection) conn);
    } else {
        String urlStr = url.toString();
        if (urlStr.startsWith("file:") || urlStr.startsWith("jndi:") || urlStr.startsWith("http:") || urlStr.startsWith("https:")) {
            if (urlStr.endsWith(Constants.JAR_EXT)) {
                URL jarURL = UriUtil.buildJarUrl(urlStr);
                callback.scan((JarURLConnection) jarURL.openConnection());
            } else {
                File f;
                try {
                    f = new File(url.toURI());
                    if (f.isFile() && scanAllFiles) {
                        // Treat this file as a JAR
                        URL jarURL = UriUtil.buildJarUrl(f);
                        callback.scan((JarURLConnection) jarURL.openConnection());
                    } else if (f.isDirectory() && scanAllDirectories) {
                        File metainf = new File(f.getAbsoluteFile() + File.separator + "META-INF");
                        if (metainf.isDirectory()) {
                            callback.scan(f);
                        }
                    }
                } catch (Throwable t) {
                    ExceptionUtils.handleThrowable(t);
                    // Wrap the exception and re-throw
                    IOException ioe = new IOException();
                    ioe.initCause(t);
                    throw ioe;
                }
            }
        }
    }
}
Also used : JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) File(java.io.File) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection) URL(java.net.URL)

Example 72 with JarURLConnection

use of java.net.JarURLConnection in project tomcat70 by apache.

the class UrlJar method createJarInputStream.

private NonClosingJarInputStream createJarInputStream() throws IOException {
    JarURLConnection jarConn = (JarURLConnection) url.openConnection();
    URL resourceURL = jarConn.getJarFileURL();
    URLConnection resourceConn = resourceURL.openConnection();
    resourceConn.setUseCaches(false);
    return new NonClosingJarInputStream(resourceConn.getInputStream());
}
Also used : JarURLConnection(java.net.JarURLConnection) URL(java.net.URL) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection)

Example 73 with JarURLConnection

use of java.net.JarURLConnection in project tomcat70 by apache.

the class BaseDirContext method addResourcesJar.

// ------------------------------------------------------------- Properties
/**
 * Add a resources JAR. The contents of /META-INF/resources/ will be used if
 * a requested resource can not be found in the main context.
 */
public void addResourcesJar(URL url) {
    try {
        JarURLConnection conn = (JarURLConnection) url.openConnection();
        JarFile jarFile = conn.getJarFile();
        ZipEntry entry = jarFile.getEntry("/");
        WARDirContext warDirContext = new WARDirContext(jarFile, new WARDirContext.Entry("/", entry));
        warDirContext.loadEntries();
        altDirContexts.add(warDirContext);
    } catch (IOException ioe) {
        log.warn(sm.getString("resources.addResourcesJarFail", url), ioe);
    }
}
Also used : JarURLConnection(java.net.JarURLConnection) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) JarFile(java.util.jar.JarFile)

Example 74 with JarURLConnection

use of java.net.JarURLConnection in project elki by elki-project.

the class CheckELKIServices method checkServices.

/**
 * Retrieve all properties and check them.
 *
 * @param update Folder to update service files in
 */
public void checkServices(String update) {
    TreeSet<String> props = new TreeSet<>();
    Enumeration<URL> us;
    try {
        us = getClass().getClassLoader().getResources(ELKIServiceLoader.RESOURCE_PREFIX);
    } catch (IOException e) {
        throw new AbortException("Error enumerating service folders.", e);
    }
    while (us.hasMoreElements()) {
        URL u = us.nextElement();
        try {
            if (("jar".equals(u.getProtocol()))) {
                JarURLConnection con = (JarURLConnection) u.openConnection();
                try (JarFile jar = con.getJarFile()) {
                    Enumeration<JarEntry> entries = jar.entries();
                    while (entries.hasMoreElements()) {
                        String prop = entries.nextElement().getName();
                        if (prop.startsWith(ELKIServiceLoader.RESOURCE_PREFIX)) {
                            props.add(prop.substring(ELKIServiceLoader.RESOURCE_PREFIX.length()));
                        } else if (prop.startsWith(ELKIServiceLoader.FILENAME_PREFIX)) {
                            props.add(prop.substring(ELKIServiceLoader.FILENAME_PREFIX.length()));
                        }
                    }
                }
                continue;
            }
            if ("file".equals(u.getProtocol())) {
                props.addAll(Arrays.asList(new File(u.toURI()).list()));
            }
        } catch (IOException | URISyntaxException e) {
            throw new AbortException("Error enumerating service folders.", e);
        }
    }
    for (String prop : props) {
        if (LOG.isVerbose()) {
            LOG.verbose("Checking property: " + prop);
        }
        checkService(prop, update);
    }
}
Also used : JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URL(java.net.URL) TreeSet(java.util.TreeSet) JarFile(java.util.jar.JarFile) File(java.io.File) AbortException(de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)

Example 75 with JarURLConnection

use of java.net.JarURLConnection in project elki by elki-project.

the class CheckParameterizables method checkParameterizables.

/**
 * Validate all "Parameterizable" objects for parts of the API contract that
 * cannot be specified in Java interfaces (such as constructors, static
 * methods)
 */
public void checkParameterizables() {
    LoggingConfiguration.setVerbose(Level.VERBOSE);
    knownParameterizables = new ArrayList<>();
    try {
        Enumeration<URL> us = getClass().getClassLoader().getResources(ELKIServiceLoader.RESOURCE_PREFIX);
        while (us.hasMoreElements()) {
            URL u = us.nextElement();
            if ("file".equals(u.getProtocol())) {
                for (String prop : new File(u.toURI()).list()) {
                    try {
                        knownParameterizables.add(Class.forName(prop));
                    } catch (ClassNotFoundException e) {
                        LOG.warning("Service file name is not a class name: " + prop);
                        continue;
                    }
                }
            } else if (("jar".equals(u.getProtocol()))) {
                JarURLConnection con = (JarURLConnection) u.openConnection();
                try (JarFile jar = con.getJarFile()) {
                    Enumeration<JarEntry> entries = jar.entries();
                    while (entries.hasMoreElements()) {
                        String prop = entries.nextElement().getName();
                        if (prop.startsWith(ELKIServiceLoader.RESOURCE_PREFIX)) {
                            prop = prop.substring(ELKIServiceLoader.RESOURCE_PREFIX.length());
                        } else if (prop.startsWith(ELKIServiceLoader.FILENAME_PREFIX)) {
                            prop = prop.substring(ELKIServiceLoader.FILENAME_PREFIX.length());
                        } else {
                            continue;
                        }
                        try {
                            knownParameterizables.add(Class.forName(prop));
                        } catch (ClassNotFoundException e) {
                            LOG.warning("Service file name is not a class name: " + prop);
                            continue;
                        }
                    }
                }
            }
        }
    } catch (IOException | URISyntaxException e) {
        throw new AbortException("Error enumerating service folders.", e);
    }
    final String internal = de.lmu.ifi.dbs.elki.utilities.optionhandling.Parameterizer.class.getPackage().getName();
    for (final Class<?> cls : ELKIServiceRegistry.findAllImplementations(Object.class, false, false)) {
        // Classes in the same package are special and don't cause warnings.
        if (cls.getName().startsWith(internal)) {
            continue;
        }
        try {
            State state = State.NO_CONSTRUCTOR;
            state = checkV3Parameterization(cls, state);
            if (state == State.ERROR) {
                continue;
            }
            state = checkDefaultConstructor(cls, state);
            if (state == State.ERROR) {
                continue;
            }
            boolean expectedParameterizer = checkSupertypes(cls);
            if (state == State.NO_CONSTRUCTOR && expectedParameterizer) {
                LOG.verbose(// 
                "Class " + cls.getName() + " implements a parameterizable interface, but doesn't have a public and parameterless constructor!");
            }
            if (state == State.INSTANTIABLE && !expectedParameterizer) {
                LOG.verbose(// 
                "Class " + cls.getName() + " has a parameterizer, but there is no service file for any of its interfaces.");
            }
        } catch (NoClassDefFoundError e) {
            LOG.verbose("Class discovered but not found: " + cls.getName() + " (missing: " + e.getMessage() + ")");
        }
    }
}
Also used : Enumeration(java.util.Enumeration) JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) JarFile(java.util.jar.JarFile) AbstractParameterizer(de.lmu.ifi.dbs.elki.utilities.optionhandling.AbstractParameterizer) URL(java.net.URL) JarFile(java.util.jar.JarFile) File(java.io.File) AbortException(de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)

Aggregations

JarURLConnection (java.net.JarURLConnection)220 URL (java.net.URL)159 JarFile (java.util.jar.JarFile)128 IOException (java.io.IOException)119 JarEntry (java.util.jar.JarEntry)104 File (java.io.File)90 URLConnection (java.net.URLConnection)88 ArrayList (java.util.ArrayList)30 InputStream (java.io.InputStream)26 MalformedURLException (java.net.MalformedURLException)25 URISyntaxException (java.net.URISyntaxException)21 Enumeration (java.util.Enumeration)17 Manifest (java.util.jar.Manifest)16 CodeSource (java.security.CodeSource)12 FileInputStream (java.io.FileInputStream)11 LinkedHashSet (java.util.LinkedHashSet)11 URI (java.net.URI)10 Attributes (java.util.jar.Attributes)10 ZipEntry (java.util.zip.ZipEntry)9 FileNotFoundException (java.io.FileNotFoundException)8