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:com.clustercontrol.plugin.ClassUtils.java

/**
 * ??URLCLASSPATH??<br/>/*  www  .  jav a2 s.c  o m*/
 * @param ?URL
 * @throws IOException
 */
public static void addURL(URL u) throws IOException {
    URLClassLoader sysLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();

    for (URL url : sysLoader.getURLs()) {
        if (url.toString().equals(u.toString())) {
            log.info("URL " + u + " is already in CLASSPATH");
            return;
        }
    }

    Class<URLClassLoader> sysClass = URLClassLoader.class;
    try {
        Method method = sysClass.getDeclaredMethod("addURL", new Class[] { URL.class });
        method.setAccessible(true);
        method.invoke(sysLoader, new Object[] { u });
    } catch (Throwable t) {
        log.warn(t.getMessage(), t);
        throw new IOException("could not add URL " + u + " to CLASSPATH");
    }
}

From source file:com.streamsets.pipeline.stage.destination.mapreduce.MapreduceUtils.java

/**
 * Add jars whose names contain the given patterns to the job's classpath.
 *
 * Searches the {@link ClassLoader} - which must be an instance of {@link URLClassLoader} or an
 * {@link IllegalStateException} will be thrown - of the {@link MapreduceUtils} class itself for the jar matching
 * a pattern indicated each arguments.//from w ww .  ja v  a2  s  . co  m
 *
 * Each supplied jarPattern is treated as a case-insensitive substring for a full jar path.  This method expects
 * to find at least one jar matching each substring, or an {@link IllegalArgumentException} will be thrown.  If more
 * than one jar is matched, then the result will depend on the allowMultiple parameter - if true, then all will be
 * added, while if false, an {@link IllegalArgumentException} will be thrown.
 *
 * Care has <b>NOT</b> been taken to optimize the performance of this method; the runtime is M * N where M is the
 * number of entries on the classpath and N is the number of given jarPatterns.  It is assumed that this method
 * will be called infrequently and with relatively few jarPatterns.
 *
 * @param conf the Hadoop conf for the MapReduce job
 * @param allowMultiple whether multiple matching jars are allowed to be added for a single pattern
 * @param jarPatterns the patterns to search for within the classpath
 */
public static void addJarsToJob(Configuration conf, boolean allowMultiple, String... jarPatterns) {
    final ClassLoader loader = MapreduceUtils.class.getClassLoader();
    if (!(loader instanceof URLClassLoader)) {
        throw new IllegalStateException(String.format(
                "ClassLoader for %s is not an instance of URLClassLoader (it is %s), and thus this method cannot be used",
                MapreduceUtils.class.getCanonicalName(), loader.getClass().getCanonicalName()));
    }
    final URLClassLoader urlClassLoader = (URLClassLoader) loader;

    addJarsToJob(conf, allowMultiple, urlClassLoader.getURLs(), jarPatterns);
}

From source file:com.katsu.dwm.reflection.JarUtils.java

public static File getRootApplicationContentPath() {
    URLClassLoader sysloader = (URLClassLoader) JarUtils.class.getClassLoader();
    URL[] urls = sysloader.getURLs();
    String classesPath = "WEB-INF/classes/";
    if (urls != null) {
        for (URL url : urls) {
            try {
                if (url.getPath().endsWith(classesPath)) {
                    return new File(url.toURI()).getParentFile().getParentFile();
                }//from w ww  . ja va  2 s.  co m
            } catch (Exception e) {
                logger.error(e);
            }
        }
    }
    return null;
}

From source file:com.katsu.dwm.reflection.JarUtils.java

/**
 * Retrieve all jar files in directoy/*from   www  . jav a  2s.c o m*/
 * @return array of jar {@link File Files}
 */
public static List<File> getJarFiles() {
    URLClassLoader sysloader = (URLClassLoader) JarUtils.class.getClassLoader();
    URL[] urls = sysloader.getURLs();
    List<File> result = new ArrayList<File>();
    if (urls != null) {
        for (URL url : urls) {
            try {
                logger.trace("URL detected: " + url);
                if (url.getPath().toLowerCase().endsWith(".jar")) {
                    result.add(new File(url.toURI()));
                }
            } catch (Exception e) {
                logger.error(e);
            }
        }
    }
    return result;
}

From source file:org.usergrid.rest.AbstractRestTest.java

public static void dumpClasspath(ClassLoader loader) {
    System.out.println("Classloader " + loader + ":");

    if (loader instanceof URLClassLoader) {
        URLClassLoader ucl = (URLClassLoader) loader;
        System.out.println("\t" + Arrays.toString(ucl.getURLs()));
    } else {//from ww  w.j  a  v a2  s.co  m
        System.out.println("\t(cannot display components as not a URLClassLoader)");
    }

    if (loader.getParent() != null) {
        dumpClasspath(loader.getParent());
    }
}

From source file:jp.terasoluna.fw.batch.unit.util.ClassLoaderUtils.java

