Search in sources :

Example 21 with JarURLConnection

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

the class JspCTldLocationsCache method scanJars.

/*
     * Scans all JARs accessible to the webapp's classloader and its
     * parent classloaders for TLDs.
     *
     * The list of JARs always includes the JARs under WEB-INF/lib, as well as
     * all shared JARs in the classloader delegation chain of the webapp's
     * classloader.
     *
     * Considering JARs in the classloader delegation chain constitutes a
     * Tomcat-specific extension to the TLD search
     * order defined in the JSP spec. It allows tag libraries packaged as JAR
     * files to be shared by web applications by simply dropping them in a
     * location that all web applications have access to (e.g.,
     * <CATALINA_HOME>/common/lib).
     *
     * The set of shared JARs to be scanned for TLDs is narrowed down by
     * the <tt>noTldJars</tt> class variable, which contains the names of JARs
     * that are known not to contain any TLDs.
     */
private void scanJars() throws Exception {
    ClassLoader loader = webappLoader;
    while (loader != null) {
        if (loader instanceof URLClassLoader) {
            URL[] urls = ((URLClassLoader) loader).getURLs();
            for (URL url : urls) {
                URLConnection conn = url.openConnection();
                if (conn instanceof JarURLConnection) {
                    if (needScanJar(loader, webappLoader, ((JarURLConnection) conn).getJarFile().getName())) {
                        scanJar((JarURLConnection) conn, true);
                    }
                } else {
                    String urlStr = url.toString();
                    if (urlStr.startsWith(FILE_PROTOCOL) && urlStr.endsWith(JAR_FILE_SUFFIX) && needScanJar(loader, webappLoader, urlStr)) {
                        URL jarURL = new URL("jar:" + urlStr + "!/");
                        scanJar((JarURLConnection) jarURL.openConnection(), true);
                    }
                }
            }
        }
        loader = loader.getParent();
    }
}
Also used : JarURLConnection(java.net.JarURLConnection) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) URL(java.net.URL) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection)

Example 22 with JarURLConnection

use of java.net.JarURLConnection in project jdk8u_jdk by JetBrains.

the class AppletPanel method getAccessControlContext.

/**
     * get the context for the AppletClassLoader we are creating.
     * the context is granted permission to create the class loader,
     * connnect to the codebase, and whatever else the policy grants
     * to all codebases.
     */
private AccessControlContext getAccessControlContext(final URL codebase) {
    PermissionCollection perms = (PermissionCollection) AccessController.doPrivileged(new PrivilegedAction() {

        @Override
        public Object run() {
            Policy p = java.security.Policy.getPolicy();
            if (p != null) {
                return p.getPermissions(new CodeSource(null, (java.security.cert.Certificate[]) null));
            } else {
                return null;
            }
        }
    });
    if (perms == null)
        perms = new Permissions();
    //XXX: this is needed to be able to create the classloader itself!
    perms.add(SecurityConstants.CREATE_CLASSLOADER_PERMISSION);
    Permission p;
    java.net.URLConnection urlConnection = null;
    try {
        urlConnection = codebase.openConnection();
        p = urlConnection.getPermission();
    } catch (java.io.IOException ioe) {
        p = null;
    }
    if (p != null)
        perms.add(p);
    if (p instanceof FilePermission) {
        String path = p.getName();
        int endIndex = path.lastIndexOf(File.separatorChar);
        if (endIndex != -1) {
            path = path.substring(0, endIndex + 1);
            if (path.endsWith(File.separator)) {
                path += "-";
            }
            perms.add(new FilePermission(path, SecurityConstants.FILE_READ_ACTION));
        }
    } else {
        URL locUrl = codebase;
        if (urlConnection instanceof JarURLConnection) {
            locUrl = ((JarURLConnection) urlConnection).getJarFileURL();
        }
        String host = locUrl.getHost();
        if (host != null && (host.length() > 0))
            perms.add(new SocketPermission(host, SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION));
    }
    ProtectionDomain domain = new ProtectionDomain(new CodeSource(codebase, (java.security.cert.Certificate[]) null), perms);
    AccessControlContext acc = new AccessControlContext(new ProtectionDomain[] { domain });
    return acc;
}
Also used : java.io(java.io) JarURLConnection(java.net.JarURLConnection) SocketPermission(java.net.SocketPermission) URL(java.net.URL) SocketPermission(java.net.SocketPermission)

