KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > transaction > TransactionDirector


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * TransactionDirector.java
20  *
21  * This object is responsible for setting up the transaction manager and for
22  * supplying access to the transaction manger
23  */

24
25 // package path
26
package com.rift.coad.lib.transaction;
27
28 // java imports
29
import javax.transaction.UserTransaction JavaDoc;
30 import javax.transaction.TransactionManager JavaDoc;
31
32 // log 4 j imports
33
import org.apache.log4j.Logger;
34
35 // JOTM imports
36
import org.objectweb.jotm.Jotm;
37
38 // coadunation imports
39
import com.rift.coad.lib.naming.ContextManager;
40
41 /**
42  * This object is responsible for setting up the transaction manager and for
43  * supplying access to the transaction manger. It sets up the JOTM transaction
44  * manager or Jonas Transaction manager.
45  *
46  * @author Brett Chaldecott
47  */

48 public class TransactionDirector {
49     
50     // the user transaction context
51
private final static String JavaDoc TRANSACTION_CONTEXT_BASE =
52             "java:comp/";
53     private final static String JavaDoc USER_TRANSACTION_CONTEXT_BINDING =
54             "UserTransaction";
55     private final static String JavaDoc TRANSACTION_MANAGER_CONTEXT_BINDING =
56             "TransactionManager";
57     
58     // logger manager reference
59
private Logger log = Logger.getLogger(TransactionDirector.class.getName());
60     
61     // private member variables
62
private static TransactionDirector singleton = null;
63     private Jotm jotm = null;
64     private UserTransaction JavaDoc userTransaction = null;
65     private TransactionManager JavaDoc transactionManager = null;
66     
67     /**
68      * Creates a new instance of TransactionDirector
69      */

70     public TransactionDirector() throws TransactionException {
71         try {
72             jotm = new Jotm(true,true);
73             ContextManager contextManager =
74                     new ContextManager(TRANSACTION_CONTEXT_BASE);
75             userTransaction = jotm.getUserTransaction();
76             contextManager.bind(USER_TRANSACTION_CONTEXT_BINDING,
77                     userTransaction);
78             transactionManager = jotm.getTransactionManager();
79             contextManager.bind(TRANSACTION_MANAGER_CONTEXT_BINDING,
80                     transactionManager);
81         } catch (Exception JavaDoc ex) {
82             throw new TransactionException(
83                     "Failed to instanciate the transaction directory because : "
84                     + ex.getMessage(),ex);
85         }
86     }
87     
88     
89     /**
90      * This method inits and returns a reference to the transaction director.
91      *
92      * @return A reference to the Transaction Directory.
93      * @exception TransactionException
94      */

95     public static synchronized TransactionDirector init()
96             throws TransactionException {
97         if (singleton == null) {
98             singleton = new TransactionDirector();
99         }
100         return singleton;
101     }
102     
103     
104     /**
105      * This method returns a reference to the transaction instance.
106      *
107      * @return A reference to the transaction director instance.
108      * @exception TransactionException
109      */

110     public static synchronized TransactionDirector getInstance()
111             throws TransactionException {
112         if (singleton == null) {
113             throw new TransactionException(
114                     "The transaction director is not instanciated.");
115         }
116         return singleton;
117     }
118     
119     
120     /**
121      * This method returns the reference to the user transaction.
122      *
123      * @return A reference to the user transaction object.
124      */

125     public UserTransaction JavaDoc getUserTransaction() {
126         return userTransaction;
127     }
128     
129     
130     /**
131      * This method returns a reference to the transaction manager.
132      *
133      * @return A reference to the transaction manager object.
134      */

135     public TransactionManager JavaDoc getTransactionManager() {
136         return transactionManager;
137     }
138     
139     
140     /**
141      * This method stops the transaction manager.
142      */

143     public void stop() {
144         try {
145             jotm.stop();
146         } catch (Exception JavaDoc ex) {
147             log.error("Failed to shut down the transaction manager : " +
148                     ex.getMessage(),ex);
149         }
150     }
151 }
152
Popular Tags