KickJava   Java API By Example, From Geeks To Geeks.

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


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: EntityEJBHomeHandler.java 1912 2005-06-16 22:29:56Z jlaskowski $
44  */

45 package org.openejb.client;
46
47 import java.lang.reflect.Method JavaDoc;
48 import java.rmi.RemoteException JavaDoc;
49
50 import javax.ejb.EJBObject JavaDoc;
51 import javax.ejb.Handle JavaDoc;
52
53 /**
54  *
55  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
56  * @since 11/25/2001
57  */

58 public class EntityEJBHomeHandler extends EJBHomeHandler {
59     
60     public EntityEJBHomeHandler(){
61     }
62     
63     public EntityEJBHomeHandler(EJBMetaDataImpl ejb, ServerMetaData server, ClientMetaData client){
64         super(ejb, server, client);
65     }
66    
67     /**
68      * <P>
69      * EJB 1.1 --
70      * 9.1.8 Finder method return type
71      *
72      * 9.1.8.1 Single-object finder
73      *
74      * Some finder methods (such as ejbFindByPrimaryKey) are designed to return
75      * at most one entity object. For these single-object finders, the result type
76      * of the find<METHOD>(...)method defined in the entity bean’s home interface
77      * is the entity bean’s remote interface. The result type of the corresponding
78      * ejbFind<METHOD>(...) method defined in the entity’s implementation class is
79      * the entity bean’s primary key type.
80      *
81      * 9.1.8.2 Multi-object finders
82      *
83      * Some finder methods are designed to return multiple entity objects. For
84      * these multi-object finders, the result type of the find<METHOD>(...)method
85      * defined in the entity bean’s home interface is a col-lection of objects
86      * implementing the entity bean’s remote interface. The result type of the
87      * corresponding ejbFind<METHOD>(...) implementation method defined in the
88      * entity bean’s implementation class is a collection of objects of the entity
89      * bean’s primary key type.
90      *
91      * The Bean Provider can choose two types to define a collection type for a finder:
92      * • the JDK™ 1.1 java.util.Enumeration interface
93      * • the Java™ 2 java.util.Collection interface
94      *
95      * A Bean Provider that wants to ensure that the entity bean is compatible
96      * with containers and clients based on JDK TM 1.1 software must use the
97      * java.util.Enumeration interface for the finder’s result type.
98      * </P>
99      * <P>
100      * Locates and returns a new EJBObject or a collection
101      * of EJBObjects. The EJBObject(s) is a new proxy with
102      * a new handler. This implementation should not be
103      * sent outside the virtual machine.
104      * </P>
105      * <P>
106      * This method propogates to the container
107      * system.
108      * </P>
109      * <P>
110      * The find method is required to be defined
111      * by the bean's home interface of Entity beans.
112      * </P>
113      *
114      * @param method
115      * @param args
116      * @param proxy
117      * @return Returns an new EJBObject proxy and handler
118      * @exception Throwable
119      */

120     protected Object JavaDoc findX(Method JavaDoc method, Object JavaDoc[] args, Object JavaDoc proxy) throws Throwable JavaDoc {
121         EJBRequest req = new EJBRequest( EJB_HOME_FIND );
122         
123         req.setMethodParameters( args );
124         req.setMethodInstance( method );
125         req.setClientIdentity( client.getClientIdentity() );
126         req.setDeploymentCode( ejb.deploymentCode );
127         req.setDeploymentId( ejb.deploymentID );
128         req.setPrimaryKey( primaryKey );
129         
130         EJBResponse res = request( req );
131         
132         Object JavaDoc primKey = null;
133         EJBObjectHandler handler = null;
134         Object JavaDoc[] primaryKeys = null;
135
136         switch (res.getResponseCode()) {
137         case EJB_ERROR:
138             throw (Throwable JavaDoc)res.getResult();
139         case EJB_SYS_EXCEPTION:
140             throw (Throwable JavaDoc)res.getResult();
141         case EJB_APP_EXCEPTION:
142             throw (Throwable JavaDoc)res.getResult();
143         
144         case EJB_OK_FOUND:
145             primKey = res.getResult();
146             handler = EJBObjectHandler.createEJBObjectHandler(ejb,server,client,primKey);
147             handler.setEJBHomeProxy((EJBHomeProxy)proxy);
148             registerHandler(ejb.deploymentID+":"+primKey, handler);
149             return handler.createEJBObjectProxy();
150         
151         case EJB_OK_FOUND_COLLECTION:
152             // The result is an array of primary keys
153
// We are going to convert those primary keys into
154
// EJBObject instance and reuse the same array to
155
// create the collection.
156
primaryKeys = (Object JavaDoc[])res.getResult();
157
158             for (int i=0; i < primaryKeys.length; i++){
159                 primKey = primaryKeys[i];
160                 handler = EJBObjectHandler.createEJBObjectHandler(ejb,server,client,primKey);
161                 handler.setEJBHomeProxy((EJBHomeProxy)proxy);
162                 registerHandler(ejb.deploymentID+":"+primKey, handler);
163                 primaryKeys[i] = handler.createEJBObjectProxy();
164             }
165             return java.util.Arrays.asList( primaryKeys );
166         case EJB_OK_FOUND_ENUMERATION:
167             // TODO: it's almost duplication of what's in EJB_OK_FOUND_COLLECTION
168
primaryKeys = (Object JavaDoc[])res.getResult();
169
170             for (int i=0; i < primaryKeys.length; i++){
171                 primKey = primaryKeys[i];
172                 handler = EJBObjectHandler.createEJBObjectHandler(ejb,server,client,primKey);
173                 handler.setEJBHomeProxy((EJBHomeProxy)proxy);
174                 registerHandler(ejb.deploymentID+":"+primKey, handler);
175                 primaryKeys[i] = handler.createEJBObjectProxy();
176             }
177             return new org.openejb.util.ArrayEnumeration( java.util.Arrays.asList( primaryKeys ) );
178         default:
179             throw new RemoteException JavaDoc("Received invalid response code from server: "+res.getResponseCode());
180         }
181     }
182     /**
183      * <P>
184      * Attempts to remove an EJBObject from the
185      * container system. The EJBObject to be removed
186      * is represented by the primaryKey passed
187      * into the remove method of the EJBHome.
188      * </P>
189      * <P>
190      * This method propogates to the container system.
191      * </P>
192      * <P>
193      * remove(Object primary) is a method of javax.ejb.EJBHome
194      * </P>
195      * <P>
196      * Checks if the caller is authorized to invoke the
197      * javax.ejb.EJBHome.remove on the EJBHome of the
198      * deployment.
199      * </P>
200      *
201      * @param method
202      * @param args
203      * @return Returns null
204      * @exception Throwable
205      * @see javax.ejb.EJBHome
206      * @see javax.ejb.EJBHome#remove
207      */

208     protected Object JavaDoc removeByPrimaryKey(Method JavaDoc method, Object JavaDoc[] args, Object JavaDoc proxy) throws Throwable JavaDoc{
209         
210         Object JavaDoc primKey = args[0];
211                
212         if ( primKey == null ) throw new NullPointerException JavaDoc("The primary key is null.");
213         //TODO: Send EJBRequest to server
214

215         /*
216          * This operation takes care of invalidating all the EjbObjectProxyHanders
217          * associated with the same RegistryId. See this.createProxy().
218          */

219         invalidateAllHandlers(ejb.deploymentID+":"+primKey);
220         return null;
221     }
222     /**
223      * <P>
224      * Attempts to remove an EJBObject from the
225      * container system. The EJBObject to be removed
226      * is represented by the javax.ejb.Handle object passed
227      * into the remove method in the EJBHome.
228      * </P>
229      * <P>
230      * This method propogates to the container system.
231      * </P>
232      * <P>
233      * remove(Handle handle) is a method of javax.ejb.EJBHome
234      * </P>
235      * <P>
236      * Checks if the caller is authorized to invoke the
237      * javax.ejb.EJBHome.remove on the EJBHome of the
238      * deployment.
239      * </P>
240      *
241      * @param method
242      * @param args
243      * @return Returns null
244      * @exception Throwable
245      * @see javax.ejb.EJBHome
246      * @see javax.ejb.EJBHome#remove
247      */

248     protected Object JavaDoc removeWithHandle(Method JavaDoc method, Object JavaDoc[] args, Object JavaDoc proxy) throws Throwable JavaDoc{
249         
250         if ( args[0] == null ) throw new RemoteException JavaDoc("Handler is null");
251
252         //DMB: There is a better way to do this,
253
// but this will work for now.
254
Handle JavaDoc handle = (Handle JavaDoc)args[0];
255         EJBObject JavaDoc ejbObject = handle.getEJBObject();
256         ejbObject.remove();
257         return null;
258     }
259     
260 }
261
Popular Tags