KickJava   Java API By Example, From Geeks To Geeks.

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


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  * CMPHelper.java
26  *
27  * Created on April 25, 2002
28  */

29 package com.sun.jdo.spi.persistence.support.sqlstore.ejb;
30
31 import java.util.ResourceBundle JavaDoc;
32
33 import javax.ejb.EJBObject JavaDoc;
34 import javax.ejb.EJBLocalObject JavaDoc;
35 import javax.ejb.EJBContext JavaDoc;
36 import javax.ejb.EntityContext JavaDoc;
37
38 import com.sun.jdo.api.persistence.support.JDOFatalInternalException;
39 import com.sun.jdo.api.persistence.support.PersistenceManager;
40 import com.sun.jdo.api.persistence.support.PersistenceManagerFactory;
41 import com.sun.jdo.api.persistence.support.Transaction;
42
43 import com.sun.jdo.spi.persistence.utility.I18NHelper;
44 import com.sun.jdo.spi.persistence.utility.logging.Logger;
45 import com.sun.jdo.spi.persistence.support.sqlstore.LogHelperPersistenceManager;
46 import com.sun.jdo.spi.persistence.support.sqlstore.impl.PersistenceManagerImpl;
47 import com.sun.jdo.spi.persistence.support.sqlstore.impl.PersistenceManagerWrapper;
48
49   /** Provides helper methods for CMP support implementation with the
50    * application server specific information. Calls corresponding methods
51    * on the registered class which implements ContainerHelper interface.
52    */

