Search in sources :

Example 1 with JarURLConnection

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

the class MetaInfConfiguration method getTlds.

/**
     * Find all .tld files in the given jar.
     * 
     * @param uri the uri to jar file
     * @return the collection of tlds as url references  
     * @throws IOException if unable to scan the jar file
     */
public Collection<URL> getTlds(URI uri) throws IOException {
    HashSet<URL> tlds = new HashSet<URL>();
    String jarUri = uriJarPrefix(uri, "!/");
    URL url = new URL(jarUri);
    JarURLConnection jarConn = (JarURLConnection) url.openConnection();
    jarConn.setUseCaches(Resource.getDefaultUseCaches());
    JarFile jarFile = jarConn.getJarFile();
    Enumeration<JarEntry> entries = jarFile.entries();
    while (entries.hasMoreElements()) {
        JarEntry e = entries.nextElement();
        String name = e.getName();
        if (name.startsWith("META-INF") && name.endsWith(".tld")) {
            tlds.add(new URL(jarUri + name));
        }
    }
    if (!Resource.getDefaultUseCaches())
        jarFile.close();
    return tlds;
}
Also used : JarURLConnection(java.net.JarURLConnection) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URL(java.net.URL) HashSet(java.util.HashSet)

Example 2 with JarURLConnection

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

the class JarFileResource method exists.

/* ------------------------------------------------------------ */
/**
     * Returns true if the represented resource exists.
     */
@Override
public boolean exists() {
    if (_exists)
        return true;
    if (_urlString.endsWith("!/")) {
        String file_url = _urlString.substring(4, _urlString.length() - 2);
        try {
            return newResource(file_url).exists();
        } catch (Exception e) {
            LOG.ignore(e);
            return false;
        }
    }
    boolean check = checkConnection();
    // Is this a root URL?
    if (_jarUrl != null && _path == null) {
        // Then if it exists it is a directory
        _directory = check;
        return true;
    } else {
        // Can we find a file for it?
        boolean close_jar_file = false;
        JarFile jar_file = null;
        if (check)
            // Yes
            jar_file = _jarFile;
        else {
            // No - so lets look if the root entry exists.
            try {
                JarURLConnection c = (JarURLConnection) ((new URL(_jarUrl)).openConnection());
                c.setUseCaches(getUseCaches());
                jar_file = c.getJarFile();
                close_jar_file = !getUseCaches();
            } catch (Exception e) {
                LOG.ignore(e);
            }
        }
        // Do we need to look more closely?
        if (jar_file != null && _entry == null && !_directory) {
            // OK - we have a JarFile, lets look for the entry
            JarEntry entry = jar_file.getJarEntry(_path);
            if (entry == null) {
                // the entry does not exist
                _exists = false;
            } else if (entry.isDirectory()) {
                _directory = true;
                _entry = entry;
            } else {
                // Let's confirm is a file
                JarEntry directory = jar_file.getJarEntry(_path + '/');
                if (directory != null) {
                    _directory = true;
                    _entry = directory;
                } else {
                    // OK is a file
                    _directory = false;
                    _entry = entry;
                }
            }
        }
        if (close_jar_file && jar_file != null) {
            try {
                jar_file.close();
            } catch (IOException ioe) {
                LOG.ignore(ioe);
            }
        }
    }
    _exists = (_directory || _entry != null);
    return _exists;
}
Also used : JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) URL(java.net.URL)

Example 3 with JarURLConnection

use of java.net.JarURLConnection in project mongo-java-driver by mongodb.

the class ClientMetadataHelper method getDriverVersion.

private static String getDriverVersion() {
    String driverVersion = "unknown";
    try {
        CodeSource codeSource = InternalStreamConnectionInitializer.class.getProtectionDomain().getCodeSource();
        if (codeSource != null && codeSource.getLocation() != null) {
            String path = codeSource.getLocation().getPath();
            URL jarUrl = path.endsWith(".jar") ? new URL("jar:file:" + path + "!/") : null;
            if (jarUrl != null) {
                JarURLConnection jarURLConnection = (JarURLConnection) jarUrl.openConnection();
                Manifest manifest = jarURLConnection.getManifest();
                String version = (String) manifest.getMainAttributes().get(new Attributes.Name("Build-Version"));
                if (version != null) {
                    driverVersion = version;
                }
            }
        }
    } catch (SecurityException e) {
    // do nothing
    } catch (IOException e) {
    // do nothing
    }
    return driverVersion;
}
Also used : JarURLConnection(java.net.JarURLConnection) BsonString(org.bson.BsonString) IOException(java.io.IOException) CodeSource(java.security.CodeSource) Manifest(java.util.jar.Manifest) URL(java.net.URL)

