KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > jboss > JBossLoaderNaming


1 /*
2  * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

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 JavaDoc;
12 import java.lang.reflect.Method JavaDoc;
13 import java.net.URL JavaDoc;
14
15 public class JBossLoaderNaming {
16
17   private static final String JavaDoc UCL = "org.jboss.mx.loading.UnifiedClassLoader";
18   private static final String JavaDoc UCL3 = "org.jboss.mx.loading.UnifiedClassLoader3";
19
20   private static String JavaDoc serverHomeDir = null;
21   private static String JavaDoc serverBaseDir = null;
22   private static String JavaDoc serverTempDir = null;
23
24   private static boolean initialized = false;
25
26   public static synchronized void initialize(ClassLoader JavaDoc bootLoader, File JavaDoc homeDir, File JavaDoc baseDir, File JavaDoc tempDir)
27       throws Exception JavaDoc {
28     if (initialized) { throw new IllegalStateException JavaDoc("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 JavaDoc getLoaderName(ClassLoader JavaDoc loader) throws Exception JavaDoc {
41     if (loader == null) { throw new NullPointerException JavaDoc("null loader"); }
42     if (!initialized) { throw new IllegalStateException JavaDoc("not yet initialized"); }
43
44     Class JavaDoc type = loader.getClass();
45     String JavaDoc 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 JavaDoc("Support missing for loader of type: " + className);
52   }
53
54   private static String JavaDoc makeUCLName(ClassLoader JavaDoc loader, boolean methodIsOnSuper) throws Exception JavaDoc {
55     final Class JavaDoc clazz = loader.getClass();
56
57     Class JavaDoc lookup = clazz;
58     if (methodIsOnSuper) {
59       lookup = clazz.getSuperclass();
60     }
61
62     Method JavaDoc getUrl = lookup.getDeclaredMethod("getURL", new Class JavaDoc[] {});
63     URL JavaDoc url = (URL JavaDoc) getUrl.invoke(loader, new Object JavaDoc[] {});
64
65     Method JavaDoc getOrigUrl = lookup.getDeclaredMethod("getOrigURL", new Class JavaDoc[] {});
66     URL JavaDoc origUrl = (URL JavaDoc) getOrigUrl.invoke(loader, new Object JavaDoc[] {});
67
68     if (url == null && origUrl == null) { return null; }
69
70     final URL JavaDoc u;
71     if ((url == null) || url.toExternalForm().startsWith(serverTempDir)) {
72       u = origUrl;
73     } else {
74       u = url;
75     }
76
77     String JavaDoc 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 JavaDoc getShortName(Class JavaDoc clazz) {
89     String JavaDoc s = clazz.getName();
90     return s.substring(s.lastIndexOf('.') + 1);
91   }
92
93 }
94
Popular Tags