001: /*
002: * @(#) Jotm.java
003: *
004: * JOTM: Java Open Transaction Manager
005: *
006: *
007: * This module was originally developed by
008: *
009: * - Bull S.A. as part of the JOnAS application server code released in
010: * July 1999 (www.bull.com)
011: *
012: * --------------------------------------------------------------------------
013: * The original code and portions created by Bull SA are
014: * Copyright (c) 1999 BULL SA
015: * All rights reserved.
016: *
017: * Redistribution and use in source and binary forms, with or without
018: * modification, are permitted provided that the following conditions are met:
019: *
020: * -Redistributions of source code must retain the above copyright notice, this
021: * list of conditions and the following disclaimer.
022: *
023: * -Redistributions in binary form must reproduce the above copyright notice,
024: * this list of conditions and the following disclaimer in the documentation
025: * and/or other materials provided with the distribution.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
028: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
029: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
030: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
031: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
032: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
033: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
034: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
035: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
036: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
037: * POSSIBILITY OF SUCH DAMAGE.
038: *
039: * --------------------------------------------------------------------------
040: * $Id: Jotm.java,v 1.6 2003/12/05 18:14:44 trentshue Exp $
041: * --------------------------------------------------------------------------
042: */
043: package org.objectweb.jotm;
044:
045: import java.rmi.NoSuchObjectException;
046: import java.rmi.RemoteException;
047:
048: import javax.naming.Context;
049: import javax.naming.InitialContext;
050: import javax.naming.NamingException;
051: import javax.rmi.PortableRemoteObject;
052: import javax.transaction.UserTransaction;
053:
054: import org.objectweb.carol.util.configuration.ConfigurationRepository;
055: import org.objectweb.carol.util.configuration.ConfigurationException;
056: import org.objectweb.transaction.jta.TMService;
057: import org.objectweb.transaction.jta.TransactionManager;
058:
059: /**
060: * This class represents an instance of JOTM.
061: *
062: * @author jeff mesnil
063: */
064: public class Jotm implements TMService {
065:
066: /**
067: * Current instance
068: */
069: Current current = null;
070:
071: /**
072: * TransactionRecovery instance
073: */
074: TransactionRecovery transactionrecovery = null;
075:
076: /**
077: * JNDI name of the TransactionFactory
078: */
079: private static final String TMFACTORY_NAME = "TMFactory";
080:
081: /**
082: * TransactionFactory
083: */
084: private TransactionFactory tf = null;
085:
086: /**
087: * is the TransactionFactory is local or not
088: */
089: private boolean local;
090:
091: /**
092: * is the TransactionFacotory bound to JNDI or not
093: */
094: private boolean bound;
095:
096: /**
097: * has binding of TransactionFactory fails
098: */
099: 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 {
117: if (TraceTm.jotm.isInfoEnabled()) {
118: TraceTm.jotm.info("JOTM started with a "
119: + (local ? "local" : "remote")
120: + " transaction factory" + " which is "
121: + (bound ? "" : "not") + " bound.");
122: }
123:
124: this .local = local;
125: this .bound = bound;
126: // CAROL initialization
127: TraceTm.jotm.info("CAROL initialization");
128:
129: try {
130: ConfigurationRepository.init();
131: } catch (ConfigurationException e) {
132: TraceTm.jotm.error("CAROL initialization failed", e);
133: }
134:
135: if (local) {
136: try {
137: tf = new TransactionFactoryImpl();
138: } catch (RemoteException e) {
139: // should not happen: TransactionFactoryImpl throws
140: // RemoteException only because it extends Remote interface
141: TraceTm.jotm
142: .error(
143: "Instanciation of TransactionFactory failed",
144: e);
145: }
146:
147: if (bound) {
148: Context ictx;
149: try {
150: ictx = new InitialContext();
151: ictx.rebind(TMFACTORY_NAME, tf);
152:
153: if (TraceTm.jotm.isDebugEnabled()) {
154: TraceTm.jotm
155: .debug("TransactionFactory bound with name "
156: + TMFACTORY_NAME);
157: }
158: } catch (NamingException e) {
159: TraceTm.jotm.error(
160: "TransactionFactory rebind failed", e);
161: boundFailed = true;
162: throw e;
163: }
164: }
165: } else {
166: Context ictx;
167:
168: try {
169: ictx = new InitialContext();
170: tf = (TransactionFactory) ictx.lookup(TMFACTORY_NAME);
171: } catch (NamingException e) {
172: TraceTm.jotm.error("TransactionFactory lookup failed",
173: e);
174: throw e;
175: }
176: }
177:
178: try {
179: current = new Current(tf);
180: } catch (Exception e) {
181: ;
182: }
183:
184: }
185:
186: /**
187: * @see org.objectweb.transaction.jta.TMService#getTransactionManager()
188: */
189: public TransactionManager getTransactionManager() {
190: if (TraceTm.jotm.isDebugEnabled()) {
191: TraceTm.jotm.debug("TransactionManager=" + current);
192: }
193: return (TransactionManager) current;
194: }
195:
196: /**
197: * @see org.objectweb.transaction.jta.TMService#getTransactionManager()
198: */
199: public TransactionRecovery getTransactionRecovery() {
200: transactionrecovery = (TransactionRecovery) Current
201: .getTransactionRecovery();
202: if (TraceTm.jotm.isDebugEnabled()) {
203: TraceTm.jotm.debug("TransactionRecovery="
204: + transactionrecovery);
205: }
206: return transactionrecovery;
207: }
208:
209: /**
210: * @see org.objectweb.transaction.jta.TMService#getUserTransaction()
211: */
212: public UserTransaction getUserTransaction() {
213: if (TraceTm.jotm.isDebugEnabled()) {
214: TraceTm.jotm.debug("UserTransaction=" + current);
215: }
216: return (UserTransaction) current;
217: }
218:
219: /**
220: * @see org.objectweb.transaction.jta.TMService#stop()
221: */
222: public void stop() {
223: TraceTm.jotm.info("stop JOTM");
224: // remove current transaction if there's still one in ThreadLocal
225:
226: try {
227: current.forget();
228: } catch (Exception e) {
229: ;
230: }
231:
232: try {
233: transactionrecovery.forget();
234: } catch (Exception e) {
235: ;
236: }
237:
238: // unexport remote objects
239: if (local) {
240: if (bound && !boundFailed) {
241: try {
242: InitialContext ictx = new InitialContext();
243: ictx.unbind(TMFACTORY_NAME);
244:
245: if (TraceTm.jotm.isDebugEnabled()) {
246: TraceTm.jotm
247: .debug("TransactionFactory unbound");
248: }
249: } catch (Exception e) {
250: TraceTm.jotm.warn(
251: "an exception has prevented the TransactionFactory"
252: + " to be unbound", e);
253: }
254: }
255:
256: if (tf != null) {
257: try {
258: PortableRemoteObject.unexportObject(tf);
259:
260: if (TraceTm.jotm.isDebugEnabled()) {
261: TraceTm.jotm
262: .debug("TransactionFactory unexported");
263: }
264: } catch (NoSuchObjectException e) {
265: TraceTm.jotm.warn(
266: "an exception has prevented the TransactionFactory"
267: + " to be unbound", e);
268: }
269: }
270: }
271: tf = null;
272: }
273: }
|