KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > ejb > local > LocalHomeFactoryImpl


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.services.ejb.local;
19
20
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.ejb.EJBLocalHome JavaDoc;
25 import javax.naming.Context JavaDoc;
26 import javax.naming.NamingException JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 import org.sape.carbon.core.component.ComponentConfiguration;
32 import org.sape.carbon.core.component.lifecycle.Configurable;
33 import org.sape.carbon.core.config.InvalidConfigurationException;
34
35 import org.sape.carbon.services.ejb.EnterpriseBeanConfiguration;
36 import org.sape.carbon.services.ejb.HomeFactoryClassCastException;
37 import org.sape.carbon.services.ejb.HomeFactoryConfiguration;
38 import org.sape.carbon.services.ejb.HomeFactoryException;
39 import org.sape.carbon.services.ejb.HomeFactoryNamingException;
40
41 /**
42  * <p>The implementation of the Local Home Factory <br>
43  *
44  * The following list summarizes the key entites involved in using this service
45  * <ul>
46  * <li>Configuration Interface {@link org.sape.carbon.services.ejb.HomeFactoryConfiguration}</li>
47  * <li>Functional Interface {@link org.sape.carbon.services.ejb.local.LocalHomeFactory}</li>
48  * </ul>
49  * </p>
50  * @see org.sape.carbon.services.ejb.HomeFactoryConfiguration
51  * @see org.sape.carbon.services.ejb.local.LocalHomeFactory
52  * @see org.sape.carbon.services.ejb.EnterpriseBeanConfiguration
53  *
54  * @since carbon 1.0
55  * @author Erik M Gottesman, June 2002
56  * @version $Revision: 1.15 $($Author: dvoet $ / $Date: 2003/11/05 17:45:16 $)
57  * @stereotype implementationClass
58  */

