1 5 package com.tc.jboss; 6 7 import com.tc.object.bytecode.hook.impl.ClassProcessorHelper; 8 import com.tc.object.loaders.NamedClassLoader; 9 import com.tc.object.loaders.Namespace; 10 11 import java.io.File ; 12 import java.lang.reflect.Method ; 13 import java.net.URL ; 14 15 public class JBossLoaderNaming { 16 17 private static final String UCL = "org.jboss.mx.loading.UnifiedClassLoader"; 18 private static final String UCL3 = "org.jboss.mx.loading.UnifiedClassLoader3"; 19 20 private static String serverHomeDir = null; 21 private static String serverBaseDir = null; 22 private static String serverTempDir = null; 23 24 private static boolean initialized = false; 25 26 public static synchronized void initialize(ClassLoader bootLoader, File homeDir, File baseDir, File tempDir) 27 throws Exception { 28 if (initialized) { throw new IllegalStateException ("already initialized"); } 29 initialized = true; 30 31 NamedClassLoader ncl = (NamedClassLoader) bootLoader; 32 ncl.__tc_setClassLoaderName(Namespace.createLoaderName(Namespace.JBOSS_NAMESPACE, "boot")); 33 ClassProcessorHelper.registerGlobalLoader(ncl); 34 35 serverHomeDir = homeDir.getAbsoluteFile().toURL().toExternalForm(); 36 serverBaseDir = baseDir.getAbsoluteFile().toURL().toExternalForm(); 37 serverTempDir = tempDir.getAbsoluteFile().toURL().toExternalForm(); 38 } 39 40 public synchronized static String getLoaderName(ClassLoader loader) throws Exception { 41 if (loader == null) { throw new NullPointerException ("null loader"); } 42 if (!initialized) { throw new IllegalStateException ("not yet initialized"); } 43 44 Class type = loader.getClass(); 45 String className = type.getName(); 46 47 if (UCL3.equals(className)) { 48 return makeUCLName(loader, true); 49 } else if (UCL.equals(className)) { return makeUCLName(loader, false); } 50 51 throw new UnsupportedOperationException ("Support missing for loader of type: " + className); 52 } 53 54 private static String makeUCLName(ClassLoader loader, boolean methodIsOnSuper) throws Exception { 55 final Class clazz = loader.getClass(); 56 57 Class lookup = clazz; 58 if (methodIsOnSuper) { 59 lookup = clazz.getSuperclass(); 60 } 61 62 Method getUrl = lookup.getDeclaredMethod("getURL", new Class [] {}); 63 URL url = (URL ) getUrl.invoke(loader, new Object [] {}); 64 65 Method getOrigUrl = lookup.getDeclaredMethod("getOrigURL", new Class [] {}); 66 URL origUrl = (URL ) getOrigUrl.invoke(loader, new Object [] {}); 67 68 if (url == null && origUrl == null) { return null; } 69 70 final URL u; 71 if ((url == null) || url.toExternalForm().startsWith(serverTempDir)) { 72 u = origUrl; 73 } else { 74 u = url; 75 } 76 77 String urlString = u.toExternalForm(); 78 79 if (urlString.startsWith(serverHomeDir)) { 80 urlString = urlString.substring(serverHomeDir.length()); 81 } else if (urlString.startsWith(serverBaseDir)) { 82 urlString = urlString.substring(serverBaseDir.length()); 83 } 84 85 return Namespace.createLoaderName(Namespace.JBOSS_NAMESPACE, getShortName(clazz) + ":" + urlString); 86 } 87 88 private static String getShortName(Class clazz) { 89 String s = clazz.getName(); 90 return s.substring(s.lastIndexOf('.') + 1); 91 } 92 93 } 94 | Popular Tags |