Search in sources :

Example 6 with JarURLConnection

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

the class DeleteTempJar method realMain.

public static void realMain(String[] args) throws Exception {
    final File zf = File.createTempFile("deletetemp", ".jar");
    zf.deleteOnExit();
    try (FileOutputStream fos = new FileOutputStream(zf);
        JarOutputStream jos = new JarOutputStream(fos)) {
        JarEntry je = new JarEntry("entry");
        jos.putNextEntry(je);
        jos.write("hello, world".getBytes("ASCII"));
    }
    HttpServer server = HttpServer.create(new InetSocketAddress((InetAddress) null, 0), 0);
    HttpContext context = server.createContext("/", new HttpHandler() {

        public void handle(HttpExchange e) {
            try (FileInputStream fis = new FileInputStream(zf)) {
                e.sendResponseHeaders(200, zf.length());
                OutputStream os = e.getResponseBody();
                byte[] buf = new byte[1024];
                int count = 0;
                while ((count = fis.read(buf)) != -1) {
                    os.write(buf, 0, count);
                }
            } catch (Exception ex) {
                unexpected(ex);
            } finally {
                e.close();
            }
        }
    });
    server.start();
    URL url = new URL("jar:http://localhost:" + new Integer(server.getAddress().getPort()).toString() + "/deletetemp.jar!/");
    JarURLConnection c = (JarURLConnection) url.openConnection();
    JarFile f = c.getJarFile();
    check(f.getEntry("entry") != null);
    System.out.println(f.getName());
    server.stop(0);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) JarURLConnection(java.net.JarURLConnection) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) JarOutputStream(java.util.jar.JarOutputStream) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) FileInputStream(java.io.FileInputStream) URL(java.net.URL) FileOutputStream(java.io.FileOutputStream) JarFile(java.util.jar.JarFile) File(java.io.File) InetAddress(java.net.InetAddress)

Example 7 with JarURLConnection

use of java.net.JarURLConnection in project jackrabbit-oak by apache.

the class SpringBootSupport method processDescriptors.

public static List<BundleDescriptor> processDescriptors(List<BundleDescriptor> descriptors) throws IOException {
    List<BundleDescriptor> processed = Lists.newArrayList();
    for (BundleDescriptor desc : descriptors) {
        if (desc.getRevision() == null) {
            URL u = new URL(desc.getUrl());
            URLConnection uc = u.openConnection();
            if (uc instanceof JarURLConnection && uc.getClass().getName().startsWith(SPRING_BOOT_PACKAGE)) {
                Revision rev = new SpringBootJarRevision(((JarURLConnection) uc).getJarFile(), uc.getLastModified());
                desc = new BundleDescriptor(desc.getClassLoader(), desc.getUrl(), desc.getHeaders(), rev, desc.getServices());
            }
        }
        processed.add(desc);
    }
    return processed;
}
Also used : BundleDescriptor(org.apache.felix.connect.launch.BundleDescriptor) Revision(org.apache.felix.connect.Revision) JarURLConnection(java.net.JarURLConnection) URL(java.net.URL) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection)

Example 8 with JarURLConnection

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

the class OriginalTldLocationsCache 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 webappLoader = Thread.currentThread().getContextClassLoader();
    ClassLoader loader = webappLoader;
    while (loader != null) {
        if (loader instanceof URLClassLoader) {
            URL[] urls = ((URLClassLoader) loader).getURLs();
            for (int i = 0; i < urls.length; i++) {
                URLConnection conn = urls[i].openConnection();
                if (conn instanceof JarURLConnection) {
                    if (needScanJar(loader, webappLoader, ((JarURLConnection) conn).getJarFile().getName())) {
                        scanJar((JarURLConnection) conn, true);
                    }
                } else {
                    String urlStr = urls[i].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 9 with JarURLConnection

use of java.net.JarURLConnection in project robovm by robovm.

the class TrustedCertificateStore method list.

private String[] list(URI file) {
    if ("file".equals(file.getScheme())) {
        return new File(file).list();
    }
    if ("jar".equals(file.getScheme())) {
        try {
            JarURLConnection conn = (JarURLConnection) file.toURL().openConnection();
            JarFile jarFile = conn.getJarFile();
            String uriStr = file.toString();
            if (!uriStr.endsWith("/")) {
                uriStr += "/";
            }
            String path = uriStr.substring(uriStr.lastIndexOf('!') + 1);
            if (path.startsWith("/")) {
                path = path.substring(1);
            }
            Enumeration<JarEntry> en = jarFile.entries();
            List<String> result = new ArrayList<String>();
            while (en.hasMoreElements()) {
                JarEntry entry = en.nextElement();
                String name = entry.getName();
                if (name.startsWith(path) && !name.endsWith("/")) {
                    int lastSlash = name.lastIndexOf('/');
                    if (lastSlash == path.length() - 1) {
                        result.add(name.substring(lastSlash + 1));
                    }
                }
            }
            return result.toArray(new String[result.size()]);
        } catch (IOException e) {
        }
    }
    return null;
}
Also used : JarURLConnection(java.net.JarURLConnection) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 10 with JarURLConnection

use of java.net.JarURLConnection in project robovm by robovm.

the class OldJarURLConnectionTest method test_getCertificates.

public void test_getCertificates() throws Exception {
    URL u = createContent("TestCodeSigners.jar", "Test.class");
    juc = (JarURLConnection) u.openConnection();
    assertNull(juc.getCertificates());
    JarEntry je = juc.getJarEntry();
    JarFile jf = juc.getJarFile();
    InputStream is = jf.getInputStream(je);
    is.skip(je.getSize());
    Certificate[] certs = juc.getCertificates();
    assertEquals(3, certs.length);
    URL invURL = createContent("InvalidJar.jar", "Test.class");
    JarURLConnection juConn = (JarURLConnection) invURL.openConnection();
    try {
        juConn.getCertificates();
        fail("IOException was not thrown.");
    } catch (java.io.IOException io) {
    //expected
    }
}
Also used : InputStream(java.io.InputStream) JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) URL(java.net.URL) Certificate(java.security.cert.Certificate)

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