KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > rpc > rmi > server > RMIServerRPCImpl


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: RMIServerRPCImpl.java 792 2006-06-27 14:03:00Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.rpc.rmi.server;
27
28 import java.rmi.RemoteException JavaDoc;
29
30 import javax.rmi.PortableRemoteObject JavaDoc;
31
32 import org.objectweb.easybeans.api.EZBContainer;
33 import org.objectweb.easybeans.api.Factory;
34 import org.objectweb.easybeans.rpc.api.EJBRequest;
35 import org.objectweb.easybeans.rpc.api.EJBResponse;
36 import org.objectweb.easybeans.server.Embedded;
37
38 /**
39  * Server side object which handle the EJB requests.
40  * @author Florent Benoit
41  */

42 public class RMIServerRPCImpl extends PortableRemoteObject JavaDoc implements RMIServerRPC {
43
44     /**
45      * Server on which it depends.
46      */

47     private Embedded ejb3server;
48
49
50     /**
51      * Retry time when container is not available.
52      */

53     private static final int WAIT_TIME = 1000;
54
55     /**
56      * This invoker will discuss with the embedded server when receiving requests.
57      * @param ejb3server the server on which send requests.
58      * @throws RemoteException if RPC fails
59      */

60     public RMIServerRPCImpl(final Embedded ejb3server) throws RemoteException JavaDoc {
61         super();
62         this.ejb3server = ejb3server;
63     }
64
65     /**
66      * Handle a request and send back a response.<br>
67      * It finds the right container and its factory and send the request to the factory.
68      * @param request the ejb request to handle.
69      * @return a response.
70      * @throws RemoteException if there are errors on the prococol.
71      */

72     public EJBResponse getEJBResponse(final EJBRequest request) throws RemoteException JavaDoc {
73
74         String JavaDoc id = request.getContainerId();
75         if (id == null) {
76             throw new RemoteException JavaDoc("No valid container id");
77         }
78         // Get the container
79
EZBContainer container = ejb3server.getContainer(id);
80         if (container == null) {
81             throw new RemoteException JavaDoc("Cannot find the container with id '" + id + "'.");
82         }
83
84         // Once the container is found, get the factory
85
String JavaDoc factoryName = request.getFactory();
86
87
88         // while container is not available, stop the current request
89
while (!container.isAvailable()) {
90             //TODO: change it to a semaphore ?
91
try {
92                 Thread.sleep(WAIT_TIME);
93             } catch (InterruptedException JavaDoc e) {
94                 e.printStackTrace();
95             }
96         }
97
98         // Get the container
99
Factory factory = container.getFactory(factoryName);
100         if (factory == null) {
101             throw new RemoteException JavaDoc("Cannot find the factory with name '" + factoryName + "'.");
102         }
103
104         // Now, need to invoke the bean
105
return factory.rpcInvoke(request);
106         //throw new RemoteException("Error in invoking method on factory", e.getCause());
107
}
108
109 }
110
Popular Tags