001: /*
002: * @(#) TransactionFactoryImpl.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: TransactionFactoryImpl.java,v 1.12 2005/04/22 17:51:56 tonyortiz Exp $
041: * --------------------------------------------------------------------------
042: */
043: package org.objectweb.jotm;
044:
045: import java.net.InetAddress;
046: import java.util.Vector;
047: import java.rmi.RemoteException;
048: import javax.transaction.xa.Xid;
049: import javax.rmi.PortableRemoteObject;
050:
051: public class TransactionFactoryImpl extends PortableRemoteObject
052: implements TransactionFactory {
053:
054: /**
055: * @serial
056: */
057: int timeoutMax = 3600; // 1 hour
058:
059: private Vector coordinatorList = new Vector();
060:
061: /**
062: * Constructor of the Transaction Factory
063: */
064: public TransactionFactoryImpl() throws RemoteException {
065: if (TraceTm.jotm.isDebugEnabled()) {
066: TraceTm.jotm.debug("default constructor");
067: }
068: }
069:
070: /**
071: * Create a new Control implementation on JTM.
072: *
073: * @return The Control object for the transaction
074: */
075: public synchronized Control create(int timeout)
076: throws RemoteException {
077: if (TraceTm.jotm.isDebugEnabled()) {
078: TraceTm.jotm.debug("timeout=" + timeout);
079: }
080:
081: ControlImpl ctrl = null;
082:
083: // checks timeout value
084: if (timeout == 0 || timeout > timeoutMax)
085: timeout = timeoutMax;
086:
087: // makes a new xid
088: // - should pass servername + ip addr. (LATER)
089: XidImpl xid = new XidImpl("TMServer", 0);
090:
091: // Creates a new ControlImpl
092: try {
093: ctrl = new ControlImpl(timeout, xid, null);
094: } catch (Exception e) {
095: TraceTm.jotm.error("Cannot create ControlImpl", e);
096: }
097: return ctrl;
098: }
099:
100: /**
101: * Recreate locally a Control object for an existing transaction. It is
102: * possible to call recreate for a transaction already known. In this
103: * case, recreate simply returns the existing Control object.
104: *
105: * @return The Control object for the transaction
106: */
107: public synchronized Control recreate(TransactionContext ctx)
108: throws RemoteException {
109: if (TraceTm.jotm.isDebugEnabled()) {
110: TraceTm.jotm.debug("TransactionContext=" + ctx);
111: }
112:
113: // Check if Control already exists
114: ControlImpl ctrl = null;
115:
116: synchronized (coordinatorList) {
117: for (int i = 0; i < coordinatorList.size(); i++) {
118: Coordinator coord = (Coordinator) coordinatorList
119: .elementAt(i);
120: if (coord.equals(ctx.getCoordinator())) {
121: if (TraceTm.jotm.isDebugEnabled()) {
122: TraceTm.jotm
123: .debug("recreate: Control already in the list");
124: }
125: ctrl = (ControlImpl) coord;
126: break;
127: }
128: }
129: }
130: if (ctrl != null) {
131: if (TraceTm.jotm.isDebugEnabled()) {
132: TraceTm.jotm.debug("recreate twice");
133: }
134: return ctrl;
135: }
136:
137: // TODO: Build an xid with same gtrid and a new bqual
138: Xid xid = ctx.getXid();
139:
140: // Creates a new ControlImpl and register it to the sup-coord.
141: // coordinator may be a JOnAS Coordinator or a org.omg.CosTransactions.Coordinator
142: try {
143: ctrl = new ControlImpl(ctx.getTimeout(), xid, ctx
144: .getCoordinator());
145: } catch (Exception e) {
146: TraceTm.jotm.error("Cannot create ControlImpl", e);
147: }
148: return ctrl;
149: }
150:
151: /**
152: * management method
153: * @return the port number
154: */
155: public int getPortNumber() throws RemoteException {
156: // no possibility with portable remote object
157: return 0;
158: }
159:
160: /**
161: * management method
162: * @return the local host name
163: */
164: public String getHostName() throws RemoteException {
165: try {
166: return InetAddress.getLocalHost().getHostName();
167: } catch (Exception e) {
168: throw new RemoteException("" + e);
169: }
170: }
171: }
|