Search in sources :

Example 41 with JarURLConnection

use of java.net.JarURLConnection in project Bytecoder by mirkosertic.

the class URLClassPath method check.

/*
     * Check whether the resource URL should be returned.
     * Throw exception on failure.
     * Called internally within this file.
     */
public static void check(URL url) throws IOException {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        URLConnection urlConnection = url.openConnection();
        Permission perm = urlConnection.getPermission();
        if (perm != null) {
            try {
                security.checkPermission(perm);
            } catch (SecurityException se) {
                // security managers
                if ((perm instanceof java.io.FilePermission) && perm.getActions().indexOf("read") != -1) {
                    security.checkRead(perm.getName());
                } else if ((perm instanceof java.net.SocketPermission) && perm.getActions().indexOf("connect") != -1) {
                    URL locUrl = url;
                    if (urlConnection instanceof JarURLConnection) {
                        locUrl = ((JarURLConnection) urlConnection).getJarFileURL();
                    }
                    security.checkConnect(locUrl.getHost(), locUrl.getPort());
                } else {
                    throw se;
                }
            }
        }
    }
}
Also used : JarURLConnection(java.net.JarURLConnection) Permission(java.security.Permission) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection) URL(java.net.URL)

Example 42 with JarURLConnection

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

the class ShippedCodeAccess method getShippedRuntimeCodePath.

/**
 * Returns path for the shipped code from the provided location. If location is plain file returns its absolute path
 * as string. If location is inside jar file, unpacks desired resource to the temporal location and returns path to
 * that location.
 *
 * @param rootName
 *            name of shipped root to be located
 * @return the path pointing to the shipped code
 */
protected static String getShippedRuntimeCodePath(String rootName) {
    try {
        URL resourceUrl = getResource(rootName);
        final URLConnection connection = resourceUrl.openConnection();
        if (connection instanceof JarURLConnection) {
            return recursivelyCopyContent((JarURLConnection) connection, rootName);
        }
        return new File(resourceUrl.toURI()).getCanonicalFile().getAbsolutePath().replace("\\", "\\\\");
    } catch (final Exception e) {
        throw new RuntimeException("Error while getting shipped code path.", e);
    }
}
Also used : JarURLConnection(java.net.JarURLConnection) JarFile(java.util.jar.JarFile) File(java.io.File) URL(java.net.URL) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException)

Example 43 with JarURLConnection

use of java.net.JarURLConnection in project mlib by myshzzx.

the class JarTest1 method t1.

@Test
@Ignore
public void t1() throws IOException {
    final URL url = new URL("jar:file:///L:/javafx-mx.jar!/");
    final JarURLConnection jar = (JarURLConnection) url.openConnection();
    final JarFile jarFile = jar.getJarFile();
    jarFile.stream().forEach(e -> {
        System.out.println(e.getName());
        System.out.println(e.getTime());
    });
}
Also used : JarURLConnection(java.net.JarURLConnection) JarFile(java.util.jar.JarFile) URL(java.net.URL) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 44 with JarURLConnection

use of java.net.JarURLConnection in project gate-core by GateNLP.

the class Tools method findSubclasses.

/**
 * Finds all subclasses of a given class or interface. It will only search
 * within the loaded packages and not the entire classpath.
 * @param parentClass the class for which subclasses are sought
 * @return a list of {@link Class} objects.
 */
