1 22 package org.jboss.tm.remoting.server; 23 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import javax.management.ObjectName ; 29 import javax.naming.Context ; 30 import javax.naming.InitialContext ; 31 32 import javax.transaction.TransactionManager ; 33 34 import org.jboss.remoting.InvokerLocator; 35 import org.jboss.system.ServiceMBeanSupport; 36 import org.jboss.tm.TxManager; 37 import org.jboss.tm.TMUtil; 38 import org.jboss.tm.remoting.RemoteProxy; 39 import org.jboss.tm.remoting.client.ClientUserTransaction; 40 import org.jboss.tm.remoting.interfaces.TransactionFactory; 41 42 48 public class DistributedTransactionManager extends ServiceMBeanSupport 49 implements DistributedTransactionManagerMBean 50 { 51 53 public static final String SUBSYSTEM = "DTM"; 54 55 private static final String [] addInvocationHandlerSignature = 56 new String [] { 57 "java.lang.String", 58 "org.jboss.remoting.ServerInvocationHandler" 59 }; 60 61 private static final String [] removeInvocationHandlerSignature = 62 new String [] { "java.lang.String" }; 63 64 private static final Object [] removeInvocationHandlerParams = 65 new Object [] { SUBSYSTEM }; 66 67 public static final String USER_TRANSACTION_JNDI_NAME = "UserTransaction"; 68 69 71 private List connectors; 72 private InvokerLocator[] locators; 73 private String [] locatorURIs; 74 private boolean interpositionEnabled; 75 76 78 protected void startService() 79 throws Exception 80 { 81 DTMServant dtmServant = new DTMServant(this); 82 List locatorList = new ArrayList (); 83 List locatorURIList = new ArrayList (); 84 Iterator i = connectors.iterator(); 85 86 while (i.hasNext()) 87 { 88 ObjectName objectName = (ObjectName ) i.next(); 90 getServer().invoke(objectName, 91 "addInvocationHandler", 92 new Object [] {SUBSYSTEM, 93 new DTMInvocationHandler(dtmServant)}, 94 addInvocationHandlerSignature); 95 getLog().debug("Added DTM invocation handler to connector " + 96 objectName); 97 98 InvokerLocator locator = 100 (InvokerLocator) getServer().getAttribute(objectName, "Locator"); 101 locatorList.add(locator); 102 String locatorURI = locator.getLocatorURI(); 103 locatorURIList.add(locatorURI); 104 } 105 locators = (InvokerLocator[]) locatorList.toArray(new InvokerLocator[0]); 106 locatorURIs = (String []) locatorURIList.toArray(new String [0]); 107 108 TransactionManager tm = TMUtil.getTransactionManager(); 110 111 if (tm instanceof TxManager) 112 { 113 TxManager txManager = (TxManager)tm; 114 txManager.setDTMEnabled(true); 115 txManager.setDTMCoordinatorFactory(dtmServant); 116 txManager.setDTMResourceFactory(dtmServant); 117 txManager.setDTMStringRemoteRefConverter(dtmServant); 118 txManager.setInterpositionEnabled(interpositionEnabled); 119 } 120 121 Context ctx = new InitialContext (); 123 TransactionFactory transactionFactory = 124 (TransactionFactory) RemoteProxy.create(TransactionFactory.class, 125 0, 126 locators); 127 ctx.bind(ClientUserTransaction.TX_FACTORY_JNDI_NAME, transactionFactory); 128 129 ctx.bind(USER_TRANSACTION_JNDI_NAME, 131 ClientUserTransaction.getSingleton()); 132 } 133 134 protected void stopService() 135 throws Exception 136 { 137 TxManager tm = (TxManager)TMUtil.getTransactionManager(); 139 tm.setDTMResourceFactory(null); 140 141 Iterator i = connectors.iterator(); 142 143 while (i.hasNext()) 144 { 145 ObjectName objectName = (ObjectName ) i.next(); 147 getServer().invoke(objectName, 148 "removeInvocationHandler", 149 removeInvocationHandlerParams, 150 removeInvocationHandlerSignature); 151 getLog().debug("Removed DTM invocation handler from connector " + 152 objectName); 153 } 154 155 tm.setDTMEnabled(false); 157 tm.setDTMCoordinatorFactory(null); 158 tm.setDTMResourceFactory(null); 159 160 Context ctx = new InitialContext (); 163 ctx.unbind(ClientUserTransaction.TX_FACTORY_JNDI_NAME); 164 ctx.unbind(USER_TRANSACTION_JNDI_NAME); 165 } 166 167 169 public List getConnectors() 170 { 171 return connectors; 172 } 173 174 public void setConnectors(List connectors) 175 { 176 this.connectors = connectors; 177 } 178 179 public InvokerLocator[] getLocators() 180 { 181 return locators; 182 } 183 184 public String [] getLocatorURIs() 185 { 186 return locatorURIs; 187 } 188 189 public boolean getInterpositionEnabled() 190 { 191 return interpositionEnabled; 192 } 193 194 public void setInterpositionEnabled(boolean newValue) 195 { 196 interpositionEnabled = newValue; 197 if (getState() == STARTED) 198 { 199 TxManager tm = (TxManager)TMUtil.getTransactionManager(); 200 tm.setInterpositionEnabled(newValue); 201 } 202 } 203 204 } 205 | Popular Tags |