List of usage examples for java.lang ClassLoader getResources
public Enumeration<URL> getResources(String name) throws IOException
From source file:org.mule.util.ClassUtils.java
public static Enumeration<URL> getResources(final String resourceName, final Class<?> callingClass) { Enumeration<URL> enumeration = AccessController.doPrivileged(new PrivilegedAction<Enumeration<URL>>() { public Enumeration<URL> run() { try { final ClassLoader cl = Thread.currentThread().getContextClassLoader(); return cl != null ? cl.getResources(resourceName) : null; } catch (IOException e) { return null; }//from www . j a v a 2s . c o m } }); if (enumeration == null) { enumeration = AccessController.doPrivileged(new PrivilegedAction<Enumeration<URL>>() { public Enumeration<URL> run() { try { return ClassUtils.class.getClassLoader().getResources(resourceName); } catch (IOException e) { return null; } } }); } if (enumeration == null) { enumeration = AccessController.doPrivileged(new PrivilegedAction<Enumeration<URL>>() { public Enumeration<URL> run() { try { return callingClass.getClassLoader().getResources(resourceName); } catch (IOException e) { return null; } } }); } return enumeration; }
From source file:grails.core.DefaultGrailsApplication.java
protected static void initialiseGroovyExtensionModules() { if (extensionMethodsInitialized) return;/* ww w . ja va 2 s . co m*/ extensionMethodsInitialized = true; Map<CachedClass, List<MetaMethod>> map = new HashMap<CachedClass, List<MetaMethod>>(); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); try { Enumeration<URL> resources = classLoader.getResources(ExtensionModuleScanner.MODULE_META_INF_FILE); while (resources.hasMoreElements()) { URL url = resources.nextElement(); if (url.getPath().contains("groovy-all")) { // already registered continue; } Properties properties = new Properties(); InputStream inStream = null; try { inStream = url.openStream(); properties.load(inStream); ((MetaClassRegistryImpl) GroovySystem.getMetaClassRegistry()) .registerExtensionModuleFromProperties(properties, classLoader, map); } catch (IOException e) { throw new GroovyRuntimeException("Unable to load module META-INF descriptor", e); } finally { if (inStream != null) { inStream.close(); } } } } catch (IOException ignored) { } for (Map.Entry<CachedClass, List<MetaMethod>> moduleMethods : map.entrySet()) { CachedClass cls = moduleMethods.getKey(); cls.addNewMopMethods(moduleMethods.getValue()); } }
From source file:voldemort.store.readonly.mr.utils.HadoopUtils.java
public static String printAllClassLoaderPaths(String fileName, ClassLoader loader) { try {/*from w w w. j av a 2s .c o m*/ for (Enumeration itr = loader.getResources(fileName); itr.hasMoreElements();) { URL url = (URL) itr.nextElement(); logger.info("findContainingJar finds url:" + url); } } catch (IOException e) { throw new RuntimeException(e); } return null; }
From source file:voldemort.store.readonly.mr.utils.HadoopUtils.java
public static String findContainingJar(String fileName, ClassLoader loader) { try {/*from w w w .j a va 2 s . c o m*/ for (Enumeration itr = loader.getResources(fileName); itr.hasMoreElements();) { URL url = (URL) itr.nextElement(); logger.info("findContainingJar finds url:" + url); if ("jar".equals(url.getProtocol())) { String toReturn = url.getPath(); if (toReturn.startsWith("file:")) { toReturn = toReturn.substring("file:".length()); } toReturn = URLDecoder.decode(toReturn, "UTF-8"); return toReturn.replaceAll("!.*$", ""); } } } catch (IOException e) { throw new RuntimeException(e); } return null; }
From source file:ml.shifu.dtrain.DTrainRequestProcessor.java
/** * Find a jar that contains a class of the same name, if any. It will return a jar file, even if that is not the * first thing on the class path that has a class with the same name. * /*from w w w . ja v a 2 s.co m*/ * @param myClass * the class to find * @return a jar file that contains the class, or null */ @SuppressWarnings("rawtypes") private static String findContainingJar(Class myClass) { ClassLoader loader = myClass.getClassLoader(); String classFile = myClass.getName().replaceAll("\\.", "/") + ".class"; try { for (Enumeration itr = loader.getResources(classFile); itr.hasMoreElements();) { URL url = (URL) itr.nextElement(); if ("jar".equals(url.getProtocol())) { String toReturn = url.getPath(); if (toReturn.startsWith("file:")) { toReturn = toReturn.substring("file:".length()); } // URLDecoder is a misnamed class, since it actually decodes // x-www-form-urlencoded MIME type rather than actual // URL encoding (which the file path has). Therefore it // would // decode +s to ' 's which is incorrect (spaces are actually // either unencoded or encoded as "%20"). Replace +s first, // so // that they are kept sacred during the decoding process. toReturn = toReturn.replaceAll("\\+", "%2B"); toReturn = URLDecoder.decode(toReturn, "UTF-8"); return toReturn.replaceAll("!.*$", ""); } else if ("file".equals(url.getProtocol())) { String toReturn = url.getPath(); toReturn = toReturn.replaceAll("\\+", "%2B"); toReturn = URLDecoder.decode(toReturn, "UTF-8"); return toReturn.replaceAll("!.*$", ""); } } } catch (IOException e) { throw new RuntimeException(e); } return null; }
From source file:org.geoserver.ManifestLoader.java
private static Map<String, Manifest> loadManifest(final ClassLoader loader) throws IllegalArgumentException { if (loader == null) { throw new IllegalArgumentException("Unable to run with null arguments"); }//from ww w. ja va2 s . c o m Map<String, Manifest> manifests = new HashMap<String, Manifest>(); try { Enumeration<URL> resources = loader.getResources("META-INF/MANIFEST.MF"); while (resources.hasMoreElements()) { InputStream is = null; try { URL resource = resources.nextElement(); if (LOGGER.isLoggable(Level.FINE)) LOGGER.fine("Loading resources: " + resource.getFile()); is = resource.openStream(); manifests.put(resource.getPath(), new Manifest(is)); } catch (IOException e) { // handle LOGGER.log(java.util.logging.Level.SEVERE, "Error loading resources file: " + e.getLocalizedMessage(), e); } finally { IOUtils.closeQuietly(is); } } } catch (IOException e) { LOGGER.log(java.util.logging.Level.SEVERE, "Error loading resources file: " + e.getLocalizedMessage(), e); } return manifests; }
From source file:ml.shifu.guagua.yarn.GuaguaYarnClient.java
/** * Find a jar that contains a class of the same name, if any. It will return a jar file, even if that is not the * first thing on the class path that has a class with the same name. * //from w w w.j a v a 2 s .c o m * @param my_class * the class to find. * @return a jar file that contains the class, or null. * @throws IOException * in case when read class file. */ private static String findContainingJar(Class<?> my_class) { ClassLoader loader = my_class.getClassLoader(); String class_file = my_class.getName().replaceAll("\\.", "/") + ".class"; try { for (Enumeration<?> itr = loader.getResources(class_file); itr.hasMoreElements();) { URL url = (URL) itr.nextElement(); if ("jar".equals(url.getProtocol())) { String toReturn = url.getPath(); if (toReturn.startsWith("file:")) { toReturn = toReturn.substring("file:".length()); } // URLDecoder is a misnamed class, since it actually decodes // x-www-form-urlencoded MIME type rather than actual // URL encoding (which the file path has). Therefore it would // decode +s to ' 's which is incorrect (spaces are actually // either unencoded or encoded as "%20"). Replace +s first, so // that they are kept sacred during the decoding process. toReturn = toReturn.replaceAll("\\+", "%2B"); toReturn = URLDecoder.decode(toReturn, "UTF-8"); return toReturn.replaceAll("!.*$", ""); } } } catch (IOException e) { throw new RuntimeException(e); } return null; }
From source file:org.getobjects.foundation.NSJavaRuntime.java
public static URL[] NSFindPackageLocations(ClassLoader _loader, String _pkg) { if (_pkg == null) return null; if (_loader == null) _loader = Thread.currentThread().getContextClassLoader(); String path = _pkg.replace('.', '/'); Enumeration<URL> resources; try {//w w w .jav a 2 s . c o m /* Here we find all "directories" for the package name. That is, a class * could be stored in different entities of the CLASSPATH! */ resources = _loader.getResources(path); } catch (IOException e) { if (log.isInfoEnabled()) { /* could be a test for existence, hence no warn log */ log.info("could not get resource dirs of package: " + path); } return null; } if (resources == null || !resources.hasMoreElements()) return null; URL firstURL = resources.nextElement(); if (!resources.hasMoreElements()) /* optimization */ return firstURL != null ? new URL[] { firstURL } : null; ArrayList<URL> urls = new ArrayList<URL>(4); urls.add(firstURL); while (resources.hasMoreElements()) urls.add(resources.nextElement()); return urls.toArray(new URL[urls.size()]); }
From source file:com.streamsets.datacollector.cluster.ClusterProviderImpl.java
private static Properties readDataCollectorProperties(ClassLoader cl) throws IOException { Properties properties = new Properties(); while (cl != null) { Enumeration<URL> urls = cl.getResources(DATA_COLLECTOR_LIBRARY_PROPERTIES); if (urls != null) { while (urls.hasMoreElements()) { URL url = urls.nextElement(); LOG.trace("Loading data collector library properties: {}", url); try (InputStream inputStream = url.openStream()) { properties.load(inputStream); }// w w w. j a v a 2s . c o m } } cl = cl.getParent(); } LOG.trace("Final properties: {} ", properties); return properties; }
From source file:org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil.java
/** * Find a jar that contains a class of the same name, if any. It will return * a jar file, even if that is not the first thing on the class path that * has a class with the same name. Looks first on the classpath and then in * the <code>packagedClasses</code> map. * @param my_class the class to find./*ww w . j a v a2 s. co m*/ * @return a jar file that contains the class, or null. * @throws IOException */ private static String findContainingJar(Class<?> my_class, Map<String, String> packagedClasses) throws IOException { ClassLoader loader = my_class.getClassLoader(); String class_file = my_class.getName().replaceAll("\\.", "/") + ".class"; // first search the classpath for (Enumeration<URL> itr = loader.getResources(class_file); itr.hasMoreElements();) { URL url = itr.nextElement(); if ("jar".equals(url.getProtocol())) { String toReturn = url.getPath(); if (toReturn.startsWith("file:")) { toReturn = toReturn.substring("file:".length()); } // URLDecoder is a misnamed class, since it actually decodes // x-www-form-urlencoded MIME type rather than actual // URL encoding (which the file path has). Therefore it would // decode +s to ' 's which is incorrect (spaces are actually // either unencoded or encoded as "%20"). Replace +s first, so // that they are kept sacred during the decoding process. toReturn = toReturn.replaceAll("\\+", "%2B"); toReturn = URLDecoder.decode(toReturn, "UTF-8"); return toReturn.replaceAll("!.*$", ""); } } // now look in any jars we've packaged using JarFinder. Returns null when // no jar is found. return packagedClasses.get(class_file); }