1 23 24 29 30 package com.sun.jdo.spi.persistence.support.sqlstore.ejb; 31 32 import java.util.ArrayList ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 import java.util.ResourceBundle ; 36 37 import javax.transaction.*; 38 import javax.naming.InitialContext ; 39 40 import javax.ejb.EJBObject ; 41 import javax.ejb.EJBLocalObject ; 42 import javax.ejb.EJBContext ; 43 import javax.ejb.EntityContext ; 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 68 public class SunTransactionHelper extends TransactionHelperImpl 69 implements ApplicationLoaderEventListener, 70 ConnectorNamingEventListener 71 { 72 73 74 private final static ResourceBundle messages = I18NHelper.loadBundle( 75 "com.sun.jdo.spi.persistence.support.sqlstore.Bundle", SunTransactionHelper.class.getClassLoader()); 77 78 private static List pmf_list; 79 80 private final static Object pmf_listSyncObject = new Object (); 81 82 85 private List applicationLifeCycleEventListeners = new ArrayList (); 86 87 90 static { 91 SunTransactionHelper helper = new SunTransactionHelper(); 92 EJBHelper.registerTransactionHelper (helper); 93 ApplicationLoaderEventNotifier.getInstance().addListener(helper); 96 ConnectorRuntime.getRuntime().getResourceRebindEventNotifier().addListener(helper); 97 pmf_list = new ArrayList (); 98 } 99 100 101 SunTransactionHelper() { } 102 103 static private class TransactionManagerFinder { 105 106 static private final String PM_TM_NAME = "java:pm/TransactionManager"; 109 static private final String AS_TM_NAME = "java:appserver/TransactionManager"; 112 static TransactionManager tm = null; 114 115 static TransactionManager appserverTM = null; 117 118 static { 119 try { 120 tm = (TransactionManager) (new InitialContext ()).lookup(PM_TM_NAME); 121 appserverTM = (TransactionManager) (new InitialContext ()).lookup(AS_TM_NAME); 122 } catch (Exception e) { 123 throw new JDOFatalInternalException(e.getMessage()); 124 } 125 } 126 } 127 128 129 public Transaction getTransaction(){ 130 try{ 131 return TransactionManagerFinder.tm.getTransaction(); 132 } catch (Exception e) { 133 throw new JDOFatalInternalException(e.getMessage()); 134 } catch (ExceptionInInitializerError err) { 135 throw new JDOFatalInternalException(err.getMessage()); 136 } 137 } 138 139 140 public UserTransaction getUserTransaction() { 141 try { 142 InitialContext ctx = 143 (InitialContext ) Class.forName("javax.naming.InitialContext").newInstance(); 145 return (UserTransaction)ctx.lookup("java:comp/UserTransaction"); } catch (Exception e) { 147 throw new JDOFatalInternalException(e.getMessage()); 148 } 149 } 150 151 152 public PersistenceManagerFactory replaceInternalPersistenceManagerFactory( 153 PersistenceManagerFactory pmf) { 154 155 synchronized(pmf_listSyncObject) { 156 int i = pmf_list.indexOf(pmf); 157 if (i == -1) { 158 pmf_list.add(pmf); 160 return pmf; 161 } 162 163 return (PersistenceManagerFactory)pmf_list.get(i); 164 } 165 } 166 167 176 public String getDDLNamePrefix(Object info) { 177 return DeploymentHelper.getDDLNamePrefix(info); 178 } 179 180 193 public java.sql.Connection getNonTransactionalConnection( 194 Object resource, String username, String password) 195 throws java.sql.SQLException { 196 197 java.sql.Connection rc = null; 198 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", resource.getClass().getName())); 210 } 211 return rc; 212 } 213 214 215 public TransactionManager getLocalTransactionManager() { 216 try { 217 return TransactionManagerFinder.appserverTM; 218 } catch (ExceptionInInitializerError err) { 219 throw new JDOFatalInternalException(err.getMessage()); 220 } 221 } 222 223 226 public void registerApplicationLifeCycleEventListener( 227 ApplicationLifeCycleEventListener listener) { 228 synchronized(applicationLifeCycleEventListeners) { 229 applicationLifeCycleEventListeners.add(listener); 230 } 231 } 232 233 235 238 public void handleApplicationEvent(ApplicationEvent event) { 239 if(ApplicationEvent.AFTER_APPLICATION_UNLOAD == event.getEventType() ) { 241 ClassLoader classLoader = event.getClassLoader(); 242 for (Iterator iterator = applicationLifeCycleEventListeners.iterator(); 243 iterator.hasNext();) { 244 ApplicationLifeCycleEventListener applicationLifeCycleEventListener = 245 (ApplicationLifeCycleEventListener) iterator.next(); 246 applicationLifeCycleEventListener.notifyApplicationUnloaded(classLoader); 247 } 248 } 249 } 250 251 254 public void handleEjbContainerEvent(EjbContainerEvent event) { 255 } 257 258 261 public void connectorNamingEventPerformed(ConnectorNamingEvent event){ 262 if(event.getEventType() == ConnectorNamingEvent.EVENT_OBJECT_REBIND){ 263 String dsName = ResourceInstaller.getPMJndiName(event.getJndiName()); 264 cleanUpResources(dsName); 265 } } 267 268 272 private void cleanUpResources(String name) { 273 synchronized(pmf_listSyncObject) { 274 for (Iterator 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 |