Example usage for java.net URLClassLoader getURLs

List of usage examples for java.net URLClassLoader getURLs

Introduction

In this page you can find the example usage for java.net URLClassLoader getURLs.

Prototype

public URL[] getURLs() 

Source Link

Document

Returns the search path of URLs for loading classes and resources.

Usage

From source file:android.databinding.tool.util.GenerationalClassUtil.java

private static void buildCache() {
    L.d("building generational class cache");
    ClassLoader classLoader = GenerationalClassUtil.class.getClassLoader();
    Preconditions.check(classLoader instanceof URLClassLoader,
            "Class loader must be an" + "instance of URLClassLoader. %s", classLoader);
    //noinspection ConstantConditions
    final URLClassLoader urlClassLoader = (URLClassLoader) classLoader;
    sCache = new List[ExtensionFilter.values().length];
    for (ExtensionFilter filter : ExtensionFilter.values()) {
        sCache[filter.ordinal()] = new ArrayList();
    }/* ww w.j a  v  a2 s . co  m*/
    for (URL url : urlClassLoader.getURLs()) {
        L.d("checking url %s for intermediate data", url);
        try {
            final File file = new File(url.toURI());
            if (!file.exists()) {
                L.d("cannot load file for %s", url);
                continue;
            }
            if (file.isDirectory()) {
                // probably exported classes dir.
                loadFromDirectory(file);
            } else {
                // assume it is a zip file
                loadFomZipFile(file);
            }
        } catch (IOException | URISyntaxException e) {
            L.d("cannot open zip file from %s", url);
        }
    }
}

From source file:net.componio.opencms.junit.base.OpenCmsTestProperties.java

/**
 * Returns the absolute path name for the given relative path name if it was
 * found by the context Classloader of the current Thread.<p>
 *
 * The argument has to denote a resource within the Classloaders scope. A
 * <code>{@link java.net.URLClassLoader}</code> implementation for example
 * would try to match a given path name to some resource under it's URL
 * entries.<p>//w w w  . j  av a  2 s  . c  o m
 *
 * As the result is internally obtained as an URL it is reduced to a file
 * path by the call to
 * <code>{@link java.net.URL#getFile()}</code>. Therefore the returned
 * String will start with a '/' (no problem for java.io).<p>
 *
 * @param fileName the filename to return the path from the Classloader for
 *
 * @return the absolute path name for the given relative path name if it was
 * found by the context Classloader of the current Thread or an empty String
 * if it was not found
 *
 * @see Thread#getContextClassLoader()
 */
public static String getResourcePathFromClassloader(String fileName) {

    boolean isFolder = CmsResource.isFolder(fileName);
    String result = "";
    URL inputUrl = Thread.currentThread().getContextClassLoader().getResource(fileName);
    if (inputUrl != null) {
        // decode name here to avoid url encodings in path name
        result = CmsFileUtil.normalizePath(inputUrl);
        if (isFolder && !CmsResource.isFolder(result)) {
            result = result + '/';
        }
    } else {
        try {
            URLClassLoader cl = (URLClassLoader) Thread.currentThread().getContextClassLoader();
            URL[] paths = cl.getURLs();
            LOG.error(Messages.get().getBundle().key(Messages.ERR_MISSING_CLASSLOADER_RESOURCE_2, fileName,
                    Arrays.asList(paths)));
        } catch (Throwable t) {
            LOG.error(Messages.get().getBundle().key(Messages.ERR_MISSING_CLASSLOADER_RESOURCE_1, fileName));
        }
    }
    return result;
}

From source file:com.google.gwt.dev.resource.impl.ResourceOracleImpl.java

