KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > transaction > memory > jca > MapXAResource


1 /*
2  * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//transaction/src/java/org/apache/commons/transaction/memory/jca/MapXAResource.java,v 1.1 2004/11/18 23:27:17 ozeigermann Exp $
3 <<<<<<< .mine
4  * $Revision: 1.1 $
5  * $Date: 2005-02-26 14:16:14 +0100 (Sa, 26 Feb 2005) $
6 =======
7  * $Revision$
8  * $Date: 2005-02-26 14:16:14 +0100 (Sa, 26 Feb 2005) $
9 >>>>>>> .r168169
10  *
11  * ====================================================================
12  *
13  * Copyright 1999-2002 The Apache Software Foundation
14  *
15  * Licensed under the Apache License, Version 2.0 (the "License");
16  * you may not use this file except in compliance with the License.
17  * You may obtain a copy of the License at
18  *
19  * http://www.apache.org/licenses/LICENSE-2.0
20  *
21  * Unless required by applicable law or agreed to in writing, software
22  * distributed under the License is distributed on an "AS IS" BASIS,
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24  * See the License for the specific language governing permissions and
25  * limitations under the License.
26  *
27  */

28
29 package org.apache.commons.transaction.memory.jca;
30
31 import java.io.PrintWriter JavaDoc;
32
33 import javax.transaction.xa.XAException JavaDoc;
34 import javax.transaction.xa.XAResource JavaDoc;
35 import javax.transaction.xa.Xid JavaDoc;
36
37 import org.apache.commons.transaction.memory.TransactionalMapWrapper;
38 import org.apache.commons.transaction.util.LoggerFacade;
39 import org.apache.commons.transaction.util.PrintWriterLogger;
40 import org.apache.commons.transaction.util.xa.AbstractTransactionalResource;
41 import org.apache.commons.transaction.util.xa.AbstractXAResource;
42 import org.apache.commons.transaction.util.xa.TransactionalResource;
43
44 /**
45  *
46  * @version $Revision$
47  *
48  */

49 public class MapXAResource extends AbstractXAResource {
50
51     TransactionalMapWrapper map;
52     LoggerFacade loggerFacade;
53
54     public MapXAResource(TransactionalMapWrapper map) {
55         this.map = map;
56         // log important stuff to standard out as long as nothing else is configured
57
this.loggerFacade = new PrintWriterLogger(new PrintWriter JavaDoc(System.out), "WebDAVXAResource", false);
58     }
59
60     public MapXAResource(TransactionalMapWrapper map, LoggerFacade loggerFacade) {
61         this.map = map;
62         this.loggerFacade = loggerFacade;
63     }
64
65     public int getTransactionTimeout() throws XAException JavaDoc {
66         return 0;
67     }
68
69     public boolean setTransactionTimeout(int seconds) throws XAException JavaDoc {
70         return false;
71     }
72
73     public boolean isSameRM(XAResource JavaDoc xares) throws XAException JavaDoc {
74         return (xares != null && xares instanceof MapXAResource && map.equals(((MapXAResource) xares).map));
75     }
76
77     public Xid JavaDoc[] recover(int flag) throws XAException JavaDoc {
78         return null;
79     }
80
81     public LoggerFacade getLoggerFacade() {
82         return loggerFacade;
83     }
84     
85     public void setLoggerFacade(LoggerFacade loggerFacade) {
86         this.loggerFacade = loggerFacade;
87     }
88
89     protected void setLoggerFacade(PrintWriter JavaDoc out) {
90         loggerFacade = new PrintWriterLogger(out, "WebDAVXAResource", true);
91     }
92
93     protected TransactionalResource createTransactionResource(Xid JavaDoc xid) throws Exception JavaDoc {
94         return new MapTransactionalResource(xid, map, getLoggerFacade());
95     }
96
97     protected boolean includeBranchInXid() {
98         return true;
99     }
100
101     protected static class MapTransactionalResource extends AbstractTransactionalResource {
102
103         TransactionalMapWrapper map;
104         private TransactionalMapWrapper.TxContext txContext = null;
105
106         LoggerFacade loggerFacade;
107
108         public MapTransactionalResource(Xid JavaDoc xid, TransactionalMapWrapper map, LoggerFacade loggerFacade) {
109             super(xid);
110             this.map = map;
111             this.loggerFacade = loggerFacade;
112         }
113
114         public void commit() throws XAException JavaDoc {
115             try {
116                 map.commitTransaction();
117             } catch (IllegalStateException JavaDoc e) {
118                 throw new XAException JavaDoc(e.toString());
119             }
120         }
121
122         public void rollback() throws XAException JavaDoc {
123             // resume if suspended, because the transactional map throws an
124
// exception if we call prepare on suspended txns
125
if (isSuspended())
126                 resume();
127
128             try {
129                 map.rollbackTransaction();
130             } catch (IllegalStateException JavaDoc e) {
131                 throw new XAException JavaDoc(e.toString());
132             }
133         }
134
135         public int prepare() throws XAException JavaDoc {
136             // resume if suspended, because the transactional map throws an
137
// exception if we call prepare on suspended txns
138
if (isSuspended())
139                 resume();
140
141             if (map.isTransactionMarkedForRollback()) {
142                 throw new XAException JavaDoc(XAException.XA_RBROLLBACK);
143             }
144
145             return (map.isReadOnly() ? XA_RDONLY : XA_OK);
146         }
147
148         public void suspend() throws XAException JavaDoc {
149             if (isSuspended()) {
150                 throw new XAException JavaDoc(XAException.XAER_PROTO);
151             }
152             this.txContext = map.suspendTransaction();
153         }
154         
155         public void resume() throws XAException JavaDoc {
156             if (!isSuspended()) {
157                 throw new XAException JavaDoc(XAException.XAER_PROTO);
158             }
159             map.resumeTransaction(this.txContext);
160             this.txContext = null;
161         }
162         
163         public void begin() throws XAException JavaDoc {
164             if (isSuspended()) {
165                 throw new XAException JavaDoc(XAException.XAER_PROTO);
166             }
167             this.map.startTransaction();
168         }
169
170         private boolean isSuspended() {
171             return this.txContext != null;
172         }
173     }
174 }
Popular Tags