View Javadoc

1   /*
2    * Created on May 12, 2004
3    *
4    */
5   package org.neo.swarm.services.classloader;
6   
7   import java.util.Collection;
8   import java.util.Iterator;
9   
10  import org.neo.swarm.SwarmContainer;
11  
12  /***
13   * Performs distributed class loading on classes matching the wildcard. Must be installed on Containers wanting to pool
14   * class definitions, Install using -Djava.system.class.loader=org.neo.swarm.service.classloader.DistributedClassLoader
15   * 
16   * @author navery
17   */
18  public class DistributedClassLoader extends ClassLoader implements RemoteClassLoader {
19  
20  	public static int hits = 0;
21  	public static String applicationClassmatching = ".*agent.*";
22  	public static SwarmContainer container = null;
23  
24  	/***
25  	 * Provide delegation constructor use to chain classloader heirarchy
26  	 *  
27  	 */
28  	public DistributedClassLoader(ClassLoader parent) {
29  		super(parent);
30  	}
31  
32  	/***
33  	 * Standard ctor which uses the getSystemClassLoader() as the parent
34  	 *  
35  	 */
36  	public DistributedClassLoader() {
37  		super();
38  	}
39  	
40  	protected Class findClass(String name) throws ClassNotFoundException {
41  		return super.findClass(name);
42  	}
43  
44  	protected Class loadClass(String name, boolean arg1) throws ClassNotFoundException {
45  		if (!name.matches(applicationClassmatching)) {
46  			//		System.out.println("load class x deferring:" + serviceName);
47  			return super.loadClass(name, arg1);
48  		}
49  		try {
50  			hits++;
51  
52  			System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxx locating class in system class loader:" + name);
53  			//			// ask the bootstrapper to locate it... if it fails then we query the remote classloaders
54  			return super.loadClass(name, arg1);
55  		} catch (ClassNotFoundException ex) {
56  			try {
57  				System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxx FAILED - querying remote containers for:" + name);
58  
59  				// we search the remote app contexts, if we get a response then put it into the local class cache and
60  				// return the defined class
61  				Collection remoteClassLoaders = container.getComponentsOfType(RemoteClassLoader.class);
62  				Iterator iter = remoteClassLoaders.iterator();
63  				while (iter.hasNext()) {
64  					RemoteClassLoader remoteClassLoader = (RemoteClassLoader) iter.next();
65  					try {
66  						Class remoteClass = remoteClassLoader.loadApplicationClass(name);
67  						System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxx FOUND - querying remote containers for:" + name);
68  
69  						return remoteClass;
70  					} catch (ClassNotFoundException ex2) {
71  						System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxx FAILED- carry on searching for:" + name);
72  						// this remote instance failed for some reason
73  					} catch (Exception ex2) {
74  						ex.printStackTrace();
75  					}
76  				}
77  			} catch (Exception ex3) {
78  				ex.printStackTrace();
79  			}
80  		}
81  		throw new ClassNotFoundException(name);
82  	}
83  
84  	/***
85  	 * Remoted call that remote containers call to load foreign classes.
86  	 */
87  	public Class loadApplicationClass(String name) throws ClassNotFoundException {
88  		// searches the local class cache, if it doesnt find it then return null;
89  		try {
90  			System.out.println("loadApploicationClass called..........");
91  			// we immediately defer to the bootstrapper which would have loaded the user application
92  			return super.loadClass(name);
93  		} catch (ClassNotFoundException ex) {
94  			return Class.forName(null);
95  		}
96  	}
97  }