KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/connector/src/java/org/apache/webdav/connector/WebDAVManagedConnection.java,v 1.2.2.1 2004/09/20 08:07:29 ozeigermann Exp $
3  * $Revision: 1.2.2.1 $
4  * $Date: 2004/09/20 08:07:29 $
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 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import javax.resource.ResourceException JavaDoc;
33 import javax.resource.spi.ConnectionEvent JavaDoc;
34 import javax.resource.spi.ConnectionEventListener JavaDoc;
35 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
36 import javax.resource.spi.LocalTransaction JavaDoc;
37 import javax.resource.spi.ManagedConnection JavaDoc;
38 import javax.resource.spi.ManagedConnectionMetaData JavaDoc;
39 import javax.security.auth.Subject JavaDoc;
40 import javax.transaction.xa.XAResource JavaDoc;
41
42 import org.apache.commons.httpclient.HttpException;
43 import org.apache.commons.httpclient.URIException;
44 import org.apache.webdav.lib.WebdavResource;
45
46 /**
47  *
48  * @version $Revision: 1.2.2.1 $
49  *
50  */

51 public class WebDAVManagedConnection implements ManagedConnection JavaDoc {
52
53     protected WebDAVXAResource xares = null;
54
55     protected WebDAVLocalTransaction tx = null;
56
57     protected String JavaDoc name = null;
58
59     protected WebdavResource webdavResource;
60
61     protected WebDAVConnectionSpec webDAVConnectionSpec;
62
63     protected WebDAVConnection connection = null;
64
65     protected List JavaDoc listeners = new ArrayList JavaDoc();
66
67     protected PrintWriter JavaDoc out;
68
69     public WebDAVManagedConnection(ConnectionRequestInfo JavaDoc cxRequestInfo) throws HttpException, IOException JavaDoc {
70         open((WebDAVConnectionSpec) cxRequestInfo);
71     }
72
73     public WebdavResource getWebdavResource() {
74         return webdavResource;
75     }
76
77     public void close() {
78         ConnectionEvent JavaDoc event = new ConnectionEvent JavaDoc(this, ConnectionEvent.CONNECTION_CLOSED);
79         event.setConnectionHandle(connection);
80         for (Iterator JavaDoc it = listeners.iterator(); it.hasNext();) {
81             ((ConnectionEventListener JavaDoc) it.next()).connectionClosed(event);
82         }
83     }
84
85     /**
86      * @see ManagedConnection#getConnection(Subject, ConnectionRequestInfo)
87      */

88     public Object JavaDoc getConnection(Subject JavaDoc subject, ConnectionRequestInfo JavaDoc cxRequestInfo) throws ResourceException JavaDoc {
89
90         if (connection == null) {
91             connection = new WebDAVConnection(this);
92         }
93         return connection;
94     }
95
96     /**
97      * @see ManagedConnection#destroy()
98      */

99     public void destroy() throws ResourceException JavaDoc {
100
101         if (connection != null) {
102             connection.invalidate();
103             connection = null;
104         }
105
106         listeners = null;
107         name = null;
108         xares = null;
109         tx = null;
110         try {
111             webdavResource.close();
112         } catch (IOException JavaDoc e) {
113             throw new ResourceException JavaDoc(e);
114         }
115     }
116
117     /**
118      * @see ManagedConnection#cleanup()
119      */

120     public void cleanup() throws ResourceException JavaDoc {
121         // XXX We should only reset internal state to put our
122
// physical connection back to the pool. As I have
123
// no idea how to recycle a WebdavResource a have to
124
// fully destroy it (Olli Z.)
125

126         if (connection != null) {
127             connection.invalidate();
128             connection = null;
129         }
130
131         name = null;
132         xares = null;
133         tx = null;
134         try {
135             webdavResource.close();
136         } catch (IOException JavaDoc e) {
137             throw new ResourceException JavaDoc(e);
138         }
139     }
140
141     /**
142      * @see ManagedConnection#associateConnection(Object)
143      */

144     public void associateConnection(Object JavaDoc connection) throws ResourceException JavaDoc {
145         if (!(connection instanceof WebDAVConnection)) {
146             throw new ResourceException JavaDoc("Connection is not of type WebDAVConnection");
147         }
148
149         this.connection = (WebDAVConnection) connection;
150         try {
151             open(this.connection.mc.webDAVConnectionSpec);
152         } catch (URIException e) {
153             throw new ResourceException JavaDoc("Could not associate connection", e);
154         } catch (IOException JavaDoc e) {
155             throw new ResourceException JavaDoc("Could not associate connection", e);
156         }
157         this.connection.mc = this;
158     }
159
160     /**
161      * @see ManagedConnection#addConnectionEventListener(ConnectionEventListener)
162      */

163     public void addConnectionEventListener(ConnectionEventListener JavaDoc listener) {
164
165         listeners.add(listener);
166     }
167
168     /**
169      * @see ManagedConnection#removeConnectionEventListener(ConnectionEventListener)
170      */

171     public void removeConnectionEventListener(ConnectionEventListener JavaDoc listener) {
172
173         listeners.remove(listener);
174     }
175
176     /**
177      * @see ManagedConnection#getXAResource()
178      */

179     public XAResource JavaDoc getXAResource() throws ResourceException JavaDoc {
180         return xares;
181     }
182
183     /**
184      * @see ManagedConnection#getLocalTransaction()
185      */

186     public LocalTransaction JavaDoc getLocalTransaction() throws ResourceException JavaDoc {
187         return tx;
188     }
189
190     /**
191      * @see ManagedConnection#getMetaData()
192      */

193     public ManagedConnectionMetaData JavaDoc getMetaData() throws ResourceException JavaDoc {
194
195         return null;
196     }
197
198     /**
199      * @see ManagedConnection#setLogWriter(PrintWriter)
200      */

201     public void setLogWriter(PrintWriter JavaDoc out) throws ResourceException JavaDoc {
202         this.out = out;
203         xares.setLoggerFacade(out);
204     }
205
206     /**
207      * @see ManagedConnection#getLogWriter()
208      */

209     public PrintWriter JavaDoc getLogWriter() throws ResourceException JavaDoc {
210
211         return out;
212     }
213
214     protected void open(WebDAVConnectionSpec webDAVConnectionSpec) throws IOException JavaDoc, URIException {
215         this.webDAVConnectionSpec = webDAVConnectionSpec;
216         System.out.println("Opening: "+webDAVConnectionSpec.getHttpURL()); // FIXME
217
webdavResource = new WebdavResource(webDAVConnectionSpec.getHttpURL());
218         System.out.println("Opened"); // FIXME
219
String JavaDoc owner = webDAVConnectionSpec.getHttpURL().getUser();
220         if (owner == null)
221             owner = "WebDAV Connector";
222
223         tx = new WebDAVLocalTransaction(webdavResource, owner, webDAVConnectionSpec.getTimeout());
224         xares = new WebDAVXAResource(webdavResource, owner);
225     }
226
227 }
Popular Tags