Search in sources :

Example 61 with JarURLConnection

use of java.net.JarURLConnection in project openj9 by eclipse.

the class RecreateClassUtils method createClassFile.

/* Reads a class file from the jar file from which the class was loaded
	 * and creates a local copy in current working directory.
	 */
public static void createClassFile(Class<?> testClass, String classFileName) throws Exception {
    URL url = testClass.getResource(classFileName + ".class");
    if (url != null) {
        URLConnection connection = url.openConnection();
        if (connection instanceof JarURLConnection) {
            JarURLConnection urlConnection = (JarURLConnection) connection;
            urlConnection.connect();
            try (InputStream inStream = urlConnection.getInputStream()) {
                File outFile = new File(classFileName + ".class");
                try (FileOutputStream outStream = new FileOutputStream(outFile)) {
                    byte[] data = new byte[1024];
                    int size;
                    while ((size = inStream.read(data)) > 0) {
                        outStream.write(data, 0, size);
                    }
                }
            }
        }
    }
}
Also used : JarURLConnection(java.net.JarURLConnection) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) URL(java.net.URL) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection)

Example 62 with JarURLConnection

use of java.net.JarURLConnection in project openj9 by eclipse.

the class CustomPartitioningURLLoader method locateClass.

private int locateClass(String className) {
    int classAtEntry = -1;
    String name = className.replace('.', '/').concat(".class");
    for (int index = 0; index < urls.length; index++) {
        URL currentUrl = urls[index];
        if (currentUrl.getProtocol().equals("jar")) {
            JarEntry entry = null;
            JarFile jf = (JarFile) jarCache.get(currentUrl);
            if (jf == null) {
                /* First time we have encountered this jar.
					 * Lets cache its metaData.
					 */
                try {
                    URL jarFileUrl = ((JarURLConnection) currentUrl.openConnection()).getJarFileURL();
                    JarURLConnection jarUrlConnection = (JarURLConnection) new URL("jar", "", jarFileUrl.toExternalForm() + "!/").openConnection();
                    try {
                        jf = jarUrlConnection.getJarFile();
                    } catch (Exception e) {
                    }
                    if (jf != null) {
                        jarCache.put(currentUrl, jf);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (jf != null) {
                    Manifest manifest = null;
                    java.security.cert.Certificate[] certs = null;
                    URL csUrl = currentUrl;
                    CodeSource codeSource;
                    try {
                        manifest = jf.getManifest();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    entry = jf.getJarEntry(name);
                    if (entry != null) {
                        certs = entry.getCertificates();
                    }
                    codeSource = new CodeSource(csUrl, certs);
                    CustomLoaderMetaDataCache metaData = new CustomLoaderMetaDataCache();
                    metaData.manifest = manifest;
                    metaData.codeSource = codeSource;
                    metaDataArray[index] = metaData;
                }
            }
            if (entry == null && jf != null) {
                entry = jf.getJarEntry(name);
            }
            if (entry != null) {
                /* We have the first match on the class path, return the current url index */
                return index;
            }
        } else {
            String filename = currentUrl.getFile();
            String host = currentUrl.getHost();
            if (host != null && host.length() > 0) {
                filename = new StringBuffer(host.length() + filename.length() + name.length() + 2).append("//").append(host).append(filename).append(name).toString();
            } else {
                filename = new StringBuffer(filename.length() + name.length()).append(filename).append(name).toString();
            }
            File file = new File(filename);
            // Don't throw exceptions for speed
            if (file.exists()) {
                if (metaDataArray[index] == null) {
                    java.security.cert.Certificate[] certs = null;
                    CustomLoaderMetaDataCache metaData = new CustomLoaderMetaDataCache();
                    metaData.manifest = null;
                    metaData.codeSource = new CodeSource(currentUrl, certs);
                    metaDataArray[index] = metaData;
                }
                return index;
            }
        }
    }
    return classAtEntry;
}
Also used : JarURLConnection(java.net.JarURLConnection) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) Manifest(java.util.jar.Manifest) CodeSource(java.security.CodeSource) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) HelperAlreadyDefinedException(com.ibm.oti.shared.HelperAlreadyDefinedException) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 63 with JarURLConnection

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

the class PluginClassLoader method findResourceFromEmbeddedJars.

private URL findResourceFromEmbeddedJars(final String resourceName) {
    for (final String embeddedJarPath : this.embeddedJarPathsInNestedJar) {
        final URL embeddedJarUrl;
        final JarURLConnection embeddedJarUrlConnection;
        final JarInputStream embeddedJarInputStream;
        try {
            embeddedJarUrl = getEmbeddedJarUrl(embeddedJarPath);
            embeddedJarUrlConnection = getEmbeddedJarUrlConnection(embeddedJarUrl);
            embeddedJarInputStream = getEmbeddedJarInputStream(embeddedJarUrlConnection);
        } catch (IOException ex) {
            // TODO: Notify this to reporters as far as possible.
            System.err.println("Failed to load entry in embedded JAR: " + resourceName + " / " + embeddedJarPath);
            ex.printStackTrace();
            continue;
        }
        JarEntry jarEntry;
        try {
            jarEntry = embeddedJarInputStream.getNextJarEntry();
        } catch (IOException ex) {
            // TODO: Notify this to reporters as far as possible.
            System.err.println("Failed to load entry in embedded JAR: " + resourceName + " / " + embeddedJarPath);
            ex.printStackTrace();
            continue;
        }
        jarEntries: while (jarEntry != null) {
            if (jarEntry.getName().equals(resourceName)) {
                // The URL lives only in the JVM execution.
                try {
                    // The URL lives only in the JVM execution.
                    return new URL("embulk-plugin-jar", "", -1, embeddedJarUrl + "!!/" + resourceName, new PluginClassUrlStreamHandler("embulk-plugin-jar"));
                } catch (MalformedURLException ex) {
                    // TODO: Notify this to reporters as far as possible.
                    System.err.println("Failed to load entry in embedded JAR: " + resourceName + " / " + embeddedJarPath);
                    ex.printStackTrace();
                    break jarEntries;
                }
            }
            try {
                jarEntry = embeddedJarInputStream.getNextJarEntry();
            } catch (IOException ex) {
                // TODO: Notify this to reporters as far as possible.
                System.err.println("Failed to load entry in embedded JAR: " + resourceName + " / " + embeddedJarPath);
                ex.printStackTrace();
                break jarEntries;
            }
        }
    }
    return null;
}
Also used : MalformedURLException(java.net.MalformedURLException) JarInputStream(java.util.jar.JarInputStream) JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) URL(java.net.URL)

