KickJava   Java API By Example, From Geeks To Geeks.

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


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  * TransactionHelperImpl.java
26  *
27  * Created on January 17, 2002
28  */

29
30 package com.sun.jdo.spi.persistence.support.sqlstore.ejb;
31
32 import javax.transaction.*;
33
34 import com.sun.jdo.api.persistence.model.ClassLoaderStrategy;
35 import com.sun.jdo.api.persistence.support.JDOFatalInternalException;
36 import com.sun.jdo.api.persistence.support.PersistenceManagerFactory;
37
38 /** This is an abstract class which is a generic implementation of the
39 * TransactionHelper interface. Each concrete implementation that extends
40 * this class is used for information about the distributed
41 * transaction environment.
42 *
43 * The class that extends this class must implement <code>getTransaction</code>
44 * and <code>getUserTransaction</code> methods and replace any other method
45 * implementation if it is necessary.
46 *
47 * Such class must register itself by a static method at class initialization time.
48 * For example,
49 * <blockquote><pre>
50 * import com.sun.jdo.spi.persistence.support.sqlstore.ejb.*;
51 * class blackHerringTransactionHelper extends TransactionHelperImpl {
52 * static EJBHelper.register(new blackHerringTransactionHelper());
53 * ...
54 * }
55 * </pre></blockquote>
56 */

