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.jetty;
06:
07: import com.tc.object.bytecode.hook.impl.ClassProcessorHelper;
08: import com.tc.object.loaders.NamedClassLoader;
09: import com.tc.object.loaders.Namespace;
10:
11: import java.lang.reflect.InvocationTargetException;
12: import java.lang.reflect.Method;
13:
14: public class JettyLoaderNaming {
15:
16: public static void nameAndRegisterClasspathLoader(ClassLoader loader) {
17: check(loader);
18: ((NamedClassLoader) loader).__tc_setClassLoaderName(Namespace
19: .createLoaderName(Namespace.JETTY_NAMESPACE, loader
20: .getClass().getName()));
21: register((NamedClassLoader) loader);
22: }
23:
24: public static void nameAndRegisterWebAppLoader(ClassLoader loader) {
25: check(loader);
26:
27: String contextPath = getContextPath(loader);
28:
29: ((NamedClassLoader) loader).__tc_setClassLoaderName(Namespace
30: .createLoaderName(Namespace.JETTY_NAMESPACE, "path:"
31: + contextPath));
32: register((NamedClassLoader) loader);
33: }
34:
35: private static String getContextPath(ClassLoader loader) {
36: try {
37: Method getContext = loader.getClass().getMethod(
38: "getContext", new Class[] {});
39: Object context = getContext.invoke(loader, new Object[] {});
40:
41: Method getContextPath = context.getClass().getMethod(
42: "getContextPath", new Class[] {});
43: return (String) getContextPath.invoke(context,
44: new Object[] {});
45: } catch (Throwable t) {
46: if (t instanceof InvocationTargetException) {
47: t = ((InvocationTargetException) t)
48: .getTargetException();
49: }
50:
51: throw new RuntimeException(t);
52: }
53: }
54:
55: private static void check(ClassLoader loader) {
56: if (loader == null) {
57: throw new NullPointerException("loader is null");
58: }
59: if (!(loader instanceof NamedClassLoader)) {
60: //
61: throw new IllegalArgumentException(
62: "Missing NamedClassLoader interface, type "
63: + loader.getClass().getName());
64: }
65: }
66:
67: private static void register(NamedClassLoader loader) {
68: ClassProcessorHelper.registerGlobalLoader(loader);
69: }
70: }
|