KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > perseus > connector > ra > fos > FosXAResourceFactory


1 /**
2  * perseus/connector: this is an implementation of some JCA-related technologies
3  * (resource adapters and managers) for the ObjectWeb consortium.
4  * Copyright (C) 2001-2002 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: pascal.dechamboux@rd.francetelecom.com
21  *
22  */

23
24 package org.objectweb.perseus.connector.ra.fos;
25
26 import org.objectweb.perseus.fos.api.FosException;
27 import org.objectweb.perseus.fos.api.FosLoggerFactory;
28 import org.objectweb.perseus.fos.api.FosManager;
29 import org.objectweb.perseus.fos.api.FosTransaction;
30 import org.objectweb.perseus.fos.lib.FosTxContext;
31 import org.objectweb.util.monolog.api.BasicLevel;
32 import org.objectweb.util.monolog.api.Logger;
33
34 import java.util.HashMap JavaDoc;
35 import javax.transaction.xa.Xid JavaDoc;
36
37 /**
38  * @author S. Chassande-Barrioz, P. Dechamboux
39  */

40 public class FosXAResourceFactory {
41     /**
42      * The logger into which traces about FosXAResourceFactory are produced.
43      */

44     private Logger logger;
45     /**
46      * The logger into which traces about FosXAResource are produced.
47      */

48     private Logger entityLogger;
49     /**
50      * The factory for creating new FosTransaction.
51      */

52     private FosManager txContextFactory;
53     /**
54      * The hashed structure that stores FosTransaction that have been associated
55      * with a particular XID.
56      */

57     private HashMap JavaDoc txContexts = new HashMap JavaDoc();
58
59     /**
60      * Delegates recovery to the FOS manager.
61      */

62     Xid JavaDoc[] getXidForRecovery() {
63         return txContextFactory.getXidForRecovery();
64     }
65
66     /**
67      * Constructs a FosXAResourceFactory.
68      * @param flf The logger factory in order to allocate relevant loggers for
69      * XAResource-related entities.
70      */

71     FosXAResourceFactory(FosLoggerFactory flf, FosManager fm) {
72         logger = flf.getLogger(FosLoggerFactory.XARESOURCE, true);
73         entityLogger = flf.getLogger(FosLoggerFactory.XARESOURCE, false);
74         if (FosLoggerFactory.DEBUG)
75             logger.log(BasicLevel.DEBUG,
76                        "Constructs a new FosXAResourceFactory");
77         txContextFactory = fm;
78     }
79
80     /**
81      * Creates a new XAResource for coordinating transaction in a potentially
82      * multi-RM environment.
83      */

84     FosXAResource createXAResource() throws FosException {
85         FosXAResource res = new FosXAResource(entityLogger, this);
86         if (FosLoggerFactory.DEBUG)
87             logger.log(BasicLevel.DEBUG, "Creates a XAResource: " + res);
88         return res;
89     }
90
91     /**
92      * Releases a XAResource that is not used any more.
93      * @param xar The released XAResource.
94      */

95     void releaseXAResource(FosXAResource xar) {
96         if (FosLoggerFactory.DEBUG)
97             logger.log(BasicLevel.DEBUG, "Releases a XAResource: " + xar);
98     }
99
100     /**
101      * Looks for a FosTxContext associated with the particular transaction.
102      * @param xid The DTP transaction identifier.
103      */

104     FosTxContext getTxContext(Xid JavaDoc xid) {
105         if (FosLoggerFactory.DEBUG)
106             logger.log(BasicLevel.DEBUG, "Looks for the TxContext associated with XID: " + xid);
107         FosTxContext txc = (FosTxContext) txContexts.get(xid);
108         if (FosLoggerFactory.DEBUG)
109             logger.log(BasicLevel.DEBUG, "Found: " + txc);
110         return txc;
111     }
112
113     /**
114      * Creates a FosTxContext and associates it with the given DTP transaction.
115      * @param xid The DTP transaction identifier.
116      */

117     FosTxContext createTxContext(Xid JavaDoc xid) throws FosException {
118         if (FosLoggerFactory.DEBUG)
119             logger.log(BasicLevel.DEBUG, "Creates a FosTxContext to be associated with XID: " + xid);
120         FosTxContext txc = (FosTxContext) txContextFactory.createTxContext();
121         txc.begin(xid);
122         txContexts.put(xid, txc);
123         if (FosLoggerFactory.DEBUG)
124             logger.log(BasicLevel.DEBUG, "New one is: " + txc);
125         return txc;
126     }
127
128     /**
129      * Releases a DTP transaction context and its related resources.
130      * @param xid The DTP transaction identifier.
131      */

132     void releaseTxContext(Xid JavaDoc xid) throws FosException {
133         if (FosLoggerFactory.DEBUG)
134             logger.log(BasicLevel.DEBUG, "Dissociates the FosTxContext associated with XID: " + xid);
135         FosTxContext txc = getTxContext(xid);
136         if (FosLoggerFactory.DEBUG)
137             logger.log(BasicLevel.DEBUG, "Releases FosTxContext: " + txc);
138         if (txc == null) {
139             logger.log(BasicLevel.WARN, "Try to release unknow XID context: " + xid);
140             return;
141         }
142         txContexts.remove(xid);
143         txContextFactory.releaseTxContext(txc);
144     }
145 }
146
Popular Tags