1 25 26 package org.objectweb.jonas.jtm; 27 28 import java.rmi.RemoteException ; 29 30 import javax.management.MBeanServer ; 31 import javax.management.ObjectName ; 32 import javax.management.modelmbean.ModelMBean ; 33 import javax.naming.Context ; 34 import javax.naming.InitialContext ; 35 import javax.naming.NamingException ; 36 import javax.transaction.UserTransaction ; 37 import javax.transaction.xa.Xid ; 38 39 import org.apache.commons.modeler.ManagedBean; 40 import org.apache.commons.modeler.Registry; 41 42 import org.objectweb.jotm.Current; 43 import org.objectweb.jotm.TransactionFactory; 44 import org.objectweb.jotm.TransactionFactoryImpl; 45 import org.objectweb.transaction.jta.TransactionManager; 46 47 import org.objectweb.jonas.common.Log; 48 import org.objectweb.jonas.jmx.J2eeObjectName; 49 import org.objectweb.jonas.jmx.JmxService; 50 import org.objectweb.jonas.jmx.JonasObjectName; 51 import org.objectweb.jonas.management.JonasMBeanTools; 52 import org.objectweb.jonas.naming.NamingManager; 53 import org.objectweb.jonas.service.AbsServiceImpl; 54 import org.objectweb.jonas.service.ServiceException; 55 import org.objectweb.jonas.service.ServiceManager; 56 57 import org.objectweb.util.monolog.api.BasicLevel; 58 import org.objectweb.util.monolog.api.Logger; 59 60 68 public class TransactionServiceImpl extends AbsServiceImpl implements TransactionService, TransactionServiceImplMBean { 69 70 private static Logger logger = null; 71 72 75 public static final String SERVICE_NAME = "jtm"; 76 80 static final String TIMEOUT = "jonas.service.jtm.timeout"; 81 84 static final String REMOTE = "jonas.service.jtm.remote"; 85 88 static final String CLASS = "jonas.service.jtm.class"; 89 90 95 private TransactionFactory tm = null; 96 97 101 private Current current = null; 102 103 private Xid [] myXid = null; 104 105 private int timeout; 107 private boolean jtmlocal; 108 private InitialContext ictx; 109 110 113 private Registry oRegistry = null; 114 117 private MBeanServer mbeanServer = null; 118 122 128 public void doInit(Context ctx) throws ServiceException { 129 130 logger = Log.getLogger(Log.JONAS_SERVER_PREFIX); 132 super.initLogger(Log.getLogger(Log.JONAS_MANAGEMENT_PREFIX)); 133 134 String remote = "false"; 136 try { 137 remote = (String ) ctx.lookup(REMOTE); 138 } catch (NamingException e) { 139 } 141 if ((remote != null) && (!remote.equals(""))) { 142 jtmlocal = !remote.equalsIgnoreCase("true"); 143 } else { 144 jtmlocal = false; 145 } 146 147 String tstr = "60"; 148 try { 149 tstr = (String ) ctx.lookup(TIMEOUT); 150 } catch (NamingException e) { 151 } 153 if ((tstr != null) && (!tstr.equals(""))) { 154 timeout = (new Integer (tstr)).intValue(); 155 } else { 156 timeout = 60; 157 } 158 159 try { 161 mbeanServer = 162 ((JmxService) ServiceManager.getInstance().getJmxService()).getJmxServer(); 163 } catch (Exception e) { 164 mbeanServer = null; 166 } 167 oRegistry = JonasMBeanTools.getRegistry(); 169 170 if (logger.isLoggable(BasicLevel.DEBUG)) { 171 logger.log(BasicLevel.DEBUG, "TransactionService initialized. Timeout = " + timeout); 172 } 173 } 174 175 180 public void doStart() throws ServiceException { 181 try { 182 ictx = NamingManager.getInstance().getInitialContext(); 183 } catch (NamingException e) { 184 logger.log(BasicLevel.ERROR, "TransactionService: Cannot get InitialContext:\n" + e); 185 throw new ServiceException("TransactionService: Cannot get InitialContext", e); 186 } 187 188 if (jtmlocal) { 190 if (logger.isLoggable(BasicLevel.DEBUG)) { 192 logger.log(BasicLevel.DEBUG, "working with a colocated Transaction Manager "); 193 } 194 195 197 try { 199 if (logger.isLoggable(BasicLevel.DEBUG)) { 200 logger.log(BasicLevel.DEBUG, "Create and register TM factory"); 201 } 202 tm = new TransactionFactoryImpl(); 203 ictx.rebind("TMFactory", tm); 204 } catch (RemoteException e) { 205 logger.log(BasicLevel.ERROR, "TransactionService: Cannot create TransactionFactory:\n" + e); 206 throw new ServiceException("TransactionService: Cannot create TransactionFactory", 207 e); 208 } catch (NamingException e) { 209 logger.log(BasicLevel.ERROR, "TransactionService: Cannot rebind TM:\n" + e); 210 throw new ServiceException("TransactionService: Cannot rebind TM", e); 211 } 212 } else { 213 if (logger.isLoggable(BasicLevel.DEBUG)) { 215 logger.log(BasicLevel.DEBUG, "working with a remote Transaction Manager "); 216 } 217 final int maxloops = 5; 218 for (int i = 0; i <= maxloops; i++) { 219 try { 220 tm = (TransactionFactory) ictx.lookup("TMFactory"); 221 break; 222 } catch (NamingException e) { 223 if (i < maxloops) { 224 logger.log(BasicLevel.WARN, "Cannot get TM factory - retrying..."); 225 try { 226 Thread.sleep(2000 * (i + 1)); 227 } catch (InterruptedException e2) { 228 throw new ServiceException("Cannot get TM factory", e2); 229 } 230 } else { 231 logger.log(BasicLevel.ERROR, "TransactionService: Cannot get TM factory:\n" + e); 232 throw new ServiceException("TransactionService: Cannot get TM factory", e); 233 } 234 } 235 } 236 } 237 238 current = new Current(tm); 246 setTimeout(timeout); 247 248 try { 249 if (mbeanServer != null) { 250 mbeanServer.registerMBean(this, JonasObjectName.transactionService()); 252 253 try { 256 String sJTAResourceName = "JTAResource"; 257 ObjectName onJTAResource = 258 J2eeObjectName.JTAResource(getDomainName(), 259 getJonasServerName(), 260 sJTAResourceName); 261 JTAResource jtaResourceMBean = new JTAResource(onJTAResource.toString(), this, 262 new Integer (timeout), 263 new Boolean (jtmlocal), 264 new Integer (tm.getPortNumber()), 265 tm.getHostName()); 266 ManagedBean oManaged = oRegistry.findManagedBean("JTAResource"); 267 ModelMBean oMBean = oManaged.createMBean(jtaResourceMBean); 268 if (logger.isLoggable(BasicLevel.DEBUG)) { 269 logger.log(BasicLevel.DEBUG, "JTAResource J2EEResource created"); 270 } 271 mbeanServer.registerMBean(oMBean, onJTAResource); 272 } catch (Exception e) { 273 e.printStackTrace(); 274 logger.log(BasicLevel.ERROR, "JOnAS: Cannot register JTAResource mBean" + e); 275 } 276 } 277 } catch (ServiceException se) { 278 } catch (Exception e) { 280 logger.log(BasicLevel.ERROR, "TransactionService: Cannot start the Transaction service:\n" + e); 281 throw new ServiceException("TransactionService: Cannot start the Transaction service", e); 282 } 283 284 if (logger.isLoggable(BasicLevel.DEBUG)) { 285 logger.log(BasicLevel.DEBUG, "TransactionService started, default timeout= " + timeout); 286 } 287 288 289 try { 291 Current.getTransactionRecovery().startResourceManagerRecovery(); 292 } catch (Exception ex) { 293 if (logger.isLoggable(BasicLevel.DEBUG)) { 294 logger.log(BasicLevel.DEBUG, "JOTM startResourceManagerRecovery failed: ", ex); 295 } 296 throw new ServiceException("Cannot start resource manager recovery", ex); 297 } 298 } 299 300 305 public void doStop() throws ServiceException { 306 if (logger.isLoggable(BasicLevel.DEBUG)) { 307 logger.log(BasicLevel.DEBUG, "Stop of TransactionService not already implemented"); 308 } 309 } 310 311 315 319 public Current getCurrent() { 320 return current; 321 } 322 323 327 public TransactionManager getTransactionManager() { 328 return (TransactionManager) current; 329 } 330 331 335 public UserTransaction getUserTransaction() { 336 return (UserTransaction ) current; 337 } 338 339 343 public TransactionFactory getTransactionFactory() { 344 return tm; 345 } 346 347 public int getTimeout() { 348 return current.getDefaultTimeout(); 349 } 350 351 355 public void setTimeout(int t) { 356 357 if (logger.isLoggable(BasicLevel.DEBUG)) { 358 logger.log(BasicLevel.DEBUG, "" + t); 359 } 360 current.setDefaultTimeout(t); 361 362 if (jtmlocal) { 365 try { 366 if (logger.isLoggable(BasicLevel.DEBUG)) { 367 logger.log(BasicLevel.DEBUG, "Register UserTransactionFactory"); 368 } 369 NamingManager.getInstance().getInitialContext().rebind("javax.transaction.UserTransaction", current); 370 } catch (NamingException e) { 371 logger.log(BasicLevel.ERROR, "Cannot rebind UserTransaction:" + e); 372 } 373 } 374 375 } 376 377 381 protected int getTotalBegunTransactions() { 382 return current.getTotalBegunTransactions(); 383 } 384 385 389 protected int getTotalCommittedTransactions() { 390 return current.getTotalCommittedTransactions(); 391 } 392 393 397 protected int getTotalCurrentTransactions() { 398 return current.getTotalCurrentTransactions(); 399 } 400 401 405 protected int getTotalExpiredTransactions() { 406 return current.getTotalExpiredTransactions(); 407 } 408 409 413 protected int getTotalRolledbackTransactions() { 414 return current.getTotalRolledbackTransactions(); 415 } 416 417 420 protected void resetAllTxTotalCounters() { 421 current.resetAllTxTotalCounters(); 422 } 423 424 428 protected Xid [] getAllActiveXids() { 429 return (current.getAllXid()); 430 } 431 432 436 protected String [] getAllActiveTx() { 437 String [] mysArray; 438 439 mysArray = current.getAllTx(); 440 return mysArray; 441 } 442 443 447 protected String [] getAllRecoveryTx() { 448 String [] mysArray; 449 450 mysArray = current.getAllRcTx(); 451 return mysArray; 452 } 453 454 458 protected String [] getAllXAResource(String xtx) { 459 String [] mysArray; 460 461 mysArray = current.getAllXaTx(xtx); 462 return mysArray; 463 } 464 465 468 protected int commitXAResource(String xatx) { 469 int commiterror; 470 commiterror = current.actionXAResource("commit", xatx); 471 return commiterror; 472 } 473 474 477 protected int rollbackXAResource(String xatx) { 478 int rollbackerror; 479 rollbackerror = current.actionXAResource("rollback", xatx); 480 return rollbackerror; 481 } 482 483 486 protected int forgetXAResource(String xatx) { 487 int forgeterror; 488 forgeterror = current.actionXAResource("forget", xatx); 489 return forgeterror; 490 } 491 } 492 493 | Popular Tags |