KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > client > EJBHomeHandler


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact dev@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://www.openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: EJBHomeHandler.java 1921 2005-06-19 22:40:34Z jlaskowski $
44  */

45 package org.openejb.client;
46
47 import java.io.Externalizable JavaDoc;
48 import java.io.IOException JavaDoc;
49 import java.io.ObjectInput JavaDoc;
50 import java.io.ObjectOutput JavaDoc;
51 import java.lang.reflect.Method JavaDoc;
52 import java.rmi.RemoteException JavaDoc;
53
54 import javax.ejb.EJBHome JavaDoc;
55 import javax.ejb.Handle JavaDoc;
56
57 import org.openejb.client.proxy.ProxyManager;
58
59 /**
60  * Handles invocations from an EJBHomeProxy.
61  *
62  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
63  */

64 public abstract class EJBHomeHandler extends EJBInvocationHandler implements Externalizable JavaDoc {
65
66     protected static final Method JavaDoc GETEJBMETADATA= getMethod(EJBHome JavaDoc.class, "getEJBMetaData", null);
67     protected static final Method JavaDoc GETHOMEHANDLE = getMethod(EJBHome JavaDoc.class, "getHomeHandle", null);
68     protected static final Method JavaDoc REMOVE_W_KEY = getMethod(EJBHome JavaDoc.class, "remove", new Class JavaDoc []{Object JavaDoc.class});
69     protected static final Method JavaDoc REMOVE_W_HAND = getMethod(EJBHome JavaDoc.class, "remove", new Class JavaDoc []{Handle JavaDoc.class});
70     protected static final Method JavaDoc GETHANDLER = getMethod(EJBHomeProxy.class, "getEJBHomeHandler", null);
71     
72     /**
73      * Constructs an EJBHomeHandler to handle invocations from an EJBHomeProxy.
74      */

75     public EJBHomeHandler() {
76     }
77     
78     public EJBHomeHandler(EJBMetaDataImpl ejb, ServerMetaData server, ClientMetaData client){
79         super(ejb, server, client);
80     }
81     
82     public static EJBHomeHandler createEJBHomeHandler(EJBMetaDataImpl ejb, ServerMetaData server, ClientMetaData client) {
83         
84         switch (ejb.type) {
85             case EJBMetaDataImpl.BMP_ENTITY:
86             case EJBMetaDataImpl.CMP_ENTITY:
87                 
88                 return new EntityEJBHomeHandler(ejb, server, client);
89             
90             case EJBMetaDataImpl.STATEFUL:
91                 
92                 return new StatefulEJBHomeHandler(ejb, server, client);
93             
94             case EJBMetaDataImpl.STATELESS:
95                 
96                 return new StatelessEJBHomeHandler(ejb, server, client);
97         }
98         return null;
99
100     }
101     
102 // protected abstract EJBObjectHandler newEJBObjectHandler();
103

104     public EJBHomeProxy createEJBHomeProxy(){
105         try{
106         Class JavaDoc[] interfaces = new Class JavaDoc[]{ EJBHomeProxy.class, ejb.homeClass };
107         return (EJBHomeProxy) ProxyManager.newProxyInstance(interfaces, this);
108         } catch (IllegalAccessException JavaDoc e){
109             //TODO: Better exception handling.
110
e.printStackTrace();
111         }
112         return null;
113     }
114
115     protected Object JavaDoc _invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc{
116
117         String JavaDoc methodName = method.getName();
118         
119         try{
120
121             if (method.getDeclaringClass() == Object JavaDoc.class ) {
122                 if ( method.equals( TOSTRING ) ){
123                     return "proxy="+this;
124                 } else if ( method.equals( EQUALS ) ) {
125                     //TODO
126
return Boolean.FALSE;
127                     // Maybe turn this into Externalizable
128
} else if ( method.equals( HASHCODE ) ) {
129                     return new Integer JavaDoc( this.hashCode() );
130                     //TODO
131
// Maybe turn this into Externalizable
132
} else {
133                     throw new UnsupportedOperationException JavaDoc("Unkown method: "+method);
134                 }
135             } else if (method.getDeclaringClass() == EJBHomeProxy.class ) {
136                 if ( method.equals( GETHANDLER ) ){
137                     return this;
138                 } else if (methodName.equals("writeReplace")) {
139                     return new EJBHomeProxyHandle(this);
140                 } else if (methodName.equals("readResolve")) {
141                     //TODO
142
throw new UnsupportedOperationException JavaDoc("Unkown method: "+method);
143                     // Maybe turn this into Externalizable
144
} else {
145                     throw new UnsupportedOperationException JavaDoc("Unkown method: "+method);
146                 }
147             }
148         /*-------------------------------------------------------*/
149         // Process the specific method invoked //
150

151                 
152         /*-- CREATE ------------- <HomeInterface>.create(<x>) ---*/
153             if ( methodName.equals("create") ) {
154                 return create(method, args, proxy);
155                 
156         /*-- FIND X --------------- <HomeInterface>.find<x>() ---*/
157             } else if ( methodName.startsWith("find") ){
158                 return findX(method, args, proxy);
159                 
160                     
161         /*-- GET EJB METADATA ------ EJBHome.getEJBMetaData() ---*/
162
163             } else if ( method.equals( GETEJBMETADATA ) ) {
164                 return getEJBMetaData(method, args, proxy);
165
166
167         /*-- GET HOME HANDLE -------- EJBHome.getHomeHandle() ---*/
168
169             } else if ( method.equals( GETHOMEHANDLE ) ) {
170                 return getHomeHandle(method, args, proxy);
171
172
173         /*-- REMOVE ------------------------ EJBHome.remove() ---*/
174
175             } else if ( method.equals( REMOVE_W_HAND ) ) {
176                 return removeWithHandle(method, args, proxy);
177
178             } else if ( method.equals( REMOVE_W_KEY ) ) {
179                 return removeByPrimaryKey(method, args, proxy);
180
181
182         /*-- UNKOWN ---------------------------------------------*/
183             } else {
184
185                 throw new UnsupportedOperationException JavaDoc("Unkown method: "+method);
186
187             }
188         //TODO:1: Catch this in the server-side and return an OpenEJB specific
189
// exception class.
190
} catch ( org.openejb.SystemException se ) {
191             invalidateReference();
192             throw new RemoteException JavaDoc("Container has suffered a SystemException",se.getRootCause());
193         }
194         
195             
196     }
197
198     /*-------------------------------------------------*/
199     /* Home interface methods */
200     /*-------------------------------------------------*/
201     
202     /**
203      * <P>
204      * Creates a new EJBObject and returns it to the
205      * caller. The EJBObject is a new proxy with a
206      * new handler. This implementation should not be
207      * sent outside the virtual machine.
208      * </P>
209      * <P>
210      * This method propogates to the container
211      * system.
212      * </P>
213      * <P>
214      * The create method is required to be defined
215      * by the bean's home interface.
216      * </P>
217      *
218      * @param method
219      * @param args
220      * @param proxy
221      * @return Returns an new EJBObject proxy and handler
222      * @exception Throwable
223      */

224     protected Object JavaDoc create(Method JavaDoc method, Object JavaDoc[] args, Object JavaDoc proxy) throws Throwable JavaDoc{
225         EJBRequest req = new EJBRequest( EJB_HOME_CREATE );
226         
227         req.setClientIdentity( client.getClientIdentity() );
228         req.setDeploymentCode( ejb.deploymentCode );
229         req.setDeploymentId( ejb.deploymentID );
230         req.setMethodInstance( method );
231         req.setMethodParameters( args );
232         
233         EJBResponse res = request( req );
234   
235         switch (res.getResponseCode()) {
236         case EJB_SYS_EXCEPTION:
237             throw (Throwable JavaDoc)res.getResult();
238         case EJB_APP_EXCEPTION:
239             throw (Throwable JavaDoc)res.getResult();
240         case EJB_ERROR:
241             throw (Throwable JavaDoc)res.getResult();
242         case EJB_OK:
243             // Create the EJBObject proxy
244
Object JavaDoc primKey = res.getResult();
245             EJBObjectHandler handler = EJBObjectHandler.createEJBObjectHandler(ejb,server,client,primKey);
246             handler.setEJBHomeProxy((EJBHomeProxy)proxy);
247             //TODO:1: Add the proxy to the handler registry
248
return handler.createEJBObjectProxy();
249         default:
250             throw new RemoteException JavaDoc("Received invalid response code from server: "+res.getResponseCode());
251         }
252     }
253
254     /**
255      * <P>
256      * Locates and returns a new EJBObject or a collection
257      * of EJBObjects. The EJBObject(s) is a new proxy with
258      * a new handler. This implementation should not be
259      * sent outside the virtual machine.
260      * </P>
261      * <P>
262      * This method propogates to the container
263      * system.
264      * </P>
265      * <P>
266      * The find method is required to be defined
267      * by the bean's home interface of Entity beans.
268      * </P>
269      *
270      * @param method
271      * @param args
272      * @param proxy
273      * @return Returns an new EJBObject proxy and handler
274      * @exception Throwable
275      */

276     protected abstract Object JavaDoc findX(Method JavaDoc method, Object JavaDoc[] args, Object JavaDoc proxy) throws Throwable JavaDoc;
277
278     /*-------------------------------------------------*/
279     /* EJBHome methods */
280     /*-------------------------------------------------*/
281
282     /**
283      * <P>
284      * Returns an EJBMetaData implementation that is
285      * valid inside this virtual machine. This
286      * implementation should not be sent outside the
287      * virtual machine.
288      * </P>
289      * <P>
290      * This method does not propogate to the container
291      * system.
292      * </P>
293      * <P>
294      * getMetaData is a method of javax.ejb.EJBHome
295      * </P>
296      * <P>
297      * Checks if the caller is authorized to invoke the
298      * javax.ejb.EJBHome.getMetaData on the EJBHome of the
299      * deployment.
300      * </P>
301      *
302      * @return Returns an IntraVmMetaData
303      * @exception Throwable
304      * @see javax.ejb.EJBHome
305      * @see javax.ejb.EJBHome#getEJBMetaData
306      */

307     protected Object JavaDoc getEJBMetaData(Method JavaDoc method, Object JavaDoc[] args, Object JavaDoc proxy) throws Throwable JavaDoc{
308         return ejb;
309     }
310
311     /**
312      * <P>
313      * Returns a HomeHandle implementation that is
314      * valid inside this virtual machine. This
315      * implementation should not be sent outside the
316      * virtual machine.
317      * </P>
318      * <P>
319      * This method does not propogate to the container
320      * system.
321      * </P>
322      * <P>
323      * getHomeHandle is a method of javax.ejb.EJBHome
324      * </P>
325      * <P>
326      * Checks if the caller is authorized to invoke the
327      * javax.ejb.EJBHome.getHomeHandle on the EJBHome of the
328      * deployment.
329      * </P>
330      *
331      * @param proxy
332      * @return Returns an IntraVmHandle
333      * @exception Throwable
334      * @see javax.ejb.EJBHome
335      * @see javax.ejb.EJBHome#getHomeHandle
336      */

337     protected Object JavaDoc getHomeHandle(Method JavaDoc method, Object JavaDoc[] args, Object JavaDoc proxy) throws Throwable JavaDoc{
338         //return new EJBHomeHandle(this);
339
return new EJBHomeHandle((EJBHomeProxy)proxy);
340     }
341     
342     /**
343      * <P>
344      * Attempts to remove an EJBObject from the
345      * container system. The EJBObject to be removed
346      * is represented by the javax.ejb.Handle object passed
347      * into the remove method in the EJBHome.
348      * </P>
349      * <P>
350      * This method propogates to the container system.
351      * </P>
352      * <P>
353      * remove(Handle handle) is a method of javax.ejb.EJBHome
354      * </P>
355      * <P>
356      * Checks if the caller is authorized to invoke the
357      * javax.ejb.EJBHome.remove on the EJBHome of the
358      * deployment.
359      * </P>
360      *
361      * @param method
362      * @param args
363      * @return Returns null
364      * @exception Throwable
365      * @see javax.ejb.EJBHome
366      * @see javax.ejb.EJBHome#remove
367      */

368     protected abstract Object JavaDoc removeWithHandle(Method JavaDoc method, Object JavaDoc[] args, Object JavaDoc proxy) throws Throwable JavaDoc;
369
370     /**
371      * <P>
372      * Attempts to remove an EJBObject from the
373      * container system. The EJBObject to be removed
374      * is represented by the primaryKey passed
375      * into the remove method of the EJBHome.
376      * </P>
377      * <P>
378      * This method propogates to the container system.
379      * </P>
380      * <P>
381      * remove(Object primary) is a method of javax.ejb.EJBHome
382      * </P>
383      * <P>
384      * Checks if the caller is authorized to invoke the
385      * javax.ejb.EJBHome.remove on the EJBHome of the
386      * deployment.
387      * </P>
388      *
389      * @param method
390      * @param args
391      * @return Returns null
392      * @exception Throwable
393      * @see javax.ejb.EJBHome
394      * @see javax.ejb.EJBHome#remove
395      */

396     protected abstract Object JavaDoc removeByPrimaryKey(Method JavaDoc method, Object JavaDoc[] args, Object JavaDoc proxy) throws Throwable JavaDoc;
397
398
399     /**
400      * The object implements the readExternal method to restore its
401      * contents by calling the methods of DataInput for primitive
402      * types and readObject for objects, strings and arrays. The
403      * readExternal method must read the values in the same sequence
404      * and with the same types as were written by writeExternal.
405      *
406      * @param in the stream to read data from in order to restore the object
407      * @exception IOException if I/O errors occur
408      * @exception ClassNotFoundException If the class for an object being
409      * restored cannot be found.
410      */

411     public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc,ClassNotFoundException JavaDoc {
412     }
413     /**
414      * The object implements the writeExternal method to save its contents
415      * by calling the methods of DataOutput for its primitive values or
416      * calling the writeObject method of ObjectOutput for objects, strings,
417      * and arrays.
418      *
419      * @serialData Overriding methods should use this tag to describe
420      * the data layout of this Externalizable object.
421      * List the sequence of element types and, if possible,
422      * relate the element to a public/protected field and/or
423      * method of this Externalizable class.
424      *
425      * @param out the stream to write the object to
426      * @exception IOException Includes any I/O exceptions that may occur
427      */

428     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc {
429     }
430
431 }
432
433
434
Popular Tags