Example 64 with JarURLConnection

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

the class PluginClassLoader method getEmbeddedJarUrlConnection.

private JarURLConnection getEmbeddedJarUrlConnection(final URL embeddedJarUrl) throws IOException {
    final JarURLConnection embeddedJarUrlConnection;
    try {
        final URLConnection urlConnection = embeddedJarUrl.openConnection();
        embeddedJarUrlConnection = (JarURLConnection) urlConnection;
    } catch (IOException ex) {
        // TODO: Notify this to reporters as far as possible.
        System.err.println("Failed to load entry in embedded JAR: " + embeddedJarUrl.toString());
        ex.printStackTrace();
        throw ex;
    }
    return embeddedJarUrlConnection;
}
Also used : JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection)

Example 65 with JarURLConnection

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

the class JarPluginLoader method load.

public static JarPluginLoader load(final Path jarPath, final PluginClassLoaderFactory classLoaderFactory) throws InvalidJarPluginException {
    final JarURLConnection connection = openJarUrlConnection(jarPath);
    final Manifest manifest = loadJarPluginManifest(connection, jarPath);
    final Attributes manifestAttributes = manifest.getMainAttributes();
    final int spiVersion = getPluginSpiVersionFromManifest(manifestAttributes);
    if (spiVersion == 0) {
        final String mainClassName = getPluginMainClassNameFromManifest(manifestAttributes);
        final List<String> pluginClassPath = getPluginClassPathFromManifest(manifestAttributes);
        final Class mainClass = loadJarPluginMainClass(jarPath, mainClassName, pluginClassPath, classLoaderFactory);
        return new JarPluginLoader(manifest, manifestAttributes, mainClass);
    }
    throw new InvalidJarPluginException("Unknown SPI version of JAR plugin: " + spiVersion);
}
Also used : JarURLConnection(java.net.JarURLConnection) Attributes(java.util.jar.Attributes) Manifest(java.util.jar.Manifest)

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