001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JOTMComponent.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.component.jotm;
025:
026: import java.rmi.RemoteException;
027:
028: import javax.naming.InitialContext;
029: import javax.naming.NamingException;
030: import javax.transaction.SystemException;
031: import javax.transaction.TransactionManager;
032:
033: import org.objectweb.jotm.Current;
034: import org.objectweb.jotm.TimerManager;
035: import org.objectweb.jotm.TransactionFactory;
036: import org.objectweb.jotm.TransactionFactoryImpl;
037: import org.ow2.easybeans.component.api.EZBComponentException;
038: import org.ow2.easybeans.component.itf.TMComponent;
039: import org.ow2.util.log.Log;
040: import org.ow2.util.log.LogFactory;
041:
042: /**
043: * Creates and binds the transaction factory and usertransaction object.
044: * @author Florent Benoit
045: */
046: public class JOTMComponent implements TMComponent {
047:
048: /**
049: * Default Transaction timeout.
050: */
051: private static final int DEFAULT_TIMEOUT = 60;
052:
053: /**
054: * Logger.
055: */
056: private static Log logger = LogFactory.getLog(JOTMComponent.class);
057:
058: /**
059: * Transaction manager reference.
060: */
061: private TransactionManager transactionManager = null;
062:
063: /**
064: * Transaction timeout (in seconds).
065: */
066: private int timeout = DEFAULT_TIMEOUT;
067:
068: /**
069: * Init method.<br/> This method is called before the start method.
070: * @throws EZBComponentException if the initialization has failed.
071: */
072: public void init() throws EZBComponentException {
073:
074: }
075:
076: /**
077: * Start method.<br/> This method is called after the init method.
078: * @throws EZBComponentException if the start has failed.
079: */
080: public void start() throws EZBComponentException {
081:
082: // Creates a Transaction factory
083: TransactionFactory transactionFactory;
084: try {
085: transactionFactory = new TransactionFactoryImpl();
086: } catch (RemoteException e) {
087: throw new EZBComponentException(
088: "Cannot create transaction factory", e);
089: }
090:
091: // Bind it
092: try {
093: new InitialContext()
094: .rebind("TMFactory", transactionFactory);
095: } catch (NamingException e) {
096: throw new EZBComponentException(
097: "Cannot bind transaction factory", e);
098: }
099:
100: transactionManager = new Current(transactionFactory);
101: try {
102: transactionManager.setTransactionTimeout(this .timeout);
103: } catch (SystemException se) {
104: throw new EZBComponentException(
105: "Cannot set Transaction Timeout", se);
106: }
107: try {
108: new InitialContext().rebind(JNDI_NAME, transactionManager);
109: } catch (NamingException e) {
110: throw new EZBComponentException(
111: "Cannot bind user transaction", e);
112: }
113:
114: // info
115: logger.info("Register {0} as transaction manager object",
116: JNDI_NAME);
117: }
118:
119: /**
120: * Stop method.<br/> This method is called when component needs to be
121: * stopped.
122: * @throws EZBComponentException if the stop is failing.
123: */
124: public void stop() throws EZBComponentException {
125: // Unbind factory
126: try {
127: new InitialContext().unbind("TMFactory");
128: } catch (NamingException e) {
129: throw new EZBComponentException(
130: "Cannot unbind transaction factory", e);
131: }
132:
133: // Unbind user transaction object
134: try {
135: new InitialContext().unbind(JNDI_NAME);
136: } catch (NamingException e) {
137: throw new EZBComponentException(
138: "Cannot unbind user transaction", e);
139: }
140:
141: // Stop timer
142: TimerManager.stop(true);
143: }
144:
145: /**
146: * Gets the transaction manager object.
147: * @return instance of the transaction manager
148: */
149: public TransactionManager getTransactionManager() {
150: return transactionManager;
151: }
152:
153: /**
154: * Set the Transaction Timeout.
155: * @param timeout Timeout (in seconds)
156: */
157: public void setTimeout(final int timeout) {
158: this.timeout = timeout;
159: }
160: }
|