KickJava   Java API By Example, From Geeks To Geeks.

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


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: LocalCallInvocationHandler.java 1133 2006-10-04 14:30:41Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.rpc;
27
28 import java.io.Externalizable JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.ObjectInput JavaDoc;
31 import java.io.ObjectOutput JavaDoc;
32 import java.lang.reflect.Method JavaDoc;
33 import java.util.HashMap JavaDoc;
34
35 import javax.ejb.NoSuchEJBException JavaDoc;
36
37 import org.objectweb.easybeans.api.EZBContainer;
38 import org.objectweb.easybeans.api.EZBServer;
39 import org.objectweb.easybeans.api.Factory;
40 import org.objectweb.easybeans.rpc.api.EJBResponse;
41 import org.objectweb.easybeans.rpc.api.RPCException;
42 import org.objectweb.easybeans.rpc.util.Hash;
43 import org.objectweb.easybeans.server.EmbeddedManager;
44
45 /**
46  * Object acting as the proxy for local interfaces calls.
47  * @author Florent Benoit
48  */

49 public class LocalCallInvocationHandler extends AbsInvocationHandler implements Externalizable JavaDoc {
50
51     /**
52      * UID for serialization.
53      */

54     private static final long serialVersionUID = -4327634481654235615L;
55
56     /**
57      * Embedded server ID.
58      */

59     private Integer JavaDoc embeddedID = null;
60
61
62     /**
63      * Factory for sending requests (build in constructor or when serialization
64      * occurs).
65      */

66     private transient Factory factory = null;
67
68     /**
69      * Build a new Invocation handler.
70      * @param embeddedID the Embedded server ID.
71      * @param containerId the id of the container that will be called on the
72      * remote side.
73      * @param factoryName the name of the remote factory.
74      * @param useID true if all instance build with this ref are unique
75      * (stateful), false if it references the same object (stateless)
76      */

77     public LocalCallInvocationHandler(final Integer JavaDoc embeddedID, final String JavaDoc containerId, final String JavaDoc factoryName,
78             final boolean useID) {
79         super(containerId, factoryName, useID);
80
81         // Server ID
82
this.embeddedID = embeddedID;
83
84         // Init the factory
85
initFactory();
86     }
87
88     /**
89      * Default constructor (used for serialization).
90      */

91     public LocalCallInvocationHandler() {
92         super(null, null, false);
93     }
94
95     /**
96      * Initialize the factory object with the given infos.
97      */

98     private void initFactory() {
99         // Get Embedded server
100
EZBServer ejb3Server = EmbeddedManager.getEmbedded(embeddedID);
101         if (ejb3Server == null) {
102             throw new IllegalStateException JavaDoc("Cannot find the server with id '" + embeddedID + "'.");
103         }
104
105         // Get the container
106
EZBContainer container = ejb3Server.getContainer(getContainerId());
107         if (container == null) {
108             throw new IllegalStateException JavaDoc("Cannot find the container with id '" + getContainerId() + "'.");
109         }
110
111         factory = container.getFactory(getFactoryName());
112         if (factory == null) {
113             throw new IllegalStateException JavaDoc("Cannot find the factory with name '" + getFactoryName() + "'.");
114         }
115     }
116
117     /**
118      * Processes a method invocation on a proxy instance and returns the result.
119      * This method will be invoked on an invocation handler when a method is
120      * invoked on a proxy instance that it is associated with.
121      * @param proxy the proxy instance that the method was invoked on
122      * @param method the <code>Method</code> instance corresponding to the
123      * interface method invoked on the proxy instance. The declaring
124      * class of the <code>Method</code> object will be the interface
125      * that the method was declared in, which may be a superinterface of
126      * the proxy interface that the proxy class inherits the method
127      * through.
128      * @param args an array of objects containing the values of the arguments
129      * passed in the method invocation on the proxy instance, or
130      * <code>null</code> if interface method takes no arguments.
131      * Arguments of primitive types are wrapped in instances of the
132      * appropriate primitive wrapper class, such as
133      * <code>java.lang.Integer</code> or <code>java.lang.Boolean</code>.
134      * @return the value to return from the method invocation on the proxy
135      * instance.
136      * @throws Exception the exception to throw from the method invocation on
137      * the proxy instance.
138      */

139     public Object JavaDoc invoke(final Object JavaDoc proxy, final Method JavaDoc method, final Object JavaDoc[] args) throws Exception JavaDoc {
140         // bean removed ?
141
if (isRemoved()) {
142             throw new NoSuchEJBException JavaDoc("The bean has been removed");
143         }
144
145         // Methods on the Object.class are not send on the remote side
146
if (method.getDeclaringClass().getName().equals("java.lang.Object")) {
147             return handleObjectMethods(method, args);
148         }
149         if (getHashedMethods() == null) {
150             setHashedMethods(new HashMap JavaDoc<Method JavaDoc, Long JavaDoc>());
151         }
152
153         Long JavaDoc hashLong = getHashedMethods().get(method);
154         if (hashLong == null) {
155             hashLong = Long.valueOf(Hash.hashMethod(method));
156             getHashedMethods().put(method, hashLong);
157         }
158
159         long hash = hashLong.longValue();
160
161         // Now, need to invoke the bean
162
EJBResponse response = null;
163         response = factory.localCall(hash, args, getBeanId());
164         setBeanId(response.getBeanId());
165
166         // bean removed ?
167
setRemoved(response.isRemoved());
168
169         RPCException rpcException = response.getRPCException();
170         if (rpcException != null) {
171             handleThrowable(rpcException.getCause(), rpcException.isApplicationException(), method);
172         }
173
174
175
176
177         return response.getValue();
178
179     }
180
181     /**
182      * Save our content.
183      * @param out the stream to write the object to
184      * @throws IOException Includes any I/O exceptions that may occur
185      */

186     public void writeExternal(final ObjectOutput JavaDoc out) throws IOException JavaDoc {
187         // server ID
188
out.writeObject(embeddedID);
189         // Container ID
190
out.writeObject(getContainerId());
191         // Factory name
192
out.writeObject(getFactoryName());
193         // interface class name
194
out.writeObject(getInterfaceClassName());
195         // boolean (extending java.rmi.remote ?)
196
out.writeBoolean(isExtendingRmiRemote());
197
198         // boolean (useID flag)
199
out.writeBoolean(isUsingID());
200
201         // boolean (removed)
202
out.writeBoolean(isRemoved());
203     }
204
205     /**
206      * Build our content.
207      * @param in the stream to read data from in order to restore the object
208      * @exception IOException if I/O errors occur
209      * @exception ClassNotFoundException If the class for an object being
210      * restored cannot be found.
211      */

212     public void readExternal(final ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
213         // read ServerID
214
this.embeddedID = (Integer JavaDoc) in.readObject();
215         // read Container id
216
setContainerId((String JavaDoc) in.readObject());
217         // read factory's name
218
setFactoryName((String JavaDoc) in.readObject());
219
220         // interface class name
221
setInterfaceClassName((String JavaDoc) in.readObject());
222
223         // boolean flag
224
setExtendingRmiRemote(in.readBoolean());
225
226         // useID flag
227
setUseID(in.readBoolean());
228
229         // removed flag
230
setRemoved(in.readBoolean());
231
232         // init Factory object (transient)
233
initFactory();
234
235     }
236
237 }
238
Popular Tags