KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jotm > Jotm


1 /*
2  * @(#) Jotm.java
3  *
4  * JOTM: Java Open Transaction Manager
5  *
6  *
7  * This module was originally developed by
8  *
9  * - Bull S.A. as part of the JOnAS application server code released in
10  * July 1999 (www.bull.com)
11  *
12  * --------------------------------------------------------------------------
13  * The original code and portions created by Bull SA are
14  * Copyright (c) 1999 BULL SA
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions are met:
19  *
20  * -Redistributions of source code must retain the above copyright notice, this
21  * list of conditions and the following disclaimer.
22  *
23  * -Redistributions in binary form must reproduce the above copyright notice,
24  * this list of conditions and the following disclaimer in the documentation
25  * and/or other materials provided with the distribution.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  *
39  * --------------------------------------------------------------------------
40  * $Id: Jotm.java,v 1.6 2003/12/05 18:14:44 trentshue Exp $
41  * --------------------------------------------------------------------------
42  */

43 package org.objectweb.jotm;
44
45 import java.rmi.NoSuchObjectException JavaDoc;
46 import java.rmi.RemoteException JavaDoc;
47
48 import javax.naming.Context JavaDoc;
49 import javax.naming.InitialContext JavaDoc;
50 import javax.naming.NamingException JavaDoc;
51 import javax.rmi.PortableRemoteObject JavaDoc;
52 import javax.transaction.UserTransaction JavaDoc;
53
54 import org.objectweb.carol.util.configuration.ConfigurationRepository;
55 import org.objectweb.carol.util.configuration.ConfigurationException;
56 import org.objectweb.transaction.jta.TMService;
57 import org.objectweb.transaction.jta.TransactionManager;
58
59 /**
60  * This class represents an instance of JOTM.
61  *
62  * @author jeff mesnil
63 */

