1    //
2    // SwitchRMI  Framework
3    // Copyright (c) 2000-2002 by Michael J. Henderson & Associates.
4    //
5    // Michael Henderson
6    // http://switchrmi.sf.net
7    // mailto:mikehenderson@dunelm.org.uk
8    //
9    // This library is free software.
10   //
11   // You may redistribute it and/or modify it under the terms of the GNU
12   // Lesser General Public License as published by the Free Software Foundation.
13   //
14   // Version 2.1 of the license should be included with this distribution in
15   // the file LICENSE, as well as License.html. If the license is not
16   // included with this distribution, you may find a copy at the FSF web
17   // site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
18   // Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
19   //
20   // This library is distributed in the hope that it will be useful,
21   // but WITHOUT ANY WARRANTY; without even the implied waranty of
22   // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23   // Lesser General Public License for more details.
24   //
25   // $Id: RmiJavaObjectFactory.java,v 1.1 2002/11/11 22:19:48 mikehenderson Exp $
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  }