57 abstract public class TransactionHelperImpl
58     implements TransactionHelper {
59
60     static final String JavaDoc DEFAULT_STRING = "default"; // NOI18N
61

62     /** Set ClassLoaderStrategy to be "reload" in the managed
63      * environment.
64      */

65     static {
66         if (System.getProperty(
67                 ClassLoaderStrategy.PROPERTY_MULTIPLE_CLASS_LOADERS) == null)
68             ClassLoaderStrategy.setStrategy(
69                     ClassLoaderStrategy.MULTIPLE_CLASS_LOADERS_RELOAD);
70     }
71
72     /** Identifies the managed environment behavior.
73      * @return true as this implementation represents the managed environment.
74      */

75     public boolean isManaged() {
76         return true;
77     }
78
79
80     /** Identify the Transaction context for the calling thread, and return a
81      * Transaction instance that can be used to register synchronizations,
82      * and used as the key for HashMaps. The returned Transaction must implement
83      * <code>equals()</code> and <code>hashCode()</code> based on the global transaction id.
84      * <P>All Transaction instances returned by this method called in the same
85      * Transaction context must compare equal and return the same hashCode.
86      * The Transaction instance returned will be held as the key to an
87      * internal HashMap until the Transaction completes. If there is no transaction
88      * associated with the current thread, this method returns null.
89      * @return the Transaction instance for the calling thread
90      */

91     abstract public Transaction getTransaction();
92
93     /** Returns the UserTransaction associated with the calling thread. If there
94      * is no transaction currently in progress, this method returns null.
95      * @return the UserTransaction instance for the calling thread
96      */

97     abstract public UserTransaction getUserTransaction();
98
99     /** Translate local representation of the Transaction Status to
100      * javax.transaction.Status value if necessary. Otherwise this method
101      * should return the value passed to it as an argument.
102      * <P>This method is used during afterCompletion callbacks to translate
103      * the parameter value passed by the application server to the
104      * afterCompletion method. The return value must be one of:
105      * <code>javax.transaction.Status.STATUS_COMMITTED</code> or
106      * <code>javax.transaction.Status.STATUS_ROLLED_BACK</code>.
107      * @param st local Status value
108      * @return the javax.transaction.Status value of the status
109      */

110     public int translateStatus(int st) {
111         return st;
112     }
113
114     /** Replace newly created instance of PersistenceManagerFactory
115      * with the hashed one if it exists. The replacement is necessary only if
116      * the JNDI lookup always returns a new instance. Otherwise this method
117      * returns the object passed to it as an argument.
118      *
119      * PersistenceManagerFactory is uniquely identified by
120      * ConnectionFactory.hashCode() if ConnectionFactory is
121      * not null; otherwise by ConnectionFactoryName.hashCode() if
122      * ConnectionFactoryName is not null; otherwise
123      * by the combination of URL.hashCode() + userName.hashCode() +
124      * password.hashCode() + driverName.hashCode();
125      *
126      * @param pmf PersistenceManagerFactory instance to be replaced
127      * @return the PersistenceManagerFactory known to the runtime
128      */

129     public PersistenceManagerFactory replaceInternalPersistenceManagerFactory(
130     PersistenceManagerFactory pmf) {
131
132     return pmf;
133     }
134
135     /** Called at the beginning of the Transaction.beforeCompletion() to
136      * register the component with the app server only if necessary.
137      * The component argument is an array of Objects.
138      * The first element is com.sun.jdo.spi.persistence.support.sqlstore.Transaction
139      * object responsible for transaction completion.
140      * The second element is com.sun.jdo.api.persistence.support.PersistenceManager
141      * object that has been associated with the Transaction context for the
142      * calling thread.
143      * The third element is javax.transaction.Transaction object that has been
144      * associated with the given instance of PersistenceManager.
145      * The return value is passed unchanged to the postInvoke method.
146      *
147      * @param component an array of Objects
148      * @return implementation-specific Object
149      */

150     public Object JavaDoc preInvoke(Object JavaDoc component) {
151         return null;
152     }
153
154     /** Called at the end of the Transaction.beforeCompletion() to
155      * de-register the component with the app server if necessary.
156      * The parameter is the return value from preInvoke, and can be any
157      * Object.
158      *
159      * @param im implementation-specific Object
160      */

161     public void postInvoke(Object JavaDoc im) {
162     }
163
164     /** Called in a managed environment to register internal Synchronization object
165     * with the Transaction Synchronization. If available, this registration
166     * provides special handling of the registered instance, calling it after
167     * all user defined Synchronization instances.
168     *
169     * @param jta the Transaction instance for the calling thread.
170     * @param sync the internal Synchronization instance to register.
171     * @throws javax.transaction.RollbackException.
172     * @throws javax.transaction.SystemException
173     */

174     public void registerSynchronization(Transaction jta, Synchronization sync)
175         throws RollbackException, SystemException {
176
177         jta.registerSynchronization(sync);
178     }
179
180     /** Called in a managed environment to get a Connection from the application
181      * server specific resource. In a non-managed environment returns null as
182      * it should not be called.
183      * This is a generic implementation for the case of javax.sql.DataSource as
184      * the resource type.
185      *
186      * @param resource the application server specific resource.
187      * @param username the resource username. If null, Connection is requested
188      * without username and password validation.
189      * @param password the password for the resource username.
190      * @return a Connection.
191      * @throws java.sql.SQLException.
192      */

193     public java.sql.Connection JavaDoc getConnection(Object JavaDoc resource, String JavaDoc username,
194                 String JavaDoc password) throws java.sql.SQLException JavaDoc {
195         java.sql.Connection JavaDoc rc = null;
196         if (resource instanceof javax.sql.DataSource JavaDoc) {
197             javax.sql.DataSource JavaDoc ds = (javax.sql.DataSource JavaDoc)resource;
198             if (username == null) {
199                 rc = ds.getConnection();
200             } else {
201                 rc = ds.getConnection(username, password);
202             }
203         }
204         return rc;
205     }
206
207     /** Called in a managed environment to get a non-transactional Connection
208      * from the application server specific resource.
209      *
210      * @param resource the application server specific resource.
211      * @param username the resource username. If null, Connection is requested
212      * without username and password validation.
213      * @param password the password for the resource username.
214      * @return a Connection.
215      * @throw java.sql.SQLException.
216      */

217     abstract public java.sql.Connection JavaDoc getNonTransactionalConnection(
218         Object JavaDoc resource, String JavaDoc username, String JavaDoc password)
219         throws java.sql.SQLException JavaDoc;
220
221     /** Called in a managed environment to access a TransactionManager
222      * for managing local transaction boundaries and registering synchronization
223      * for call backs during completion of a local transaction.
224      *
225      * @return javax.transaction.TransactionManager
226      */

227     abstract public TransactionManager getLocalTransactionManager();
228
229     /**
230      * This method unwraps given Statement and return the Statement from
231      * JDBC driver if possible.
232      */

233     public java.sql.Statement JavaDoc unwrapStatement(java.sql.Statement JavaDoc stmt) {
234         return stmt;
235     }
236
237     /**
238      * Set environment specific default values for the given PersistenceManagerFactory.
239      * In most app servers optimistic and retainValues flags should be false.
240      * For any other settings this method should be overritten.
241      *
242      * @param pmf the PersistenceManagerFactory.
243      */

244     public void setPersistenceManagerFactoryDefaults(PersistenceManagerFactory pmf) {
245         pmf.setOptimistic(false);
246         //pmf.setRetainValues(false);
247
}
248
249     /**
250      * Returns name prefix for DDL files extracted from the info instance by the
251      * application server specific code.
252      *
253      * @param info the instance to use for the name generation.
254      * @return name prefix as String.
255      */

256     public String JavaDoc getDDLNamePrefix(Object JavaDoc info) {
257         return DEFAULT_STRING;
258     }
259
260     /**
261      * @inheritDoc
262      */

263     public void registerApplicationLifeCycleEventListener(
264             ApplicationLifeCycleEventListener listener) {
265         
266     }
267     
268 }
269
Popular Tags