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: RmiProxyObjectFactory.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   
31   import java.lang.reflect.*;
32   
33   import java.net.*;
34   
35   import java.util.*;
36   
37   import javax.naming.*;
38   import javax.naming.spi.*;
39   
40   import org.apache.log4j.Logger;
41   
42   public class RmiProxyObjectFactory
43       extends RmiObjectFactoryBase
44       implements ObjectFactory
45   {
46       private static final Logger log = 
47               Logger.getLogger(RmiProxyObjectFactory.class.getName());
48       private String url;
49       private String protocol;
50   
51       public RmiProxyObjectFactory()
52       {
53       }
54   
55       public Object getObjectInstance(Object obj, Name name, Context ctx, 
56                                       Hashtable env)
57                                throws Exception
58       {
59           Reference ref = (Reference) obj;
60   
61           ctx = ((JndiContextImpl) ctx).getRootContext();
62   
63           URL[] codebase = getCodebase(name, env);
64   
65           if (log.isDebugEnabled())
66           {
67               log.debug("codebase = " + codebase);
68           }
69   
70           ClassLoader loader = getClass().getClassLoader();
71   
72           if (codebase != null)
73           {
74               loader = new URLClassLoader(codebase, loader);
75           }
76   
77           //load interfaces rom loader
78           Class[] interfaces = getInterfaces(name, env, loader);
79           Class clazz = loader.loadClass(getClassName(name, env));
80           Object target = clazz.newInstance();
81           RmiClientInvocationHandler handler = 
82                   new RmiClientInvocationHandler(
83                           (String) getTypeInfoValue("url", name, env), interfaces, 
84                           (String) getTypeInfoValue("protocol", name, env), ctx);
85   
86           return Proxy.newProxyInstance(getClass().getClassLoader(), interfaces, 
87                                         handler);
88       }
89   }