00001
00026 package org.objectweb.jonas.jtm;
00027
00028 import java.rmi.RemoteException;
00029
00030 import javax.management.MBeanServer;
00031 import javax.management.ObjectName;
00032 import javax.management.modelmbean.ModelMBean;
00033 import javax.naming.Context;
00034 import javax.naming.InitialContext;
00035 import javax.naming.NamingException;
00036 import javax.transaction.UserTransaction;
00037 import javax.transaction.xa.Xid;
00038
00039 import org.apache.commons.modeler.ManagedBean;
00040 import org.apache.commons.modeler.Registry;
00041 import org.objectweb.jonas.common.Log;
00042 import org.objectweb.jonas.jmx.J2eeObjectName;
00043 import org.objectweb.jonas.jmx.JmxService;
00044 import org.objectweb.jonas.jmx.JonasObjectName;
00045 import org.objectweb.jonas.management.JonasMBeanTools;
00046 import org.objectweb.jonas.naming.NamingManager;
00047 import org.objectweb.jonas.service.AbsServiceImpl;
00048 import org.objectweb.jonas.service.ServiceException;
00049 import org.objectweb.jonas.service.ServiceManager;
00050 import org.objectweb.jotm.Current;
00051 import org.objectweb.jotm.TransactionFactory;
00052 import org.objectweb.jotm.TransactionFactoryImpl;
00053 import org.objectweb.transaction.jta.TransactionManager;
00054 import org.objectweb.util.monolog.api.BasicLevel;
00055 import org.objectweb.util.monolog.api.Logger;
00056
00065 public class TransactionServiceImpl extends AbsServiceImpl implements TransactionService, TransactionServiceImplMBean {
00066
00067 private static Logger logger = null;
00068
00072 public static final String SERVICE_NAME = "jtm";
00073
00077 static final String TIMEOUT = "jonas.service.jtm.timeout";
00081 static final String REMOTE = "jonas.service.jtm.remote";
00085 static final String CLASS = "jonas.service.jtm.class";
00086
00092 private TransactionFactory tm = null;
00093
00098 private Current current = null;
00099
00100 private Xid myXid [] = null;
00101
00102
00103 private int timeout;
00104 private boolean jtmlocal;
00105 private InitialContext ictx;
00106
00110 private Registry oRegistry = null;
00114 private MBeanServer mbeanServer = null;
00115
00116
00117
00118
00125 public void doInit(Context ctx) throws ServiceException {
00126
00127
00128 logger = Log.getLogger(Log.JONAS_SERVER_PREFIX);
00129 super.initLogger(Log.getLogger(Log.JONAS_MANAGEMENT_PREFIX));
00130
00131
00132 String remote = "false";
00133 try {
00134 remote = (String) ctx.lookup(REMOTE);
00135 } catch (NamingException e) {
00136
00137 }
00138 if ((remote != null) && (!remote.equals(""))) {
00139 jtmlocal = !remote.equalsIgnoreCase("true");
00140 } else {
00141 jtmlocal = false;
00142 }
00143
00144 String tstr = "60";
00145 try {
00146 tstr = (String) ctx.lookup(TIMEOUT);
00147 } catch (NamingException e) {
00148
00149 }
00150 if ((tstr != null) && (!tstr.equals(""))) {
00151 timeout = (new Integer(tstr)).intValue();
00152 } else {
00153 timeout = 60;
00154 }
00155
00156
00157 try {
00158 mbeanServer =
00159 ((JmxService) ServiceManager.getInstance().getJmxService()).getJmxServer();
00160 } catch (Exception e) {
00161
00162 mbeanServer = null;
00163 }
00164
00165 oRegistry = JonasMBeanTools.getRegistry();
00166
00167 logger.log(BasicLevel.DEBUG, "TransactionService initialized. Timeout = " + timeout);
00168 }
00169
00175 public void doStart() throws ServiceException {
00176 try {
00177 ictx = NamingManager.getInstance().getInitialContext();
00178 } catch (NamingException e) {
00179 logger.log(BasicLevel.ERROR, "TransactionService: Cannot get InitialContext:\n" + e);
00180 throw new ServiceException("TransactionService: Cannot get InitialContext", e);
00181 }
00182
00183
00184 if (jtmlocal) {
00185
00186 logger.log(BasicLevel.DEBUG, "working with a colocated Transaction Manager ");
00187
00188
00189
00190
00191 try {
00192 logger.log(BasicLevel.DEBUG, "Create and register TM factory");
00193 tm = new TransactionFactoryImpl();
00194 ictx.rebind("TMFactory", tm);
00195 } catch (RemoteException e) {
00196 logger.log(BasicLevel.ERROR, "TransactionService: Cannot create TransactionFactory:\n" + e);
00197 throw new ServiceException("TransactionService: Cannot create TransactionFactory",
00198 e);
00199 } catch (NamingException e) {
00200 logger.log(BasicLevel.ERROR, "TransactionService: Cannot rebind TM:\n" + e);
00201 throw new ServiceException("TransactionService: Cannot rebind TM", e);
00202 }
00203 } else {
00204
00205 logger.log(BasicLevel.DEBUG, "working with a remote Transaction Manager ");
00206 final int maxloops = 5;
00207 for (int i = 0; i <= maxloops; i++) {
00208 try {
00209 tm = (TransactionFactory) ictx.lookup("TMFactory");
00210 break;
00211 } catch (NamingException e) {
00212 if (i < maxloops) {
00213 logger.log(BasicLevel.WARN, "Cannot get TM factory - retrying...");
00214 try {
00215 Thread.sleep(2000 * (i + 1));
00216 } catch (InterruptedException e2) {
00217 throw new ServiceException("Cannot get TM factory", e2);
00218 }
00219 } else {
00220 logger.log(BasicLevel.ERROR, "TransactionService: Cannot get TM factory:\n" + e);
00221 throw new ServiceException("TransactionService: Cannot get TM factory", e);
00222 }
00223 }
00224 }
00225 }
00226
00227
00228
00229
00230
00231
00232
00233
00234 current = new Current(tm);
00235 setTimeout(timeout);
00236
00237 try {
00238 if (mbeanServer != null) {
00239
00240 mbeanServer.registerMBean(this, JonasObjectName.transactionService());
00241
00242
00243
00244 try {
00245 String sJTAResourceName = "JTAResource";
00246 ObjectName onJTAResource =
00247 J2eeObjectName.JTAResource(getDomainName(),
00248 getJonasServerName(),
00249 sJTAResourceName);
00250 JTAResource jtaResourceMBean = new JTAResource(onJTAResource.toString(), this,
00251 new Integer(timeout),
00252 new Boolean(jtmlocal),
00253 new Integer(tm.getPortNumber()),
00254 tm.getHostName());
00255 ManagedBean oManaged = oRegistry.findManagedBean("JTAResource");
00256 ModelMBean oMBean = oManaged.createMBean(jtaResourceMBean);
00257 if (logger.isLoggable(BasicLevel.DEBUG)) {
00258 logger.log(BasicLevel.DEBUG, "JTAResource J2EEResource created");
00259 }
00260 mbeanServer.registerMBean(oMBean, onJTAResource);
00261 } catch (Exception e) {
00262 e.printStackTrace();
00263 logger.log(BasicLevel.ERROR, "JOnAS: Cannot register JTAResource mBean" + e);
00264 }
00265 }
00266 } catch (ServiceException se) {
00267
00268 } catch (Exception e) {
00269 logger.log(BasicLevel.ERROR, "TransactionService: Cannot start the Transaction service:\n" + e);
00270 throw new ServiceException("TransactionService: Cannot start the Transaction service", e);
00271 }
00272
00273 logger.log(BasicLevel.DEBUG, "TransactionService started, default timeout= " + timeout);
00274 }
00275
00281 public void doStop() throws ServiceException {
00282 logger.log(BasicLevel.DEBUG, "Stop of TransactionService not already implemented");
00283 }
00284
00285
00286
00287
00288
00293 public Current getCurrent() {
00294 return current;
00295 }
00296
00301 public TransactionManager getTransactionManager() {
00302 return (TransactionManager) current;
00303 }
00304
00309 public UserTransaction getUserTransaction() {
00310 return (UserTransaction) current;
00311 }
00312
00317 public TransactionFactory getTransactionFactory() {
00318 return tm;
00319 }
00320
00325 public void setTimeout(int t) {
00326
00327 logger.log(BasicLevel.DEBUG, "" + t);
00328 current.setDefaultTimeout(t);
00329
00330
00331
00332 if (jtmlocal) {
00333 try {
00334 logger.log(BasicLevel.DEBUG, "Register UserTransactionFactory");
00335 NamingManager.getInstance().getInitialContext().rebind("javax.transaction.UserTransaction", current);
00336 } catch (NamingException e) {
00337 logger.log(BasicLevel.ERROR, "Cannot rebind UserTransaction:" + e);
00338 }
00339 }
00340
00341 }
00342
00347 protected int getTotalBegunTransactions() {
00348 return current.getTotalBegunTransactions();
00349 }
00350
00355 protected int getTotalCommittedTransactions() {
00356 return current.getTotalCommittedTransactions();
00357 }
00358
00363 protected int getTotalCurrentTransactions() {
00364 return current.getTotalCurrentTransactions();
00365 }
00366
00371 protected int getTotalExpiredTransactions() {
00372 return current.getTotalExpiredTransactions();
00373 }
00374
00379 protected int getTotalRolledbackTransactions() {
00380 return current.getTotalRolledbackTransactions();
00381 }
00382
00386 protected void resetAllTxTotalCounters() {
00387 current.resetAllTxTotalCounters();
00388 }
00389
00394 protected Xid [] getAllActiveXids() {
00395 return (current.getAllXid());
00396 }
00397
00402 protected String [] getAllActiveTx() {
00403 String [] mysArray;
00404
00405 mysArray = current.getAllTx();
00406 return mysArray;
00407 }
00408 }
00409