/**
 * <pre>/*from   ww  w . ja  v a  2s  . co  m*/
 * ??????url???????
 * ???????
 * 
 * ????
 * ????????????
 * {@link #resetClassLoader()}???????
 * </pre>
 * 
 * @param url ?
 */
public static void addClassPath(URL url) {
    Assert.notNull(url);
    ClassLoader cl = getClassLoader();

    URLClassLoader newCl = null;
    if (cl instanceof URLClassLoader) {
        @SuppressWarnings("resource")
        URLClassLoader ucl = (URLClassLoader) cl;
        newCl = new URLClassLoader(ucl.getURLs(), cl);
    } else {
        newCl = new URLClassLoader(null, cl);
    }

    Method method = getAddURLMethod();

    try {
        method.invoke(newCl, url);
    } catch (Exception e) {
        throw new UTRuntimeException(e);
    }

    previousClassLoader = cl;
    setClassLoader(newCl);
}

From source file:com.qwazr.compiler.JavaCompiler.java

private final static String buildClassPath(File[] classPathArray, Collection<URL> urlCollection)
        throws MalformedURLException, URISyntaxException {
    final List<String> classPathes = new ArrayList<>();

    URLClassLoader classLoader = (URLClassLoader) URLClassLoader.getSystemClassLoader();
    if (classLoader != null && classLoader.getURLs() != null) {
        for (URL url : classLoader.getURLs()) {
            String path = new File(url.toURI()).getAbsolutePath();
            classPathes.add(path);/*from ww w  .j  a  va2  s.  c om*/
            urlCollection.add(url);
        }
    }

    if (classPathArray != null) {
        for (File classPathFile : classPathArray) {
            if (classPathFile.isDirectory()) {
                for (File f : classPathFile.listFiles((FileFilter) FileFileFilter.FILE)) {
                    classPathes.add(f.getAbsolutePath());
                    urlCollection.add(f.toURI().toURL());
                }
            } else if (classPathFile.isFile()) {
                classPathes.add(classPathFile.getAbsolutePath());
                urlCollection.add(classPathFile.toURI().toURL());
            }
        }
    }
    if (classPathes.isEmpty())
        return null;
    return StringUtils.join(classPathes, File.pathSeparator);
}

From source file:com.revolsys.util.JavaBeanUtil.java

public static boolean isDefinedInClassLoader(final ClassLoader classLoader, final URL resourceUrl) {
    if (classLoader instanceof URLClassLoader) {
        final String resourceUrlString = resourceUrl.toString();
        final URLClassLoader urlClassLoader = (URLClassLoader) classLoader;
        for (final URL url : urlClassLoader.getURLs()) {
            if (resourceUrlString.contains(url.toString())) {
                return true;
            }/*from   w w w.  ja  v a  2 s.  co  m*/
        }
        return false;
    } else {
        return true;
    }
}

From source file:com.ibm.team.build.internal.hjplugin.RTCFacadeFactory.java

private static URL getHjplugin_rtcJar(ClassLoader originalClassLoader, String fullClassName,
        PrintStream debugLog) {/*from   w  w  w  .  j ava  2 s . co  m*/
    if (originalClassLoader instanceof URLClassLoader) {
        URLClassLoader urlClassLoader = (URLClassLoader) originalClassLoader;
        URL[] originalURLs = urlClassLoader.getURLs();
        for (URL url : originalURLs) {
            String file = url.getFile();
            if (file.contains("com.ibm.team.build.hjplugin-rtc")) { //$NON-NLS-1$ //$NON-NLS-2$
                debug(debugLog, "Found hjplugin-rtc jar " + url.getFile()); //$NON-NLS-1$
                return url;
            }
        }
        debug(debugLog, "Did not find hjplugin-rtc jar from URLClassLoader"); //$NON-NLS-1$
    }
    String realClassName = fullClassName.replace('.', '/') + ".class"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    URL url = originalClassLoader.getResource(realClassName);
    debug(debugLog, "Found " + realClassName + " in " + url.toString()); //$NON-NLS-1$ //$NON-NLS-2$
    try {
        URLConnection connection = url.openConnection();
        if (connection instanceof JarURLConnection) {
            JarURLConnection jarConnection = (JarURLConnection) connection;
            debug(debugLog, "hjplugin-rtc jar from the connection " + jarConnection.getJarFileURL()); //$NON-NLS-1$
            return jarConnection.getJarFileURL();
        }
    } catch (IOException e) {
        debug(debugLog, "Unable to obtain URLConnection ", e); //$NON-NLS-1$ 
    }
    debug(debugLog, "Unable to find hjplugin-rtc.jar"); //$NON-NLS-1$ 
    return null;
}

From source file:com.googlecode.fightinglayoutbugs.FindBugsRunner.java

private static List<String> getClassPath(Class<?> suiteClass) {
    List<String> classPath = new ArrayList<String>();
    URLClassLoader classLoader = (URLClassLoader) suiteClass.getClassLoader();
    do {/*from www  .  jav  a  2  s . c  om*/
        for (URL url : classLoader.getURLs()) {
            final String temp = url.toString();
            if (temp.startsWith("file:")) {
                @SuppressWarnings("deprecation")
                String path = URLDecoder.decode(temp.substring("file:".length()));
                classPath.add(path);
            } else {
                throw new RuntimeException(
                        "Don't know how to convert class path URL '" + temp + "' into a path.");
            }
        }
        ClassLoader parentClassLoader = classLoader.getParent();
        classLoader = (parentClassLoader instanceof URLClassLoader && parentClassLoader != classLoader
                ? (URLClassLoader) parentClassLoader
                : null);
    } while (classLoader != null);
    return classPath;
}