private static void addAllClassPathEntries(TreeLogger logger, ClassLoader classLoader,
        List<ClassPathEntry> classPath) {
    // URL is expensive in collections, so we use URI instead
    // See://from   w  w w  . j av  a 2 s .com
    // http://michaelscharf.blogspot.com/2006/11/javaneturlequals-and-hashcode-make.html
    Set<URI> seenEntries = new HashSet<URI>();
    for (; classLoader != null; classLoader = classLoader.getParent()) {
        if (classLoader instanceof URLClassLoader) {
            URLClassLoader urlClassLoader = (URLClassLoader) classLoader;
            URL[] urls = urlClassLoader.getURLs();
            for (URL url : urls) {
                URI uri;
                try {
                    uri = url.toURI();
                } catch (URISyntaxException e) {
                    logger.log(TreeLogger.WARN, "Error processing classpath URL '" + url + "'", e);
                    continue;
                }
                if (seenEntries.contains(uri)) {
                    continue;
                }
                seenEntries.add(uri);
                Throwable caught;
                try {
                    ClassPathEntry entry = createEntryForUrl(logger, url);
                    if (entry != null) {
                        classPath.add(entry);
                    }
                    continue;
                } catch (AccessControlException e) {
                    if (logger.isLoggable(TreeLogger.DEBUG)) {
                        logger.log(TreeLogger.DEBUG, "Skipping URL due to access restrictions: " + url);
                    }
                    continue;
                } catch (URISyntaxException e) {
                    caught = e;
                } catch (IOException e) {
                    caught = e;
                }
                logger.log(TreeLogger.WARN, "Error processing classpath URL '" + url + "'", caught);
            }
        }
    }
}

From source file:com.huawei.streaming.cql.DriverContext.java

/**
 * classpathjar//ww  w  . j  a  v a 2s.c o m
 *
 * @param pathsToRemove jar
 * @throws IOException jar
 */
private static void removeFromClassPath(String[] pathsToRemove) throws IOException {
    Thread curThread = Thread.currentThread();
    URLClassLoader loader = (URLClassLoader) curThread.getContextClassLoader();
    Set<URL> newPath = new HashSet<URL>(Arrays.asList(loader.getURLs()));

    if (pathsToRemove != null) {
        for (String onestr : pathsToRemove) {
            if (StringUtils.indexOf(onestr, FILE_PREFIX) == 0) {
                onestr = StringUtils.substring(onestr, CQLConst.I_7);
            }

            URL oneurl = (new File(onestr)).toURI().toURL();
            newPath.remove(oneurl);
        }
    }

    loader = new URLClassLoader(newPath.toArray(new URL[0]));
    curThread.setContextClassLoader(loader);
}

From source file:azkaban.common.utils.Utils.java

public static List<String> getClassLoaderDescriptions(ClassLoader loader) {
    List<String> values = new ArrayList<String>();
    while (loader != null) {
        if (loader instanceof URLClassLoader) {
            URLClassLoader urlLoader = (URLClassLoader) loader;
            for (URL url : urlLoader.getURLs())
                values.add(url.toString());
        } else {/* w ww .  j  a  va 2s .  co  m*/
            values.add(loader.getClass().getName());
        }
        loader = loader.getParent();
    }
    return values;
}

From source file:net.pms.external.ExternalFactory.java

private static void purgeCode(String mainClass, URL newUrl) {
    Class<?> clazz1 = null;

    for (Class<?> clazz : externalListenerClasses) {
        if (mainClass.equals(clazz.getCanonicalName())) {
            clazz1 = clazz;//www.  ja v  a  2  s .c  o m
            break;
        }
    }

    if (clazz1 == null) {
        return;
    }

    externalListenerClasses.remove(clazz1);
    ExternalListener remove = null;
    for (ExternalListener list : externalListeners) {
        if (list.getClass().equals(clazz1)) {
            remove = list;
            break;
        }
    }

    RendererConfiguration.resetAllRenderers();

    if (remove != null) {
        externalListeners.remove(remove);
        remove.shutdown();
        LooksFrame frame = (LooksFrame) PMS.get().getFrame();
        frame.getPt().removePlugin(remove);
    }

    for (int i = 0; i < 3; i++) {
        System.gc();
    }

    URLClassLoader cl = (URLClassLoader) clazz1.getClassLoader();
    URL[] urls = cl.getURLs();
    for (URL url : urls) {
        String mainClass1 = getMainClass(url);

        if (mainClass1 == null || !mainClass.equals(mainClass1)) {
            continue;
        }

        File f = url2file(url);
        File f1 = url2file(newUrl);

        if (f1 == null || f == null) {
            continue;
        }

        if (!f1.getName().equals(f.getName())) {
            addToPurgeFile(f);
        }
    }
}

