List of usage examples for java.lang ClassLoader getSystemResources
public static Enumeration<URL> getSystemResources(String name) throws IOException
From source file:com.iflytek.edu.cloud.frame.web.servlet.PrintProjectVersionServlet.java
@Override public void init(ServletConfig config) throws ServletException { StringBuilder sBuilder = new StringBuilder("\n"); try {/*from www . ja v a2s . c o m*/ Enumeration<java.net.URL> urls; ClassLoader classLoader = findClassLoader(); if (classLoader != null) { urls = classLoader.getResources("META-INF/res/env.properties"); } else { urls = ClassLoader.getSystemResources("META-INF/res/env.properties"); } if (urls != null) { while (urls.hasMoreElements()) { java.net.URL url = urls.nextElement(); try { BufferedReader reader = new BufferedReader( new InputStreamReader(url.openStream(), "utf-8")); Properties properties = new Properties(); properties.load(reader); sBuilder.append("??").append(properties.getProperty("project.name")) .append(", "); sBuilder.append("").append(properties.getProperty("build.version")) .append(", "); sBuilder.append("").append(properties.getProperty("build.time")) .append(".\n"); } catch (Throwable t) { logger.error(t.getMessage(), t); } } } } catch (Throwable t) { logger.error(t.getMessage(), t); } String projcode = PropertiesConfiguration.getProjCode(); if (StringUtils.hasLength(projcode)) { sBuilder.append("superdiamond client info: project=").append(projcode); sBuilder.append(", profile=").append(PropertiesConfiguration.getProfile()); sBuilder.append(", host=").append(PropertiesConfiguration.getHost()); sBuilder.append(", port=").append(PropertiesConfiguration.getPort()).append(".\n"); } String info = sBuilder.toString(); System.out.println( "==========================================================================================================================="); System.out.println(info); System.out.println( "==========================================================================================================================="); }
From source file:com.springframework.core.io.support.SpringFactoriesLoader.java
/** * Load the fully qualified class names of factory implementations of the * given type from {@value #FACTORIES_RESOURCE_LOCATION}, using the given * class loader.//from w w w.ja v a2 s . c o m * @param factoryClass the interface or abstract class representing the factory * @param classLoader the ClassLoader to use for loading resources; can be * {@code null} to use the default * @see #loadFactories * @throws IllegalArgumentException if an error occurs while loading factory names */ public static List<String> loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) { String factoryClassName = factoryClass.getName(); try { Enumeration<URL> urls = (classLoader != null ? classLoader.getResources(FACTORIES_RESOURCE_LOCATION) : ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION)); List<String> result = new ArrayList<String>(); while (urls.hasMoreElements()) { URL url = urls.nextElement(); Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url)); String factoryClassNames = properties.getProperty(factoryClassName); result.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(factoryClassNames))); } return result; } catch (IOException ex) { throw new IllegalArgumentException("Unable to load [" + factoryClass.getName() + "] factories from location [" + FACTORIES_RESOURCE_LOCATION + "]", ex); } }
From source file:com.sfxie.extension.spring4.properties.PropertiesLoaderUtils.java
/** * Load all properties from the specified class path resource * (in ISO-8859-1 encoding), using the given class loader. * <p>Merges properties if more than one resource of the same name * found in the class path.// ww w . j av a 2 s . co m * @param resourceName the name of the class path resource * @param classLoader the ClassLoader to use for loading * (or {@code null} to use the default class loader) * @return the populated Properties instance * @throws IOException if loading failed */ public static Properties loadAllProperties(String resourceName, ClassLoader classLoader) throws IOException { Assert.notNull(resourceName, "Resource name must not be null"); ClassLoader classLoaderToUse = classLoader; if (classLoaderToUse == null) { classLoaderToUse = ClassUtils.getDefaultClassLoader(); } Enumeration<URL> urls = (classLoaderToUse != null ? classLoaderToUse.getResources(resourceName) : ClassLoader.getSystemResources(resourceName)); Properties props = new Properties(); while (urls.hasMoreElements()) { URL url = urls.nextElement(); URLConnection con = url.openConnection(); ResourceUtils.useCachesIfNecessary(con); InputStream is = con.getInputStream(); try { if (resourceName != null && resourceName.endsWith(XML_FILE_EXTENSION)) { props.loadFromXML(is); } else { props.load(is); } } finally { is.close(); } } return props; }
From source file:com.weibo.api.motan.core.extension.ExtensionLoader.java
private ConcurrentMap<String, Class<T>> loadExtensionClasses(String prefix) { String fullName = prefix + type.getName(); List<String> classNames = new ArrayList<String>(); try {//from ww w .j a v a 2 s . c o m Enumeration<URL> urls; if (classLoader == null) { urls = ClassLoader.getSystemResources(fullName); } else { urls = classLoader.getResources(fullName); } if (urls == null || !urls.hasMoreElements()) { return new ConcurrentHashMap<String, Class<T>>(); } while (urls.hasMoreElements()) { URL url = urls.nextElement(); parseUrl(type, url, classNames); } } catch (Exception e) { throw new MotanFrameworkException( "ExtensionLoader loadExtensionClasses error, prefix: " + prefix + " type: " + type.getClass(), e); } return loadClass(classNames); }
From source file:edu.buffalo.cse.pigout.PigOutServer.java
private URL locateJarFromResources(String jarName) throws IOException { Enumeration<URL> urls = ClassLoader.getSystemResources(jarName); URL resourceLocation = null;/* w w w .j a v a 2 s . c om*/ if (urls.hasMoreElements()) { resourceLocation = urls.nextElement(); } if (urls.hasMoreElements()) { StringBuffer sb = new StringBuffer("Found multiple resources that match "); sb.append(jarName); sb.append(": "); sb.append(resourceLocation); while (urls.hasMoreElements()) { sb.append(urls.nextElement()); sb.append("; "); } log.debug(sb.toString()); } return resourceLocation; }
From source file:com.sworddance.util.CUtilities.java
/** * @param searchRoot//from w w w. j a va 2 s . com * @param fileName * @param optional * @param searchPaths * @return a de-duped Enumeration<URL> never returns null. */ private static Collection<URL> getResources(Object searchRoot, String fileName, boolean optional, List<String> searchPaths) { ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); ClassLoader searchRootClassLoader = searchRoot != null ? searchRoot.getClass().getClassLoader() : null; HashMap<String, URL> results = new HashMap<String, URL>(); for (String searchPath : searchPaths) { Enumeration<URL> resource = null; if (searchRootClassLoader != null) { try { resource = searchRootClassLoader.getResources(searchPath); for (URL url : NotNullIterator.<URL>newNotNullIterator(resource)) { results.put(url.toString(), url); } } catch (IOException e) { // TODO what? } } if (contextClassLoader != null) { try { resource = contextClassLoader.getResources(searchPath); for (URL url : NotNullIterator.<URL>newNotNullIterator(resource)) { results.put(url.toString(), url); } } catch (IOException e) { // TODO what? } } if (resource == null) { try { resource = ClassLoader.getSystemResources(searchPath); for (URL url : NotNullIterator.<URL>newNotNullIterator(resource)) { results.put(url.toString(), url); } } catch (IOException e) { // TODO what? } } } if (isEmpty(results) && !optional) { if (fileName != null) { throw new ApplicationNullPointerException(fileName, " not found in ", join(searchPaths, ","), " java.class.path=", System.getProperty("java.class.path"), " java.library.path=", System.getProperty("java.library.path"), " searchRoot =", getClassSafely(searchRoot)); } else { throw new ApplicationNullPointerException("No listed file found ", join(searchPaths, ","), " java.class.path=", System.getProperty("java.class.path"), " java.library.path=", System.getProperty("java.library.path"), " searchRoot =", getClassSafely(searchRoot)); } } else { return results.values(); } }
From source file:onl.netfishers.netshot.Database.java
/** * List classes in a given package.//from w w w. java 2 s . c o m * * @param packageName the package name * @return the list * @throws ClassNotFoundException the class not found exception * @throws IOException Signals that an I/O exception has occurred. */ public static List<Class<?>> listClassesInPackage(String packageName) throws ClassNotFoundException, IOException { String path = packageName.replace('.', '/'); Enumeration<URL> resources = ClassLoader.getSystemResources(path); List<String> dirs = new ArrayList<String>(); while (resources.hasMoreElements()) { URL resource = resources.nextElement(); dirs.add(URLDecoder.decode(resource.getFile(), "UTF-8")); } TreeSet<String> classes = new TreeSet<String>(); for (String directory : dirs) { classes.addAll(findClasses(directory, packageName)); } ArrayList<Class<?>> classList = new ArrayList<Class<?>>(); for (String clazz : classes) { classList.add(Class.forName(clazz)); } return classList; }
From source file:org.apache.blur.command.BaseCommandManager.java
protected void lookForCommandsToRegisterInClassPath() throws IOException { Enumeration<URL> systemResources = ClassLoader .getSystemResources(META_INF_SERVICES_ORG_APACHE_BLUR_COMMAND_COMMANDS); loadCommandClasses(systemResources, getClass().getClassLoader(), BigInteger.ZERO); }
From source file:org.ebayopensource.turmeric.plugins.maven.resources.ResourceLocator.java
private Location lookInClasspath(String pathref) throws MojoExecutionException { log.debug("Looking for resource in project classpath: " + pathref); if (log.isDebugEnabled()) { StringBuilder dbg = new StringBuilder(); dbg.append("System.getProperty('java.class.path')="); String rawcp = System.getProperty("java.class.path"); for (String cp : rawcp.split(File.pathSeparator)) { dbg.append("\n ").append(cp); }/* w w w .j ava 2 s . c o m*/ log.debug(dbg.toString()); ClassLoader cl = this.getClass().getClassLoader(); if (cl instanceof URLClassLoader) { dbg = new StringBuilder(); dbg.append("URLClassLoader("); dbg.append(cl.getClass().getName()); dbg.append("):"); URLClassLoader ucl = (URLClassLoader) cl; for (URL url : ucl.getURLs()) { dbg.append("\n ").append(url.toExternalForm()); } log.debug(dbg.toString()); } } List<URL> resources = new ArrayList<URL>(); try { Enumeration<URL> enurls = ClassLoader.getSystemResources(pathref); if (enurls != null) { while (enurls.hasMoreElements()) { URL url = enurls.nextElement(); if (!resources.contains(url)) { resources.add(url); } } } addFoundResource(resources, pathref, Thread.currentThread().getContextClassLoader()); addFoundResource(resources, pathref, this.getClass().getClassLoader()); if (resources.isEmpty()) { log.debug("NOT FOUND in project classpath"); return null; } if (resources.size() > 1) { log.warn("Found more than 1 classpath entry for: " + pathref); for (URL url : resources) { log.warn(" + " + url.toExternalForm()); } } URI uri = resources.get(0).toURI(); log.debug("FOUND resource in project classpath: " + uri); return new Location(uri, project); } catch (IOException e) { throw new MojoExecutionException( "Unable to process resource lookup in project classpath: " + e.getMessage(), e); } catch (URISyntaxException e) { throw new MojoExecutionException( "Unable to process resource lookup in project classpath: " + e.getMessage(), e); } }
From source file:org.springframework.core.io.support.PathMatchingResourcePatternResolver.java
/** * Find all class location resources with the given path via the ClassLoader. * Called by {@link #findAllClassPathResources(String)}. * @param path the absolute path within the classpath (never a leading slash) * @return a mutable Set of matching Resource instances * @since 4.1.1//from ww w. j av a2 s .c o m */ protected Set<Resource> doFindAllClassPathResources(String path) throws IOException { Set<Resource> result = new LinkedHashSet<>(16); ClassLoader cl = getClassLoader(); Enumeration<URL> resourceUrls = (cl != null ? cl.getResources(path) : ClassLoader.getSystemResources(path)); while (resourceUrls.hasMoreElements()) { URL url = resourceUrls.nextElement(); result.add(convertClassLoaderURL(url)); } if ("".equals(path)) { // The above result is likely to be incomplete, i.e. only containing file system references. // We need to have pointers to each of the jar files on the classpath as well... addAllClassLoaderJarRoots(cl, result); } return result; }