KickJava   Java API By Example, From Geeks To Geeks.

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


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

29
30 package com.sun.jdo.spi.persistence.support.sqlstore.ejb;
31
32 import java.util.ArrayList JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.ResourceBundle JavaDoc;
36
37 import javax.transaction.*;
38 import javax.naming.InitialContext JavaDoc;
39
40 import javax.ejb.EJBObject JavaDoc;
41 import javax.ejb.EJBLocalObject JavaDoc;
42 import javax.ejb.EJBContext JavaDoc;
43 import javax.ejb.EntityContext JavaDoc;
44
45 import com.sun.jts.jta.*;
46
47 import com.sun.appserv.jdbc.DataSource;
48
49 import com.sun.jdo.api.persistence.support.JDOFatalInternalException;
50 import com.sun.jdo.api.persistence.support.PersistenceManagerFactory;
51 import com.sun.jdo.spi.persistence.utility.I18NHelper;
52
53 import com.sun.enterprise.connectors.ConnectorNamingEvent;
54 import com.sun.enterprise.connectors.ConnectorNamingEventListener;
55 import com.sun.enterprise.connectors.ConnectorRuntime;
56 import com.sun.enterprise.resource.ResourceInstaller;
57 import com.sun.enterprise.server.event.ApplicationEvent;
58 import com.sun.enterprise.server.event.ApplicationLoaderEventListener;
59 import com.sun.enterprise.server.event.ApplicationLoaderEventNotifier;
60 import com.sun.enterprise.server.event.EjbContainerEvent;
61
62 /** Sun specific implementation for TransactionHelper interface.
63 * Though this class does not have special implementation for
64 * <code>registerSynchronization</code>, it uses a special Transaction
65 * object that registers Synchronization instance to be processed after
66 * any bean's beforeCompletion method.
67 */

