1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package com.mjh.switchrmi.jndi;
28
29 import com.mjh.switchrmi.*;
30 import com.mjh.util.*;
31
32 import java.lang.reflect.*;
33
34 import java.net.*;
35
36 import java.util.*;
37
38 import javax.naming.*;
39 import javax.naming.spi.*;
40
41 import org.apache.log4j.Logger;
42
43 public class RmiJavaObjectFactory
44 extends RmiObjectFactoryBase
45 implements ObjectFactory
46 {
47 private static final Logger log =
48 Logger.getLogger(RmiJavaObjectFactory.class.getName());
49
50 public RmiJavaObjectFactory()
51 {
52 }
53
54 public Object getObjectInstance(Object obj, Name name, Context ctx,
55 Hashtable env)
56 throws Exception
57 {
58 try
59 {
60 if (log.isDebugEnabled())
61 {
62 log.debug("obj = " + obj);
63 log.debug("name = " + name);
64 log.debug("ctx = " + ctx);
65 log.debug("env = " + env);
66 }
67
68 Reference ref = (Reference) obj;
69 URL[] codebase = getCodebase(name, env);
70
71 if (log.isDebugEnabled())
72 {
73 log.debug("codebase = " + codebase);
74 }
75
76 ClassLoader loader = getClass().getClassLoader();
77
78 if (codebase != null)
79 {
80 loader = new URLClassLoader(codebase, loader);
81 }
82
83 Class clazz = loader.loadClass(getClassName(name, env));
84 Object target = clazz.newInstance();
85 Class[] interfaces = getInterfaces(name, env, loader);
86 JavaInvocationHandler handler =
87 new JavaInvocationHandler(interfaces, target);
88
89 return Proxy.newProxyInstance(loader, interfaces, handler);
90 }
91 catch (Exception ex)
92 {
93 if (log.isDebugEnabled())
94 {
95 log.debug("Exception: ", ex);
96 }
97
98 throw ex;
99 }
100 }
101
102 private class JavaInvocationHandler
103 extends RmiInvocationHandlerBase
104 {
105 public Object service;
106 private ClassLoader loader;
107
108 public JavaInvocationHandler(Class[] interfaces, Object object)
109 {
110 super(interfaces);
111 service = object;
112 }
113
114 public Object rmiInvoke(Object target, Method method, Object[] args)
115 throws Throwable
116 {
117 if (log.isDebugEnabled())
118 {
119 log.debug("service = " + service);
120 log.debug("method = " + method.getName());
121 log.debug("method.declaringClass = "
122 + method.getDeclaringClass().getName());
123 }
124
125 Object result = null;
126
127 try
128 {
129 result = method.invoke(service, args);
130 }
131 catch (Exception ex)
132 {
133 if (log.isDebugEnabled())
134 {
135 log.debug("Exception ", ex);
136 }
137 }
138
139 return result;
140 }
141 }
142 }