package net.sourceforge.jdbclogger.core.config;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
/**
* list resources available from the classpath
*
* @author stoughto
*/
public class ResourceList {
public static Collection<String> getResources(String regex) {
Pattern pattern;
pattern = Pattern.compile(regex);
return ResourceList.getResources(pattern);
}
/**
* for all elements of java.class.path get a Collection of resources
* Pattern pattern = Pattern.compile(".*"); gets all resources
*
* @param pattern the pattern to match
* @return the resources in the order they are found
*/
public static Collection<String> getResources(Pattern pattern) {
ArrayList<String> retval = new ArrayList<String>();
String classPath = System.getProperty("java.class.path", ".");
String[] classPathElements = classPath.split(":");
for (String element : classPathElements) {
retval.addAll(getResources(element, pattern));
}
return retval;
}
private static Collection<String> getResources(String element, Pattern pattern) {
ArrayList<String> retval = new ArrayList<String>();
File file = new File(element);
if (file.isDirectory()) {
retval.addAll(getResourcesFromDirectory(file, pattern));
} else {
retval.addAll(getResourcesFromJarFile(file, pattern));
}
return retval;
}
private static Collection<String> getResourcesFromJarFile(File file, Pattern pattern) {
ArrayList<String> retval = new ArrayList<String>();
ZipFile zf;
try {
zf = new ZipFile(file);
} catch (ZipException e) {
return retval;
} catch (IOException e) {
return retval;
}
Enumeration e = zf.entries();
while (e.hasMoreElements()) {
ZipEntry ze = (ZipEntry) e.nextElement();
String fileName = ze.getName();
boolean accept = pattern.matcher(fileName).matches();
if (accept) {
retval.add(fileName);
}
}
try {
zf.close();
} catch (IOException e1) {
throw new Error(e1);
}
return retval;
}
private static Collection<String> getResourcesFromDirectory(File directory, Pattern pattern) {
ArrayList<String> retval = new ArrayList<String>();
File[] fileList = directory.listFiles();
for (File file : fileList) {
if (file.isDirectory()) {
retval.addAll(getResourcesFromDirectory(file, pattern));
} else {
try {
String fileName = file.getCanonicalPath();
boolean accept = pattern.matcher(fileName).matches();
if (accept) {
retval.add(fileName);
}
} catch (IOException e) {
throw new Error(e);
}
}
}
return retval;
}
}