KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > core > entity > 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 "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES 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  * EXOFFICE TECHNOLOGIES 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 1999 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: EntityEjbObjectHandler.java 1921 2005-06-19 22:40:34Z jlaskowski $
44  */

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

65 public class EntityEjbObjectHandler extends EjbObjectProxyHandler {
66
67     private final static class RegistryEntry{
68         final Object JavaDoc primaryKey;
69         final Object JavaDoc deploymentId;
70         final Object JavaDoc containerId;
71
72         RegistryEntry(Object JavaDoc primaryKey, Object JavaDoc deploymentId, Object JavaDoc containerId) {
73             if(primaryKey==null || deploymentId==null || containerId==null) {
74                 throw new IllegalArgumentException JavaDoc();
75             }
76             this.primaryKey=primaryKey;
77             this.deploymentId=deploymentId;
78             this.containerId=containerId;
79         }
80
81         public boolean equals(Object JavaDoc other) {
82             if(other==this) {
83                 return true;
84             }
85             if(other instanceof RegistryEntry) {
86                 RegistryEntry otherEntry = (RegistryEntry) other;
87                 return primaryKey.equals(otherEntry.primaryKey) &&
88                     deploymentId.equals(otherEntry.deploymentId) &&
89                     containerId.equals(otherEntry.containerId);
90             }
91             return false;
92         }
93
94         public int hashCode() {
95             return primaryKey.hashCode();
96         }
97     }
98     /*
99     * The registryId is a logical identifier that is used as a key when placing EntityEjbObjectHandler into
100     * the BaseEjbProxyHanlder's liveHandleRegistry. EntityEjbObjectHandlers that represent the same
101     * bean identity (keyed by the registry id) will be stored together so that they can be removed together
102     * when the BaseEjbProxyHandler.invalidateAllHandlers is invoked. The EntityEjbObjectHandler uses a
103     * compound key composed of the entity bean's primary key, deployment id, and
104     * container id. This uniquely identifies the bean identity that is proxied by this handler allowing it
105     * to be removed with other handlers bound to the same registry id.
106     */

107     private Object JavaDoc registryId;
108                     
109     public EntityEjbObjectHandler(RpcContainer container, Object JavaDoc pk, Object JavaDoc depID){
110         super(container, pk, depID);
111     }
112     
113     /*
114     * This method generates a logically unique entity bean identifier from the primary key,
115     * deployment id, and container id. This registry key is then used as an index for the associated
116     * entity bean in the BaseEjbProxyHandler.liveHandleRegistry. The liveHandleRegistry tracks
117     * handler for the same bean identity so that they can removed together when one of the remove() operations
118     * is called.
119     */

120     public static Object JavaDoc getRegistryId(Object JavaDoc primKey, Object JavaDoc deployId, Container contnr){
121         return new RegistryEntry(primKey, deployId, contnr.getContainerID());
122     }
123     
124     /**
125     * The Registry id is a logical identifier that is used as a key when placing EjbObjectProxyHanlders into
126     * the BaseEjbProxyHanlder's liveHandleRegistry. EjbObjectProxyHanlders that represent the same
127     * bean identity (keyed by the registry id) will be stored together so that they can be removed together
128     * when the BaseEjbProxyHandler.invalidateAllHandlers is invoked.
129     *
130     * This method is implemented by the subclasses to return an id that logically identifies
131     * bean identity for a specific deployment id and container. The EntityEjbObjectHandler
132     * overrides this method to return a compound key composed of the bean's primary key, deployment id, and
133     * container id. This uniquely identifies the bean identity that is proxied by this handler.
134     */

135     public Object JavaDoc getRegistryId(){
136         if(registryId== null)
137             registryId= getRegistryId(primaryKey, deploymentID, container);
138         return registryId;
139     }
140     
141     
142     protected Object JavaDoc getPrimaryKey(Method JavaDoc method, Object JavaDoc[] args, Object JavaDoc proxy) throws Throwable JavaDoc{
143         return primaryKey;
144     }
145     
146     /**
147      * Entity beans are uniquely identifed by primary key, deloyment id, and the container they are
148      * running in.
149      *
150      * @param method
151      * @param args
152      * @param proxy
153      * @return Object
154      * @exception Throwable
155      */

156     protected Object JavaDoc isIdentical(Method JavaDoc method, Object JavaDoc[] args, Object JavaDoc proxy) throws Throwable JavaDoc{
157         checkAuthorization(method);
158         
159         Object JavaDoc hndr = ProxyManager.getInvocationHandler(proxy);
160         
161         if(hndr instanceof EntityEjbObjectHandler){
162         
163             EntityEjbObjectHandler handler = (EntityEjbObjectHandler)hndr;
164             
165             /*
166             * The registry id is a compound key composed of the bean's primary key, deployment id, and
167             * container id. It uniquely identifies the entity bean that is proxied by the EntityEjbObjectHandler
168             * within the IntraVM.
169             */

170             if(this.getRegistryId().equals(handler.getRegistryId())){
171                 return Boolean.TRUE;
172             }
173         }
174         return Boolean.FALSE;
175         
176     }
177     
178     protected Object JavaDoc remove(Method JavaDoc method, Object JavaDoc[] args, Object JavaDoc proxy) throws Throwable JavaDoc{
179         checkAuthorization(method);
180         Object JavaDoc value = container.invoke(deploymentID, method, args, primaryKey, getThreadSpecificSecurityIdentity());
181         /*
182         * This operation takes care of invalidating all the EjbObjectProxyHanders associated with
183         * the same RegistryId. See this.createProxy().
184         */

185         invalidateAllHandlers(getRegistryId());
186         return value;
187     }
188     
189 }
190
Popular Tags