From source file:com.tacitknowledge.util.discovery.ClasspathUtils.java

/**
 * Get the list of classpath components/* ww w  .ja v  a 2s  .  co  m*/
 *
 * @param ucl url classloader
 * @return List of classpath components
 */
private static List getUrlClassLoaderClasspathComponents(URLClassLoader ucl) {
    List components = new ArrayList();

    URL[] urls = new URL[0];

    // Workaround for running on JBoss with UnifiedClassLoader3 usage
    // We need to invoke getClasspath() method instead of getURLs()
    if (ucl.getClass().getName().equals("org.jboss.mx.loading.UnifiedClassLoader3")) {
        try {
            Method classPathMethod = ucl.getClass().getMethod("getClasspath", new Class[] {});
            urls = (URL[]) classPathMethod.invoke(ucl, new Object[0]);
        } catch (Exception e) {
            LogFactory.getLog(ClasspathUtils.class)
                    .debug("Error invoking getClasspath on UnifiedClassLoader3: ", e);
        }
    } else {
        // Use regular ClassLoader method to get classpath
        urls = ucl.getURLs();
    }

    for (int i = 0; i < urls.length; i++) {
        URL url = urls[i];
        components.add(getCanonicalPath(url.getPath()));
    }

    return components;
}

From source file:com.krawler.runtime.utils.URLClassLoaderUtil.java

public void addJarURL(URL u) throws Exception {
    try {//from w ww  .  ja va2 s .  c  o m

        URLClassLoader sysLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
        URL urls[] = sysLoader.getURLs();
        for (int i = 0; i < urls.length; i++) {
            if (StringUtils.equalsIgnoreCase(urls[i].toString(), u.toString())) {
                if (log.isDebugEnabled()) {
                    log.debug("URL " + u + " is already in the CLASSPATH");
                }
                return;
            }
        }
        Class sysclass = URLClassLoader.class;
        Method method = sysclass.getDeclaredMethod("addURL", parameters);
        method.setAccessible(true);
        method.invoke(sysLoader, new Object[] { u });
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("Error, could not add URL to system classloader" + e.getMessage());
    }

}

From source file:com.thoughtworks.go.domain.materials.tfs.TfsSDKCommandBuilderTest.java

private URL getLog4jJarFromClasspath() throws URISyntaxException {
    URLClassLoader classLoader = (URLClassLoader) this.getClass().getClassLoader();
    for (URL u : classLoader.getURLs()) {
        String jarPath = u.getPath();
        if (jarPath.endsWith(".jar") && jarPath.contains("log4j")) {
            return u;
        }/*  w  w w  .j a  v  a2s  .  c o m*/
    }
    return null;
}

From source file:com.olacabs.fabric.compute.builder.impl.JarScanner.java

private URL[] genUrls(URL[] jarFileURLs) {
    URLClassLoader loader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    URL[] originalURLs = loader.getURLs();
    Set<URL> mergedJarURLs = new HashSet<URL>(originalURLs.length + jarFileURLs.length);
    mergedJarURLs.addAll(Arrays.asList(originalURLs));
    mergedJarURLs.addAll(Arrays.asList(jarFileURLs));
    return mergedJarURLs.toArray(new URL[mergedJarURLs.size()]);
}