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) 2010 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.services.monitoring.jmx;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.Properties;
22  import java.util.Map.Entry;
23  
24  import org.theshoemakers.which4j.Which4J;
25  
26  /**
27   * Helper class which provides the functionality used through the ClassLoaderMBean.
28   *
29   * @svnLink $Revision: 4253 $;$Date: 2010-12-21 11:08:04 +0100 (Di, 21. Dez 2010) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/jmx/src/main/java/ch/elca/el4j/services/monitoring/jmx/ClassLoaderInvestigator.java $
30   *
31   * @author Simon Stelling (SST)
32   */
33  public class ClassLoaderInvestigator {
34  	
35  	/**
36  	 * The class investigated by this Investigator.
37  	 */
38  	private Class<?> inspectedClass;
39  	
40  	/**
41  	 * Sole constructor.
42  	 * @param className the name of the class to investigate.
43  	 */
44  	public ClassLoaderInvestigator(String className) throws ClassNotFoundException {
45  		inspectedClass = Class.forName(className);
46  	}
47  	
48  	/**
49  	 * @return the classloader hierarchy of the investigated class. the first element is the lowest
50  	 * in the hierarchy, the topmost is the loader which has the bootstrap classloader as parent.
51  	 */
52  	public String[] getClassLoaderHierarchy() {
53  		List<String> loaders = new ArrayList<String>();
54  		ClassLoader currentLoader = inspectedClass.getClassLoader();
55  		while (currentLoader != null) {
56  			loaders.add(currentLoader.getClass().getName());
57  			currentLoader = currentLoader.getParent();
58  		}
59  		return loaders.toArray(new String[0]);
60  	}
61  
62  	/**
63  	 * @return classpath obtained from Which4J for the inspected class
64  	 */
65  	public String getClassPathForInspectedClass() {
66  		return Which4J.which(inspectedClass);
67  	}
68  	
69  	/**
70  	 * @return classpath obtained from Which4J for the inspected class using its ClassLoader
71  	 */
72  	public String getClassPathForInspectedClassUsingItsLoader() {
73  		return Which4J.which(inspectedClass, inspectedClass.getClassLoader());
74  	}
75  	
76  	/**
77  	 * @return the system properties as an array of 2-element arrays
78  	 */
79  	public String[][] getProperties() {
80  		Properties p = System.getProperties();
81  		String[][] list = new String[2][p.entrySet().size()];
82  		int i = 0;
83  		for (Entry<Object, Object> e : p.entrySet()) {
84  			list[i] = new String[] {e.getKey().toString(), e.getValue().toString()};
85  			i++;
86  		}
87  		return list;
88  	} 
89  
90  }