es.prodevelop.pui9.classpath.UrlClassloaderClasspathFinder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of es.prodevelop.pui9.utils Show documentation
Show all versions of es.prodevelop.pui9.utils Show documentation
Multiple utilities for PUI9 (Gson, CSV, Exceptions...)
The newest version!
package es.prodevelop.pui9.classpath;
import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
/**
* This class retrieves the classpath when using a Classloader of URLClassLoader
* type (for instance, when using Tomcat)
*
* @author Marc Gil - [email protected]
*/
public class UrlClassloaderClasspathFinder implements IClasspathFinder {
private List classPath;
@Override
public List getClasspath() {
if (classPath == null) {
classPath = new ArrayList<>();
if (!(PuiClassLoaderUtils.getClassLoader() instanceof URLClassLoader)) {
return classPath;
}
URLClassLoader classLoader = (URLClassLoader) PuiClassLoaderUtils.getClassLoader();
try {
for (URL url : classLoader.getURLs()) {
try {
File file = new File(url.toURI());
if (file.exists() && file.canRead()) {
classPath.add(file);
}
} catch (URISyntaxException e) {
}
}
} catch (Exception e) {
}
}
return classPath;
}
}