Search in sources :

Example 36 with JarURLConnection

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

the class Connections method getLastModified.

/**
 * Gets last modified date of the given {@link URL}
 *
 * @param url
 * @return last modified timestamp or <code>null</code> if not available
 * @throws IOException
 */
public static Time getLastModified(final URL url) throws IOException {
    // check if url points to a local file
    final File file = Files.getLocalFileFromUrl(url);
    if (file != null) {
        // in that case we can get the timestamp faster
        return Files.getLastModified(file);
    }
    // otherwise open the url and proceed
    URLConnection connection = url.openConnection();
    final long milliseconds;
    try {
        if (connection instanceof JarURLConnection) {
            JarURLConnection jarUrlConnection = (JarURLConnection) connection;
            URL jarFileUrl = jarUrlConnection.getJarFileURL();
            URLConnection jarFileConnection = jarFileUrl.openConnection();
            jarFileConnection.setDoInput(false);
            // get timestamp from JAR
            milliseconds = jarFileConnection.getLastModified();
        } else {
            // get timestamp from URL
            milliseconds = connection.getLastModified();
        }
        // return null if timestamp is unavailable
        if (milliseconds == 0) {
            return null;
        }
        // return UNIX timestamp
        return Time.millis(milliseconds);
    } finally {
        closeQuietly(connection);
    }
}
Also used : JarURLConnection(java.net.JarURLConnection) File(java.io.File) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection) URL(java.net.URL)

Example 37 with JarURLConnection

use of java.net.JarURLConnection in project android_packages_apps_Settings by crdroidandroid.

the class ClassScanner method getClassesForPackage.

public List<Class<?>> getClassesForPackage(String packageName) throws ClassNotFoundException {
    final List<Class<?>> classes = new ArrayList<>();
    try {
        final Enumeration<URL> resources = Thread.currentThread().getContextClassLoader().getResources(packageName.replace('.', '/'));
        if (!resources.hasMoreElements()) {
            return classes;
        }
        URL url = resources.nextElement();
        while (url != null) {
            final URLConnection connection = url.openConnection();
            if (connection instanceof JarURLConnection) {
                loadClassFromJar((JarURLConnection) connection, packageName, classes);
            } else {
                loadClassFromDirectory(new File(URLDecoder.decode(url.getPath(), "UTF-8")), packageName, classes);
            }
            if (resources.hasMoreElements()) {
                url = resources.nextElement();
            } else {
                break;
            }
        }
    } catch (final IOException e) {
        throw new ClassNotFoundException("Error when parsing " + packageName, e);
    }
    return classes;
}
Also used : JarURLConnection(java.net.JarURLConnection) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) File(java.io.File) URL(java.net.URL) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection)

Example 38 with JarURLConnection

use of java.net.JarURLConnection in project android_packages_apps_Settings by SudaMod.

the class ClassScanner method getClassesForPackage.

public List<Class<?>> getClassesForPackage(String packageName) throws ClassNotFoundException {
    final List<Class<?>> classes = new ArrayList<>();
    try {
        final Enumeration<URL> resources = Thread.currentThread().getContextClassLoader().getResources(packageName.replace('.', '/'));
        if (!resources.hasMoreElements()) {
            return classes;
        }
        URL url = resources.nextElement();
        while (url != null) {
            final URLConnection connection = url.openConnection();
            if (connection instanceof JarURLConnection) {
                loadClassFromJar((JarURLConnection) connection, packageName, classes);
            } else {
                loadClassFromDirectory(new File(URLDecoder.decode(url.getPath(), "UTF-8")), packageName, classes);
            }
            if (resources.hasMoreElements()) {
                url = resources.nextElement();
            } else {
                break;
            }
        }
    } catch (final IOException e) {
        throw new ClassNotFoundException("Error when parsing " + packageName, e);
    }
    return classes;
}
Also used : JarURLConnection(java.net.JarURLConnection) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) File(java.io.File) URL(java.net.URL) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection)

Example 39 with JarURLConnection

use of java.net.JarURLConnection in project incubator-rya by apache.

the class RyaBannerProvider method loadVersion.

/**
 * Loads the version number from the Rya Shell's MANIFEST.MF file.
 *
 * @return The version number of the Rya Shell.
 */
private String loadVersion() {
    final String className = getClass().getSimpleName() + ".class";
    final String classPath = getClass().getResource(className).toString();
    try {
        final URL classUrl = new URL(classPath);
        final JarURLConnection jarConnection = (JarURLConnection) classUrl.openConnection();
        final Manifest manifest = jarConnection.getManifest();
        final Attributes attributes = manifest.getMainAttributes();
        return attributes.getValue("Implementation-Version");
    } catch (final IOException e) {
        log.error("Could not load the application's version from it's manifest.", e);
    }
    return "UNKNOWN";
}
Also used : JarURLConnection(java.net.JarURLConnection) Attributes(java.util.jar.Attributes) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) URL(java.net.URL)

Example 40 with JarURLConnection

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

the class ClassPathScanner method checkJarFile.

private void checkJarFile(final ClassLoader classLoader, final URL url, final String pckgname) throws IOException {
    final JarURLConnection conn = (JarURLConnection) url.openConnection();
    final JarFile jarFile = conn.getJarFile();
    final Enumeration<JarEntry> entries = jarFile.entries();
    while (entries.hasMoreElements()) {
        final JarEntry jarEntry = entries.nextElement();
        String name = jarEntry.getName();
        if (name.endsWith(".class")) {
            name = name.substring(0, name.length() - 6).replace('/', '.');
            if (name.startsWith(pckgname)) {
                addClass(classLoader, name);
            }
        }
    }
}
Also used : JarURLConnection(java.net.JarURLConnection) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry)

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