KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ejb > containers > EJBLocalObjectImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.ejb.containers;
25
26 import java.io.Serializable JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import javax.ejb.*;
29
30 import java.lang.reflect.Proxy JavaDoc;
31 import java.lang.reflect.InvocationHandler JavaDoc;
32
33 import com.sun.ejb.*;
34 import com.sun.ejb.portable.*;
35 import com.sun.enterprise.*;
36
37 import com.sun.enterprise.log.Log;
38 import com.sun.enterprise.util.LocalStringManagerImpl;
39
40 import java.util.logging.*;
41 import java.util.HashMap JavaDoc;
42 import com.sun.logging.*;
43
44 import java.io.IOException JavaDoc;
45
46 import com.sun.ejb.spi.io.IndirectlySerializable;
47 import com.sun.ejb.spi.io.SerializableObjectFactory;
48
49 /**
50  * Implementation of the EJBLocalObject interface.
51  * This is NOT serializable to prevent local references from leaving
52  * the JVM.
53  * It is extended by the generated concrete type-specific EJBLocalObject
54  * implementation (e.g. Hello_EJBLocalObject).
55  *
56  * @author Mahesh Kannan
57  */

58 public abstract class EJBLocalObjectImpl
59     extends EJBLocalRemoteObject
60     implements EJBLocalObject, IndirectlySerializable
61 {
62     private static Logger _logger;
63     private static LocalStringManagerImpl localStrings =
64         new LocalStringManagerImpl(EJBLocalObjectImpl.class);
65     private static Class JavaDoc[] NO_PARAMS = new Class JavaDoc[] {};
66     private static Method JavaDoc REMOVE_METHOD = null;
67
68     static {
69         _logger=LogDomains.getLogger(LogDomains.EJB_LOGGER);
70
71         try {
72             REMOVE_METHOD =
73                 EJBLocalObject.class.getMethod("remove", NO_PARAMS);
74         } catch ( NoSuchMethodException JavaDoc e ) {
75             _logger.log(Level.FINE, "Exception retrieving remove method", e);
76         }
77
78     }
79
80     // True this local object instance represents the LocalHome view
81
// False if this local object instance represents the LocalBusiness view
82
private boolean isLocalHomeView;
83
84     private HashMap JavaDoc<String JavaDoc, Object JavaDoc> clientObjectMap =
85         new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
86     /**
87      * Get the client object corresponding to an EJBLocalObjectImpl.
88      * Users of this class cannot
89      * assume they can cast an EJBLocalObjectImpl to the object that
90      * the client uses, and vice-versa. This is overridden in the
91      * InvocationHandler. Only applicable for local home view.
92      */

93     protected Object JavaDoc getClientObject() {
94         return this;
95     }
96     
97     void mapClientObject(String JavaDoc intfClassName, Object JavaDoc obj) {
98         clientObjectMap.put(intfClassName, obj);
99     }
100     
101     Object JavaDoc getClientObject(String JavaDoc intfClassName) {
102         return clientObjectMap.get(intfClassName);
103     }
104
105     void setIsLocalHomeView(boolean flag) {
106         isLocalHomeView = flag;
107     }
108
109     boolean isLocalHomeView() {
110         return isLocalHomeView;
111     }
112
113     /**
114      * Since EJBLocalObject might be a dynamic proxy, the container can't assume
115      * it can cast from EJBLocalObject to EJBLocalObjectImpl. This convenience
116      * method is used to hide the logic behind the translation from an
117      * client-side EJBLocalObject to the corresponding EJBLocalObjectImpl.
118      *
119      * In the case of a proxy, the invocation handler is the
120      * EJBLocalObjectImpl. Otherwise, the argument is returned as is.
121      * NOTE : To translate in the other direction, use
122      * EJBLocalObjectImpl.getEJBLocalObject()
123      *
124      */

125     static EJBLocalObjectImpl toEJBLocalObjectImpl(EJBLocalObject localObj) {
126         EJBLocalObjectImpl localObjectImpl;
127
128         if( localObj instanceof EJBLocalObjectImpl ) {
129             localObjectImpl = (EJBLocalObjectImpl) localObj;
130         } else {
131             localObjectImpl = (EJBLocalObjectImpl)
132                 Proxy.getInvocationHandler(localObj);
133         }
134
135         return localObjectImpl;
136     }
137     
138     public EJBLocalHome getEJBLocalHome() throws EJBException {
139         container.authorizeLocalMethod(
140             BaseContainer.EJBLocalObject_getEJBLocalHome);
141         container.checkExists(this);
142         
143         return container.getEJBLocalHome();
144     }
145     
146     public void remove() throws RemoveException, EJBException {
147         container.authorizeLocalMethod(BaseContainer.EJBLocalObject_remove);
148         
149         try {
150             container.removeBean(this, REMOVE_METHOD, true);
151         } catch(java.rmi.RemoteException JavaDoc re) {
152             // This should never be thrown for local invocations, but it's
153
// part of the removeBean signature. If for some strange
154
// reason it happens, convert to EJBException
155
EJBException ejbEx =new EJBException("unexpected RemoteException");
156             ejbEx.initCause(re);
157             throw ejbEx;
158         }
159     }
160     
161     public Object JavaDoc getPrimaryKey()
162         throws EJBException
163     {
164         if ( container instanceof EntityContainer ) {
165             container.authorizeLocalMethod(
166                 BaseContainer.EJBLocalObject_getPrimaryKey);
167             container.checkExists(this);
168             
169             return primaryKey;
170         }
171         else {
172             throw new EJBException(localStrings.getLocalString(
173             "containers.invalid_operation",
174             "Invalid operation for Session EJBs."));
175         }
176     }
177     
178     public boolean isIdentical(EJBLocalObject other)
179         throws EJBException
180     {
181         container.authorizeLocalMethod(
182             BaseContainer.EJBLocalObject_isIdentical);
183         container.checkExists(this);
184         
185         // For all types of beans (entity, stful/stless session),
186
// there is exactly one EJBLocalObject instance per bean identity.
187
if ( this == other )
188             return true;
189         else
190             return false;
191     }
192     
193     /**
194      * Called from EJBUtils.EJBObjectOutputStream.replaceObject
195      */

196     public SerializableObjectFactory getSerializableObjectFactory() {
197         // Note: for stateful SessionBeans, the EJBLocalObjectImpl contains
198
// a pointer to the EJBContext. We should not serialize it here.
199

200         return new SerializableLocalObject
201             (container.getEjbDescriptor().getUniqueId(), isLocalHomeView,
202              primaryKey);
203     }
204     
205     public static final class SerializableLocalObject
206         implements SerializableObjectFactory
207     {
208         private long containerId;
209         private boolean localHomeView;
210         private Object JavaDoc primaryKey;
211
212         SerializableLocalObject(long containerId,
213                                 boolean localHomeView,
214                                 Object JavaDoc primaryKey) {
215             this.containerId = containerId;
216             this.localHomeView = localHomeView;
217             this.primaryKey = primaryKey;
218         }
219         
220         public Object JavaDoc createObject()
221             throws IOException JavaDoc
222         {
223             BaseContainer container = (BaseContainer) Switch.getSwitch().
224                 getContainerFactory().getContainer(containerId);
225                 
226             if( localHomeView ) {
227                 EJBLocalObjectImpl ejbLocalObjectImpl =
228                     container.getEJBLocalObjectImpl(primaryKey);
229                 // Return the client EJBLocalObject.
230
return ejbLocalObjectImpl.getClientObject();
231             } else {
232                 EJBLocalObjectImpl ejbLocalBusinessObjectImpl =
233                     container.getEJBLocalBusinessObjectImpl(primaryKey);
234                 // Return the client EJBLocalObject.
235
return ejbLocalBusinessObjectImpl.getClientObject();
236             }
237         }
238
239     }
240 }
241
Popular Tags