All Downloads are FREE. Search and download functionalities are using the official Maven repository.

es.prodevelop.pui9.classpath.UrlClassloaderClasspathFinder Maven / Gradle / Ivy

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;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy