KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > rpc > RemoteCallFactory


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: RemoteCallFactory.java 883 2006-07-18 09:35:14Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.rpc;
27
28 import java.lang.reflect.Proxy JavaDoc;
29 import java.util.Hashtable JavaDoc;
30
31 import javax.naming.Context JavaDoc;
32 import javax.naming.Name JavaDoc;
33 import javax.naming.RefAddr JavaDoc;
34 import javax.naming.Reference JavaDoc;
35 import javax.naming.spi.ObjectFactory JavaDoc;
36
37 import org.objectweb.easybeans.api.EZBContainerException;
38
39 /**
40  * Factory creating an EJB proxy for remote calls.
41  * @author Florent Benoit.
42  */

43 public class RemoteCallFactory implements ObjectFactory JavaDoc {
44
45     /**
46      * @return an instance of a proxy (an EJB) that handle local calls.
47      * @param obj the reference containing data to build instance
48      * @param name Name of context, relative to ctx, or null.
49      * @param nameCtx Context relative to which 'name' is named.
50      * @param environment Environment to use when creating the context *
51      * @throws Exception if this object factory encountered an exception while
52      * attempting to create an object, and no other object factories are
53      * to be tried.
54      */

55     public Object JavaDoc getObjectInstance(final Object JavaDoc obj, final Name JavaDoc name, final Context JavaDoc nameCtx, final Hashtable JavaDoc<?, ?> environment)
56             throws Exception JavaDoc {
57         if (obj instanceof Reference JavaDoc) {
58             Reference JavaDoc ref = (Reference JavaDoc) obj;
59
60             // get the embeddedID, getContainerId(), getFactoryName()
61
RefAddr JavaDoc containerIDAddr = ref.get(LocalCallRef.CONTAINER_ID);
62             RefAddr JavaDoc factoryNameAddr = ref.get(LocalCallRef.FACTORY_NAME);
63             RefAddr JavaDoc itfClassNameAddr = ref.get(LocalCallRef.INTERFACE_NAME);
64             RefAddr JavaDoc useIDAddr = ref.get(LocalCallRef.USE_ID);
65
66             String JavaDoc containerID = (String JavaDoc) containerIDAddr.getContent();
67             String JavaDoc factoryName = (String JavaDoc) factoryNameAddr.getContent();
68             String JavaDoc itfClassName = (String JavaDoc) itfClassNameAddr.getContent();
69             boolean useID = Boolean.valueOf((String JavaDoc) useIDAddr.getContent()).booleanValue();
70
71             // Build new Handler
72
ClientRPCInvocationHandler handler = new ClientRPCInvocationHandler(containerID, factoryName, useID);
73             // Set the environment used by client to do the lookup.
74
handler.setRMIEnv(environment);
75
76             // Get current classloader
77
ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
78
79             // load class
80
Class JavaDoc clz = null;
81             try {
82                 clz = classLoader.loadClass(itfClassName);
83             } catch (ClassNotFoundException JavaDoc e) {
84                 throw new EZBContainerException("Cannot find the class '" + itfClassName + "' in Classloader '"
85                         + classLoader + "'.", e);
86             }
87
88             // set the interface class
89
handler.setInterfaceClass(clz);
90
91             return Proxy.newProxyInstance(classLoader, new Class JavaDoc[] {clz}, handler);
92         }
93         throw new IllegalStateException JavaDoc("Can only build object with a reference");
94     }
95 }
96
Popular Tags