KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#) TransactionFactoryImpl.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: TransactionFactoryImpl.java,v 1.12 2005/04/22 17:51:56 tonyortiz Exp $
41  * --------------------------------------------------------------------------
42  */

43 package org.objectweb.jotm;
44
45 import java.net.InetAddress JavaDoc;
46 import java.util.Vector JavaDoc;
47 import java.rmi.RemoteException JavaDoc;
48 import javax.transaction.xa.Xid JavaDoc;
49 import javax.rmi.PortableRemoteObject JavaDoc;
50
51 public class TransactionFactoryImpl
52     extends PortableRemoteObject JavaDoc
53     implements TransactionFactory {
54
55     /**
56      * @serial
57      */

58     int timeoutMax = 3600; // 1 hour
59

60     private Vector JavaDoc coordinatorList = new Vector JavaDoc();
61
62     /**
63      * Constructor of the Transaction Factory
64      */

65     public TransactionFactoryImpl() throws RemoteException JavaDoc {
66         if (TraceTm.jotm.isDebugEnabled()) {
67             TraceTm.jotm.debug("default constructor");
68         }
69     }
70
71     /**
72      * Create a new Control implementation on JTM.
73      *
74      * @return The Control object for the transaction
75      */

76     public synchronized Control create(int timeout) throws RemoteException JavaDoc {
77         if (TraceTm.jotm.isDebugEnabled()) {
78             TraceTm.jotm.debug("timeout=" + timeout);
79         }
80
81         ControlImpl ctrl = null;
82
83         // checks timeout value
84
if (timeout == 0 || timeout > timeoutMax)
85             timeout = timeoutMax;
86
87         // makes a new xid
88
// - should pass servername + ip addr. (LATER)
89
XidImpl xid = new XidImpl("TMServer", 0);
90
91         // Creates a new ControlImpl
92
try {
93             ctrl = new ControlImpl(timeout, xid, null);
94         } catch (Exception JavaDoc e) {
95             TraceTm.jotm.error("Cannot create ControlImpl", e);
96         }
97         return ctrl;
98     }
99
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) throws RemoteException JavaDoc {
108         if (TraceTm.jotm.isDebugEnabled()) {
109             TraceTm.jotm.debug("TransactionContext=" + ctx);
110         }
111
112         // Check if Control already exists
113
ControlImpl ctrl = null;
114
115         synchronized (coordinatorList) {
116             for (int i = 0; i < coordinatorList.size(); i++) {
117                 Coordinator coord = (Coordinator) coordinatorList.elementAt(i);
118                 if (coord.equals(ctx.getCoordinator())) {
119                     if (TraceTm.jotm.isDebugEnabled()) {
120                         TraceTm.jotm.debug("recreate: Control already in the list");
121                     }
122                     ctrl = (ControlImpl) coord;
123                     break;
124                 }
125             }
126         }
127         if (ctrl != null) {
128             if (TraceTm.jotm.isDebugEnabled()) {
129                 TraceTm.jotm.debug("recreate twice");
130             }
131             return ctrl;
132         }
133
134         // TODO: Build an xid with same gtrid and a new bqual
135
Xid xid = ctx.getXid();
136
137         // Creates a new ControlImpl and register it to the sup-coord.
138
// coordinator may be a JOnAS Coordinator or a org.omg.CosTransactions.Coordinator
139
try {
140             ctrl = new ControlImpl(ctx.getTimeout(), xid, ctx.getCoordinator());
141         } catch (Exception JavaDoc e) {
142             TraceTm.jotm.error("Cannot create ControlImpl", e);
143         }
144         return ctrl;
145     }
146
147     /**
148      * management method
149      * @return the port number
150      */

151     public int getPortNumber() throws RemoteException JavaDoc {
152         // no possibility with portable remote object
153
return 0;
154     }
155
156     /**
157      * management method
158      * @return the local host name
159      */

160     public String JavaDoc getHostName() throws RemoteException JavaDoc {
161         try {
162             return InetAddress.getLocalHost().getHostName();
163         } catch (Exception JavaDoc e) {
164             throw new RemoteException JavaDoc("" + e);
165         }
166     }
167 }
168
Popular Tags