001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.jboss;
006:
007: import com.tc.object.bytecode.hook.impl.ClassProcessorHelper;
008: import com.tc.object.loaders.NamedClassLoader;
009: import com.tc.object.loaders.Namespace;
010: import com.tc.util.runtime.Os;
011:
012: import java.io.File;
013: import java.lang.reflect.Method;
014: import java.net.MalformedURLException;
015: import java.net.URL;
016:
017: public class JBossLoaderNaming {
018:
019: private static final String UCL = "org.jboss.mx.loading.UnifiedClassLoader";
020: private static final String UCL3 = "org.jboss.mx.loading.UnifiedClassLoader3";
021: private static final String CACHE_LOADER = "org.jboss.mx.loading.HeirarchicalLoaderRepository3$CacheClassLoader";
022:
023: private static String serverHomeDir = null;
024: private static String serverBaseDir = null;
025: private static String serverTempDir = null;
026:
027: private static boolean initialized = false;
028:
029: public static synchronized void initialize(ClassLoader bootLoader,
030: File homeDir, File baseDir, File tempDir) throws Exception {
031: if (initialized) {
032: throw new IllegalStateException("already initialized");
033: }
034: initialized = true;
035:
036: NamedClassLoader ncl = (NamedClassLoader) bootLoader;
037: ncl.__tc_setClassLoaderName(Namespace.createLoaderName(
038: Namespace.JBOSS_NAMESPACE, "boot"));
039: ClassProcessorHelper.registerGlobalLoader(ncl);
040:
041: serverHomeDir = fixUpUrl(homeDir.getAbsoluteFile().toURL())
042: .toExternalForm();
043: serverBaseDir = fixUpUrl(baseDir.getAbsoluteFile().toURL())
044: .toExternalForm();
045: serverTempDir = fixUpUrl(tempDir.getAbsoluteFile().toURL())
046: .toExternalForm();
047:
048: }
049:
050: private static URL fixUpUrl(URL url) throws MalformedURLException {
051: if (Os.isWindows()) {
052: // make sure the drive letter is always uppercase
053: String file = url.getFile();
054:
055: if (file.matches("^/[A-Za-z]:/.+$")) {
056: char drive = Character.toUpperCase(file.charAt(1));
057: file = "/" + drive + file.substring(2);
058: }
059:
060: return new URL(url.getProtocol(), url.getHost(), file);
061: }
062:
063: return url;
064: }
065:
066: public synchronized static String getLoaderName(ClassLoader loader)
067: throws Exception {
068: if (loader == null) {
069: throw new NullPointerException("null loader");
070: }
071: if (!initialized) {
072: throw new IllegalStateException("not yet initialized");
073: }
074:
075: Class type = loader.getClass();
076: String className = type.getName();
077:
078: if (UCL3.equals(className)) {
079: return makeUCLName(loader, true);
080: } else if (UCL.equals(className)) {
081: return makeUCLName(loader, false);
082: }
083:
084: if (CACHE_LOADER.equals(className)) {
085: return null;
086: }
087:
088: throw new UnsupportedOperationException(
089: "Support missing for loader of type: " + className);
090: }
091:
092: private static String makeUCLName(ClassLoader loader,
093: boolean methodIsOnSuper) throws Exception {
094: final Class clazz = loader.getClass();
095:
096: Class lookup = clazz;
097: if (methodIsOnSuper) {
098: lookup = clazz.getSuperclass();
099: }
100:
101: Method getUrl = lookup.getDeclaredMethod("getURL",
102: new Class[] {});
103: URL url = (URL) getUrl.invoke(loader, new Object[] {});
104:
105: Method getOrigUrl = lookup.getDeclaredMethod("getOrigURL",
106: new Class[] {});
107: URL origUrl = (URL) getOrigUrl.invoke(loader, new Object[] {});
108:
109: if (url == null && origUrl == null) {
110: return null;
111: }
112:
113: final URL u;
114: if ((url == null)
115: || url.toExternalForm().startsWith(serverTempDir)) {
116: u = origUrl;
117: } else {
118: u = url;
119: }
120:
121: String urlString = fixUpUrl(u).toExternalForm();
122:
123: if (urlString.startsWith(serverHomeDir)) {
124: urlString = urlString.substring(serverHomeDir.length());
125: } else if (urlString.startsWith(serverBaseDir)) {
126: urlString = urlString.substring(serverBaseDir.length());
127: }
128:
129: return Namespace.createLoaderName(Namespace.JBOSS_NAMESPACE,
130: getShortName(clazz) + ":" + urlString);
131: }
132:
133: private static String getShortName(Class clazz) {
134: String s = clazz.getName();
135: return s.substring(s.lastIndexOf('.') + 1);
136: }
137:
138: }
|