KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tm > remoting > server > DistributedTransactionManager


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.tm.remoting.server;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.management.ObjectName JavaDoc;
29 import javax.naming.Context JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31
32 import javax.transaction.TransactionManager JavaDoc;
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 /**
43  * Service MBean that implements the distributed transaction manager.
44  *
45  * @author <a HREF="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
46  * @version $Revision: 57024 $
47  */

48 public class DistributedTransactionManager extends ServiceMBeanSupport
49       implements DistributedTransactionManagerMBean
50 {
51    // Constants -----------------------------------------------------
52

53    public static final String JavaDoc SUBSYSTEM = "DTM";
54    
55    private static final String JavaDoc[] addInvocationHandlerSignature =
56       new String JavaDoc[] {
57          "java.lang.String",
58          "org.jboss.remoting.ServerInvocationHandler"
59       };
60    
61    private static final String JavaDoc[] removeInvocationHandlerSignature =
62       new String JavaDoc[] { "java.lang.String" };
63    
64    private static final Object JavaDoc[] removeInvocationHandlerParams =
65       new Object JavaDoc[] { SUBSYSTEM };
66
67    public static final String JavaDoc USER_TRANSACTION_JNDI_NAME = "UserTransaction";
68
69    // Attributes ----------------------------------------------------
70

71    private List JavaDoc connectors;
72    private InvokerLocator[] locators;
73    private String JavaDoc[] locatorURIs;
74    private boolean interpositionEnabled;
75    
76    // ServiceMBeanSupport overrides ---------------------------------
77

78    protected void startService()
79          throws Exception JavaDoc
80    {
81       DTMServant dtmServant = new DTMServant(this);
82       List JavaDoc locatorList = new ArrayList JavaDoc();
83       List JavaDoc locatorURIList = new ArrayList JavaDoc();
84       Iterator JavaDoc i = connectors.iterator();
85
86       while (i.hasNext())
87       {
88          // Add DTM invocation handler to connector
89
ObjectName JavaDoc objectName = (ObjectName JavaDoc) i.next();
90          getServer().invoke(objectName,
91                             "addInvocationHandler",
92                             new Object JavaDoc[] {SUBSYSTEM,
93                                           new DTMInvocationHandler(dtmServant)},
94                             addInvocationHandlerSignature);
95          getLog().debug("Added DTM invocation handler to connector " +
96                         objectName);
97          
98          // Get the connector's invoker locator and locator URI
99
InvokerLocator locator =
100             (InvokerLocator) getServer().getAttribute(objectName, "Locator");
101          locatorList.add(locator);
102          String JavaDoc locatorURI = locator.getLocatorURI();
103          locatorURIList.add(locatorURI);
104       }
105       locators = (InvokerLocator[]) locatorList.toArray(new InvokerLocator[0]);
106       locatorURIs = (String JavaDoc[]) locatorURIList.toArray(new String JavaDoc[0]);
107       
108       // Set the TxManager's DTM CordinatorFactory and ResourceFactory.
109
TransactionManager JavaDoc 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       // Bind the DTM TransactionFactory proxy into JNDI
122
Context JavaDoc ctx = new InitialContext JavaDoc();
123       TransactionFactory transactionFactory =
124          (TransactionFactory) RemoteProxy.create(TransactionFactory.class,
125                                                  0,
126                                                  locators);
127       ctx.bind(ClientUserTransaction.TX_FACTORY_JNDI_NAME, transactionFactory);
128
129       // Bind the UserTransaction reference into JNDI
130
ctx.bind(USER_TRANSACTION_JNDI_NAME,
131                ClientUserTransaction.getSingleton());
132    }
133  
134    protected void stopService()
135          throws Exception JavaDoc
136    {
137       // Unset the TxManager's DTM ResourceFactory.
138
TxManager tm = (TxManager)TMUtil.getTransactionManager();
139       tm.setDTMResourceFactory(null);
140
141       Iterator JavaDoc i = connectors.iterator();
142       
143       while (i.hasNext())
144       {
145          // Remove DTM invocation handler from connector
146
ObjectName JavaDoc objectName = (ObjectName JavaDoc) 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       // Unset the TxManager's DTM CordinatorFactory and ResourceFactory.
156
tm.setDTMEnabled(false);
157       tm.setDTMCoordinatorFactory(null);
158       tm.setDTMResourceFactory(null);
159       
160       // Unbind the DTM TransactionFactory proxy
161
// and the UserTransaction reference from JNDI
162
Context JavaDoc ctx = new InitialContext JavaDoc();
163       ctx.unbind(ClientUserTransaction.TX_FACTORY_JNDI_NAME);
164       ctx.unbind(USER_TRANSACTION_JNDI_NAME);
165    }
166
167    // DistributedTransactionManagerMBean methods --------------------
168

169    public List JavaDoc getConnectors()
170    {
171       return connectors;
172    }
173    
174    public void setConnectors(List JavaDoc connectors)
175    {
176       this.connectors = connectors;
177    }
178
179    public InvokerLocator[] getLocators()
180    {
181       return locators;
182    }
183    
184    public String JavaDoc[] 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