KickJava   Java API By Example, From Geeks To Geeks.

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


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

25
26 package org.objectweb.easybeans.rpc;
27
28 import java.io.Serializable JavaDoc;
29 import java.lang.reflect.InvocationHandler JavaDoc;
30 import java.lang.reflect.Method JavaDoc;
31 import java.rmi.Remote JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import javax.ejb.EJBException JavaDoc;
36
37 /**
38  * Abstract class used by remote or local invocation handler.
39  * @author Florent Benoit
40  */

41 public abstract class AbsInvocationHandler implements InvocationHandler JavaDoc, Serializable JavaDoc {
42
43     /**
44      * Bean has been removed.
45      */

46     private boolean removed = false;
47
48     /**
49      * Container id.
50      */

51     private String JavaDoc containerId = null;
52
53     /**
54      * Factory name.
55      */

56     private String JavaDoc factoryName = null;
57
58     /**
59      * Bean id.
60      */

61     private Long JavaDoc beanId = null;
62
63     /**
64      * Map between method and its hash.
65      */

66     private transient Map JavaDoc<Method JavaDoc, Long JavaDoc> hashedMethods = null;
67
68     /**
69      * Interface used by this handler.
70      */

71     private String JavaDoc interfaceClassName = null;
72
73     /**
74      * Boolean used to know if this class extends java.rmi.Remote class.
75      */

76     private boolean isItfExtendingRmiRemote = false;
77
78     /**
79      * useID true if all instance build with this ref are unique (stateful), false if it references the same object (stateless).
80      */

81     private boolean useID = false;
82
83     /**
84      * Build a new Invocation handler.
85      * @param containerId the id of the container that will be called on the
86      * remote side.
87      * @param factoryName the name of the remote factory.
88      * @param useID true if all instance build with this ref are unique
89      * (stateful), false if it references the same object (stateless)
90      */

91     public AbsInvocationHandler(final String JavaDoc containerId, final String JavaDoc factoryName, final boolean useID) {
92         this.containerId = containerId;
93         this.factoryName = factoryName;
94         this.useID = useID;
95         this.hashedMethods = new HashMap JavaDoc<Method JavaDoc, Long JavaDoc>();
96     }
97
98     /**
99      * Manages all methods of java.lang.Object class.
100      * @param method the <code>Method</code> instance corresponding to the
101      * interface method invoked on the proxy instance. The declaring
102      * class of the <code>Method</code> object will be the interface
103      * that the method was declared in, which may be a superinterface of
104      * the proxy interface that the proxy class inherits the method
105      * through.
106      * @param args an array of objects containing the values of the arguments
107      * passed in the method invocation on the proxy instance
108      * @return the value of the called method.
109      */

110     protected Object JavaDoc handleObjectMethods(final Method JavaDoc method, final Object JavaDoc[] args) {
111         String JavaDoc methodName = method.getName();
112
113         if (methodName.equals("equals")) {
114             return Boolean.valueOf(this.toString().equals(args[0].toString()));
115         } else if (methodName.equals("toString")) {
116             return this.toString();
117         } else if (methodName.equals("hashCode")) {
118             return Integer.valueOf(this.toString().hashCode());
119         } else {
120             throw new IllegalStateException JavaDoc("Method '" + methodName + "' is not present on Object.class.");
121         }
122     }
123
124     /**
125      * Handle the given throwable and throw the correct exception to the client.
126      * @param originalThrowable the exception that has been thrown on the server side.
127      * @param isApplicationException true if it is an application exception, else false.
128      * @param method the <code>Method</code> instance corresponding to the
129      * interface method invoked on the proxy instance.
130      * @throws Exception the exception that has been wrapped or not for the client.
131      */

132     protected void handleThrowable(final Throwable JavaDoc originalThrowable, final boolean isApplicationException,
133             final Method JavaDoc method) throws Exception JavaDoc {
134
135         // Handle the exception
136
// Checked application exception --> throw exception without wrapping it.
137

138         Class JavaDoc[] exceptions = method.getExceptionTypes();
139         if (exceptions != null) {
140             for (Class JavaDoc clazz : exceptions) {
141                 // Is an Exception but not a runtime exception
142
if (clazz.isInstance(originalThrowable)
143                         && originalThrowable instanceof Exception JavaDoc
144                         && !(originalThrowable instanceof RuntimeException JavaDoc)) {
145                     throw (Exception JavaDoc) originalThrowable;
146                 }
147             }
148         }
149
150         // EJBException --> throw exception without wrapping it.
151
if (originalThrowable instanceof EJBException JavaDoc) {
152             throw (EJBException JavaDoc) originalThrowable;
153         }
154
155         // Runtime Exception :
156
// 1/ It's an application exception : throw exception without wrapping it.
157
// 2/ It's not an application exception : Wrap exception in an EJBException.
158
if (originalThrowable instanceof RuntimeException JavaDoc) {
159             if (isApplicationException) {
160                 throw (RuntimeException JavaDoc) originalThrowable;
161             }
162             // not an application exception
163
throw new EJBException JavaDoc((RuntimeException JavaDoc) originalThrowable);
164         }
165
166         // Exception but not checked (wrap it in an EJBException)
167
if (originalThrowable instanceof Exception JavaDoc) {
168             throw new EJBException JavaDoc((Exception JavaDoc) originalThrowable.getCause());
169         }
170
171         // Other case (throwable): Wrap throwable in an Exception and then in an EJBException
172
// First wrap is required as EJBException accept only Exception, not throwable
173
throw new EJBException JavaDoc(new Exception JavaDoc("Unexpected Exception", originalThrowable));
174     }
175
176     /**
177      * Sets the id of the bean.
178      * @param beanId the new ID.
179      */

180     protected void setBeanId(final Long JavaDoc beanId) {
181         this.beanId = beanId;
182     }
183
184     /**
185      * Sets the hashed methods.
186      * @param hashedMethods the hash for each method.
187      */

188     protected void setHashedMethods(final Map JavaDoc<Method JavaDoc, Long JavaDoc> hashedMethods) {
189         this.hashedMethods = hashedMethods;
190     }
191
192     /**
193      * Gets the bean id.
194      * @return the bean id.
195      */

196     protected Long JavaDoc getBeanId() {
197         return beanId;
198     }
199
200     /**
201      * @return the container id.
202      */

203     protected String JavaDoc getContainerId() {
204         return containerId;
205     }
206
207     /**
208      * @return the name of the factory.
209      */

210     protected String JavaDoc getFactoryName() {
211         return factoryName;
212     }
213
214     /**
215      * @return the hashes for each method.
216      */

217     protected Map JavaDoc<Method JavaDoc, Long JavaDoc> getHashedMethods() {
218         return hashedMethods;
219     }
220
221     /**
222      * Sets the container ID.
223      * @param containerId the identifier of the container.
224      */

225     protected void setContainerId(final String JavaDoc containerId) {
226         this.containerId = containerId;
227     }
228
229     /**
230      * Sets the factory's name.
231      * @param factoryName the name of the factory.
232      */

233     protected void setFactoryName(final String JavaDoc factoryName) {
234         this.factoryName = factoryName;
235     }
236
237     /**
238      * @return the name of the interface that represents this handler.
239      */

240     public String JavaDoc getInterfaceClassName() {
241         return interfaceClassName;
242     }
243
244     /**
245      * Sets the name of the interface that represents this handler.
246      * @param interfaceClassName the name of the interface.
247      */

248     protected void setInterfaceClassName(final String JavaDoc interfaceClassName) {
249         this.interfaceClassName = interfaceClassName;
250     }
251
252     /**
253      * Sets the interface that represents this handler.
254      * @param clz the instance of the interface.
255      */

256     public void setInterfaceClass(final Class JavaDoc clz) {
257         if (Remote JavaDoc.class.isAssignableFrom(clz)) {
258             isItfExtendingRmiRemote = true;
259         }
260         setInterfaceClassName(clz.getName());
261     }
262
263
264     /**
265      * Sets the flag if interface is extending java.rmi.Remote.
266      * @param isItfExtendingRmiRemote true if it extending, else false.
267      */

268     public void setExtendingRmiRemote(final boolean isItfExtendingRmiRemote) {
269         this.isItfExtendingRmiRemote = isItfExtendingRmiRemote;
270     }
271
272     /**
273      * @return true if the interface used by this handler is extending java.rmi.Remote.
274      */

275     public boolean isExtendingRmiRemote() {
276         return isItfExtendingRmiRemote;
277     }
278
279     /**
280      * Sets the flag if all instance build with this ref are unique or not.
281      * @param useID true : (stateful), false if it references the same object (stateless).
282      */

283     public void setUseID(final boolean useID) {
284         this.useID = useID;
285     }
286
287     /**
288      * @return true : (stateful), false if it references the same object (stateless).
289      */

290     public boolean isUsingID() {
291         return useID;
292     }
293
294     /**
295      * Gets a string representation for this handler.
296      * @return a string representation for this handler.
297      */

298     @Override JavaDoc
299     public String JavaDoc toString() {
300         StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
301         sb.append(factoryName);
302         sb.append("_");
303         sb.append(interfaceClassName);
304         sb.append("/");
305         sb.append(containerId);
306         if (useID) {
307             sb.append("@");
308             sb.append(System.identityHashCode(this));
309         }
310         return sb.toString();
311     }
312
313     /**
314      * @return true if the bean has been removed
315      */

316     public boolean isRemoved() {
317         return removed;
318     }
319
320     /**
321      * Sets the removed flag.
322      * @param removed if bean has been removed.
323      */

324     public void setRemoved(final boolean removed) {
325         this.removed = removed;
326     }
327 }
328
Popular Tags