001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: RemoteCallFactory.java 2010 2007-10-26 13:19:08Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.proxy.factory;
025:
026: import java.lang.reflect.Proxy;
027: import java.util.Hashtable;
028:
029: import javax.naming.Context;
030: import javax.naming.Name;
031: import javax.naming.RefAddr;
032: import javax.naming.Reference;
033: import javax.naming.spi.ObjectFactory;
034:
035: import org.ow2.easybeans.api.EZBContainerException;
036: import org.ow2.easybeans.proxy.client.ClientRPCInvocationHandler;
037: import org.ow2.easybeans.proxy.reference.AbsCallRef;
038:
039: /**
040: * Factory creating an EJB proxy for remote calls.
041: * @author Florent Benoit.
042: */
043: public class RemoteCallFactory implements ObjectFactory {
044:
045: /**
046: * @return an instance of a proxy (an EJB) that handle local calls.
047: * @param obj the reference containing data to build instance
048: * @param name Name of context, relative to ctx, or null.
049: * @param nameCtx Context relative to which 'name' is named.
050: * @param environment Environment to use when creating the context *
051: * @throws Exception if this object factory encountered an exception while
052: * attempting to create an object, and no other object factories are
053: * to be tried.
054: */
055: public Object getObjectInstance(final Object obj, final Name name,
056: final Context nameCtx, final Hashtable<?, ?> environment)
057: throws Exception {
058: if (obj instanceof Reference) {
059: Reference ref = (Reference) obj;
060:
061: // get the embeddedID, getContainerId(), getFactoryName()
062: RefAddr containerIDAddr = ref.get(AbsCallRef.CONTAINER_ID);
063: RefAddr factoryNameAddr = ref.get(AbsCallRef.FACTORY_NAME);
064: RefAddr itfClassNameAddr = ref
065: .get(AbsCallRef.INTERFACE_NAME);
066: RefAddr useIDAddr = ref.get(AbsCallRef.USE_ID);
067:
068: String containerID = (String) containerIDAddr.getContent();
069: String factoryName = (String) factoryNameAddr.getContent();
070: String itfClassName = (String) itfClassNameAddr
071: .getContent();
072: boolean useID = Boolean.valueOf(
073: (String) useIDAddr.getContent()).booleanValue();
074:
075: // Build new Handler
076: ClientRPCInvocationHandler handler = buildRemoteHandler(
077: containerID, factoryName, useID);
078: // Set the environment used by client to do the lookup.
079: handler.setRMIEnv(environment);
080:
081: // Get current classloader
082: ClassLoader classLoader = Thread.currentThread()
083: .getContextClassLoader();
084:
085: // load class
086: Class<?> clz = null;
087: try {
088: clz = classLoader.loadClass(itfClassName);
089: } catch (ClassNotFoundException e) {
090: throw new EZBContainerException(
091: "Cannot find the class '" + itfClassName
092: + "' in Classloader '" + classLoader
093: + "'.", e);
094: }
095:
096: // set the interface class
097: handler.setInterfaceClass(clz);
098:
099: // build the proxy
100: Object proxy = Proxy.newProxyInstance(classLoader,
101: new Class[] { clz }, handler);
102:
103: // Stateful case ? needs to invoke a remote method in order to
104: // initialize the ID as the ID needs to be present when the client
105: // performs the lookup.
106: if (useID) {
107: proxy.toString();
108: }
109:
110: // return the object built.
111: return proxy;
112: }
113: throw new IllegalStateException(
114: "Can only build object with a reference");
115: }
116:
117: /**
118: * Build an instance of a remote RPC handler. Can be used by subclasses to change the object.
119: * @param containerID the id of the container that will be called on the
120: * remote side.
121: * @param factoryName the name of the remote factory.
122: * @param useID true if all instance build with this ref are unique
123: * (stateful), false if it references the same object (stateless)
124: * @return an instance of a remote handler.
125: */
126: protected ClientRPCInvocationHandler buildRemoteHandler(
127: final String containerID, final String factoryName,
128: final boolean useID) {
129: return new ClientRPCInvocationHandler(containerID, factoryName,
130: useID);
131: }
132: }
|