53 public class CMPHelper {
54
55     /** I18N message handler */
56     private final static ResourceBundle JavaDoc messages = I18NHelper.loadBundle(
57         "com.sun.jdo.spi.persistence.support.sqlstore.Bundle", // NOI18N
58
CMPHelper.class.getClassLoader());
59
60     /** The logger */
61     private static Logger logger = LogHelperPersistenceManager.getLogger();
62
63    /** Reference to a class that implements ContainerHelper interface for this
64     * particular application server. In a non-managed environment the
65     * will throw JDOFatalInternalException for any method invocation.
66     */

67     private static ContainerHelper containerHelper = null;
68
69     /** This counter is used to populate primary key columns for CMP beans with
70      * an unknown Primary Key Class. It is a single counter per vm and is
71      * initialized at the start up time.
72      */

73    private static long counter = System.currentTimeMillis();
74
75    /** Register class that implements ContainerHelper interface
76     * Should be called by a static method at class initialization time.
77     *
78     * @param h application server specific implemetation of the ContainerHelper
79     * interface.
80     */

81     public static void registerContainerHelper (ContainerHelper h) {
82         containerHelper = h;
83     }
84
85     /** Increments the counter and returns the value. Used to populate primary
86      * key columns for EJB with an unknown Primary Key Class.
87      * @return the next value for the counter.
88      */

89
90     public synchronized static long getNextId() {
91         counter++;
92         return counter;
93     }
94
95     /** Called in a CMP supported environment to get a Container instance that
96      * will be passed unchanged to the required methods. In a non-managed environment
97      * throws JDOFatalInternalException.
98      * The info argument can be an array of Objects if necessary.
99      *
100      * @see getEJBObject(Object, Object)
101      * @see getEJBLocalObject(Object, Object)
102      * @see getEJBLocalObject(Object, Object, EJBContext)
103      * @see removeByEJBLocalObject(EJBLocalObject, Object)
104      * @see removeByPK(Object, Object)
105      * @param info Object with the request information that is application server
106      * specific.
107      * @return a Container instance as an Object.
108      * @throws JDOFatalInternalException if ContainerHelper instance is not registered.
109      */

110     public static Object JavaDoc getContainer(Object JavaDoc info) {
111         return getContainerHelper().getContainer(info);
112     }
113  
114     /** Called in a CMP supported environment to get an EJBObject reference for this
115      * primary key instance and Container object.
116      * The Container instance is acquired via #getContainer(Object).
117      *
118      * @see getContainer(Object)
119      * @param pk the primary key instance.
120      * @param container a Container instance for the request.
121      * @return a corresponding EJBObject (as an Object) to be used by
122      * the client.
123      */

124     public static EJBObject JavaDoc getEJBObject(Object JavaDoc pk, Object JavaDoc container) {
125         return getContainerHelper().getEJBObject(pk, container);
126     }
127  
128     /** Called in a managed environment to get an EJBLocalObject reference for this
129      * primary key instance and Container object.
130      * The Container instance is acquired via #getContainer(Object).
131      *
132      * @see getContainer(Object)
133      * @param pk the primary key instance.
134      * @param container a Container instance for the request.
135      * @return a corresponding EJBLocalObject (as an Object) to be used by
136      * the client.
137      */

138     public static EJBLocalObject JavaDoc getEJBLocalObject(Object JavaDoc pk, Object JavaDoc container) {
139         return getContainerHelper().getEJBLocalObject(pk, container);
140     }
141  
142     /** Called in a managed environment to get an EJBLocalObject reference for this
143      * primary key instance, Container object, and EJBContext of the calling bean.
144      * Allows the container to check if this method is called during ejbRemove
145      * that is part of a cascade-delete remove.
146      * The Container instance is acquired via #getContainer(Object).
147      *
148      * @see getContainer(Object)
149      * @param pk the primary key instance.
150      * @param container a Container instance for the request.
151      * @param context an EJBContext of the calling bean.
152      * @return a corresponding EJBLocalObject (as an Object) to be used by
153      * the client.
154      */

155     public static EJBLocalObject JavaDoc getEJBLocalObject(Object JavaDoc pk, Object JavaDoc container,
156         EJBContext JavaDoc context) {
157         return getContainerHelper().getEJBLocalObject(pk, container, context);
158     }
159  
160     /** Called in a managed environment to remove a bean for a given EJBLocalObject,
161      * and Container instance.
162      * The Container instance is acquired via #getContainer(Object).
163      *
164      * @see getContainer(Object)
165      * @param ejb the EJBLocalObject for the bean to be removed.
166      * @param container a Container instance for the request.
167      */

168     public static void removeByEJBLocalObject(EJBLocalObject JavaDoc ejb, Object JavaDoc container) {
169         getContainerHelper().removeByEJBLocalObject(ejb, container);
170     }
171  
172     /** Called in a managed environment to remove a bean for a given primary key
173      * and Container instance.
174      * The Container instance is acquired via #getContainer(Object).
175      *
176      * @see getContainer(Object)
177      * @param pk the primary key for the bean to be removed.
178      * @param container a Container instance for the request.
179      */

180     public static void removeByPK(Object JavaDoc pk, Object JavaDoc container) {
181         getContainerHelper().removeByPK(pk, container);
182     }
183
184     /** Called in a managed environment to mark EntityContext of the
185      * bean as already removed during cascade-delete operation.
186      * Called by the generated ejbRemove method before calling ejbRemove of the
187      * related beans (via removeByEJBLocalObject) that are to be cascade-deleted.
188      *
189      * The Container instance is acquired via #getContainer(Object).
190      *
191      * @param context the EntityContext of the bean beeing removed.
192      */

193     public static void setCascadeDeleteAfterSuperEJBRemove(EntityContext JavaDoc context) {
194         getContainerHelper().setCascadeDeleteAfterSuperEJBRemove(context);
195     }
196
197     /** Called in a CMP environment to lookup PersistenceManagerFactory
198      * referenced by this Container instance as the CMP resource.
199      * The Container instance is acquired via #getContainer(Object).
200      *
201      * @see getContainer(Object)
202      * @param container a Container instance for the request.
203      */

204     public static PersistenceManagerFactory getPersistenceManagerFactory(Object JavaDoc container) {
205         return getContainerHelper().getPersistenceManagerFactory(container);
206     }
207
208     /** Called in a CMP environment to verify that the specified object
209      * is of a valid local interface type.
210      * The Container instance is acquired via #getContainer(Object).
211      *
212      * @see getContainer(Object)
213      * @param o the instance to validate.
214      * @param container a Container instance for the request.
215      */

216     public static void assertValidLocalObject(Object JavaDoc o, Object JavaDoc container) {
217         getContainerHelper().assertValidLocalObject(o, container);
218     }
219
220     /** Called in a CMP environment to verify that the specified object
221      * is of a valid remote interface type.
222      * The Container instance is acquired via #getContainer(Object).
223      *
224      * @see getContainer(Object)
225      * @param o the instance to validate.
226      * @param container a Container instance for the request.
227      */

228     public static void assertValidRemoteObject(Object JavaDoc o, Object JavaDoc container) {
229         getContainerHelper().assertValidRemoteObject(o, container);
230     }
231
232     /** Called in a CMP supported environment. Notifies the container that
233      * ejbSelect had been called.
234      * The Container instance is acquired via #getContainer(Object).
235      *
236      * @see getContainer(Object)
237      * @param container a Container instance for the request.
238      */

239     public static void preSelect(Object JavaDoc container) {
240         getContainerHelper().preSelect(container);
241     }
242  
243     /**
244      * Called in CMP environment to get NumericConverter policy referenced
245      * by this Container instance.
246      * @see getContainer(Object)
247      * @param container a Container instance for the request
248      * @return a valid NumericConverter policy type
249      */

250     public static int getNumericConverterPolicy(Object JavaDoc container) {
251         return getContainerHelper().getNumericConverterPolicy(container);
252     }
253
254     /** Called in a unspecified transaction context of a managed environment.
255      * According to p.364 of EJB 2.0 spec, CMP may need to manage
256      * its own transaction when its transaction attribute is
257      * NotSupported, Never, Supports.
258      *
259      * @param pm PersistenceManager
260      */

261     public static void beginInternalTransaction(PersistenceManager pm) {
262         getContainerHelper().beginInternalTransaction(pm);
263     }
264
265     /** Called in a unspecified transaction context of a managed environment.
266      * According to p.364 of EJB 2.0 spec, CMP may need to manage
267      * its own transaction when its transaction attribute is
268      * NotSupported, Never, Supports.
269      *
270      * @param pm PersistenceManager
271      */

272     public static void commitInternalTransaction(PersistenceManager pm) {
273         getContainerHelper().commitInternalTransaction(pm);
274     }
275
276     /** Called in a unspecified transaction context of a managed environment.
277      * According to p.364 of EJB 2.0 spec, CMP may need to manage
278      * its own transaction when its transaction attribute is
279      * NotSupported, Never, Supports.
280      *
281      * @param pm PersistenceManager
282      */

283     public static void rollbackInternalTransaction(PersistenceManager pm) {
284         getContainerHelper().rollbackInternalTransaction(pm);
285     }
286
287     /** Called from read-only beans to suspend current transaction.
288      * This will guarantee that PersistenceManager is not bound to
289      * any transaction.
290      *
291      * @return javax.transaction.Transaction object representing
292      * the suspended transaction.
293      * Returns null if the calling thread is not associated
294      * with a transaction.
295      */

296     public static javax.transaction.Transaction JavaDoc suspendCurrentTransaction() {
297         return getContainerHelper().suspendCurrentTransaction();
298     }
299
300     /** Called from read-only beans to resume current transaction.
301      * This will guarantee that the transaction continues to run after
302      * read-only bean accessed its PersistenceManager.
303      *
304      * @param tx - The javax.transaction.Transaction object that
305      * represents the transaction to be resumed.
306      */

307     public static void resumeCurrentTransaction(
308             javax.transaction.Transaction JavaDoc tx) {
309
310         getContainerHelper().resumeCurrentTransaction(tx);
311     }
312
313     /** Flush transactional changes to the database.
314      * @param pm PersistenceManager
315      */

316     public static void flush(PersistenceManager pm) {
317         Transaction tx = pm.currentTransaction();
318         // flush updates to the database if transaction is active.
319
if (tx != null && tx.isActive()) {
320             PersistenceManagerWrapper pmw = (PersistenceManagerWrapper)pm;
321             PersistenceManagerImpl pmi =
322                     (PersistenceManagerImpl)pmw.getPersistenceManager();
323             pmi.internalFlush();
324         }
325     }
326
327     /** Returns a ContainerHelper instance that can be used to invoke
328      * the corresponding method.
329      * @return a ContainerHelper instance registered with this class.
330      * @throws JDOFatalInternalException if the instance is null.
331      */

332     private static ContainerHelper getContainerHelper() {
333         if (containerHelper == null) {
334             throw new JDOFatalInternalException(I18NHelper.getMessage(
335                     messages, "ejb.cmphelper.nonmanaged")); //NOI18N
336
}
337
338         return containerHelper;
339     }
340 }
341
Popular Tags