KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > lock > NodeLock


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/lock/NodeLock.java,v 1.18 2004/08/03 10:27:58 ozeigermann Exp $
3  * $Revision: 1.18 $
4  * $Date: 2004/08/03 10:27:58 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 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.slide.lock;
25
26 import java.util.Date JavaDoc;
27
28 import org.apache.commons.codec.digest.DigestUtils;
29 import org.apache.slide.common.ObjectValidationFailedException;
30 import org.apache.slide.structure.ActionNode;
31 import org.apache.slide.structure.ObjectNode;
32 import org.apache.slide.structure.SubjectNode;
33 import org.apache.slide.util.Messages;
34
35 /**
36  * NodeLock class.
37  *
38  * @version $Revision: 1.18 $
39  */

40 public final class NodeLock implements Cloneable JavaDoc, java.io.Serializable JavaDoc {
41     public final static int SHARED = 0;
42
43     public final static int EXCLUSIVE = 1;
44
45     /*
46      * Indicates that this is a transaction related lock
47      */

48     public final static int LOCAL = 2;
49
50     /**
51      * Locked object.
52      */

53     protected String JavaDoc objectUri;
54
55     /**
56      * User who is the lock owner.
57      */

58     protected String JavaDoc subjectUri;
59
60     /**
61      * Lock type.
62      */

63     protected String JavaDoc typeUri;
64
65     /**
66      * Expiration date of the lock.
67      */

68     protected Date JavaDoc expirationDate;
69
70     /**
71      * Lock is inheritable.
72      */

73     protected boolean inheritance;
74
75     /**
76      * Lock scope.
77      */

78     protected int scope;
79
80     /**
81      * Lock Id.
82      */

83     protected String JavaDoc lockId;
84
85     /**
86      * Contacting information about the lock owner
87      */

88     protected String JavaDoc ownerInfo;
89
90     /**
91      * Constructor.
92      */

93     public NodeLock() {
94     }
95
96     /**
97      * Constructor.
98      *
99      * @param locked
100      * Locked object
101      * @param user
102      * Lock owner
103      * @param lockType
104      * Type of the lock
105      * @param expirationDate
106      * Date of expiration of the lock
107      * @param inheritance
108      * True if lock is inheritable
109      */

110     public NodeLock(ObjectNode locked, SubjectNode user, ActionNode lockType,
111             Date JavaDoc expirationDate, boolean inheritance) {
112         this(locked.getUri(), user.getUri(), lockType.getUri(), expirationDate,
113                 inheritance);
114     }
115
116     /**
117      * Constructor.
118      *
119      * @param locked
120      * Locked object
121      * @param user
122      * Lock owner
123      * @param lockType
124      * Type of the lock
125      * @param expirationDate
126      * Date of expiration of the lock
127      * @param inheritance
128      * True if lock is inheritable
129      */

130     public NodeLock(ObjectNode locked, SubjectNode user, ActionNode lockType,
131             Date JavaDoc expirationDate, boolean inheritance, boolean exclusive) {
132         this(locked.getUri(), user.getUri(), lockType.getUri(), expirationDate,
133                 inheritance, exclusive);
134     }
135
136     /**
137      * Constructor.
138      *
139      * @param locked
140      * Locked object
141      * @param user
142      * Lock owner
143      * @param lockType
144      * Type of the lock
145      * @param expirationDate
146      * Date of expiration of the lock
147      * @param inheritance
148      * True if lock is inheritable
149      */

150     public NodeLock(ObjectNode locked, SubjectNode user, ActionNode lockType,
151             Date JavaDoc expirationDate, boolean inheritance, boolean exclusive,
152             String JavaDoc ownerInfo) {
153         this(locked.getUri(), user.getUri(), lockType.getUri(), expirationDate,
154                 inheritance, exclusive, ownerInfo);
155     }
156
157     /**
158      * Constructor.
159      *
160      * @param objectUri
161      * Locked object uri
162      * @param subjectUri
163      * Lock owner uri
164      * @param typeUri
165      * Lock type uri
166      * @param expirationDate
167      * Date of expiration of the lock
168      * @param inheritance
169      * True if lock is inheritable
170      */

171     public NodeLock(String JavaDoc objectUri, String JavaDoc subjectUri, String JavaDoc typeUri,
172             Date JavaDoc expirationDate, boolean inheritance) {
173         this(objectUri, subjectUri, typeUri, expirationDate, inheritance, true);
174     }
175
176     /**
177      * Constructor.
178      *
179      * @param objectUri
180      * Locked object uri
181      * @param subjectUri
182      * Lock owner uri
183      * @param typeUri
184      * Lock type uri
185      * @param expirationDate
186      * Date of expiration of the lock
187      * @param inheritance
188      * True if lock is inheritable
189      */

190     public NodeLock(String JavaDoc objectUri, String JavaDoc subjectUri, String JavaDoc typeUri,
191             Date JavaDoc expirationDate, boolean inheritance, boolean exclusive) {
192         this(objectUri, subjectUri, typeUri, expirationDate, inheritance,
193                 exclusive, null);
194     }
195
196     /**
197      * Constructor.
198      *
199      * @param objectUri
200      * Locked object uri
201      * @param subjectUri
202      * Lock owner uri
203      * @param typeUri
204      * Lock type uri
205      * @param expirationDate
206      * Date of expiration of the lock
207      * @param inheritance
208      * True if lock is inheritable
209      * @param exclusive
210      * True if lock is exclusive
211      * @param ownerInfo
212      * contacting info about the lock owner (phone number, email)
213      */

214     public NodeLock(String JavaDoc objectUri, String JavaDoc subjectUri, String JavaDoc typeUri,
215             Date JavaDoc expirationDate, boolean inheritance, boolean exclusive,
216             String JavaDoc ownerInfo) {
217         this(generateLockID(objectUri.hashCode(), subjectUri.hashCode(),
218                 typeUri.hashCode(), (expirationDate == null) ? 0
219                         : expirationDate.getTime()), objectUri, subjectUri,
220                 typeUri, expirationDate, inheritance, exclusive, ownerInfo);
221     }
222
223     /**
224      * Constructor. Creates a new lock linked to another given lock.
225      *
226      * @param lock
227      * Linked lock
228      */

229     public NodeLock(NodeLock lock, String JavaDoc typeUri) {
230         this(lock.getLockId(), lock.getObjectUri(), lock.getSubjectUri(),
231                 typeUri, lock.getExpirationDate(), lock.isInheritable(), lock
232                         .isExclusive(), lock.getOwnerInfo());
233     }
234
235     /**
236      * Constructor.
237      *
238      * @param objectUri
239      * Locked object uri
240      * @param subjectUri
241      * Lock owner uri
242      * @param typeUri
243      * Lock type uri
244      * @param expirationDate
245      * Date of expiration of the lock
246      * @param inheritance
247      * True if lock is inheritable
248      */

249     public NodeLock(String JavaDoc lockId, String JavaDoc objectUri, String JavaDoc subjectUri,
250             String JavaDoc typeUri, Date JavaDoc expirationDate, boolean inheritance,
251             boolean exclusive) {
252         this(lockId, objectUri, subjectUri, typeUri, expirationDate,
253                 inheritance, exclusive, null);
254     }
255
256     /**
257      * Constructor.
258      *
259      * @param objectUri
260      * Locked object uri
261      * @param subjectUri
262      * Lock owner uri
263      * @param typeUri
264      * Lock type uri
265      * @param expirationDate
266      * Date of expiration of the lock
267      * @param inheritance
268      * True if lock is inheritable
269      * @param exclusive
270      * True if lock is exclusive
271      * @param ownerInfo
272      * contacting info about the lock owner (phone number, email)
273      */

274     public NodeLock(String JavaDoc lockId, String JavaDoc objectUri, String JavaDoc subjectUri,
275             String JavaDoc typeUri, Date JavaDoc expirationDate, boolean inheritance,
276             boolean exclusive, String JavaDoc ownerInfo) {
277         this(lockId, objectUri, subjectUri, typeUri, expirationDate,inheritance, exclusive ? EXCLUSIVE : SHARED, ownerInfo);
278     }
279
280     public NodeLock(String JavaDoc objectUri, String JavaDoc subjectUri, String JavaDoc typeUri,
281             Date JavaDoc expirationDate, boolean inheritance, int scope,
282             String JavaDoc ownerInfo) {
283         this(generateLockID(objectUri.hashCode(), subjectUri.hashCode(),
284                 typeUri.hashCode(), (expirationDate == null) ? 0
285                         : expirationDate.getTime()), objectUri, subjectUri,
286                 typeUri, expirationDate, inheritance, scope, ownerInfo);
287     }
288
289     public NodeLock(String JavaDoc lockId, String JavaDoc objectUri, String JavaDoc subjectUri,
290             String JavaDoc typeUri, Date JavaDoc expirationDate, boolean inheritance,
291             int scope, String JavaDoc ownerInfo) {
292         this.objectUri = objectUri;
293         this.subjectUri = subjectUri;
294         this.typeUri = typeUri;
295         this.expirationDate = expirationDate;
296         this.inheritance = inheritance;
297         this.lockId = lockId;
298         this.scope = scope;
299         this.ownerInfo = ownerInfo;
300     }
301
302     /**
303      * Utility method for safely generating an lock id.
304      *
305      * @param object
306      * The hascode of the object-uri of the lock.
307      * @param subject
308      * The hashcode of the subject-uri of the lock.
309      * @param type
310      * The hashcode of the type-uri of the lock.
311      * @param expires
312      * The time in milliseconds of the expires date.
313      * @return The generated lock-id.
314      */

315     private static final String JavaDoc generateLockID(int object, int subject,
316             int type, long expires) {
317         long current = System.currentTimeMillis();
318         byte[] input = new byte[4 + 4 + 4 + 8 + 8];
319         encode(input, 0, object);
320         encode(input, 4, subject);
321         encode(input, 8, type);
322         if (expires != 0)
323             encode(input, 12, expires);
324         encode(input, 20, current);
325         return new String JavaDoc(DigestUtils.md5Hex(input));
326     }
327
328     /**
329      * Encodes the given integer into the buffer at given position. It will
330      * always append 4 bytes.
331      */

332     private static final void encode(byte[] buf, int offset, int number) {
333         buf[offset + 0] = (byte) ((number) & 0xff);
334         buf[offset + 1] = (byte) ((number >> 8) & 0xff);
335         buf[offset + 2] = (byte) ((number >> 16) & 0xff);
336         buf[offset + 3] = (byte) ((number >> 24) & 0xff);
337     }
338
339     /**
340      * Encodes the given long into the buffer at given position. It will always
341      * append 8 bytes.
342      */

343     private static final void encode(byte[] buf, int offset, long number) {
344         buf[offset + 0] = (byte) ((number) & 0xff);
345         buf[offset + 1] = (byte) ((number >> 8) & 0xff);
346         buf[offset + 2] = (byte) ((number >> 16) & 0xff);
347         buf[offset + 3] = (byte) ((number >> 24) & 0xff);
348         buf[offset + 4] = (byte) ((number >> 32) & 0xff);
349         buf[offset + 5] = (byte) ((number >> 40) & 0xff);
350         buf[offset + 6] = (byte) ((number >> 48) & 0xff);
351         buf[offset + 7] = (byte) ((number >> 56) & 0xff);
352     }
353
354     /**
355      * Locked object accessor.
356      *
357      * @return String Locked subject uri
358      */

359     public String JavaDoc getObjectUri() {
360         return this.objectUri;
361     }
362
363     /**
364      * Locked object mutator.
365      *
366      * @param objectUri
367      * Locked subject
368      */

369     public void setObjectUri(String JavaDoc objectUri) {
370         this.objectUri = objectUri;
371     }
372
373     /**
374      * Lock owner uri accessor.
375      *
376      * @return String Lock owner
377      */

378     public String JavaDoc getSubjectUri() {
379         return this.subjectUri;
380     }
381
382     /**
383      * Lock owner uri mutator.
384      *
385      * @param subjectUri
386      * Lock owner
387      */

388     void setSubjectUri(String JavaDoc subjectUri) {
389         this.subjectUri = subjectUri;
390     }
391
392     /**
393      * Lock type uri accessor.
394      *
395      * @return String Lock type uri
396      */

397     public String JavaDoc getTypeUri() {
398         return this.typeUri;
399     }
400
401     /**
402      * Lock type mutator.
403      *
404      * @param typeUri
405      * Lock type
406      */

407     void setTypeUri(String JavaDoc typeUri) {
408         this.typeUri = typeUri;
409     }
410
411     /**
412      * Expiration date accessor.
413      *
414      * @return Date Exipiration date
415      */

416     public Date JavaDoc getExpirationDate() {
417         return expirationDate;
418     }
419
420     /**
421      * Expiration date mutator.
422      *
423      * @param expirationDate
424      * Expiration date
425      */

426     void setExpirationDate(Date JavaDoc expirationDate) {
427         this.expirationDate = expirationDate;
428     }
429
430     /**
431      * Expiration test.
432      *
433      * @return boolean True if this lock has expired
434      */

435     public boolean hasExpired() {
436         //Date currentTime = new Date();
437
return (expirationDate.before(new Date JavaDoc()));
438     }
439
440     /**
441      * Inheritance flag accessor.
442      *
443      * @return boolean True if token is inheritable
444      */

445     public boolean isInheritable() {
446         return inheritance;
447     }
448
449     /**
450      * Inheritance flag mutator.
451      *
452      * @param inheritance
453      * Inheritance flag
454      */

455     void setInheritable(boolean inheritance) {
456         this.inheritance = inheritance;
457     }
458
459     /**
460      * Exclusive flag accessor.
461      *
462      * @return boolean True if token is exclusive
463      */

464     public boolean isExclusive() {
465         return (scope == EXCLUSIVE);
466     }
467
468     /**
469      * Exclusive flag accessor.
470      *
471      * @return boolean True if token is shared (ie, not exclusive)
472      */

473     public boolean isShared() {
474         return (scope == SHARED);
475     }
476
477     /**
478      * Exclusive flag accessor.
479      *
480      * @return boolean True if token is exclusive
481      */

482     public boolean isLocal() {
483         return (scope == LOCAL);
484     }
485
486     /**
487      * Exclusive flag mutator.
488      *
489      * @param exclusive
490      * Exclusive flag
491      */

492     void setExclusive(boolean exclusive) {
493         if (exclusive) {
494             scope = EXCLUSIVE;
495         } else {
496             scope = SHARED;
497         }
498     }
499
500     /**
501      * Get lock identifier.
502      *
503      * @return String lock id
504      */

505     public String JavaDoc getLockId() {
506         return lockId;
507     }
508
509     /**
510      * Set the contacting info for the lock owner (phone, email, etc.)
511      *
512      * @param ownerInfo
513      * a String
514      *
515      */

516     public void setOwnerInfo(String JavaDoc ownerInfo) {
517         this.ownerInfo = ownerInfo;
518     }
519
520     /**
521      * Get the contacting info for the lock owner (phone, email, etc.)
522      *
523      * @return a String
524      *
525      */

526     public String JavaDoc getOwnerInfo() {
527         return ownerInfo;
528     }
529
530     // --------------------------------------------------------- Object Methods
531

532     /**
533      * Clone.
534      *
535      * @return Object clone
536      */

537     public NodeLock cloneObject() {
538         NodeLock result = null;
539         try {
540             result = (NodeLock) super.clone();
541         } catch (CloneNotSupportedException JavaDoc e) {
542             e.printStackTrace();
543         }
544         return result;
545     }
546
547     /**
548      * Equals.
549      *
550      * @param obj
551      * Object to test
552      * @return boolean True if the two object are equal :
553      * <li>obj is of type SlidePermission and is not null</li>
554      * <li>All three Uris are equal</li>
555      */

556     public boolean equals(Object JavaDoc obj) {
557         boolean result = false;
558         if ((obj != null) && (obj instanceof NodeLock)) {
559             NodeLock lock = (NodeLock) obj;
560             result = this.getLockId().equals(lock.getLockId());
561         }
562         return result;
563     }
564
565     /**
566      * Validate.
567      *
568      * @param expectedUri
569      * Uri
570      */

571     public void validate(String JavaDoc expectedUri) {
572
573         if (objectUri == null)
574             throw new ObjectValidationFailedException(expectedUri, Messages
575                     .message(NodeLock.class.getName() + ".nullObjectUri"));
576
577         if (!objectUri.equals(expectedUri))
578             throw new ObjectValidationFailedException(expectedUri, Messages
579                     .message(NodeLock.class.getName() + ".incorrectObjectUri"));
580
581         if (subjectUri == null)
582             throw new ObjectValidationFailedException(expectedUri, Messages
583                     .message(NodeLock.class.getName() + ".nullSubjectUri"));
584
585         if (typeUri == null)
586             throw new ObjectValidationFailedException(expectedUri, Messages
587                     .message(NodeLock.class.getName() + ".nullTypeUri"));
588
589         if (expirationDate == null)
590             throw new ObjectValidationFailedException(expectedUri, Messages
591                     .message(NodeLock.class.getName() + ".nullExpirationDate"));
592
593         if (lockId == null)
594             throw new ObjectValidationFailedException(expectedUri, Messages
595                     .message(NodeLock.class.getName() + ".nullLockId"));
596
597     }
598
599 }
Popular Tags