KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > sqlstore > ejb > SunContainerHelper


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 /*
25  * SunContainerHelper.java
26  *
27  * Created on March 12, 2003.
28  */

29
30 package com.sun.jdo.spi.persistence.support.sqlstore.ejb;
31
32 import java.util.Iterator JavaDoc;
33 import java.util.ResourceBundle JavaDoc;
34
35 import javax.ejb.EJBObject JavaDoc;
36 import javax.ejb.EJBLocalObject JavaDoc;
37 import javax.ejb.EJBContext JavaDoc;
38 import javax.ejb.EJBException JavaDoc;
39 import javax.ejb.EntityContext JavaDoc;
40
41 import javax.naming.InitialContext JavaDoc;
42 import javax.naming.NamingException JavaDoc;
43
44 import javax.sql.DataSource JavaDoc;
45
46 import com.sun.enterprise.*;
47 import com.sun.enterprise.deployment.*;
48 import com.sun.enterprise.resource.ResourceInstaller;
49 import com.sun.enterprise.server.ApplicationRegistry;
50
51 import com.sun.ejb.Container;
52
53 import com.sun.jdo.api.persistence.support.JDOException;
54 import com.sun.jdo.api.persistence.support.JDOFatalInternalException;
55 import com.sun.jdo.api.persistence.support.JDOFatalUserException;
56 import com.sun.jdo.api.persistence.support.PersistenceManager;
57 import com.sun.jdo.api.persistence.support.PersistenceManagerFactory;
58
59 import com.sun.jdo.spi.persistence.support.sqlstore.impl.PersistenceManagerFactoryImpl;
60 import com.sun.jdo.spi.persistence.support.sqlstore.LogHelperPersistenceManager;
61 import com.sun.jdo.spi.persistence.support.sqlstore.utility.NumericConverter;
62 import com.sun.jdo.spi.persistence.utility.logging.Logger;
63 import com.sun.jdo.spi.persistence.utility.I18NHelper;
64
65 /** Implementation for Sun specific CMP and Container interactions as defined
66 * by the ContainerHelper interface.
67 *
68 * IMPORTANT: This class extends SunTransactionHelper class. Any changes to the
69 * TransactionHelper implementation must be done in the SunTransactionHelper.
70 *
71 */

