1 23 24 package com.rift.coad.client.interceptor.iiop; 26 27 import java.net.MalformedURLException ; 29 import java.net.URL ; 30 import java.net.URLClassLoader ; 31 import java.util.Enumeration ; 32 import java.rmi.server.RMIClassLoader ; 33 import java.rmi.server.RMIClassLoaderSpi ; 34 35 41 public class RemoteClassLoaderSpi extends RMIClassLoaderSpi { 42 43 private RMIClassLoaderSpi delegate = 45 RMIClassLoader.getDefaultProviderInstance(); 46 47 50 public RemoteClassLoaderSpi() { 51 } 52 53 54 64 public Class loadProxyClass (String codebase, String [] interfaces, 65 ClassLoader defaultLoader) throws MalformedURLException , 66 ClassNotFoundException { 67 System.out.println("Load a proxy class [" + codebase + "]"); 69 return delegate.loadProxyClass(codebase,interfaces, defaultLoader); 70 } 71 72 73 84 public Class loadClass(String codebase, String name, 85 ClassLoader defaultLoader) throws MalformedURLException , 86 ClassNotFoundException { 87 if (defaultLoader == null) { 89 System.out.println("Load a class [" + codebase + "] name [" + 90 name + "] defaultLoader [null]"); 91 } else { 92 System.out.println("Load a class [" + codebase + "] name [" + 93 name + "] defaultLoader ["+ 94 defaultLoader.getClass().getName()+"]"); 95 } 96 System.out.println("Thread class loader : " + Thread.currentThread(). 97 getContextClassLoader().getClass().getName()); 98 Class result = null; 99 try { 100 result = delegate.loadClass(codebase,name, Thread.currentThread(). 101 getContextClassLoader()); 102 } catch (MalformedURLException ex) { 103 System.out.println("Failed to narrow : " + ex.getMessage()); 104 ex.printStackTrace(System.out); 105 throw ex; 106 } catch (ClassNotFoundException ex) { 107 System.out.println("Failed to narrow : " + ex.getMessage()); 108 ex.printStackTrace(System.out); 109 throw ex; 110 } 111 try { 112 if (null == result && (codebase != null)) { 113 ClassLoader loader = getClassLoader(codebase); 114 System.out.println("Retrieve the result and enumerate through them"); 115 loadClasses(loader, Thread.currentThread().getContextClassLoader(), 116 loader.getResources("/")); 117 } else if (result != null){ 118 System.out.println("The class was loaded"); 119 } else { 120 System.out.println("Nothing loaded and the class is null [" + 121 codebase + "]"); 122 } 123 } catch (Exception ex) { 124 System.out.println("Caught an exception in the main load class : " 125 + ex.getMessage()); 126 ex.printStackTrace(System.out); 127 } 128 return result; 129 } 130 131 132 140 public ClassLoader getClassLoader(String codebase) throws 141 MalformedURLException { 142 System.out.println("Get class loader for : " + codebase); 143 URL url = new URL (codebase); 144 if (url.getProtocol().equals("http")) { 145 return new URLClassLoader (new URL [] {url}); 146 } 147 return delegate.getClassLoader(codebase); 148 } 149 150 151 157 public String getClassAnnotation(Class cl) { 158 String annotation = null; 159 try { 160 annotation = delegate.getClassAnnotation(cl); 161 } catch (Throwable ex) { 162 annotation = System.getProperty("java.rmi.server.codebase"); 163 } 164 return annotation; 165 } 166 167 168 176 private void loadClasses(ClassLoader source, ClassLoader target, 177 Enumeration entries) { 178 try { 179 while(entries.hasMoreElements()) { 180 URL url = (URL )entries.nextElement(); 181 System.out.println(url.toURI().toString()); 182 } 183 } catch (Exception ex) { 184 System.out.println("Caught an exception : " + ex.getMessage()); 185 ex.printStackTrace(System.out); 186 } 187 } 188 } 189 | Popular Tags |