59 public class LocalHomeFactoryImpl implements Configurable, LocalHomeFactory {
60
61     /**
62      * Provides a handle to Apache-commons logger
63      */

64     private Log log = LogFactory.getLog(this.getClass());
65     
66     private HomeFactoryConfiguration configuration;
67
68     /**
69      * A private <code>Map</code> object used to store the lookup details of
70      * EJBs associated with this home factory component
71      */

72     private Map JavaDoc ejbMap;
73
74
75     /**
76      * A private <code>Map</code> object used for caching the home interfaces of
77      * EJBs associated with this home factory component
78      */

79     private Map JavaDoc homeCache = new HashMap JavaDoc();
80
81     /**
82      * @see LocalHomeFactory
83      */

84     public EJBLocalHome JavaDoc lookup(String JavaDoc logicalName) throws HomeFactoryException {
85
86         Context JavaDoc context = null;
87
88         try {
89             context = this.configuration.getInitialContextFactory().getContext();
90
91             return lookup(logicalName, context);
92             
93         } catch (NamingException JavaDoc ne) {
94             throw new HomeFactoryNamingException(this.getClass(),
95                 "javax.naming.NamingException occurred for EJB name = "
96                 + "'"
97                 + logicalName
98                 + "'", ne);
99                 
100         } finally {
101             if (context != null) {
102                 try {
103                     context.close();
104                 } catch (NamingException JavaDoc ne) {
105                     log.debug("Caught naming exception closing context: " + ne);
106                 }
107             }
108         }
109
110         
111     }
112
113
114     /**
115      * @see LocalHomeFactory
116      */

117     public EJBLocalHome JavaDoc lookup(String JavaDoc logicalName,
118         String JavaDoc principal,
119         String JavaDoc credentials)
120         throws HomeFactoryException {
121
122         Context JavaDoc context = null;
123
124         try {
125             Map JavaDoc securityInfo = new HashMap JavaDoc();
126             securityInfo.put(Context.SECURITY_PRINCIPAL, principal);
127             securityInfo.put(Context.SECURITY_CREDENTIALS, credentials);
128             context = this.configuration.getInitialContextFactory().
129                 getContext(securityInfo);
130                 
131             return lookup(logicalName, context);
132             
133         } catch (NamingException JavaDoc ne) {
134             throw new HomeFactoryNamingException(this.getClass(),
135                 "javax.naming.NamingException occurred for EJB name = "
136                 + "'"
137                 + logicalName
138                 + "'", ne);
139                 
140         } finally {
141             if (context != null) {
142                 try {
143                     context.close();
144                 } catch (NamingException JavaDoc ne) {
145                     log.debug("Caught naming exception closing context: " + ne);
146                 }
147             }
148         }
149
150         
151     }
152
153
154     /**
155      * @see LocalHomeFactory
156      */

157     public EJBLocalHome JavaDoc lookup(String JavaDoc logicalName, Context JavaDoc context)
158         throws HomeFactoryException {
159
160         if (log.isTraceEnabled()) {
161             log.trace("Performing lookup for EJB with logical name: "
162                 + logicalName);
163         }
164
165         // Define local variables
166
EJBLocalHome JavaDoc ejbHome = null;
167
168         EnterpriseBeanConfiguration ejbDetails =
169             (EnterpriseBeanConfiguration) this.ejbMap.get(logicalName);
170
171         if (ejbDetails == null) {
172             throw new HomeFactoryNamingException(this.getClass(),
173                 "Unable to retrieve configuration details for EJB name = '"
174                 + logicalName
175                 + "'");
176         }
177
178         if (ejbDetails.isCacheable()) {
179
180             if (log.isTraceEnabled()) {
181                 log.trace("Checking home cache for EJB: "
182                     + logicalName);
183             }
184
185             /*
186              * Assumption: a compound key has not been implemented for caching
187              * a home interface in multiple security contexts
188              */

189             ejbHome = (EJBLocalHome JavaDoc) homeCache.get(logicalName);
190         }
191
192         if (ejbHome == null) {
193
194             // Get the JNDI Initial Context and lookup the JNDI bean name.
195
Object JavaDoc homeObject = null;
196
197             try {
198                 homeObject = context.lookup(logicalName);
199             } catch (NamingException JavaDoc ne) {
200                 throw new HomeFactoryNamingException(this.getClass(),
201                     "javax.naming.NamingException occurred for EJB name = "
202                     + "'"
203                     + logicalName
204                     + "'", ne);
205             }
206
207             // No RMI-IIOP check is required for local interfaces
208

209             // For Java RMI, simply downcast the home object to EJBHome
210
try {
211                 ejbHome = (EJBLocalHome JavaDoc) homeObject;
212             } catch (ClassCastException JavaDoc cce) {
213                 throw new HomeFactoryClassCastException(this.getClass(),
214                     "java.lang.ClassCastException "
215                     + "occurred while attempting to downcast the EJB home "
216                     + "reference - logical name = '"
217                     + logicalName
218                     + "'", cce);
219             }
220
221             /*
222              * If the EJB is declared as cacheable, store a reference to its
223              * home interface in order to expedite future lookups
224              */

225             if (ejbDetails.isCacheable()) {
226
227                 if (log.isTraceEnabled()) {
228                     log.trace("Adding EJB: " + logicalName
229                         + " to the home interface cache");
230                 }
231                 cacheHome(logicalName, ejbHome);
232             }
233         }
234
235         // Return the EJBLocalHome object to the caller
236
return ejbHome;
237     }
238
239
240     /**
241      * Configures the component. This is preceded and followed by the suspend
242      * and resume operations if they are available on the component.
243      *
244      * @param configuration A HomeFactoryConfiguration instance
245      */

246     public void configure(ComponentConfiguration configuration) {
247         
248         try {
249             this.configuration = (HomeFactoryConfiguration) configuration;
250         } catch (ClassCastException JavaDoc cce) {
251             throw new InvalidConfigurationException(
252                 this.getClass(),
253                 configuration.getConfigurationName(),
254                 "ConfigurationInterface",
255                 "Configuration interface must be of type " +
256                     HomeFactoryConfiguration.class.getName());
257         }
258
259         /*
260          * Build the internal data structures that will be used to hold
261          * EJB configuration details and (optionally) cached home interfaces
262          */

263         EnterpriseBeanConfiguration ejbs[];
264
265         ejbs = this.configuration.getEnterpriseBean();
266
267         this.ejbMap = new HashMap JavaDoc();
268
269         for (int i = 0; i < ejbs.length; i++) {
270             this.ejbMap.put(ejbs[i].getLogicalName(), ejbs[i]);
271         }
272     }
273
274
275     /**
276      * @see LocalHomeFactory
277      */

278     public EnterpriseBeanConfiguration getEJBDetails(String JavaDoc logicalName) {
279         return (EnterpriseBeanConfiguration)
280             this.ejbMap.get(logicalName);
281     }
282
283
284     /**
285      * <p>Associates the home interface object of a locally deployed EJB
286      * with a logical name (usually its JNDI name), and caches it to
287      * decrease the cost of subsequent JNDI lookups</p>
288      * @param logicalName a String representation of the EJB's logical name
289      * in JNDI
290      * @param ejbHome the home interface object of a locally deployed EJB
291      */

292     protected void cacheHome(String JavaDoc logicalName, EJBLocalHome JavaDoc ejbHome) {
293                 this.homeCache.put(logicalName, ejbHome);
294     }
295 }
296
Popular Tags