Search in sources :

Example 91 with JarURLConnection

use of java.net.JarURLConnection in project Openfire by igniterealtime.

the class PluginClassLoader method unloadJarFiles.

/**
 * Unload any JAR files that have been cached by this plugin
 */
public void unloadJarFiles() {
    for (JarURLConnection url : cachedJarFiles) {
        try {
            Log.info("Unloading plugin JAR file " + url.getJarFile().getName());
            url.getJarFile().close();
        } catch (Exception e) {
            Log.error("Failed to unload JAR file", e);
        }
    }
}
Also used : JarURLConnection(java.net.JarURLConnection) MalformedURLException(java.net.MalformedURLException)

Example 92 with JarURLConnection

use of java.net.JarURLConnection in project undertow by undertow-io.

the class URLResource method openConnection.

private void openConnection() {
    if (!connectionOpened) {
        connectionOpened = true;
        URLConnection connection = null;
        try {
            try {
                connection = url.openConnection();
            } catch (IOException e) {
                lastModified = null;
                contentLength = null;
                return;
            }
            if (url.getProtocol().equals("jar")) {
                connection.setUseCaches(false);
                URL jar = ((JarURLConnection) connection).getJarFileURL();
                lastModified = new Date(new File(jar.getFile()).lastModified());
            } else {
                lastModified = new Date(connection.getLastModified());
            }
            contentLength = connection.getContentLengthLong();
        } finally {
            if (connection != null) {
                try {
                    IoUtils.safeClose(connection.getInputStream());
                } catch (IOException e) {
                // ignore
                }
            }
        }
    }
}
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) Date(java.util.Date)

Example 93 with JarURLConnection

use of java.net.JarURLConnection in project bytecode-viewer by Konloch.

the class ExternalLibrary method load.

/* (non-Javadoc)
     * @see the.bytecode.club.bytecodeviewer.loadermodel.ExternalResource#load()
     */
@Override
public JarContents<ClassNode> load() throws IOException {
    JarContents<ClassNode> contents = new JarContents<>();
    JarURLConnection con = (JarURLConnection) getLocation().openConnection();
    JarFile jar = con.getJarFile();
    Enumeration<JarEntry> entries = jar.entries();
    while (entries.hasMoreElements()) {
        JarEntry entry = entries.nextElement();
        try (InputStream is = jar.getInputStream(entry)) {
            byte[] bytes = read(is);
            if (entry.getName().endsWith(".class")) {
                ClassNode cn = create(bytes);
                contents.getClassContents().add(cn);
            } else {
                JarResource resource = new JarResource(entry.getName(), bytes);
                contents.getResourceContents().add(resource);
            }
        }
    }
    return contents;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) JarURLConnection(java.net.JarURLConnection) InputStream(java.io.InputStream) JarResource(the.bytecode.club.bytecodeviewer.bootloader.resource.jar.JarResource) JarContents(the.bytecode.club.bytecodeviewer.bootloader.resource.jar.contents.JarContents) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry)

Example 94 with JarURLConnection

use of java.net.JarURLConnection in project binnavi by google.

the class JarPluginLoader method getMainClassName.

/**
 * Tries to find the main class name out of a JAR package.
 *
 * @param file The JAR file whose main class is determined.
 *
 * @return The name of the main class of the JAR file or null.
 *
 * @throws IOException Thrown if any IO error occurs.
 */
private static String getMainClassName(final File file) throws IOException {
    final URL jarUrl = new URL("jar", "", file.toURI().toURL() + "!/");
    final JarURLConnection urlConnection = (JarURLConnection) jarUrl.openConnection();
    final Attributes attr = urlConnection.getMainAttributes();
    return attr == null ? null : attr.getValue(Attributes.Name.MAIN_CLASS);
}
Also used : JarURLConnection(java.net.JarURLConnection) Attributes(java.util.jar.Attributes) URL(java.net.URL)

Example 95 with JarURLConnection

use of java.net.JarURLConnection in project spring-boot by spring-projects.

the class LaunchedURLClassLoaderTests method resolveFromNestedWhileThreadIsInterrupted.

@Test
void resolveFromNestedWhileThreadIsInterrupted() throws Exception {
    File file = new File(this.tempDir, "test.jar");
    TestJarCreator.createTestJar(file);
    try (JarFile jarFile = new JarFile(file)) {
        URL url = jarFile.getUrl();
        try (LaunchedURLClassLoader loader = new LaunchedURLClassLoader(new URL[] { url }, null)) {
            Thread.currentThread().interrupt();
            URL resource = loader.getResource("nested.jar!/3.dat");
            assertThat(resource.toString()).isEqualTo(url + "nested.jar!/3.dat");
            URLConnection connection = resource.openConnection();
            try (InputStream input = connection.getInputStream()) {
                assertThat(input.read()).isEqualTo(3);
            }
            ((JarURLConnection) connection).getJarFile().close();
        } finally {
            Thread.interrupted();
        }
    }
}
Also used : InputStream(java.io.InputStream) JarFile(org.springframework.boot.loader.jar.JarFile) JarFile(org.springframework.boot.loader.jar.JarFile) File(java.io.File) URL(java.net.URL) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection) Test(org.junit.jupiter.api.Test)

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