View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.util.classpath;
18  
19  import java.net.URL;
20  import java.net.URLClassLoader;
21  
22  /**
23   * Tool for analyzing the classpath (e.g. in maven projects).
24   * This tool allows the list of URLs to be easily retrieved:
25   * Call <code>getClassPath(YourMainClass.class)</code>.
26   * 
27   * Remark: '<code>mvn exec:java</code>' uses an URLClassloader for the main class and
28   * passes all project dependencies plus the target directories as URLs to that.
29   *
30   * @svnLink $Revision: 4010 $;$Date: 2009-12-01 10:59:54 +0100 (Di, 01. Dez 2009) $;$Author: jonasha $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/main/java/ch/elca/el4j/util/classpath/ClassloaderTools.java $
31   *
32   * @author David Bernhard (DBD)
33   */
34  public final class ClassloaderTools {
35  
36  	/**
37  	 * Cannot instantiate.
38  	 */
39  	private ClassloaderTools() { }
40  	
41  	/**
42  	 * Checks whether a class is loaded via an URLClassLoader,
43  	 * in which case a call to <code>getClassPath</code> will work.
44  	 * @param mainClass The class to check.
45  	 * @return Whether this class was loaded via an URLClassLoader.
46  	 */
47  	public static boolean isURLLoaded(Class < ? > mainClass) {
48  		ClassLoader cl = mainClass.getClassLoader();
49  		return (cl instanceof URLClassLoader);
50  	}
51  
52  	/**
53  	 * @param mainClass A class from which to obtain the classloader and its
54  	 * classpath.
55  	 * @return A comma-separated list of the modules on the current maven
56  	 * classpath.
57  	 * @throws RuntimeException - if the class does not
58  	 * come from an URLClassLoader
59  	 */
60  	public static String getClassPath(Class < ? > mainClass)
61  		throws RuntimeException {
62  		ClassLoader cl = mainClass.getClassLoader();
63  		
64  		if (!isURLLoaded(mainClass)) {
65  			throw new RuntimeException(
66  				"The class " + mainClass
67  				+ " was not loaded via an URLClassLoader.");
68  		}
69  		URL[] urls = ((URLClassLoader) cl).getURLs();
70  		StringBuffer classPathBuffer =  new StringBuffer();
71  		for (URL u : urls) {
72  			classPathBuffer.append(u.toExternalForm()).append(", ");
73  		}
74  		// remove last ", "
75  		classPathBuffer.delete(classPathBuffer.length() - 2, classPathBuffer.length());
76  
77  		return classPathBuffer.toString();
78  	}
79  	
80  	/**
81  	 * @param mainClass A class from which to obtain the classloader and its
82  	 * classpath.
83  	 * @return A String[] of the modules on the current maven
84  	 * classpath.
85  	 * @throws RuntimeException - if the class does not
86  	 * come from an URLClassLoader
87  	 */
88  	public static String[] getURLs(Class < ? > mainClass)
89  		throws RuntimeException {
90  		ClassLoader cl = mainClass.getClassLoader();
91  		
92  		if (!isURLLoaded(mainClass)) {
93  			throw new RuntimeException(
94  				"The class " + mainClass
95  				+ " was not loaded via an URLClassLoader.");
96  		}
97  		URL[] urls = ((URLClassLoader) cl).getURLs();
98  		String[] urlStrings = new String[urls.length];
99  		for (int i = 0; i < urls.length; i++) {
100 			urlStrings[i] = urls[i].toString();
101 		}
102 		return urlStrings;
103 	}
104 }