Example 23 with JarURLConnection

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

the class ClassLoaderResourceProviderChildrenTest method mockClassLoader.

private ClassLoader mockClassLoader(String... paths) throws MalformedURLException, IOException {
    final ClassLoader cl = Mockito.mock(ClassLoader.class);
    final JarURLConnection conn = Mockito.mock(JarURLConnection.class);
    final URLStreamHandler handler = new URLStreamHandler() {

        @Override
        protected URLConnection openConnection(final URL url) throws IOException {
            if (throwExceptionOnOpenConnection) {
                throw new IOException("Throwing up for testing that");
            }
            return conn;
        }
    };
    final JarFile f = Mockito.mock(JarFile.class);
    final URL url = new URL("jar://some.jar", "localhost", 1234, "some.jar", handler);
    final Vector<JarEntry> entries = new Vector<JarEntry>();
    for (String path : paths) {
        entries.add(new JarEntry(path));
    }
    when(cl.getResource(Matchers.contains("install"))).thenReturn(url);
    when(conn.getJarFile()).thenReturn(f);
    when(f.entries()).thenReturn(entries.elements());
    return cl;
}
Also used : URLStreamHandler(java.net.URLStreamHandler) JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) Vector(java.util.Vector) URL(java.net.URL)

Example 24 with JarURLConnection

use of java.net.JarURLConnection in project jPOS by jpos.

the class CLIPrefixedClassNameCompleter method resolveModuleEntriesFromJar.

private static List<String> resolveModuleEntriesFromJar(URL url, String _prefix) throws IOException {
    final String prefix = _prefix.endsWith("/") ? _prefix : _prefix + "/";
    List<String> resourceList = new ArrayList<String>();
    JarURLConnection conn = (JarURLConnection) url.openConnection();
    Enumeration entries = conn.getJarFile().entries();
    while (entries.hasMoreElements()) {
        JarEntry entry = (JarEntry) entries.nextElement();
        String name = entry.getName();
        if (name.startsWith(prefix) && !name.contains("$") && !entry.isDirectory()) {
            name = name.substring(prefix.length()).toLowerCase();
            if (!name.contains("/")) {
                resourceList.add(name);
            }
        }
    }
    return resourceList;
}
Also used : JarURLConnection(java.net.JarURLConnection) AttributedString(org.jline.utils.AttributedString) JarEntry(java.util.jar.JarEntry)

Example 25 with JarURLConnection

use of java.net.JarURLConnection in project jPOS by jpos.

the class ModuleUtils method resolveModuleEntriesFromJar.

private static List<String> resolveModuleEntriesFromJar(URL url, String _prefix) throws IOException {
    final String prefix = _prefix.endsWith("/") ? _prefix : _prefix + "/";
    List<String> resourceList = new ArrayList<String>();
    JarURLConnection conn = (JarURLConnection) url.openConnection();
    Enumeration entries = conn.getJarFile().entries();
    while (entries.hasMoreElements()) {
        JarEntry entry = (JarEntry) entries.nextElement();
        String name = entry.getName();
        if (name.startsWith(prefix) && !entry.isDirectory()) {
            resourceList.add(name);
        }
    }
    return resourceList;
}
Also used : Enumeration(java.util.Enumeration) JarURLConnection(java.net.JarURLConnection) ArrayList(java.util.ArrayList) JarEntry(java.util.jar.JarEntry)

Aggregations

JarURLConnection (java.net.JarURLConnection)222 URL (java.net.URL)160 JarFile (java.util.jar.JarFile)129 IOException (java.io.IOException)120 JarEntry (java.util.jar.JarEntry)105 File (java.io.File)92 URLConnection (java.net.URLConnection)89 ArrayList (java.util.ArrayList)32 InputStream (java.io.InputStream)27 MalformedURLException (java.net.MalformedURLException)25 URISyntaxException (java.net.URISyntaxException)21 Enumeration (java.util.Enumeration)17 Manifest (java.util.jar.Manifest)16 FileInputStream (java.io.FileInputStream)12 CodeSource (java.security.CodeSource)12 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