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: RMIClientRPC.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.rpc.rmi.client;
025:
026: import java.rmi.RemoteException;
027: import java.util.Hashtable;
028:
029: import javax.naming.Context;
030: import javax.naming.InitialContext;
031: import javax.naming.NamingException;
032: import javax.rmi.PortableRemoteObject;
033:
034: import org.ow2.easybeans.rpc.api.ClientRPC;
035: import org.ow2.easybeans.rpc.api.EJBRequest;
036: import org.ow2.easybeans.rpc.api.EJBResponse;
037: import org.ow2.easybeans.rpc.rmi.server.RMIServerRPC;
038:
039: /**
040: * RMI implementation of the RPC mechanism of EJB requests/responses.
041: * @author Florent Benoit
042: */
043: public class RMIClientRPC implements ClientRPC {
044:
045: /**
046: * EasyBeans factory.
047: */
048: public static final String EASYBEANS_RMI_FACTORY = "easybeans.rpc.rmi.factory";
049:
050: /**
051: * Environment to use to get the remote RPC object.
052: */
053: private Hashtable rmiClientEnvironment = null;
054:
055: /**
056: * Builds a new RMI client RPC with the given rmi environment.
057: * @param rmiClientEnvironment the RMI environment.
058: */
059: public RMIClientRPC(final Hashtable<?, ?> rmiClientEnvironment) {
060: this .rmiClientEnvironment = rmiClientEnvironment;
061: }
062:
063: /**
064: * Sends a request comes to the remote side.<br>
065: * A response is done by the remote side and it sends back a response.
066: * @param request the EJB request.
067: * @return a response that have been processed by the server.
068: */
069: @SuppressWarnings("unchecked")
070: public EJBResponse sendEJBRequest(final EJBRequest request) {
071:
072: // initial context factory ?
073: String additionalInitialFactory = System
074: .getProperty(EASYBEANS_RMI_FACTORY);
075: if (additionalInitialFactory != null) {
076: rmiClientEnvironment.put(Context.INITIAL_CONTEXT_FACTORY,
077: additionalInitialFactory);
078: }
079:
080: Context ictx = null;
081: try {
082: ictx = new InitialContext(rmiClientEnvironment);
083: } catch (NamingException ne) {
084: throw new IllegalStateException(ne);
085: }
086:
087: // Lookup server object
088: Object serverObject = null;
089: try {
090: serverObject = ictx.lookup(RMIServerRPC.RPC_JNDI_NAME);
091: } catch (NamingException ne) {
092: throw new IllegalStateException(ne);
093: }
094: // Get a connection to the RPC server
095: RMIServerRPC server = (RMIServerRPC) PortableRemoteObject
096: .narrow(serverObject, RMIServerRPC.class);
097:
098: // Send Request and get the answer
099: try {
100: return server.getEJBResponse(request);
101: } catch (RemoteException re) {
102: throw new RuntimeException(
103: "Error while handling answer on the remote side ",
104: re);
105: }
106:
107: }
108:
109: }
|