KickJava   Java API By Example, From Geeks To Geeks.

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


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
29
30 import javax.ejb.*;
31 import javax.naming.*;
32
33 import com.sun.ejb.*;
34 import com.sun.enterprise.Switch;
35
36 import java.util.logging.*;
37 import com.sun.logging.*;
38
39 import java.io.IOException JavaDoc;
40
41 import com.sun.ejb.spi.io.IndirectlySerializable;
42 import com.sun.ejb.spi.io.SerializableObjectFactory;
43
44 /**
45  * Implementation of the EJBLocalHome interface.
46  * This class is also the base class for all generated concrete EJBLocalHome
47  * implementations.
48  * At deployment time, one instance of the EJBLocalHome is created
49  * for each EJB class in a JAR that has a local home.
50  *
51  * @author Mahesh Kannan
52  */

53 public abstract class EJBLocalHomeImpl
54     implements EJBLocalHome, IndirectlySerializable
55 {
56     protected BaseContainer container;
57     private static Logger _logger;
58     static {
59         _logger=LogDomains.getLogger(LogDomains.EJB_LOGGER);
60     }
61     
62     /**
63      * Called from BaseContainer only.
64      */

65     final void setContainer(BaseContainer c) {
66         container = c;
67     }
68     
69     /**
70      * Called from concrete EJBLocalHome implementation.
71      */

72     protected final BaseContainer getContainer() {
73         return container;
74     }
75
76     /**
77      * Get the EJBLocalHome corresponding to an EJBLocalHomeImpl.
78      * These objects are one and the same when the local home is generated,
79      * but distinct in the case of dynamic proxies. Therefore, code can't
80      * assume it can cast an EJBLocalHomeImpl to the EJBLocalHome that
81      * the client uses, and vice-versa. This is overridden in the
82      * InvocationHandler.
83      */

84     protected EJBLocalHome getEJBLocalHome() {
85         return this;
86     }
87
88     /**
89      * Create a new EJBLocalObjectImpl and new EJB if necessary.
90      * This is called from the concrete "HelloEJBHomeImpl" create method.
91      * Return the EJBObjectImpl for the bean.
92      */

93     protected final EJBLocalObjectImpl createEJBLocalObjectImpl()
94         throws CreateException
95     {
96         return container.createEJBLocalObjectImpl();
97     }
98
99     /**
100      * Create a new EJBLocalBusinessObjectImpl and new EJB if necessary.
101      */

102     protected final EJBLocalObjectImpl createEJBLocalBusinessObjectImpl()
103         throws CreateException
104     {
105         return container.createEJBLocalBusinessObjectImpl();
106     }
107     
108     /**
109      * This is the implementation of the javax.ejb.EJBLocalHome remove method.
110      */

111     public final void remove(Object JavaDoc primaryKey)
112         throws RemoveException, EJBException
113     {
114         if ( !(container instanceof EntityContainer) ) {
115             // Session beans dont have primary keys. EJB2.0 Section 6.6.
116
throw new RemoveException("Attempt to call remove(Object primaryKey) on a session bean.");
117         }
118         
119         container.authorizeLocalMethod(BaseContainer.EJBLocalHome_remove_Pkey);
120         
121         Method JavaDoc method=null;
122         try {
123             method = EJBLocalHome.class.getMethod("remove",
124                                                   new Class JavaDoc[]{Object JavaDoc.class});
125         } catch ( NoSuchMethodException JavaDoc e ) {
126             _logger.log(Level.FINE, "Exception in method remove()", e);
127         }
128         
129         try {
130             ((EntityContainer)container).removeBean(primaryKey, method, true);
131         } catch(java.rmi.RemoteException JavaDoc re) {
132             // This should never be thrown for local invocations, but it's
133
// part of the removeBean signature. If for some strange
134
// reason it happens, convert to EJBException
135
EJBException ejbEx =new EJBException("unexpected RemoteException");
136             ejbEx.initCause(re);
137             throw ejbEx;
138         }
139     }
140     
141     public SerializableObjectFactory getSerializableObjectFactory() {
142         return new SerializableLocalHome(
143             container.getEjbDescriptor().getUniqueId());
144     }
145
146     public static final class SerializableLocalHome
147         implements SerializableObjectFactory
148     {
149         private long ejbId;
150
151         public SerializableLocalHome(long uniqueId) {
152             this.ejbId = uniqueId;
153         }
154
155         public Object JavaDoc createObject()
156             throws IOException JavaDoc
157         {
158             // Return the LocalHome by getting the target container based
159
// on the ejb id. Note that we can assume this is the
160
// LocalHome rather than a LocalBusinessHome since the
161
// LocalBusinessHome is never visible to the application and
162
// would never be stored in SFSB state.
163
BaseContainer container = (BaseContainer)
164                 Switch.getSwitch().getContainerFactory().getContainer(ejbId);
165             return container.getEJBLocalHome();
166         }
167     }
168 }
169
170
Popular Tags