Example 4 with JarURLConnection

use of java.net.JarURLConnection in project languagetool by languagetool-org.

the class JLanguageTool method getBuildDate.

/**
   * Returns the build date or {@code null} if not run from JAR.
   */
@Nullable
private static String getBuildDate() {
    try {
        URL res = JLanguageTool.class.getResource(JLanguageTool.class.getSimpleName() + ".class");
        if (res == null) {
            // this will happen on Android, see http://stackoverflow.com/questions/15371274/
            return null;
        }
        Object connObj = res.openConnection();
        if (connObj instanceof JarURLConnection) {
            JarURLConnection conn = (JarURLConnection) connObj;
            Manifest manifest = conn.getManifest();
            return manifest.getMainAttributes().getValue("Implementation-Date");
        } else {
            return null;
        }
    } catch (IOException e) {
        throw new RuntimeException("Could not get build date from JAR", e);
    }
}
Also used : JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) URL(java.net.URL) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with JarURLConnection

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

the class ClassNameCompleter method getClassNames.

public static String[] getClassNames() throws IOException {
    Set urls = new HashSet();
    for (ClassLoader loader = Thread.currentThread().getContextClassLoader(); loader != null; loader = loader.getParent()) {
        if (!(loader instanceof URLClassLoader)) {
            continue;
        }
        urls.addAll(Arrays.asList(((URLClassLoader) loader).getURLs()));
    }
    // Now add the URL that holds java.lang.String. This is because
    // some JVMs do not report the core classes jar in the list of
    // class loaders.
    Class[] systemClasses = new Class[] { String.class, javax.swing.JFrame.class };
    for (int i = 0; i < systemClasses.length; i++) {
        URL classURL = systemClasses[i].getResource("/" + systemClasses[i].getName().replace('.', '/') + clazzFileNameExtension);
        if (classURL != null) {
            URLConnection uc = classURL.openConnection();
            if (uc instanceof JarURLConnection) {
                urls.add(((JarURLConnection) uc).getJarFileURL());
            }
        }
    }
    Set classes = new HashSet();
    for (Iterator i = urls.iterator(); i.hasNext(); ) {
        URL url = (URL) i.next();
        try {
            File file = new File(url.getFile());
            if (file.isDirectory()) {
                Set files = getClassFiles(file.getAbsolutePath(), new HashSet(), file, new int[] { 200 });
                classes.addAll(files);
                continue;
            }
            if (!isJarFile(file)) {
                continue;
            }
            JarFile jf = new JarFile(file);
            for (Enumeration e = jf.entries(); e.hasMoreElements(); ) {
                JarEntry entry = (JarEntry) e.nextElement();
                if (entry == null) {
                    continue;
                }
                String name = entry.getName();
                if (isClazzFile(name)) {
                    /* only use class file */
                    classes.add(name);
                } else if (isJarFile(name)) {
                    classes.addAll(getClassNamesFromJar(name));
                } else {
                    continue;
                }
            }
        } catch (IOException e) {
            throw new IOException(String.format("Error reading classpath entry: %s", url), e);
        }
    }
    // now filter classes by changing "/" to "." and trimming the
    // trailing ".class"
    Set classNames = new TreeSet();
    for (Iterator i = classes.iterator(); i.hasNext(); ) {
        String name = (String) i.next();
        classNames.add(name.replace('/', '.').substring(0, name.length() - 6));
    }
    return (String[]) classNames.toArray(new String[classNames.size()]);
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Enumeration(java.util.Enumeration) JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URL(java.net.URL) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection) TreeSet(java.util.TreeSet) URLClassLoader(java.net.URLClassLoader) Iterator(java.util.Iterator) URLClassLoader(java.net.URLClassLoader) JarFile(java.util.jar.JarFile) File(java.io.File) HashSet(java.util.HashSet)

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