68 public class SunTransactionHelper extends TransactionHelperImpl
69         implements ApplicationLoaderEventListener,
70                    ConnectorNamingEventListener
71     {
72
73     /** I18N message handler */
74     private final static ResourceBundle JavaDoc messages = I18NHelper.loadBundle(
75         "com.sun.jdo.spi.persistence.support.sqlstore.Bundle", // NOI18N
76
SunTransactionHelper.class.getClassLoader());
77
78     private static List JavaDoc pmf_list;
79     
80     private final static Object JavaDoc pmf_listSyncObject = new Object JavaDoc();
81     
82     /**
83      * Array of registered ApplicationLifeCycleEventListener
84      */

85     private List JavaDoc applicationLifeCycleEventListeners = new ArrayList JavaDoc();
86
87     /** Garantees singleton.
88      * Registers itself during initial load
89      */

90     static {
91         SunTransactionHelper helper = new SunTransactionHelper();
92         EJBHelper.registerTransactionHelper (helper);
93         // Register with ApplicationLoaderEventNotifier to receive Sun
94
// Application Server specific lifecycle events.
95
ApplicationLoaderEventNotifier.getInstance().addListener(helper);
96         ConnectorRuntime.getRuntime().getResourceRebindEventNotifier().addListener(helper);
97         pmf_list = new ArrayList JavaDoc();
98     }
99  
100     /** Default constructor should not be public */
101     SunTransactionHelper() { }
102
103     // helper class for looking up the TransactionManager instances.
104
static private class TransactionManagerFinder {
105         
106         // JNDI name of the TransactionManager used for transaction synchronization.
107
static private final String JavaDoc PM_TM_NAME = "java:pm/TransactionManager"; //NOI18N
108

109         // JNDI name of the TransactionManager used for managing local transactions.
110
static private final String JavaDoc AS_TM_NAME = "java:appserver/TransactionManager"; //NOI18N
111

112         // TransactionManager instance used for transaction synchronization.
113
static TransactionManager tm = null;
114
115         // TransactionManager instance used for managing local transactions.
116
static TransactionManager appserverTM = null;
117
118         static {
119             try {
120                 tm = (TransactionManager) (new InitialContext JavaDoc()).lookup(PM_TM_NAME);
121                 appserverTM = (TransactionManager) (new InitialContext JavaDoc()).lookup(AS_TM_NAME);
122             } catch (Exception JavaDoc e) {
123                 throw new JDOFatalInternalException(e.getMessage());
124             }
125         }
126     }
127
128     /** SunTransactionHelper specific code */
129     public Transaction getTransaction(){
130        try{
131             return TransactionManagerFinder.tm.getTransaction();
132         } catch (Exception JavaDoc e) {
133             throw new JDOFatalInternalException(e.getMessage());
134         } catch (ExceptionInInitializerError JavaDoc err) {
135             throw new JDOFatalInternalException(err.getMessage());
136         }
137     }
138
139     /** SunTransactionHelper specific code */
140     public UserTransaction getUserTransaction() {
141     try {
142         InitialContext JavaDoc ctx =
143                 (InitialContext JavaDoc) Class.forName("javax.naming.InitialContext").newInstance(); //NOI18N
144

145             return (UserTransaction)ctx.lookup("java:comp/UserTransaction"); //NOI18N
146
} catch (Exception JavaDoc e) {
147         throw new JDOFatalInternalException(e.getMessage());
148     }
149     }
150
151     /** SunTransactionHelper specific code */
152     public PersistenceManagerFactory replaceInternalPersistenceManagerFactory(
153     PersistenceManagerFactory pmf) {
154
155         synchronized(pmf_listSyncObject) {
156         int i = pmf_list.indexOf(pmf);
157         if (i == -1) {
158             // New PersistenceManagerFactory. Remember it.
159
pmf_list.add(pmf);
160             return pmf;
161         }
162
163         return (PersistenceManagerFactory)pmf_list.get(i);
164         }
165     }
166
167     /**
168      * Returns name prefix for DDL files extracted from the info instance by the
169      * application server specific code.
170      * SunTransactionHelper specific code. Delegates the actual implementation
171      * to DeploymentHelper#getDDLNamePrefix(Object);
172      *
173      * @param info the instance to use for the name generation.
174      * @return name prefix as String.
175      */

176     public String JavaDoc getDDLNamePrefix(Object JavaDoc info) {
177         return DeploymentHelper.getDDLNamePrefix(info);
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      * SunTransactionHelper specific code uses com.sun.appserv.jdbc.DataSource
184      * to get a Connection.
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 getNonTransactionalConnection(
194             Object JavaDoc resource, String JavaDoc username, String JavaDoc password)
195             throws java.sql.SQLException JavaDoc {
196
197         java.sql.Connection JavaDoc rc = null;
198         // resource is expected to be com.sun.appserv.jdbc.DataSource
199
if (resource instanceof DataSource) {
200             DataSource ds = (DataSource)resource;
201             if (username == null) {
202                 rc = ds.getNonTxConnection();
203             } else {
204                 rc = ds.getNonTxConnection(username, password);
205             }
206         } else {
207             throw new JDOFatalInternalException(I18NHelper.getMessage(
208                 messages, "ejb.SunTransactionHelper.wrongdatasourcetype", //NOI18N
209
resource.getClass().getName()));
210         }
211         return rc;
212     }
213
214     /** SunTransactionHelper specific code */
215     public TransactionManager getLocalTransactionManager() {
216         try {
217             return TransactionManagerFinder.appserverTM;
218         } catch (ExceptionInInitializerError JavaDoc err) {
219                 throw new JDOFatalInternalException(err.getMessage());
220         }
221     }
222     
223     /**
224      * @inheritDoc
225      */

226     public void registerApplicationLifeCycleEventListener(
227             ApplicationLifeCycleEventListener listener) {
228         synchronized(applicationLifeCycleEventListeners) {
229              applicationLifeCycleEventListeners.add(listener);
230         }
231     }
232     
233     //-------------------ApplicationLifeCycleEventListener Methods --------------//
234

235     /**
236      * @inheritDoc
237      */

238     public void handleApplicationEvent(ApplicationEvent event) {
239             // Change to switch-case if handling more than one events.
240
if(ApplicationEvent.AFTER_APPLICATION_UNLOAD == event.getEventType() ) {
241                 ClassLoader JavaDoc classLoader = event.getClassLoader();
242                 for (Iterator JavaDoc iterator = applicationLifeCycleEventListeners.iterator();
243                      iterator.hasNext();) {
244                     ApplicationLifeCycleEventListener applicationLifeCycleEventListener =
245                             (ApplicationLifeCycleEventListener) iterator.next();
246                     applicationLifeCycleEventListener.notifyApplicationUnloaded(classLoader);
247             }
248         }
249     }
250
251     /**
252      * @inheritDoc
253      */

254     public void handleEjbContainerEvent(EjbContainerEvent event) {
255         //Ignore EjbContainerEvents
256
}
257     
258     /**
259      * @inheritDoc
260      */

261     public void connectorNamingEventPerformed(ConnectorNamingEvent event){
262         if(event.getEventType() == ConnectorNamingEvent.EVENT_OBJECT_REBIND){
263             String JavaDoc dsName = ResourceInstaller.getPMJndiName(event.getJndiName());
264             cleanUpResources(dsName);
265         } // Ignore all other events.
266
}
267
268     /**
269      * Removes all entries that correspond to the same connection factory name.
270      * @param name the connection factory name.
271      */

272     private void cleanUpResources(String JavaDoc name) {
273         synchronized(pmf_listSyncObject) {
274             for (Iterator JavaDoc it = pmf_list.iterator(); it.hasNext(); ) {
275                 PersistenceManagerFactory pmf = (PersistenceManagerFactory)it.next();
276                 if (pmf.getConnectionFactoryName().equals(name)) {
277                     it.remove();
278                 }
279             }
280         }
281     }
282
283 }
284
Popular Tags