72 public class SunContainerHelper extends SunTransactionHelper implements ContainerHelper
73     {
74
75     /** I18N message handler */
76     private final static ResourceBundle JavaDoc messages = I18NHelper.loadBundle(
77         "com.sun.jdo.spi.persistence.support.sqlstore.Bundle", // NOI18N
78
SunContainerHelper.class.getClassLoader());
79
80     /** The logger */
81     private static Logger logger = LogHelperPersistenceManager.getLogger();
82
83     /** Garantees singleton.
84      * Registers itself during initial load
85      */

86     static {
87         CMPHelper.registerContainerHelper (new SunContainerHelper());
88     }
89
90     /** Default constructor should not be public */
91     SunContainerHelper() { }
92
93     /** Get a Container helper instance that will be passed unchanged to the
94      * required methods.
95      * This is SunContainerHelper specific code.
96      *
97      * The info argument is an Object array that consistes of a class to use
98      * for the class loader and concreteImpl bean class name.
99      * @see getEJBObject(Object, Object)
100      * @see getEJBLocalObject(Object, Object)
101      * @see getEJBLocalObject(Object, Object, EJBObject)
102      * @see removeByEJBLocalObject(EJBLocalObject, Object)
103      * @see removeByPK(Object, Object)
104      * @param info Object with the request information that is application server
105      * specific.
106      * @return a Container helper instance as an Object.
107      */

108     public Object JavaDoc getContainer(Object JavaDoc info) {
109
110         Object JavaDoc[] params = (Object JavaDoc[])info;
111         Class JavaDoc cls = (Class JavaDoc)params[0];
112
113         ApplicationRegistry reg = ApplicationRegistry.getInstance();
114         Application app = reg.getApplication(cls.getClassLoader());
115         EjbCMPEntityDescriptor desc = app.getCMPDescriptorFor((String JavaDoc)params[1]);
116
117         return reg.getContainer(desc);
118     }
119
120     /** Get an EJBObject reference for this primary key and Container helper.
121      * The Container instance is acquired via #getContainer(Object).
122      * This is SunContainerHelper specific code.
123      *
124      * @see getContainer(Object)
125      * @param pk the primary key instance.
126      * @param container a Container instance for the request.
127      * @return a corresponding EJBObject instance to be used by the client.
128      */

129     public EJBObject JavaDoc getEJBObject(Object JavaDoc pk, Object JavaDoc container) {
130         try {
131             return ((Container)container).getEJBObjectForPrimaryKey(pk);
132         } catch (Exception JavaDoc ex) {
133             throw new JDOFatalInternalException(ex.getMessage(), ex);
134         }
135     }
136
137     /** Get an EJBLocalObject reference for this primary key and Container helper.
138      * The Container instance is acquired via #getContainer(Object).
139      * This is SunContainerHelper specific code.
140      *
141      * @see getContainer(Object)
142      * @param pk the primary key instance.
143      * @param containerHelper a helper instance for the request.
144      * @return a corresponding EJBLocalObject instance to be used by the client.
145      */

146     public EJBLocalObject JavaDoc getEJBLocalObject(Object JavaDoc pk, Object JavaDoc container) {
147         try {
148             return ((Container)container).getEJBLocalObjectForPrimaryKey(pk);
149         } catch (Exception JavaDoc ex) {
150             throw new JDOFatalInternalException(ex.getMessage(), ex);
151         }
152     }
153
154     /** Get an EJBLocalObject reference for this primary key and Container helper,
155      * and EJBContext of the calling bean.
156      * Allows the container to check if this method is called during ejbRemove
157      * that is part of a cascade-delete remove.
158      * The Container instance is acquired via #getContainer(Object).
159      * This is SunContainerHelper specific code.
160      *
161      * @see getContainer(Object)
162      * @param pk the primary key instance.
163      * @param containerHelper a helper instance for the request.
164      * @param context an EJBContext of the calling bean.
165      * @return a corresponding EJBLocalObject instance to be used by the client.
166      */

167     public EJBLocalObject JavaDoc getEJBLocalObject(Object JavaDoc pk, Object JavaDoc container, EJBContext JavaDoc context) {
168         EJBLocalObject JavaDoc rc = null;
169         try {
170             rc = ((Container)container).getEJBLocalObjectForPrimaryKey(pk, context);
171         } catch (Exception JavaDoc ex) {
172             processContainerException(ex);
173         }
174
175         return rc;
176     }
177
178     /** Remove a bean for a given EJBLocalObject and Container helper.
179      * The Container instance is acquired via #getContainer(Object).
180      * This is SunContainerHelper specific code.
181      *
182      * @see getContainer(Object)
183      * @param ejb the EJBLocalObject for the bean to be removed.
184      * @param container a Container instance for the request.
185      */

186     public void removeByEJBLocalObject(EJBLocalObject JavaDoc ejb, Object JavaDoc container) {
187         try {
188             ((Container)container).removeBeanUnchecked(ejb);
189         } catch (Exception JavaDoc ex) {
190             processContainerException(ex);
191         }
192     }
193
194     /** Remove a bean for a given primary key and Container helper.
195      * The Container instance is acquired via #getContainer(Object).
196      * This is SunContainerHelper specific code.
197      *
198      * @see getContainer(Object)
199      * @param pk the primary key for the bean to be removed.
200      * @param container a Container instance for the request.
201      */

202     public void removeByPK(Object JavaDoc pk, Object JavaDoc container) {
203         try {
204             ((Container)container).removeBeanUnchecked(pk);
205         } catch (Exception JavaDoc ex) {
206             processContainerException(ex);
207         }
208     }
209
210     /** Verify that this instance is of a valid local interface type for
211      * a given Container helper.
212      * The Container instance is acquired via #getContainer(Object).
213      * This is SunContainerHelper specific code.
214      *
215      * @see getContainer(Object)
216      * @param o the instance to be verified.
217      * @param container a Container instance for the request.
218      */

219     public void assertValidLocalObject(Object JavaDoc o, Object JavaDoc container) {
220         ((Container)container).assertValidLocalObject(o);
221     }
222
223     /** Verify that this instance is of a valid remote interface type for
224      * a given Container helper.
225      * The Container instance is acquired via #getContainer(Object).
226      * This is SunContainerHelper specific code.
227      *
228      * @see getContainer(Object)
229      * @param o the instance to be verified.
230      * @param container a Container instance for the request.
231      */

232     public void assertValidRemoteObject(Object JavaDoc o, Object JavaDoc container) {
233         ((Container)container).assertValidRemoteObject(o);
234     }
235
236     /** Mark the bean as already removed during cascade-delete
237      * operation for a given EntityContext.
238      * Called by the generated ejbRemove method before calling ejbRemove of the
239      * related beans (via removeByEJBLocalObject) that are to be cascade-deleted.
240      *
241      * This is SunContainerHelper specific code.
242      *
243      * @param context the EntityContext of the bean beeing removed.
244      */

245     public void setCascadeDeleteAfterSuperEJBRemove(EntityContext JavaDoc context) {
246         try {
247             ((com.sun.ejb.containers.EntityContextImpl)context).setCascadeDeleteAfterSuperEJBRemove(true);
248         } catch (Exception JavaDoc ex) {
249             processContainerException(ex);
250         }
251     }
252
253     /** Called in a CMP supported environment. Notifies the container that
254      * ejbSelect had been called.
255      * The Container instance is acquired via #getContainer(Object).
256      *
257      * This is SunContainerHelper specific code.
258      *
259      * @see getContainer(Object)
260      * @param container a Container instance for the request.
261      */

262     public void preSelect(Object JavaDoc container) {
263         ((Container)container).preSelect();
264     }
265
266     /** Called in a CMP environment to lookup PersistenceManagerFactory
267      * referenced by this Container instance as the CMP resource.
268      * The Container instance is acquired via #getContainer(Object).
269      *
270      * This is SunContainerHelper specific code.
271      *
272      * @see getContainer(Object)
273      * @param container a Container instance for the request.
274      */

275     public PersistenceManagerFactory getPersistenceManagerFactory(Object JavaDoc container) {
276         Object JavaDoc rc = null;
277         PersistenceManagerFactoryImpl pmf = null;
278
279         ResourceReferenceDescriptor cmpResource = ((Container)container).getEjbDescriptor().
280             getEjbBundleDescriptor().getCMPResourceReference();
281
282         String JavaDoc name = cmpResource.getJndiName();
283
284         try {
285             InitialContext JavaDoc ic = new InitialContext JavaDoc();
286             rc = ic.lookup(name);
287
288             if (rc instanceof PersistenceManagerFactoryImpl) {
289                 pmf = (PersistenceManagerFactoryImpl)rc;
290
291             } else if (rc instanceof javax.sql.DataSource JavaDoc) {
292                 pmf = new PersistenceManagerFactoryImpl();
293                 pmf.setConnectionFactoryName(ResourceInstaller.getPMJndiName(name));
294
295                 Iterator JavaDoc it = cmpResource.getProperties();
296                 if (it != null) {
297                     while (it.hasNext()) {
298                         NameValuePairDescriptor prop = (NameValuePairDescriptor)it.next();
299                         String JavaDoc n = prop.getName();
300
301                         // Any value that is not "true" is treated as "false":
302
boolean value = Boolean.valueOf(prop.getValue()).booleanValue();
303                         pmf.setBooleanProperty(n, value);
304
305                     }
306                 }
307
308             } else {
309                 RuntimeException JavaDoc e = new JDOFatalUserException(I18NHelper.getMessage(
310                     messages, "ejb.jndi.unexpectedinstance", //NOI18N
311
name, rc.getClass().getName()));
312                 logger.severe(e.toString());
313
314                 throw e;
315             }
316         } catch (javax.naming.NamingException JavaDoc ex) {
317             RuntimeException JavaDoc e = new JDOFatalUserException(I18NHelper.getMessage(
318                 messages, "ejb.jndi.lookupfailed", name), ex); //NOI18N
319
logger.severe(e.toString());
320
321             throw e;
322         }
323
324         return pmf;
325
326     }
327
328     /**
329      * Called in CMP environment to get NumericConverter policy referenced
330      * by this Container instance.
331      * @see getContainer(Object)
332      * @param container a Container instance for the request
333      * @return a valid NumericConverter policy type
334      */

335     public int getNumericConverterPolicy(Object JavaDoc container) {
336         return NumericConverter.DEFAULT_POLICY;
337     }
338
339     /** Called in a unspecified transaction context of a managed environment.
340      * According to p.364 of EJB 2.0 spec, CMP may need to manage
341      * its own transaction when its transaction attribute is
342      * NotSupported, Never, Supports.
343      * This is a generic implementation.
344      * Application server may like to overwrite this if necessary.
345      *
346      * @param pm PersistenceManager
347      */

348     public void beginInternalTransaction(PersistenceManager pm) {
349         pm.currentTransaction().begin();
350     }
351
352     /** Called in a unspecified transaction context of a managed environment.
353      * According to p.364 of EJB 2.0 spec, CMP may need to manage
354      * its own transaction when its transaction attribute is
355      * NotSupported, Never, Supports.
356      * This is a generic implementation.
357      * Application server may like to overwrite this if necessary.
358      *
359      * @param pm PersistenceManager
360      */

361     public void commitInternalTransaction(PersistenceManager pm) {
362         pm.currentTransaction().commit();
363     }
364
365     /** Called in a unspecified transaction context of a managed environment.
366      * According to p.364 of EJB 2.0 spec, CMP may need to manage
367      * its own transaction when its transaction attribute is
368      * NotSupported, Never, Supports.
369      * This is a generic implementation.
370      * Application server may like to overwrite this if necessary.
371      *
372      * @param pm PersistenceManager
373      */

374     public void rollbackInternalTransaction(PersistenceManager pm) {
375         pm.currentTransaction().rollback();
376     }
377
378     /** Called from read-only beans to suspend current transaction.
379      * This will guarantee that PersistenceManager is not bound to
380      * any transaction.
381      *
382      * @return javax.transaction.Transaction object representing
383      * the suspended transaction.
384      * Returns null if the calling thread is not associated
385      * with a transaction.
386      */

387     public javax.transaction.Transaction JavaDoc suspendCurrentTransaction() {
388         javax.transaction.Transaction JavaDoc tx = null;
389         try {
390             tx = getLocalTransactionManager().suspend();
391         } catch (Exception JavaDoc ex) {
392             processContainerException(ex);
393         }
394
395         return tx;
396     }
397
398     /** Called from read-only beans to resume current transaction.
399      * This will guarantee that the transaction continues to run after
400      * read-only bean accessed its PersistenceManager.
401      *
402      * @param tx - The javax.transaction.Transaction object that
403      * represents the transaction to be resumed. If this object had been
404      * returned by #suspendCurrentTransaction() call it will be null in
405      * case calling thread was not associated with a transaction.
406      */

407     public void resumeCurrentTransaction(javax.transaction.Transaction JavaDoc tx) {
408         try {
409             // Resume only real (i.e. not null transaction)
410
if (tx != null) {
411                 getLocalTransactionManager().resume(tx);
412             }
413         } catch (Exception JavaDoc ex) {
414             processContainerException(ex);
415         }
416     }
417
418     /**
419      * Checks the caught Exception, and rethrows it if it is
420      * of one of known types, or converts to an EJBException
421      * otherwise.
422      *
423      * @param ex the Exception to process.
424      * @throws RuntimeException of the appropriate type.
425      */

426     private void processContainerException(Exception JavaDoc ex) {
427         if (ex instanceof EJBException JavaDoc) {
428             throw (EJBException JavaDoc)ex;
429
430         } else if (ex instanceof IllegalArgumentException JavaDoc
431                 || ex instanceof IllegalStateException JavaDoc) {
432             throw (RuntimeException JavaDoc)ex;
433
434         } else if (ex instanceof JDOException) {
435             throw (JDOException)ex;
436
437         } else {
438             throw new EJBException JavaDoc(ex);
439         }
440     }
441 }
442
Popular Tags