KickJava   Java API By Example, From Geeks To Geeks.

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


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: EntityEJBObjectHandler.java 1921 2005-06-19 22:40:34Z jlaskowski $
44  */

45 package org.openejb.client;
46
47 import java.lang.reflect.Method JavaDoc;
48 import java.rmi.RemoteException JavaDoc;
49
50 /**
51  * This InvocationHandler and its proxy are serializable and can be used by
52  * HomeHandle, Handle, and MetaData to persist and revive handles. It maintains
53  * its original client identity which allows the container to be more discerning about
54  * allowing the revieed proxy to be used. See StatefulContaer manager for more details.
55  *
56  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
57  * @author <a HREF="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
58  */

59 public class EntityEJBObjectHandler extends EJBObjectHandler {
60     
61                     
62     public EntityEJBObjectHandler(){
63     }
64     
65     public EntityEJBObjectHandler(EJBMetaDataImpl ejb, ServerMetaData server, ClientMetaData client){
66         super(ejb, server, client);
67     }
68     
69     public EntityEJBObjectHandler(EJBMetaDataImpl ejb, ServerMetaData server, ClientMetaData client, Object JavaDoc primaryKey){
70         super(ejb, server, client, primaryKey);
71         registryId = ejb.deploymentID+":"+primaryKey;
72         registerHandler( registryId, this );
73     }
74     
75     
76     /**
77     * The Registry id is a logical identifier that is used as a key when placing EjbObjectProxyHanlders into
78     * the BaseEjbProxyHanlder's liveHandleRegistry. EjbObjectProxyHanlders that represent the same
79     * bean identity (keyed by the registry id) will be stored together so that they can be removed together
80     * when the EJBInvocationHandler.invalidateAllHandlers is invoked.
81     *
82     * This method is implemented by the subclasses to return an id that logically identifies
83     * bean identity for a specific deployment id and container. The EntityEJBObjectHandler
84     * overrides this method to return a compound key composed of the bean's primary key, deployment id, and
85     * container id. This uniquely identifies the bean identity that is proxied by this handler.
86     */

87     public Object JavaDoc getRegistryId(){
88         return registryId;
89     }
90     
91     
92     protected Object JavaDoc getPrimaryKey(Method JavaDoc method, Object JavaDoc[] args, Object JavaDoc proxy) throws Throwable JavaDoc{
93         return primaryKey;
94     }
95     
96     /**
97      * Entity beans are uniquely identifed by primary key, deloyment id, and the container they are
98      * running in.
99      *
100      * @param method
101      * @param args
102      * @param proxy
103      * @return Object
104      * @exception Throwable
105      */

106     protected Object JavaDoc isIdentical(Method JavaDoc method, Object JavaDoc[] args, Object JavaDoc proxy) throws Throwable JavaDoc{
107         if ( args[0] == null ) return Boolean.FALSE;
108
109         EJBObjectProxy ejbObject = (EJBObjectProxy)args[0];
110         EJBObjectHandler that = ejbObject.getEJBObjectHandler();
111
112         return new Boolean JavaDoc(this.registryId.equals(that.registryId));
113     
114     }
115     
116     protected Object JavaDoc remove(Method JavaDoc method, Object JavaDoc[] args, Object JavaDoc proxy) throws Throwable JavaDoc{
117         
118         EJBRequest req = new EJBRequest( EJB_OBJECT_REMOVE );
119         
120         req.setMethodParameters( args );
121         req.setMethodInstance( method );
122         req.setClientIdentity( client.getClientIdentity() );
123         req.setDeploymentCode( ejb.deploymentCode );
124         req.setDeploymentId( ejb.deploymentID );
125         req.setPrimaryKey( primaryKey );
126         
127         EJBResponse res = request( req );
128   
129         switch (res.getResponseCode()) {
130         case EJB_ERROR:
131             throw (Throwable JavaDoc)res.getResult();
132         case EJB_SYS_EXCEPTION:
133             throw (Throwable JavaDoc)res.getResult();
134         case EJB_APP_EXCEPTION:
135             throw (Throwable JavaDoc)res.getResult();
136         case EJB_OK:
137             invalidateAllHandlers(getRegistryId());
138             return null;
139         default:
140             throw new RemoteException JavaDoc("Received invalid response code from server: "+res.getResponseCode());
141         }
142     }
143     
144 }
145
Popular Tags