List of usage examples for java.lang ClassLoader getResources
public Enumeration<URL> getResources(String name) throws IOException
From source file:com.nxttxn.vramel.impl.converter.AnnotationTypeConverterLoader.java
protected void findPackages(Set<String> packages, ClassLoader classLoader) throws IOException { Enumeration<URL> resources = classLoader.getResources(META_INF_SERVICES); while (resources.hasMoreElements()) { URL url = resources.nextElement(); String path = url.getPath(); if (!visitedURIs.contains(path)) { // remember we have visited this uri so we wont read it twice visitedURIs.add(path);/*from w ww. ja va 2 s .co m*/ LOG.debug("Loading file {} to retrieve list of packages, from url: {}", META_INF_SERVICES, url); BufferedReader reader = IOHelper.buffered(new InputStreamReader(url.openStream())); try { while (true) { String line = reader.readLine(); if (line == null) { break; } line = line.trim(); if (line.startsWith("#") || line.length() == 0) { continue; } tokenize(packages, line); } } finally { IOHelper.close(reader, null, LOG); } } } }
From source file:org.apache.hadoop.sqoop.orm.CompilationManager.java
private String findJarForClass(Class<? extends Object> classObj) { ClassLoader loader = classObj.getClassLoader(); String classFile = classObj.getName().replaceAll("\\.", "/") + ".class"; try {// w w w.ja v a2 s . c o m for (Enumeration<URL> 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()); } toReturn = URLDecoder.decode(toReturn, "UTF-8"); return toReturn.replaceAll("!.*$", ""); } } } catch (IOException e) { throw new RuntimeException(e); } return null; }
From source file:org.mentawai.annotations.ApplicationManagerWithAnnotations.java
@SuppressWarnings("rawtypes") private void findAnnotatedClasses(String resources) throws IOException { ClassLoader cl = Thread.currentThread().getContextClassLoader(); Enumeration<URL> urls = cl.getResources(resources); while (urls.hasMoreElements()) { URL url = urls.nextElement(); if (url.getProtocol().equals("file")) { File file = new File(url.getFile()); findAnnotatedClass(file);//from w ww .j a va 2 s .co m } else if (url.getProtocol().equals("jar")) { URL urlJar = new URL(url.getFile().split("\\!")[0]); JarFile jarFile; try { jarFile = new JarFile(new File(urlJar.toURI().getPath())); } catch (URISyntaxException ex) { Logger.getLogger(ApplicationManagerWithAnnotations.class.getName()).log(Level.SEVERE, null, ex); continue; } Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); if (getPackageTestJar().matcher(entry.getName()).matches() && entry.getName().endsWith(".class")) { String className = entry.getName().replace('/', '.').substring(0, entry.getName().length() - 6); try { Class klass = Class.forName(className); if (BaseAction.class.isAssignableFrom(klass)) { System.out.println("Add Annotated Class:" + className); annotatedClasses.add(klass); } } catch (Exception e) { System.out.println("error! this class was not started:" + entry.getName()); } } } } } }
From source file:com.jeeframework.util.resource.ResolverUtil.java
/** * Scans for classes starting at the package provided and descending into subpackages. * Each class is offered up to the Test as it is discovered, and if the Test returns * true the class is retained. Accumulated classes can be fetched by calling * {@link #getClasses()}.//from w ww . j ava 2s.co m * * @param test an instance of {@link Test} that will be used to filter classes * @param packageName the name of the package from which to start scanning for * classes, e.g. {@code net.sourceforge.stripes} */ public void findInPackage(Test test, String packageName) { packageName = packageName.replace('.', '/'); ClassLoader loader = getClassLoader(); Enumeration<URL> urls; try { urls = loader.getResources(packageName); } catch (IOException ioe) { log.warn("Could not read package: " + packageName, ioe); return; } while (urls.hasMoreElements()) { try { String urlPath = urls.nextElement().getFile(); urlPath = URLDecoder.decode(urlPath, "UTF-8"); // If it's a file in a directory, trim the stupid file: spec if (urlPath.startsWith("file:")) { urlPath = urlPath.substring(5); } // Else it's in a JAR, grab the path to the jar if (urlPath.indexOf('!') > 0) { urlPath = urlPath.substring(0, urlPath.indexOf('!')); } log.info("Scanning for classes in [" + urlPath + "] matching criteria: " + test); File file = new File(urlPath); if (file.isDirectory()) { loadImplementationsInDirectory(test, packageName, file); } else { loadImplementationsInJar(test, packageName, file); } } catch (IOException ioe) { log.warn("could not read entries", ioe); } } }
From source file:com.meiah.core.util.StringUtil.java
public static String read(ClassLoader classLoader, String name, boolean all) throws IOException { if (all) {/*w w w .j av a 2 s .c o m*/ StringBuffer sm = new StringBuffer(); Enumeration enu = classLoader.getResources(name); while (enu.hasMoreElements()) { URL url = (URL) enu.nextElement(); InputStream is = url.openStream(); String s = read(is); if (s != null) { sm.append(s); sm.append(StringPool.NEW_LINE); } is.close(); } return sm.toString().trim(); } else { InputStream is = classLoader.getResourceAsStream(name); String s = read(is); is.close(); return s; } }
From source file:org.seedstack.seed.core.internal.CorePlugin.java
private Configuration loadBootstrapConfiguration() { MapConfiguration globalConfiguration = new MapConfiguration(new HashMap<String, Object>()); ClassLoader classLoader = SeedReflectionUtils.findMostCompleteClassLoader(); if (classLoader == null) { throw SeedException.createNew(CoreErrorCode.UNABLE_TO_FIND_CLASSLOADER); }/* ww w. ja v a2s. co m*/ try { Enumeration<URL> urls = classLoader.getResources(SEED_BOOTSTRAP_PATH); while (urls.hasMoreElements()) { URL url = urls.nextElement(); globalConfiguration.append(new PropertiesConfiguration(url)); } } catch (Exception e) { throw SeedException.wrap(e, CoreErrorCode.UNEXPECTED_EXCEPTION); } return new MapConfiguration( new ImmutableMap.Builder<String, Object>().putAll(globalConfiguration.getMap()).build()); }
From source file:org.richfaces.skin.SkinFactoryImpl.java
/** * @param loader/*from w w w. j av a 2s . c om*/ * @param properties * @param location */ protected boolean loadProperties(ClassLoader loader, Properties properties, String location) { boolean loaded = false; try { Enumeration<URL> resources = loader.getResources(location); while (resources.hasMoreElements()) { URL url = (URL) resources.nextElement(); InputStream propertyStream = null; try { propertyStream = URLToStreamHelper.urlToStream(url); properties.load(propertyStream); loaded = true; } catch (IOException e) { log.warn(Messages.getMessage(Messages.SKIN_PROPERTIES_IO_ERROR), e); continue; } finally { if (null != propertyStream) { propertyStream.close(); } } } } catch (IOException e) { // Do nothing - we can only log error, and continue to load next // property. if (log.isInfoEnabled()) { log.info(Messages.getMessage(Messages.SKIN_PROPERTIES_IO_ERROR), e); } } return loaded; }
From source file:org.ebayopensource.turmeric.plugins.maven.resources.ResourceLocator.java
private void addFoundResource(List<URL> resources, String pathref, ClassLoader cl) throws IOException { log.debug("Looking in ClassLoader: " + cl); Enumeration<URL> enurls = cl.getResources(pathref); if (enurls != null) { while (enurls.hasMoreElements()) { URL url = enurls.nextElement(); if (!resources.contains(url)) { resources.add(url);/*from ww w.j a v a2s . c o m*/ } } } }
From source file:org.seedstack.seed.core.internal.application.SeedConfigLoader.java
/** * Build the application configuration./*from w w w .jav a 2s. c o m*/ * * @param configurationResources the paths to the configuration resources * @param defaultConfiguration the default configuration registered with the SPI * @return the final configuration */ public Pair<MapConfiguration, Props> buildApplicationConfig(Set<String> configurationResources, @Nullable Map<String, String> defaultConfiguration) { final Props props = buildProps(); final Props propsOverride = buildProps(); for (String configurationResource : configurationResources) { try { ClassLoader classLoader = SeedReflectionUtils.findMostCompleteClassLoader(); if (classLoader == null) { throw SeedException.createNew(CoreErrorCode.UNABLE_TO_FIND_CLASSLOADER); } Enumeration<URL> urlEnumeration = classLoader.getResources(configurationResource); while (urlEnumeration.hasMoreElements()) { URL url = urlEnumeration.nextElement(); InputStream resourceAsStream = null; try { resourceAsStream = url.openStream(); if (isOverrideResource(configurationResource)) { LOGGER.debug("Adding {} to configuration override", url.toExternalForm()); propsOverride.load(resourceAsStream); } else { LOGGER.debug("Adding {} to configuration", url.toExternalForm()); props.load(resourceAsStream); } } finally { if (resourceAsStream != null) { try { resourceAsStream.close(); } catch (IOException e) { LOGGER.warn("Unable to close configuration resource " + configurationResource, e); } } } } } catch (IOException e) { throw SeedException.wrap(e, ApplicationErrorCode.UNABLE_TO_LOAD_CONFIGURATION_RESOURCE) .put("resource", configurationResource); } } // Build configuration return Pair.with(buildConfiguration(props, propsOverride, defaultConfiguration), props); }
From source file:org.seedstack.seed.core.internal.SeedConfigLoader.java
/** * Build the application configuration./*from w w w . ja va 2 s . c om*/ * * @param configurationResources the paths to the configuration resources * @param defaultConfiguration the default configuration registered with the SPI * @return the final configuration */ public Pair<MapConfiguration, Props> buildConfiguration(Set<String> configurationResources, @Nullable Map<String, String> defaultConfiguration) { final Props props = buildProps(); final Props propsOverride = buildProps(); for (String configurationResource : configurationResources) { try { ClassLoader classLoader = SeedReflectionUtils.findMostCompleteClassLoader(); if (classLoader == null) { throw SeedException.createNew(CoreErrorCode.UNABLE_TO_FIND_CLASSLOADER); } Enumeration<URL> urlEnumeration = classLoader.getResources(configurationResource); while (urlEnumeration.hasMoreElements()) { URL url = urlEnumeration.nextElement(); InputStream resourceAsStream = null; try { resourceAsStream = url.openStream(); if (isOverrideResource(configurationResource)) { LOGGER.debug("Adding {} to configuration override", url.toExternalForm()); propsOverride.load(resourceAsStream); } else { LOGGER.debug("Adding {} to configuration", url.toExternalForm()); props.load(resourceAsStream); } } finally { if (resourceAsStream != null) { try { resourceAsStream.close(); } catch (IOException e) { LOGGER.warn("Unable to close configuration resource " + configurationResource, e); } } } } } catch (IOException e) { throw SeedException.wrap(e, ApplicationErrorCode.UNABLE_TO_LOAD_CONFIGURATION_RESOURCE) .put("resource", configurationResource); } } // Build configuration return Pair.with(buildConfiguration(props, propsOverride, defaultConfiguration), props); }