KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > rpc > rmi > client > RMIClientRPC


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: RMIClientRPC.java 1063 2006-08-09 16:01:28Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.rpc.rmi.client;
27
28 import java.rmi.RemoteException JavaDoc;
29 import java.util.Hashtable JavaDoc;
30
31 import javax.naming.Context JavaDoc;
32 import javax.naming.InitialContext JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34 import javax.rmi.PortableRemoteObject JavaDoc;
35
36 import org.objectweb.easybeans.rpc.api.ClientRPC;
37 import org.objectweb.easybeans.rpc.api.EJBRequest;
38 import org.objectweb.easybeans.rpc.api.EJBResponse;
39 import org.objectweb.easybeans.rpc.rmi.server.RMIServerRPC;
40
41 /**
42  * RMI implementation of the RPC mechanism of EJB requests/responses.
43  * @author Florent Benoit
44  */

45 public class RMIClientRPC implements ClientRPC {
46
47     /**
48      * EasyBeans factory.
49      */

50     public static final String JavaDoc EASYBEANS_RMI_FACTORY = "easybeans.rpc.rmi.factory";
51
52     /**
53      * Environment to use to get the remote RPC object.
54      */

55     private Hashtable JavaDoc rmiClientEnvironment = null;
56
57
58     /**
59      * Builds a new RMI client RPC with the given rmi environment.
60      * @param rmiClientEnvironment the RMI environment.
61      */

62     public RMIClientRPC(final Hashtable JavaDoc<?, ?> rmiClientEnvironment) {
63         this.rmiClientEnvironment = rmiClientEnvironment;
64     }
65
66
67     /**
68      * Sends a request comes to the remote side.<br>
69      * A response is done by the remote side and it sends back a response.
70      * @param request the EJB request.
71      * @return a response that have been processed by the server.
72      */

73     @SuppressWarnings JavaDoc("unchecked")
74     public EJBResponse sendEJBRequest(final EJBRequest request) {
75
76         // initial context factory ?
77
String JavaDoc additionalInitialFactory = System.getProperty(EASYBEANS_RMI_FACTORY);
78         if (additionalInitialFactory != null) {
79             rmiClientEnvironment.put(Context.INITIAL_CONTEXT_FACTORY, additionalInitialFactory);
80         }
81
82
83         Context JavaDoc ictx = null;
84         try {
85             ictx = new InitialContext JavaDoc(rmiClientEnvironment);
86         } catch (NamingException JavaDoc ne) {
87             throw new IllegalStateException JavaDoc(ne);
88         }
89
90         // Lookup server object
91
Object JavaDoc serverObject = null;
92         try {
93             serverObject = ictx.lookup(RMIServerRPC.RPC_JNDI_NAME);
94         } catch (NamingException JavaDoc ne) {
95             throw new IllegalStateException JavaDoc(ne);
96         }
97         // Get a connection to the RPC server
98
RMIServerRPC server = (RMIServerRPC) PortableRemoteObject.narrow(serverObject, RMIServerRPC.class);
99
100         // Send Request and get the answer
101
try {
102             return server.getEJBResponse(request);
103         } catch (RemoteException JavaDoc re) {
104             throw new RuntimeException JavaDoc("Error while handling answer on the remote side ", re);
105         }
106
107     }
108
109 }
110
Popular Tags