01: /*
02: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tc.websphere;
06:
07: import com.tc.logging.TCLogger;
08: import com.tc.object.bytecode.ManagerUtil;
09: import com.tc.object.bytecode.hook.impl.ClassProcessorHelper;
10: import com.tc.object.loaders.NamedClassLoader;
11: import com.tc.object.loaders.Namespace;
12:
13: import java.io.File;
14:
15: public class WebsphereLoaderNaming {
16:
17: private static final Object[] EMPTY_OBJECT_ARRAY = new Object[] {};
18: private static final Class[] EMPTY_CLASS_ARRAY = new Class[] {};
19:
20: // loader name prefixes -- use a new one of these for each type of logical loader type in WAS
21: private static final String EAR = "ear:";
22: private static final String EAR_DEPENDENCY = "earDep:";
23:
24: public synchronized static void nameAndRegisterDependencyLoader(
25: NamedClassLoader loader, Object earFile) {
26: nameAndRegister(loader, EAR_DEPENDENCY + getEarName(earFile));
27: }
28:
29: public static void registerWebAppLoader(NamedClassLoader loader,
30: Object earFile, Object moduleRef) {
31: Object webModule = invokeMethod(moduleRef, "getModule");
32: Object webModuleFile = invokeMethod(moduleRef, "getModuleFile");
33: Object bindings = invokeMethod(webModuleFile, "getBindings");
34: String vhost = (String) invokeMethod(bindings,
35: "getVirtualHostName");
36: String contextRoot = (String) invokeMethod(webModule,
37: "getContextRoot");
38:
39: nameAndRegister(loader, EAR + getEarName(earFile) + ":" + vhost
40: + contextRoot);
41: }
42:
43: private static void nameAndRegister(NamedClassLoader loader,
44: String name) {
45: loader.__tc_setClassLoaderName(Namespace.createLoaderName(
46: Namespace.WEBSPHERE_NAMESPACE, name));
47: ClassProcessorHelper.registerGlobalLoader(loader);
48: }
49:
50: private static String getEarName(Object earFile) {
51: String orig = (String) invokeMethod(earFile, "getURI");
52: File dir = new File(orig);
53:
54: while (true) {
55: String ear = dir.getName();
56:
57: if (ear == null) {
58: RuntimeException re = new RuntimeException(
59: "Cannot determine ear name from " + orig);
60: getLogger().error(re);
61: throw re;
62: }
63:
64: if (ear.endsWith(".ear")) {
65: return ear;
66: }
67:
68: dir = dir.getParentFile();
69: }
70: }
71:
72: private static Object invokeMethod(Object obj, String name) {
73: try {
74: return obj.getClass().getMethod(name, EMPTY_CLASS_ARRAY)
75: .invoke(obj, EMPTY_OBJECT_ARRAY);
76: } catch (Exception e) {
77: getLogger().error(e);
78: throw new RuntimeException(e);
79: }
80: }
81:
82: private static TCLogger getLogger() {
83: return ManagerUtil.getLogger(WebsphereLoaderNaming.class
84: .getName());
85: }
86:
87: }
|