1 17 18 package org.apache.geronimo.system.rmi; 19 20 import java.net.MalformedURLException ; 21 import java.net.URL ; 22 23 import java.io.File ; 24 25 import java.util.StringTokenizer ; 26 27 import java.rmi.server.RMIClassLoader ; 28 import java.rmi.server.RMIClassLoaderSpi ; 29 30 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; 31 32 38 public class RMIClassLoaderSpiImpl 39 extends RMIClassLoaderSpi 40 { 41 private RMIClassLoaderSpi delegate = RMIClassLoader.getDefaultProviderInstance(); 42 43 private ConcurrentHashMap cachedCodebases = new ConcurrentHashMap(100, 0.75F); 45 46 47 public Class loadClass(String codebase, String name, ClassLoader defaultLoader) 48 throws MalformedURLException , ClassNotFoundException 49 { 50 if (codebase != null) { 51 codebase = getNormalizedCodebase(codebase); 52 } 53 54 return delegate.loadClass(codebase, name, defaultLoader); 55 } 56 57 public Class loadProxyClass(String codebase, String [] interfaces, ClassLoader defaultLoader) 58 throws MalformedURLException , ClassNotFoundException 59 { 60 if (codebase != null) { 61 codebase = getNormalizedCodebase(codebase); 62 } 63 64 return delegate.loadProxyClass(codebase, interfaces, defaultLoader); 65 } 66 67 public ClassLoader getClassLoader(String codebase) 68 throws MalformedURLException 69 { 70 if (codebase != null) { 71 codebase = getNormalizedCodebase(codebase); 72 } 73 74 return delegate.getClassLoader(codebase); 75 } 76 77 public String getClassAnnotation(Class type) { 78 Object obj = type.getClassLoader(); 79 if (obj instanceof ClassLoaderServerAware) { 80 ClassLoaderServerAware classLoader = (ClassLoaderServerAware) obj; 81 URL urls[] = classLoader.getClassLoaderServerURLs(); 82 if (null == urls) { 83 return delegate.getClassAnnotation(type); 84 } 85 StringBuffer codebase = new StringBuffer (); 86 for (int i = 0; i < urls.length; i++) { 87 URL url = normalizeURL(urls[i]); 88 if (codebase.length() != 0) { 89 codebase.append(' '); 90 } 91 codebase.append(url); 92 } 93 return codebase.toString(); 94 } 95 96 return delegate.getClassAnnotation(type); 97 } 98 99 106 private String getNormalizedCodebase(String codebase) 107 throws MalformedURLException { 108 String cachedCodebase = (String )cachedCodebases.get(codebase); 109 if (cachedCodebase != null) 110 return cachedCodebase; 111 112 String normalizedCodebase = normalizeCodebase(codebase); 113 String oldValue = (String )cachedCodebases.put(codebase, normalizedCodebase); 114 115 if (oldValue != null) { 118 cachedCodebases.remove(codebase); 119 } 120 return normalizedCodebase; } 122 123 124 static String normalizeCodebase(String input) 125 throws MalformedURLException 126 { 127 assert input != null; 128 129 StringBuffer codebase = new StringBuffer (); 130 StringBuffer working = new StringBuffer (); 131 StringTokenizer stok = new StringTokenizer (input, " \t\n\r\f", true); 132 133 while (stok.hasMoreTokens()) { 134 String item = stok.nextToken(); 135 if ( item.indexOf(':') != -1 ) 142 { 143 try { 144 URL url = new URL (item); 145 updateCodebase(working, codebase); 148 } catch (MalformedURLException ignore) { 149 } 151 } 152 153 working.append(item); 155 } 156 157 updateCodebase(working, codebase); 159 160 return codebase.toString(); 162 } 163 164 private static void updateCodebase(final StringBuffer working, final StringBuffer codebase) 165 throws MalformedURLException 166 { 167 if (working.length() != 0) { 168 URL url = normalizeURL(new URL (working.toString())); 170 172 if (codebase.length() != 0) { 174 codebase.append(" "); 175 } 176 codebase.append(url); 177 178 working.setLength(0); 180 } 181 } 182 183 static URL normalizeURL(URL url) 184 { 185 assert url != null; 186 187 if (url.getProtocol().equals("file")) { 188 String filename = url.getFile().replace('/', File.separatorChar); 189 File file = new File (filename); 190 try { 191 url = file.toURI().toURL(); 192 } 193 catch (MalformedURLException ignore) {} 194 } 195 196 return url; 197 } 198 199 public interface ClassLoaderServerAware { 200 public URL [] getClassLoaderServerURLs(); 201 } 202 } 203 | Popular Tags |