KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > component > jotm > JOTMComponent


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JOTMComponent.java 1137 2006-10-06 09:17:54Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.component.jotm;
27
28 import java.rmi.RemoteException JavaDoc;
29
30 import javax.naming.InitialContext JavaDoc;
31 import javax.naming.NamingException JavaDoc;
32 import javax.transaction.SystemException JavaDoc;
33 import javax.transaction.TransactionManager JavaDoc;
34
35 import org.objectweb.easybeans.component.api.EZBComponentException;
36 import org.objectweb.easybeans.component.itf.TMComponent;
37 import org.objectweb.easybeans.log.JLog;
38 import org.objectweb.easybeans.log.JLogFactory;
39 import org.objectweb.jotm.Current;
40 import org.objectweb.jotm.TimerManager;
41 import org.objectweb.jotm.TransactionFactory;
42 import org.objectweb.jotm.TransactionFactoryImpl;
43
44 /**
45  * Creates and binds the transaction factory and usertransaction object.
46  * @author Florent Benoit
47  */

48 public class JOTMComponent implements TMComponent {
49
50     /**
51      * Default Transaction timeout.
52      */

53     private static final int DEFAULT_TIMEOUT = 60;
54
55     /**
56      * Logger.
57      */

58     private static JLog logger = JLogFactory.getLog(JOTMComponent.class);
59
60     /**
61      * Transaction manager reference.
62      */

63     private TransactionManager JavaDoc transactionManager = null;
64
65     /**
66      * Transaction timeout (in seconds).
67      */

68     private int timeout = DEFAULT_TIMEOUT;
69
70     /**
71      * Init method.<br/> This method is called before the start method.
72      * @throws EZBComponentException if the initialization has failed.
73      */

74     public void init() throws EZBComponentException {
75
76     }
77
78     /**
79      * Start method.<br/> This method is called after the init method.
80      * @throws EZBComponentException if the start has failed.
81      */

82     public void start() throws EZBComponentException {
83
84         // Creates a Transaction factory
85
TransactionFactory transactionFactory;
86         try {
87             transactionFactory = new TransactionFactoryImpl();
88         } catch (RemoteException JavaDoc e) {
89             throw new EZBComponentException("Cannot create transaction factory", e);
90         }
91
92         // Bind it
93
try {
94             new InitialContext JavaDoc().rebind("TMFactory", transactionFactory);
95         } catch (NamingException JavaDoc e) {
96             throw new EZBComponentException("Cannot bind transaction factory", e);
97         }
98
99         transactionManager = new Current(transactionFactory);
100         try {
101             transactionManager.setTransactionTimeout(this.timeout);
102         } catch (SystemException JavaDoc se) {
103             throw new EZBComponentException("Cannot set Transaction Timeout", se);
104         }
105         try {
106             new InitialContext JavaDoc().rebind(JNDI_NAME, transactionManager);
107         } catch (NamingException JavaDoc e) {
108             throw new EZBComponentException("Cannot bind user transaction", e);
109         }
110
111         // info
112
logger.info("Register {0} as transaction manager object", JNDI_NAME);
113     }
114
115     /**
116      * Stop method.<br/> This method is called when component needs to be
117      * stopped.
118      * @throws EZBComponentException if the stop is failing.
119      */

120     public void stop() throws EZBComponentException {
121         // Unbind factory
122
try {
123             new InitialContext JavaDoc().unbind("TMFactory");
124         } catch (NamingException JavaDoc e) {
125             throw new EZBComponentException("Cannot unbind transaction factory", e);
126         }
127
128         // Unbind user transaction object
129
try {
130             new InitialContext JavaDoc().unbind(JNDI_NAME);
131         } catch (NamingException JavaDoc e) {
132             throw new EZBComponentException("Cannot unbind user transaction", e);
133         }
134
135         // Stop timer
136
TimerManager.stop(true);
137     }
138
139     /**
140      * Gets the transaction manager object.
141      * @return instance of the transaction manager
142      */

143     public TransactionManager JavaDoc getTransactionManager() {
144         return transactionManager;
145     }
146
147     /**
148      * Set the Transaction Timeout.
149      * @param timeout Timeout (in seconds)
150      */

151     public void setTimeout(final int timeout) {
152         this.timeout = timeout;
153     }
154 }
155
Popular Tags