64 public class Jotm implements TMService {
65
66     /**
67      * Current instance
68      */

69     Current current = null;
70     
71     /**
72      * TransactionRecovery instance
73      */

74     TransactionRecovery transactionrecovery = null;
75
76     /**
77      * JNDI name of the TransactionFactory
78      */

79     private static final String JavaDoc TMFACTORY_NAME = "TMFactory";
80
81     /**
82      * TransactionFactory
83      */

84     private TransactionFactory tf = null;
85
86     /**
87      * is the TransactionFactory is local or not
88      */

89     private boolean local;
90
91     /**
92      * is the TransactionFacotory bound to JNDI or not
93      */

94     private boolean bound;
95
96     /**
97      * has binding of TransactionFactory fails
98      */

99     private boolean boundFailed = false;
100
101     /**
102      * Public constructor for Jotm.
103      *
104      * If Jotm is created with a <code>local</code> transaction factory which is
105      * not <code>bound</code> to a registry, Jotm would be able to manage
106      * transactions only inside the same JVM.
107      * @param local <code>true</code> to create an instance of JOTM with a
108      * local transaction factory, <code>false</code> else
109      * @param bound <code>true</code> if the transaction factory is to be
110      * bound in a registry, <code>false</code> else (ignored if
111      * <code>local<code> is <code>false</code>)
112      *
113      * @throws NamingException thrown if the transaction factory can't be bound
114      * or looked up in a registry
115      */

116     public Jotm(boolean local, boolean bound) throws NamingException JavaDoc {
117         if (TraceTm.jotm.isInfoEnabled()) {
118             TraceTm.jotm.info(
119                 "JOTM started with a "
120                     + (local ? "local" : "remote")
121                     + " transaction factory"
122                     + " which is "
123                     + (bound ? "" : "not")
124                     + " bound.");
125         }
126         
127         this.local = local;
128         this.bound = bound;
129         // CAROL initialization
130
TraceTm.jotm.info("CAROL initialization");
131         
132         try {
133             ConfigurationRepository.init();
134         }catch (ConfigurationException e) {
135             TraceTm.jotm.error("CAROL initialization failed", e);
136         }
137         
138         if (local) {
139             try {
140                 tf = new TransactionFactoryImpl();
141             } catch (RemoteException JavaDoc e) {
142                 // should not happen: TransactionFactoryImpl throws
143
// RemoteException only because it extends Remote interface
144
TraceTm.jotm.error(
145                     "Instanciation of TransactionFactory failed",
146                     e);
147             }
148             
149             if (bound) {
150                 Context JavaDoc ictx;
151                 try {
152                     ictx = new InitialContext JavaDoc();
153                     ictx.rebind(TMFACTORY_NAME, tf);
154                     
155                     if (TraceTm.jotm.isDebugEnabled()) {
156                         TraceTm.jotm.debug("TransactionFactory bound with name " + TMFACTORY_NAME);
157                     }
158                 } catch (NamingException JavaDoc e) {
159                     TraceTm.jotm.error("TransactionFactory rebind failed", e);
160                     boundFailed = true;
161                     throw e;
162                 }
163             }
164         } else {
165             Context JavaDoc ictx;
166             
167             try {
168                 ictx = new InitialContext JavaDoc();
169                 tf = (TransactionFactory) ictx.lookup(TMFACTORY_NAME);
170             } catch (NamingException JavaDoc e) {
171                 TraceTm.jotm.error("TransactionFactory lookup failed", e);
172                 throw e;
173             }
174         }
175
176         try {
177             current = new Current(tf);
178         } catch (Exception JavaDoc e){
179             ;
180         }
181
182     }
183
184     /**
185      * @see org.objectweb.transaction.jta.TMService#getTransactionManager()
186      */

187     public TransactionManager getTransactionManager() {
188         if (TraceTm.jotm.isDebugEnabled()) {
189             TraceTm.jotm.debug("TransactionManager=" + current);
190         }
191         return (TransactionManager) current;
192     }
193     
194     /**
195      * @see org.objectweb.transaction.jta.TMService#getTransactionManager()
196      */

197     public TransactionRecovery getTransactionRecovery() {
198         transactionrecovery = (TransactionRecovery) Current.getTransactionRecovery();
199         if (TraceTm.jotm.isDebugEnabled()) {
200             TraceTm.jotm.debug("TransactionRecovery=" + transactionrecovery);
201         }
202         return transactionrecovery;
203     }
204
205     /**
206      * @see org.objectweb.transaction.jta.TMService#getUserTransaction()
207      */

208     public UserTransaction JavaDoc getUserTransaction() {
209         if (TraceTm.jotm.isDebugEnabled()) {
210             TraceTm.jotm.debug("UserTransaction=" + current);
211         }
212         return (UserTransaction JavaDoc) current;
213     }
214
215     /**
216      * @see org.objectweb.transaction.jta.TMService#stop()
217      */

218     public void stop() {
219         TraceTm.jotm.info("stop JOTM");
220         // remove current transaction if there's still one in ThreadLocal
221

222         try {
223             current.forget();
224         } catch (Exception JavaDoc e) {
225             ;
226         }
227         
228         try {
229             transactionrecovery.forget();
230         } catch (Exception JavaDoc e) {
231             ;
232         }
233
234         // unexport remote objects
235
if (local) {
236             if (bound && !boundFailed) {
237                 try {
238                     InitialContext JavaDoc ictx = new InitialContext JavaDoc();
239                     ictx.unbind(TMFACTORY_NAME);
240                     
241                     if (TraceTm.jotm.isDebugEnabled()) {
242                         TraceTm.jotm.debug("TransactionFactory unbound");
243                     }
244                 } catch (Exception JavaDoc e) {
245                     TraceTm.jotm.warn(
246                         "an exception has prevented the TransactionFactory"
247                             + " to be unbound",
248                         e);
249                 }
250             }
251
252             if (tf != null) {
253                 try {
254                     PortableRemoteObject.unexportObject(tf);
255                     
256                     if (TraceTm.jotm.isDebugEnabled()) {
257                         TraceTm.jotm.debug("TransactionFactory unexported");
258                     }
259                 } catch (NoSuchObjectException JavaDoc e) {
260                     TraceTm.jotm.warn(
261                         "an exception has prevented the TransactionFactory"
262                             + " to be unbound",
263                         e);
264                 }
265             }
266         }
267         tf = null;
268     }
269 }
270
Popular Tags