/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package some.package import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLClassLoader; import java.util.Enumeration; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.TreeMap; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.logging.Level; import java.util.logging.Logger; /** * * Adapted source from * http://www.coderanch.com/t/278514/Streams/java/Reading-files-classpath-searching-pattern */ public class ClassPathSearcher { static ClassPathSearcher instance = null; Set classPathSet = new HashSet(); static final Logger log = Logger.getLogger( ClassPathSearcher.class.getName()); private ClassPathSearcher() { } public static ClassPathSearcher getInstance() { if (instance == null) { instance = new ClassPathSearcher(); instance.init(); log.log(Level.INFO, "ClassPathSearcher initialized"); } return instance; } private void init() { ClassLoader applicationClassLoader = Thread.currentThread() .getContextClassLoader(); URL[] urls = ((URLClassLoader) applicationClassLoader).getURLs(); for (URL url : urls) { String urlStr = url.getPath(); if (urlStr.startsWith("file:")) { urlStr = urlStr.substring(5); } if (urlStr.endsWith("!/")) { urlStr = urlStr.substring(0,urlStr.length() - 2); } classPathSet.add(urlStr); } String classPath = System.getProperty("java.class.path"); String[] pathElements = classPath.split( System.getProperty("path.separator")); for (String el : pathElements) { classPathSet.add(el); } } public Map findFilesInClassPath(String fileNamePattern) { Map result = new TreeMap(); for (String element : classPathSet) { try { File newFile = new File(element); if (newFile.isDirectory()) { result.putAll(findResourceInDirectory(newFile, fileNamePattern)); } else { result.putAll(findResourceInFile(newFile, fileNamePattern)); } } catch (IOException e) { log.log(Level.WARNING, "Exception:"+ e); } } return result; } private Map findResourceInFile(File resourceFile, String fileNamePattern) throws IOException { Map result = new TreeMap(); if (resourceFile.canRead() && resourceFile.getAbsolutePath().endsWith(".jar")) { JarFile jarFile = new JarFile(resourceFile); Enumeration entries = jarFile.entries(); while (entries.hasMoreElements()) { JarEntry singleEntry = entries.nextElement(); if (singleEntry.getName().matches(fileNamePattern)) { result.put(jarFile.getName() + "/" + singleEntry.getName(), jarFile.getInputStream(singleEntry)); } } } return result; } private Map findResourceInDirectory(File directory, String fileNamePattern) throws IOException { Map result = new TreeMap(); File[] files = directory.listFiles(); for (File currentFile : files) { if (currentFile.getAbsolutePath().matches(fileNamePattern)) { result.put(currentFile.getAbsolutePath(), new FileInputStream( currentFile)); } else if (currentFile.isDirectory()) { result.putAll(findResourceInDirectory(currentFile, fileNamePattern)); } else { result.putAll(findResourceInFile(currentFile, fileNamePattern)); } } return result; } }