KickJava   Java API By Example, From Geeks To Geeks.

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


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: LocalCallFactory.java 858 2006-07-13 08:54:26Z 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 local calls.
41  * @author Florent Benoit.
42  */

43 public class LocalCallFactory 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 embeddedIDAddr = ref.get(LocalCallRef.EMBEDDED_ID);
62             RefAddr JavaDoc containerIDAddr = ref.get(LocalCallRef.CONTAINER_ID);
63             RefAddr JavaDoc factoryNameAddr = ref.get(LocalCallRef.FACTORY_NAME);
64             RefAddr JavaDoc itfClassNameAddr = ref.get(LocalCallRef.INTERFACE_NAME);
65             RefAddr JavaDoc useIDAddr = ref.get(LocalCallRef.USE_ID);
66
67             Integer JavaDoc embeddedID = Integer.valueOf((String JavaDoc) embeddedIDAddr.getContent());
68             String JavaDoc containerID = (String JavaDoc) containerIDAddr.getContent();
69             String JavaDoc factoryName = (String JavaDoc) factoryNameAddr.getContent();
70             String JavaDoc itfClassName = (String JavaDoc) itfClassNameAddr.getContent();
71             boolean useID = Boolean.valueOf((String JavaDoc) useIDAddr.getContent()).booleanValue();
72
73             // Build new Handler
74
LocalCallInvocationHandler handler = new LocalCallInvocationHandler(embeddedID, containerID, factoryName, useID);
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