KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > webdav > fileSystem > LockManager


1 package com.ibm.webdav.fileSystem;
2
3 /*
4  * (C) Copyright IBM Corp. 2000 All rights reserved.
5  *
6  * The program is provided "AS IS" without any warranty express or
7  * implied, including the warranty of non-infringement and the implied
8  * warranties of merchantibility and fitness for a particular purpose.
9  * IBM will not be liable for any damages suffered by you as a result
10  * of using the Program. In no event will IBM be liable for any
11  * special, indirect or consequential damages or lost profits even if
12  * IBM has been advised of the possibility of their occurrence. IBM
13  * will not be liable for any third party claims against you.
14  *
15  * Portions Copyright (C) Simulacra Media Ltd, 2004.
16  */

17 import java.util.*;
18
19 import javax.xml.parsers.*;
20
21 import org.w3c.dom.*;
22
23 import com.ibm.webdav.*;
24 import com.ibm.webdav.impl.*;
25
26 /** Implement the LockManager interface using the resource's
27 * PropertiesManager to persist the DAV:lockdiscovery property.
28 * @author Jim Amsden <jamsden@us.ibm.com>
29 * @see PropertiesManager
30 * @see CachedPropertiesManager
31 */

32 public class LockManager implements com.ibm.webdav.impl.LockManager {
33     private ResourceImpl resource = null;
34     private com.ibm.webdav.impl.PropertiesManager propertiesManager = null;
35 public LockManager() {
36 }
37 /** Create a lock manager for the given resource.
38 * @param resource the resource to manage locks for
39 * @param namespaceManager its namespace manager
40 * @param propertiesManager and its properties manager
41 */

42 public LockManager(ResourceImpl resource, com.ibm.webdav.impl.NamespaceManager namespaceManager, com.ibm.webdav.impl.PropertiesManager propertiesManager) {
43     initialize(resource, namespaceManager, propertiesManager);
44 }
45 /** Get the DAV:lockdiscovery property for the resource.
46 *
47 * @return an Element with tag name D:lockdiscovery
48 * @exception com.ibm.webdav.WebDAVException
49 */

50 public Element getLockDiscovery() throws WebDAVException {
51     // get the raw properties from the property manager
52
Document propertiesDocument = propertiesManager.loadProperties();
53     Element properties = propertiesDocument.getDocumentElement();
54     Element lockdiscovery = (Element)((Element) properties).getElementsByTagNameNS("DAV:", "lockdiscovery").item(0);
55
56     // timeout any expired locks by
57
// deleting any locks that have timed out
58
boolean locksHaveChanged = false;
59     if (lockdiscovery != null) {
60         NodeList locks = ((Element) lockdiscovery).getElementsByTagNameNS("DAV:", "activelock");
61         Element lock = null;
62         Date now = new Date();
63         for (int i = 0; i < locks.getLength(); i++) {
64             lock = (Element) locks.item(i);
65             ActiveLock activeLock = new ActiveLock(lock);
66             // see if the lock has timed out
67
Date expiration = activeLock.getExpiration();
68             if (expiration != null && expiration.before(now)) {
69                 // has timed out, remove the lock
70
locksHaveChanged = true;
71                 try {
72                     lockdiscovery.removeChild(lock);
73                 } catch (Exception JavaDoc exc) {
74                     exc.printStackTrace();
75                 }
76             }
77         }
78     }
79     if (locksHaveChanged) {
80         propertiesManager.saveProperties(propertiesDocument);
81     }
82     return lockdiscovery;
83 }
84 /** Get the locks that exist on this resource. May be null if the resource
85 * is not locked. Get the locks from the DAV:lockdiscovery property
86 *
87 * @return a Vector of ActiveLock objects
88 * @exception com.ibm.webdav.WebDAVException
89 */

90 public Vector getLocks() throws WebDAVException {
91     Element lockdiscovery = (Element) getLockDiscovery();
92     Vector allLocks = new Vector();
93     if (lockdiscovery != null) {
94         NodeList activeLocks = ((Element) lockdiscovery).getElementsByTagNameNS("DAV:", "activelock");
95         Element activeLock = null;
96         for (int i = 0; i < activeLocks.getLength(); i++) {
97             activeLock = (Element) activeLocks.item(i);
98             allLocks.addElement(new ActiveLock(activeLock));
99         }
100     }
101     return allLocks;
102 }
103 /** Get information about locks supported by this resource.
104 * The file system supports shared and exclusive write locks.
105 *
106 * @return an Element with tag name D:supportedlock
107 */

108 public Element getSupportedLock() {
109     Document document = null;
110
111         try {
112           document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
113         } catch(Exception JavaDoc e) {
114           e.printStackTrace(System.err);
115         }
116
117     Element supportedlock = document.createElement("D:supportedlock");
118       Element lockentry = document.createElement("D:lockentry");
119       supportedlock.appendChild(lockentry);
120         Element lockscope = document.createElement("D:lockscope");
121           lockscope.appendChild(document.createElement("D:exclusive"));
122         lockentry.appendChild(lockscope);
123         Element locktype = document.createElement("D:locktype");
124           locktype.appendChild(document.createElement("D:write"));
125         lockentry.appendChild(locktype);
126       lockentry = document.createElement("D:lockentry");
127       supportedlock.appendChild(lockentry);
128         lockscope = document.createElement("D:lockscope");
129           lockscope.appendChild(document.createElement("D:shared"));
130         lockentry.appendChild(lockscope);
131         locktype = document.createElement("D:locktype");
132           locktype.appendChild(document.createElement("D:write"));
133         lockentry.appendChild(locktype);
134
135     return supportedlock;
136 }
137 /** Initialize this lock manager
138 * @param resource the resource to manage locks for
139 * @param namespaceManager its namespace manager
140 * @param propertiesManager and its properties manager
141 */

142 public void initialize(ResourceImpl resource, com.ibm.webdav.impl.NamespaceManager namespaceManager, com.ibm.webdav.impl.PropertiesManager propertiesManager) {
143     this.resource = resource;
144     this.propertiesManager = propertiesManager;
145 }
146 /** Lock this resource Using the given activeLock. Create the
147 * lock by adding a new active lock to the lock discovery property.
148 *
149 * @param activeLock the lock to activate (i.e., persist)
150 *
151 * @return a MultiStatus containing a lockdiscovery property indicating
152 * the results of the lock operation.
153 * @exception com.ibm.webdav.WebDAVException
154 */

155 public MultiStatus lock(ActiveLock activeLock) throws WebDAVException {
156         Element activeLockEl = activeLock.asXML();
157         Document xmlDoc = activeLockEl.getOwnerDocument();
158         MultiStatus result = new MultiStatus();
159     PropertyResponse propertyResponse = new PropertyResponse(resource.getURL().toString());
160     result.addResponse(propertyResponse);
161
162     PropertyValue l = resource.getProperty( PropertyName.pnLockdiscovery);
163     Element lockdiscovery = null;
164     if (l == null) {
165         lockdiscovery = xmlDoc.createElement("D:lockdiscovery");
166         lockdiscovery.setAttribute("xmlns:D", "DAV:");
167     } else {
168         lockdiscovery = (Element) ((Element) l.value).cloneNode(true);
169     }
170     lockdiscovery.appendChild(activeLockEl);
171     propertiesManager.setProperty("DAV:lockdiscovery", lockdiscovery);
172
173     // all lock methods return the lockdiscovery property, even if the lock failed
174
PropertyName propname = PropertyName.createPropertyNameQuietly( "DAV:lockdiscovery" );
175
176     propertyResponse.addProperty( propname
177         , (Element) ((Element) lockdiscovery).cloneNode(true)
178         , WebDAVStatus.SC_OK);
179     return result;
180 }
181 /** Refresh the lock on this resource by resetting the lock timeout.
182 * The context must contain the proper authorization for the requesting
183 * principal. Refresh the lock by updating the timeout element of the
184 * active lock in the lock discovery property.
185 *
186 * @param activeLock the lock to refresh. Contains the updated timeout
187 * and expiration date.
188 *
189 * @return updated information about the lock status of this resource
190 * @exception com.ibm.webdav.WebDAVException
191 */

192 public MultiStatus refreshLock(ActiveLock activeLock) throws WebDAVException {
193     Element activeLockEl = activeLock.asXML();
194         Document xmlDoc = activeLockEl.getOwnerDocument();
195         MultiStatus result = new MultiStatus();
196     PropertyResponse propertyResponse = new PropertyResponse(resource.getURL().toString());
197     result.addResponse(propertyResponse);
198
199     // get the locks on this resource
200
PropertyValue l = resource.getProperty( PropertyName.pnLockdiscovery );
201     Element lockdiscovery = null;
202     if (l == null) {
203         lockdiscovery = xmlDoc.createElement("D:lockdiscovery");
204         lockdiscovery.setAttribute("xmlns:D", "DAV:");
205     } else {
206         lockdiscovery = (Element) ((Element) l.value).cloneNode(true);
207     }
208
209     // find the lock
210
boolean lockFound = false;
211     NodeList locks = ((Element) lockdiscovery).getElementsByTagNameNS("DAV:", "activelock");
212     Element lock = null;
213     for (int i = 0; i < locks.getLength(); i++) {
214         lock = (Element) locks.item(i);
215         ActiveLock aLock = new ActiveLock(lock);
216         if (aLock.getLockToken().equals(activeLock.getLockToken())) {
217             lockFound = true;
218             lockdiscovery.removeChild(lock);
219             break;
220         }
221     }
222     if (!lockFound) {
223         throw new WebDAVException(WebDAVStatus.SC_PRECONDITION_FAILED, "principal does not own a lock");
224     }
225     lockdiscovery.appendChild(activeLockEl);
226     propertiesManager.setProperty("DAV:lockdiscovery", lockdiscovery);
227
228     PropertyName propname = PropertyName.createPropertyNameQuietly( "DAV:lockdiscovery" );
229     // all lock methods return the lockdiscovery property, even if the lock failed
230
propertyResponse.addProperty( propname, (Element) ((Element) lockdiscovery).cloneNode(true), WebDAVStatus.SC_OK);
231     return result;
232 }
233 /** Unlock the lock identified by the lockToken on this resource by
234 * removing the active lock from the lock discovery property.
235 *
236 * @param activeLock the lock to unlock
237 *
238 * @return a MultiStatus containing any responses on resources that could not
239 * be unlocked.
240 * @exception com.ibm.webdav.WebDAVException
241 */

242 public MultiStatus unlock(ActiveLock activeLock) throws WebDAVException {
243         Element activeLockEl = activeLock.asXML();
244         Document xmlDoc = activeLockEl.getOwnerDocument();
245     MultiStatus result = new MultiStatus();
246     PropertyResponse propertyResponse = new PropertyResponse(resource.getURL().toString());
247     result.addResponse(propertyResponse);
248
249     // get the locks on this resource
250
PropertyValue l = resource.getProperty( PropertyName.pnLockdiscovery );
251     Element lockdiscovery = null;
252     if (l == null) {
253         lockdiscovery = xmlDoc.createElement("D:lockdiscovery");
254         lockdiscovery.setAttribute("xmlns:D", "DAV:");
255     } else {
256         lockdiscovery = (Element) ((Element) l.value).cloneNode(true);
257     }
258
259     // find the lock
260
ActiveLock lockToRemove = null;
261     NodeList locks = ((Element) lockdiscovery).getElementsByTagNameNS("DAV:", "activelock");
262     Element lock = null;
263     for (int i = 0; i < locks.getLength(); i++) {
264         lock = (Element) locks.item(i);
265         ActiveLock aLock = new ActiveLock(lock);
266         if (aLock.getLockToken().equals(activeLock.getLockToken())) {
267             lockToRemove = aLock;
268             lockdiscovery.removeChild(lock);
269             break;
270         }
271     }
272     if (lockToRemove == null) {
273         throw new WebDAVException(WebDAVStatus.SC_PRECONDITION_FAILED, "resource is not locked with the given lock token");
274     } else {
275         propertiesManager.setProperty("DAV:lockdiscovery", lockdiscovery);
276     }
277
278     // all lock methods return the lockdiscovery property, even if the lock failed
279
PropertyName propname = PropertyName.createPropertyNameQuietly( "DAV:lockdiscovery" );
280     propertyResponse.addProperty( propname, (Element) ((Element) lockdiscovery).cloneNode(true), WebDAVStatus.SC_OK);
281     return result;
282 }
283 }
284
Popular Tags