public static List<Class<?>> findSubclasses(Class<?> parentClass) {
    Package[] packages = Package.getPackages();
    List<Class<?>> result = new ArrayList<Class<?>>();
    for (int i = 0; i < packages.length; i++) {
        String packageDir = packages[i].getName();
        // look in the file system
        if (!packageDir.startsWith("/"))
            packageDir = "/" + packageDir;
        packageDir = packageDir.replace('.', Strings.getPathSep().charAt(0));
        URL packageURL = Gate.getClassLoader().getResource(packageDir);
        if (packageURL != null) {
            File directory = Files.fileFromURL(packageURL);
            if (directory.exists()) {
                String[] files = directory.list();
                for (int j = 0; j < files.length; j++) {
                    // we are only interested in .class files
                    if (files[j].endsWith(".class")) {
                        // removes the .class extension
                        String classname = files[j].substring(0, files[j].length() - 6);
                        try {
                            // Try to create an instance of the object
                            Class<?> aClass = Class.forName(packages[i] + "." + classname, true, Gate.getClassLoader());
                            if (parentClass.isAssignableFrom(aClass))
                                result.add(aClass);
                        } catch (ClassNotFoundException cnfex) {
                        }
                    }
                }
            } else {
                // look in jar files
                try {
                    JarURLConnection conn = (JarURLConnection) packageURL.openConnection();
                    String starts = conn.getEntryName();
                    JarFile jFile = conn.getJarFile();
                    Enumeration<JarEntry> e = jFile.entries();
                    while (e.hasMoreElements()) {
                        String entryname = e.nextElement().getName();
                        if (entryname.startsWith(starts) && // not sub dir
                        (entryname.lastIndexOf('/') <= starts.length()) && entryname.endsWith(".class")) {
                            String classname = entryname.substring(0, entryname.length() - 6);
                            if (classname.startsWith("/"))
                                classname = classname.substring(1);
                            classname = classname.replace('/', '.');
                            try {
                                // Try to create an instance of the object
                                Class<?> aClass = Class.forName(packages[i] + "." + classname, true, Gate.getClassLoader());
                                if (parentClass.isAssignableFrom(aClass))
                                    result.add(aClass);
                            } catch (ClassNotFoundException cnfex) {
                            }
                        }
                    }
                } catch (java.io.IOException ioe) {
                }
            }
        }
    }
    return result;
}
Also used : JarURLConnection(java.net.JarURLConnection) ArrayList(java.util.ArrayList) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URL(java.net.URL) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 45 with JarURLConnection

use of java.net.JarURLConnection in project earth-snow by justlive1.

the class AbstractResourceLoader method findJarMatchPath.

/**
 * 获取资源下匹配的jar中的文件
 *
 * @param resource
 * @param rootUrl
 * @param subPattern
 * @return
 * @throws IOException
 */
protected List<SourceResource> findJarMatchPath(SourceResource resource, URL rootUrl, String subPattern) throws IOException {
    List<SourceResource> all = new LinkedList<>();
    URLConnection con = rootUrl.openConnection();
    JarFile jarFile;
    String jarFileUrl;
    String rootEntryPath;
    if (con instanceof JarURLConnection) {
        JarURLConnection jarCon = (JarURLConnection) con;
        jarFile = jarCon.getJarFile();
        jarFileUrl = jarCon.getJarFileURL().toExternalForm();
        JarEntry jarEntry = jarCon.getJarEntry();
        rootEntryPath = (jarEntry != null ? jarEntry.getName() : "");
    } else {
        String urlFile = rootUrl.getFile();
        int separatorLength = BaseConstants.WAR_URL_SEPARATOR.length();
        int separatorIndex = urlFile.indexOf(BaseConstants.WAR_URL_SEPARATOR);
        if (separatorIndex == -1) {
            separatorIndex = urlFile.indexOf(BaseConstants.JAR_URL_SEPARATOR);
            separatorLength = BaseConstants.JAR_URL_SEPARATOR.length();
        }
        if (separatorIndex != -1) {
            jarFileUrl = urlFile.substring(0, separatorIndex);
            rootEntryPath = urlFile.substring(separatorIndex + separatorLength);
            jarFile = this.getJarFile(jarFileUrl);
        } else {
            jarFile = new JarFile(urlFile);
            jarFileUrl = urlFile;
            rootEntryPath = "";
        }
    }
    try {
        if (log.isDebugEnabled()) {
            log.debug("Looking for matching resources in jar file [" + jarFileUrl + "]");
        }
        if (rootEntryPath.length() > 0 && !rootEntryPath.endsWith(BaseConstants.PATH_SEPARATOR)) {
            rootEntryPath += BaseConstants.PATH_SEPARATOR;
        }
        for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements(); ) {
            JarEntry entry = entries.nextElement();
            String entryPath = entry.getName();
            if (entryPath.startsWith(rootEntryPath)) {
                String relativePath = entryPath.substring(rootEntryPath.length());
                if (matcher.match(subPattern, relativePath)) {
                    all.add(resource.createRelative(relativePath));
                }
            }
        }
    } finally {
        jarFile.close();
    }
    return all;
}
Also used : SourceResource(justlive.earth.breeze.snow.common.base.io.SourceResource) JarURLConnection(java.net.JarURLConnection) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) LinkedList(java.util.LinkedList) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection)

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