public static Collection<URL> searchResourcesInClasspath(String resourcePath) throws IOException { return searchResourcesInClasspath(Thread.currentThread().getContextClassLoader(), resourcePath, ""); }
/** * @param resource Name of resource(s) to find in classpath * @param defaultObject The default object to use to determine the class loader (if none associated with current thread.) * * @return Iterator over URL Objects */ public static Iterator<URL> getResources(String resource, Object defaultObject) throws IOException { Enumeration<URL> resources = getCurrentLoader(defaultObject).getResources(resource); List<URL> lst = new ArrayList<URL>(); while (resources.hasMoreElements()) { lst.add(resources.nextElement()); } return lst.iterator(); }
public static Collection<URL> searchResourcesInClasspath(ClassLoader classLoader, String prefix, String suffix) throws IOException { Enumeration<URL>[] enumerations = new Enumeration[]{classLoader.getResources(prefix), classLoader.getResources(prefix + "MANIFEST.MF")}; Set<URL> urls = new HashSet<URL>(); for (Enumeration<URL> enumeration : enumerations) { while (enumeration.hasMoreElements()) { URL url = enumeration.nextElement(); URLConnection urlConnection = url.openConnection(); urlConnection.setUseCaches(false); urlConnection.setDefaultUseCaches(false); if (urlConnection instanceof JarURLConnection) { JarFile jarFile = ((JarURLConnection) urlConnection).getJarFile(); if (jarFile != null) { searchJar(classLoader, urls, jarFile, prefix, suffix); } else { searchDir(urls, new File(URLDecoder.decode(url.getFile(), "UTF-8")), suffix); } } else if (urlConnection instanceof FileURLConnection) { urls.add(url); } else { throw new CommonsRuntimeException("Cannot handler URLConnection of type: " + urlConnection); } } } return urls; }
protected static void searchDir(Set<URL> resultUrls, File dir, String suffix) throws IOException { if (dir.exists() && dir.isDirectory()) { File[] files = dir.listFiles(); String absolutePath; for (File file : files) { absolutePath = file.getAbsolutePath(); if (file.isDirectory()) { searchDir(resultUrls, file, suffix); } else if (absolutePath.endsWith(suffix)) { resultUrls.add(file.toURL()); } } } }
public static Collection<URL> searchResourcesInClasspath(String prefix, String suffix) throws IOException { return searchResourcesInClasspath(Thread.currentThread().getContextClassLoader(), prefix, suffix); }
protected void loadDeclaredRulesFromClassPath() throws IOException { Collection<URL> resources = ClassUtils.searchResourcesInClasspath( RULES_XML_PATH ); if ( log.isDebugEnabled() ) log.debug( "Found " + resources.size() + " sanity-rules.xml files in the classpath" ); if ( log.isDebugEnabled() && resources.size() > 1 ) { int i = 1; for ( URL resource : resources ) { log.debug( i + ") " + resource.getFile() ); i++; } } for ( URL resource : resources ) { try { DeclaredRules dr = readDeclaredRules( resource.openStream() ); addAllDeclaredRules( dr.getDeclaredRules() ); if ( log.isDebugEnabled() ) log.debug( "Loaded " + dr.getDeclaredRules().size() + " declared rules from: " + resource ); } catch ( Throwable t ) { throw new SanityRuleException( "Problem reading declared rules from resource: " + resource, t ); } } }