KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > webdav > ActiveLock


1 package com.ibm.webdav;
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
18 import java.util.*;
19 import java.util.logging.*;
20
21 import javax.xml.parsers.*;
22
23 import org.w3c.dom.*;
24
25 /**
26  * ActiveLock contains information about locks on this resourece. The
27  * information can be used to provide details when obtaining locks, or for
28  * holding results from lock queries. It combines the activelock DAV element
29  * with the principal, and the lock expiration date.
30  *
31  * @author Jim Amsden <jamsden@us.ibm.com>
32  */

33 public class ActiveLock extends Object JavaDoc {
34
35     /**
36      * Only one exclusive lock can be granted at any time on a resource.
37      *
38      */

39     public static final String JavaDoc exclusive = "exclusive";
40
41     /**
42      * A resource may have many concurrent shared locks which indicate an
43      * intention to change the resource in some way. It is the responsibilty of
44      * the shared lock owners to coordinate their updates appropriately through
45      * other means.
46      */

47     public static final String JavaDoc shared = "shared";
48
49     /**
50      * Write locks allow a resource to be updated or deleted.
51      *
52      */

53     public static final String JavaDoc writeLock = "write";
54
55     //---------------------------------------------------------------------------------
56

57     // headers
58
private String JavaDoc timeout = "Infinite";
59
60     private String JavaDoc depth = Collection.deep;
61
62     // from the request entity
63
private String JavaDoc scope = ActiveLock.exclusive;
64
65     private String JavaDoc lockType = ActiveLock.writeLock;
66
67     private Element owner = null;
68
69     private String JavaDoc sOwner = null;
70
71     // from the response entity
72
private String JavaDoc lockToken = null;
73
74     // useful extensions:
75
private Date expiration = null; // when the lock will expire
76

77     private String JavaDoc principal = null; // the userid of the lock owner
78

79     /**
80      * Logger for this class
81      */

82     private static final Logger m_logger = Logger.getLogger(ActiveLock.class.getName());
83
84     /**
85      * The default constructor.
86      *
87      */

88     public ActiveLock() {
89     }
90
91     /**
92      * Convert an activelock Element into a ActiveLock instance for convenient
93      * access to the lock information. This method uses two IBM extensions to
94      * the WebDAV activelock element, one for the principal, and onother for the
95      * lock expiration time.
96      *
97      * @param an
98      * activelock Element containing information about the lock
99      * @exception com.ibm.webdav.WebDAVException
100      */

101     public ActiveLock(Element activeLock) throws WebDAVException {
102
103         Element lockScope = (Element) activeLock.getElementsByTagNameNS("DAV:",
104                 "lockscope").item(0);
105         setScope(((Element) lockScope.getFirstChild()).getLocalName());
106
107         Element lockType = (Element) activeLock.getElementsByTagNameNS("DAV:",
108                 "locktype").item(0);
109         setLockType(((Element) lockType.getFirstChild()).getLocalName());
110
111         Element depth = (Element) activeLock.getElementsByTagNameNS("DAV:",
112                 "depth").item(0);
113         setDepth(((Text) depth.getFirstChild()).getData());
114
115         Element owner = (Element) activeLock.getElementsByTagNameNS("DAV:",
116                 "owner").item(0);
117         if (owner != null) {
118             setOwner(owner);
119         }
120         Element timeout = (Element) activeLock.getElementsByTagNameNS("DAV:",
121                 "timeout").item(0);
122         if (timeout != null) {
123             this.timeout = ((Text) timeout.getFirstChild()).getData();
124         }
125
126         // TODO: There may be many locktokens all identifying the same lock
127
Element lockToken = (Element) activeLock.getElementsByTagNameNS("DAV:",
128                 "locktoken").item(0);
129         if (lockToken != null) {
130             Element href = (Element) lockToken.getElementsByTagNameNS("DAV:",
131                     "href").item(0);
132             if (href != null) {
133                 this.lockToken = ((Text) href.getFirstChild()).getData();
134             }
135         }
136
137         // IBM extensions:
138

139         Element principal = (Element) activeLock.getElementsByTagNameNS("DAV:",
140                 "principal").item(0);
141         if (principal != null) {
142             setPrincipal(((Text) principal.getFirstChild()).getData());
143         }
144
145         // Element principal =
146
// (Element)activeLock.getElementsByTagName("principal").item(0);
147
// if (principal != null) {
148
// setPrincipal(((Text) principal.getFirstChild()).getData());
149
// }
150

151         // Element expiration =
152
// (Element)activeLock.getElementsByTagName("expiration").item(0);
153
// if (expiration != null) {
154
// Date d = null;
155
// try {
156
// DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
157
// d = df.parse(((Text) expiration.getFirstChild()).getData());
158
// } catch (ParseException exc) {
159
// System.err.print("Unable to parse lock expiration date: " + exc);
160
// }
161
// setExpiration(d);
162
// }
163
}
164
165     /**
166      * Translate this ActiveLock instance into an activelock XML element. The
167      * activelock element will include two IBM extensions, one for the principal
168      * owning the lock (the authorization id), and another containing the
169      * expiration date for the lock.
170      *
171      * @return the DOM representation of an activelock XML element
172      */

173     public Element asXML() {
174         String JavaDoc sPrefix = "D";
175         // the document is only used create elements
176
Document document = null;
177
178         try {
179             document = DocumentBuilderFactory.newInstance()
180                     .newDocumentBuilder().newDocument();
181         } catch (Exception JavaDoc e) {
182             m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
183         }
184         //document.setVersion(Resource.XMLVersion);
185
//document.setEncoding(Resource.defaultXMLEncoding);
186

187         Element activelock = document.createElementNS("DAV:", "D:activelock");
188
189         activelock.setAttribute("xmlns:D", "DAV:");
190
191         Element lockscope = document.createElementNS("DAV:", "D:lockscope");
192
193         Element scopeEl = document.createElementNS("DAV:", "D:" + this.scope);
194
195         lockscope.appendChild(scopeEl);
196         activelock.appendChild(lockscope);
197
198         Element locktype = document.createElementNS("DAV:", "D:locktype");
199
200         Element locktypeEl = document.createElementNS("DAV:", "D:"
201                 + this.lockType);
202
203         locktype.appendChild(locktypeEl);
204         activelock.appendChild(locktype);
205
206         Element depth = document.createElementNS("DAV:", "D:depth");
207
208         depth.appendChild(document.createTextNode(this.depth));
209         activelock.appendChild(depth);
210
211         if (this.owner != null) {
212             if (owner.getPrefix() == null
213                     || owner.getPrefix().equals("D") == false) {
214
215                 Element ownerEl = document.createElementNS("DAV:", "D:owner");
216                 Element hrefEl = document.createElementNS("DAV:", "D:href");
217
218                 String JavaDoc sOwnerVal = owner.getChildNodes().item(0)
219                         .getChildNodes().item(0).getNodeValue();
220
221                 Text txtOwnerVal = document.createTextNode(sOwnerVal);
222
223                 hrefEl.appendChild(txtOwnerVal);
224
225                 ownerEl.appendChild(hrefEl);
226                 activelock.appendChild(ownerEl);
227                 owner = ownerEl;
228             } else {
229                 activelock.appendChild(document.importNode(owner, true));
230             }
231         } else if (sOwner != null) {
232             Element ownerEl = document.createElementNS("DAV:", "D:owner");
233             Element hrefEl = document.createElementNS("DAV:", "D:href");
234
235             hrefEl.appendChild(document.createTextNode(sOwner));
236             ownerEl.appendChild(hrefEl);
237             activelock.appendChild(ownerEl);
238         }
239
240         Element timeout = document.createElementNS("DAV:", "D:timeout");
241
242         timeout.appendChild(document.createTextNode(this.timeout));
243         activelock.appendChild(timeout);
244
245         if (this.lockToken != null) {
246             Element locktoken = document.createElementNS("DAV:", "D:locktoken");
247
248             Element href = document.createElementNS("DAV:", "D:href");
249
250             href.appendChild(document.createTextNode(this.lockToken));
251             locktoken.appendChild(href);
252             activelock.appendChild(locktoken);
253         }
254
255         if (getPrincipal() != null) {
256             Element principal = (Element) document.createElementNS("DAV:",
257                     "D:principal");
258             principal.appendChild(document.createTextNode(getPrincipal()));
259             activelock.appendChild(principal);
260         }
261
262         // if (getPrincipal() != null) {
263
// Element principal = (Element) document.createElement("principal");
264
// principal.appendChild(document.createTextNode(getPrincipal()));
265
// activelock.appendChild(principal);
266
// }
267

268         // if (getExpiration() != null) {
269
// Element expiration = (Element) document.createElement("expiration");
270
// expiration.appendChild(document.createTextNode(getExpiration().toString()));
271
// activelock.appendChild(expiration);
272
// }
273

274         return activelock;
275     }
276
277     /**
278      * Get the depth of the lock.
279      * <ul>
280      * <li>shallow: only this resource is locked or will be locked</li>
281      * <li>deep: this resource and recursively, all its internal members are
282      * locked</li>
283      * </ul>
284      *
285      * @return shallow or deep
286      */

287     public String JavaDoc getDepth() {
288         return depth;
289     }
290
291     /**
292      * Get the date and time when the lock will timeout. Null means it will
293      * never timeout. This is an IBM EXTENSION.
294      *
295      * @return the expiration date for the lock
296      */

297     public Date getExpiration() {
298         return expiration;
299     }
300
301     /**
302      * Get the lock token. A lock token represents the lock, and is used to
303      * unlock the resource or for any access that might change the resource
304      * state. There may be many (shared) locks on a resource, each with its own
305      * lock token. Each lock will have its own ActiveLock instance describing
306      * the lock and providing access to the lock token. See the lockdiscovery
307      * DAV property.
308      *
309      * @return the lock token identifying a lock on this resource.
310      */

311     public String JavaDoc getLockToken() {
312         return lockToken;
313     }
314
315     /**
316      * Get the type of the lock.
317      *
318      * @return write (other lock types may be supported in the future)
319      */

320     public String JavaDoc getLockType() {
321         return lockType;
322     }
323
324     /**
325      * Get the owner of the lock. The method provides information about the
326      * principal taking out the lock, but not necessarily the principal's
327      * authorization ID.
328      *
329      * @return any information that might identify the principal taking out the
330      * lock.
331      */

332     public Element getOwner() {
333         return owner;
334     }
335
336     /**
337      * Get the user authorization id of the owner of the lock. This is an IBM
338      * EXTENSION to the WebDAV activelock.
339      *
340      * @return the principal owning this lock as given in the Authorization
341      * context on lock
342      */

343     public String JavaDoc getPrincipal() {
344         return principal;
345     }
346
347     /**
348      * Get the scope of the lock.
349      *
350      * @return exclusive or shared
351      */

352     public String JavaDoc getScope() {
353         return scope;
354     }
355
356     /**
357      * Get the lock timeout.
358      *
359      * @return the lock timeout as either Second-n or "Infinite" for no timeout.
360      */

361     public String JavaDoc getTimeout() {
362         return timeout;
363     }
364
365     /**
366      * Get the time remaining before the lock times out
367      *
368      * @return the number of seconds that must elapse before the lock times out.
369      * 0 means the lock has timed out.
370      */

371     public long getTimeRemaining() {
372         long now = new Date().getTime();
373         long t = expiration.getTime() - now;
374         if (t < 0) {
375             t = 0;
376         }
377         return t;
378     }
379
380     /**
381      * Set the depth of the lock.
382      * <ul>
383      * <li>shallow: only this resource is locked or will be locked</li>
384      * <li>deep: this resource and recursively, all its internal members are
385      * locked</li>
386      * </ul>
387      *
388      * @param depth
389      * shallow or deep
390      * @exception com.ibm.webdav.ClientException
391      * thrown if the depth is incorrect
392      */

393     public void setDepth(String JavaDoc depth) throws ClientException {
394         if (!(depth.equals(Collection.shallow) || depth.equals(Collection.deep))) {
395             throw new ClientException(400, "invalid lock depth");
396         }
397         this.depth = depth;
398     }
399
400     /**
401      * Set the date and time when the lock will timeout. Null means it will
402      * never timeout. This is an IBM EXTENSION.
403      */

404     public void setExpiration(Date value) {
405         expiration = value;
406     }
407
408     /**
409      * Set the lock token.
410      *
411      * @param lockToken
412      * the lock token corresponding to a lock on a resource.
413      */

414     public void setLockToken(String JavaDoc lockToken) {
415         this.lockToken = lockToken;
416     }
417
418     /**
419      * Set the type of the lock.
420      *
421      * @param lockType
422      * write (other lock types may be supported in the future)
423      * @exception com.ibm.webdav.ClientException
424      * thrown if the lockType is incorrect. Currently, only write
425      * locks are supported.
426      */

427     public void setLockType(String JavaDoc lockType) throws ClientException {
428         if (!(lockType.equals(writeLock))) {
429             throw new ClientException(400, "invalid lock type: " + lockType);
430         }
431         this.lockType = lockType;
432     }
433
434     /**
435      * Set the owner of the lock. The method sets the information about the
436      * principal taking out the lock. This is not necessarily the authorization
437      * id of the principal owning the lock, and therefore cannot be relied upon
438      * for authentication.
439      *
440      * @param owner
441      * any information that might identify the principal taking out
442      * the lock.
443      */

444     public void setOwner(Element owner) {
445         this.owner = owner;
446     }
447
448     public void setOwner(String JavaDoc sOwner) {
449         this.sOwner = sOwner;
450     }
451
452     /**
453      * Set the user authorization id of the owner of the lock. This is and IBM
454      * EXTENSION.
455      */

456     public void setPrincipal(String JavaDoc value) {
457         principal = value;
458     }
459
460     /**
461      * Set the scope of the lock.
462      *
463      * @param scope
464      * exclusive or shared
465      * @exception com.ibm.webdav.ClientException
466      * thrown if the scope is invalid
467      */

468     public void setScope(String JavaDoc scope) throws ClientException {
469         if (!(scope.equals(exclusive) || scope.equals(shared))) {
470             throw new ClientException(400, "invalid lock scope: " + scope);
471         }
472         this.scope = scope;
473     }
474
475     /**
476      * Set the lock timeout.
477      *
478      * @param timeout
479      * the lock timeout in seconds. -1 means infinite timeout.
480      */

481     public void setTimeout(int timeout) {
482         if (timeout < 0) {
483             setTimeout("Infinite");
484         } else {
485             setTimeout("Second-" + new Integer JavaDoc(timeout).toString());
486         }
487     }
488
489     /**
490      * Set the lock timeout.
491      *
492      * @param timeout
493      * the lock timeout as either Second-n or "Infinite" for no
494      * timeout. Any other syntax is ignored.
495      */

496     public void setTimeout(String JavaDoc timeout) {
497         this.timeout = timeout;
498         if (timeout.equals("Infinite")) {
499             expiration = null;
500         } else {
501             if (timeout.startsWith("Second-")) {
502                 long t = new Long JavaDoc(timeout.substring(7)).longValue();
503                 expiration = new Date(new Date().getTime() + t * 1000);
504             }
505         }
506     }
507
508     /**
509      * Convert this ActiveLock to a String representation (an activelock XML
510      * element).
511      *
512      * @return a String representation of the ActiveLock as an activelock
513      * element
514      *
515      */

516     public String JavaDoc toString() {
517         /*
518          * ByteArrayOutputStream os = new ByteArrayOutputStream(); PrintWriter
519          * pout = new PrintWriter(os); Element activelock = (Element) asXML();
520          * try { activelock.print(pout); } catch (Exception exc) { }
521          * pout.close(); return os.toString();
522          */

523         return XMLUtility.printNode(asXML());
524     }
525 }
Popular Tags