KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webdav > connector > WebDAVXAResource


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/connector/src/java/org/apache/webdav/connector/WebDAVXAResource.java,v 1.3 2004/07/27 15:27:23 luetzkendorf Exp $
3  * $Revision: 1.3 $
4  * $Date: 2004/07/27 15:27:23 $
5  *
6  * ====================================================================
7  *
8  * Copyright 2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.webdav.connector;
25
26 import java.io.IOException JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28
29 import javax.transaction.xa.XAException JavaDoc;
30 import javax.transaction.xa.XAResource JavaDoc;
31 import javax.transaction.xa.Xid JavaDoc;
32
33 import org.apache.commons.transaction.util.LoggerFacade;
34 import org.apache.commons.transaction.util.PrintWriterLogger;
35 import org.apache.commons.transaction.util.xa.AbstractTransactionalResource;
36 import org.apache.commons.transaction.util.xa.AbstractXAResource;
37 import org.apache.commons.transaction.util.xa.TransactionalResource;
38 import org.apache.webdav.lib.WebdavResource;
39
40 /**
41  *
42  * @version $Revision: 1.3 $
43  *
44  */

45 public class WebDAVXAResource extends AbstractXAResource {
46
47     protected WebdavResource webdavResource;
48
49     protected String JavaDoc owner;
50
51     protected int timeout = 10;
52
53     protected LoggerFacade loggerFacade;
54
55     public WebDAVXAResource(WebdavResource webdavResource, String JavaDoc owner) {
56         this.webdavResource = webdavResource;
57         this.owner = owner;
58         // log important stuff to standard out as long as nothing else is configured
59
this.loggerFacade = new PrintWriterLogger(new PrintWriter JavaDoc(System.out), "WebDAVXAResource", false);
60     }
61
62     protected LoggerFacade getLoggerFacade() {
63         return loggerFacade;
64     }
65
66     protected void setLoggerFacade(PrintWriter JavaDoc out) {
67         loggerFacade = new PrintWriterLogger(out, "WebDAVXAResource", true);
68     }
69
70     public int getTransactionTimeout() throws XAException JavaDoc {
71         return timeout;
72     }
73
74     public boolean setTransactionTimeout(int seconds) throws XAException JavaDoc {
75         timeout = seconds;
76         return true;
77     }
78
79     public boolean isSameRM(XAResource JavaDoc xares) throws XAException JavaDoc {
80         return (xares != null && xares instanceof WebDAVXAResource && webdavResource
81                 .equals(((WebDAVXAResource) xares).webdavResource));
82     }
83
84     public Xid JavaDoc[] recover(int flag) throws XAException JavaDoc {
85         // FIXME no idea how to recover anything here
86
return null;
87     }
88
89     protected TransactionalResource createTransactionResource(Xid JavaDoc xid) throws Exception JavaDoc {
90         return new TransactionalWebDAVResource(xid, webdavResource, owner, timeout, getLoggerFacade());
91     }
92
93     protected boolean includeBranchInXid() {
94         return true;
95     }
96
97     protected static class TransactionalWebDAVResource extends AbstractTransactionalResource {
98
99         WebdavResource webdavResource;
100         LoggerFacade loggerFacade;
101         
102         public TransactionalWebDAVResource(Xid JavaDoc xid, WebdavResource webdavResource, String JavaDoc owner, int timeout, LoggerFacade loggerFacade) throws IOException JavaDoc {
103             super(xid);
104             this.webdavResource = webdavResource;
105             System.out.println("Statring "+webdavResource);
106             webdavResource.startTransaction(owner, timeout);
107             this.loggerFacade = loggerFacade;
108         }
109
110         public void commit() throws XAException JavaDoc {
111             try {
112                 webdavResource.commitTransaction();
113             } catch (IOException JavaDoc e) {
114                 loggerFacade.logWarning("Could not commit transaction", e);
115                 throw new XAException JavaDoc("Could not commit transaction");
116             }
117         }
118
119         public void rollback() throws XAException JavaDoc {
120             try {
121                 webdavResource.abortTransaction();
122             } catch (IOException JavaDoc e) {
123                 loggerFacade.logWarning("Could not roll back transaction", e);
124                 throw new XAException JavaDoc("Could not roll back transaction");
125             }
126         }
127
128         public int prepare() throws XAException JavaDoc {
129             return XA_OK;
130         }
131         
132         public void begin() throws XAException JavaDoc {
133         }
134
135         public void resume() throws XAException JavaDoc {
136         }
137         
138         public void suspend() throws XAException JavaDoc {
139         }
140     }
141 }
Popular Tags