KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/lock/LockImpl.java,v 1.47.2.1 2004/09/03 14:42:12 luetzkendorf Exp $
3  * $Revision: 1.47.2.1 $
4  * $Date: 2004/09/03 14:42:12 $
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 import java.util.Enumeration JavaDoc;
28 import java.util.Stack JavaDoc;
29 import java.util.Vector JavaDoc;
30
31 import org.apache.slide.common.Namespace;
32 import org.apache.slide.common.NamespaceConfig;
33 import org.apache.slide.common.ServiceAccessException;
34 import org.apache.slide.common.SlideException;
35 import org.apache.slide.common.SlideToken;
36 import org.apache.slide.common.Uri;
37 import org.apache.slide.event.EventDispatcher;
38 import org.apache.slide.event.LockEvent;
39 import org.apache.slide.event.VetoException;
40 import org.apache.slide.security.AccessDeniedException;
41 import org.apache.slide.security.Security;
42 import org.apache.slide.structure.ActionNode;
43 import org.apache.slide.structure.ObjectNode;
44 import org.apache.slide.structure.ObjectNotFoundException;
45 import org.apache.slide.structure.SubjectNode;
46 import org.apache.slide.util.Configuration;
47
48 /**
49  * Lock helper class.
50  *
51  * @version $Revision: 1.47.2.1 $
52  */

53 public final class LockImpl implements Lock {
54     
55     // ----------------------------------------------------------- Constructors
56

57     
58     /**
59      * Constructor.
60      *
61      * @param namespace Associated namespace
62      * @param namespaceConfig Namespace configuration
63      * @param securityHelper Security helper
64      */

65     public LockImpl(Namespace namespace, NamespaceConfig namespaceConfig,
66                     Security securityHelper) {
67         this.namespace = namespace;
68         this.namespaceConfig = namespaceConfig;
69         this.securityHelper = securityHelper;
70     }
71     
72     
73     // ----------------------------------------------------- Instance Variables
74

75     
76     /**
77      * Namespace.
78      */

79     private Namespace namespace;
80     
81     
82     /**
83      * Namespace configuration.
84      */

85     private NamespaceConfig namespaceConfig;
86     
87     
88     /**
89      * Security helper.
90      */

91     private Security securityHelper;
92     
93     
94     // ----------------------------------------------------------- Lock Methods
95

96     /**
97      * Put a lock on a subject.
98      *
99      * @param slideToken The token to access slide.
100      * @param lockToken Object containing all the lock information
101      * @exception ServiceAccessException Low level service access exception
102      * @exception ObjectNotFoundException One of the objects referenced
103      * in the lock token were not found
104      * @exception ObjectIsAlreadyLockedException Object is already locked
105      * with an incompatible lock token
106      * @exception AccessDeniedException Insufficient credentials to allow
107      * object locking
108      */

109     public void lock(SlideToken slideToken, NodeLock lockToken)
110         throws ServiceAccessException, ObjectIsAlreadyLockedException,
111         AccessDeniedException, ObjectNotFoundException, VetoException {
112         
113         ObjectIsAlreadyLockedException nestedException =
114             new ObjectIsAlreadyLockedException(lockToken.getObjectUri());
115         Uri objectUri = namespace.getUri(slideToken, lockToken.getObjectUri(), true);
116         boolean canLock =
117             !isLockedInternal(slideToken, lockToken, true, nestedException);
118         
119         // Then we try to lock the subject.
120
// If the User doesn't have enough priviledges to accomplish this
121
// action, we will get a SecurityException which will in turn be
122
// thrown by this function.
123
if (canLock) {
124             ObjectNode lockedObject = objectUri.getStore()
125                 .retrieveObject(objectUri);
126             securityHelper
127                 .checkCredentials(slideToken, lockedObject,
128                                   namespaceConfig.getLockObjectAction());
129             objectUri.getStore().putLock(objectUri, lockToken);
130         } else {
131             throw nestedException;
132         }
133         
134         // Fire event
135
if ( LockEvent.LOCK.isEnabled() ) EventDispatcher.getInstance().fireVetoableEvent(LockEvent.LOCK, new LockEvent(this, slideToken, namespace, objectUri));
136     }
137     
138     
139     /**
140      * Removes a lock.
141      *
142      * @param slideToken The token to access slide.
143      * @param lockToken Object containing all the lock information
144      *
145      * @return <code>true</code> if the lock could be removed
146      *
147      * @exception ServiceAccessException Low level service access exception
148      * @exception LockTokenNotFoundException Cannot find the Lock in the
149      * Lock Store service
150      */

151     public boolean unlock(SlideToken slideToken, NodeLock lockToken)
152         throws ServiceAccessException, LockTokenNotFoundException, VetoException {
153         
154         try {
155             if (!checkLockOwner(slideToken, lockToken)) {
156                 return false;
157             }
158             
159             // check the lockId
160
if (slideToken.isEnforceLockTokens() && !checkLockToken(slideToken, lockToken)) {
161                 return false;
162             }
163         } catch (ObjectNotFoundException e) {
164             return false;
165         }
166         
167         // all checks successful, so try to actually remove the lock
168
Uri lockedUri = namespace.getUri(slideToken, lockToken.getObjectUri(),
169                                          true);
170         lockedUri.getStore().removeLock(lockedUri, lockToken);
171
172         // Fire event
173
if ( LockEvent.UNLOCK.isEnabled() ) EventDispatcher.getInstance().fireVetoableEvent(LockEvent.UNLOCK, new LockEvent(this, slideToken, namespace, lockedUri));
174
175         return true;
176     }
177     
178     
179     /**
180      * Removes a set of linked locks.
181      *
182      * @param slideToken Slide token
183      * @param objectUri Uri of the locked object.
184      * @param lockId The id of the locks, which will be removed.
185      * @exception ServiceAccessException Low level service access exception
186      * @exception LockTokenNotFoundException Cannot find the Lock in the
187      * Lock Store service
188      * @exception ObjectNotFoundException One of the objects referenced
189      * in the lock token were not found
190      */

191     public void unlock(SlideToken slideToken, String JavaDoc objectUri,
192                        String JavaDoc lockId)
193         throws ServiceAccessException, LockTokenNotFoundException,
194         ObjectNotFoundException, VetoException {
195         
196         Enumeration JavaDoc locksList = enumerateLocks(slideToken, objectUri, false);
197         while (locksList.hasMoreElements()) {
198             NodeLock currentLock = (NodeLock) locksList.nextElement();
199             if (currentLock.getLockId().equals(lockId)) {
200                 if (slideToken.isEnforceLockTokens())
201                     slideToken.addLockToken(lockId);
202                 unlock(slideToken, currentLock);
203             }
204         }
205         
206     }
207     
208     
209     /**
210      * Renew a lock.
211      *
212      * @param slideToken Slide token
213      * @param lockToken Token containing the lock info.
214      * @param newExpirationDate the desired expiration date
215      * @exception ServiceAccessException Low level service access exception
216      * @exception LockTokenNotFoundException Cannot find the Lock in
217      * the Lock Store service
218      */

219     public void renew(SlideToken slideToken, NodeLock lockToken,
220                       Date JavaDoc newExpirationDate)
221         throws ServiceAccessException, LockTokenNotFoundException, VetoException {
222         lockToken.setExpirationDate(newExpirationDate);
223         Uri lockedUri = namespace.getUri(slideToken, lockToken.getObjectUri());
224         lockedUri.getStore().renewLock(lockedUri, lockToken);
225
226         // Fire event
227
if ( LockEvent.RENEW.isEnabled() ) EventDispatcher.getInstance().fireVetoableEvent(LockEvent.RENEW, new LockEvent(this, slideToken, namespace, lockedUri));
228     }
229     
230     
231     /**
232      * Renew a set of linked locks.
233      *
234      * @param slideToken Slide token
235      * @param objectUri Uri of the locked object
236      * @param lockId Id of the locks, which will be renewed.
237      * @param newExpirationDate The new expiration date of the locks
238      * @exception ServiceAccessException Low level service access exception
239      * @exception LockTokenNotFoundException Cannot find the Lock in the
240      * Lock Store service
241      * @exception ObjectNotFoundException One of the objects referenced
242      * in the lock token were not found
243      */

244     public void renew(SlideToken slideToken, String JavaDoc objectUri,
245                       String JavaDoc lockId, Date JavaDoc newExpirationDate)
246         throws ServiceAccessException, LockTokenNotFoundException,
247         ObjectNotFoundException, VetoException {
248         
249         Enumeration JavaDoc locksList = enumerateLocks(slideToken, objectUri, false);
250         while (locksList.hasMoreElements()) {
251             NodeLock currentLock = (NodeLock) locksList.nextElement();
252             if (currentLock.getLockId().equals(lockId)) {
253                 renew(slideToken, currentLock, newExpirationDate);
254             }
255         }
256         
257     }
258     
259     
260     /**
261      * Kills locks.
262      *
263      * @param slideToken Slide token
264      * @param subject Subject on which locks are to be removed
265      * @exception ServiceAccessException Low level service access exception
266      * @exception ObjectNotFoundException One of the objects referenced in
267      * the lock token were not found
268      * @exception LockTokenNotFoundException Cannot find the Lock in the
269      * Lock Store service
270      * @exception AccessDeniedException Insufficient credentials to allow
271      * forced removal of locks
272      */

273     public void kill(SlideToken slideToken, SubjectNode subject)
274         throws ServiceAccessException, AccessDeniedException,
275         LockTokenNotFoundException, ObjectNotFoundException, VetoException {
276         
277         // We retrieve the enumeration of locks which have been put on the
278
// subject.
279
Uri subjectUri = namespace.getUri(slideToken, subject.getUri());
280         Enumeration JavaDoc locks = subjectUri.getStore()
281             .enumerateLocks(subjectUri);
282         // Then, try to kill each individual lock.
283
while (locks.hasMoreElements()) {
284             securityHelper
285                 .checkCredentials(slideToken, subject,
286                                   namespaceConfig.getKillLockAction());
287             subjectUri.getStore()
288                 .killLock(subjectUri, (NodeLock) locks.nextElement());
289         }
290         
291         // Fire event
292
if ( LockEvent.KILL.isEnabled() ) EventDispatcher.getInstance().fireVetoableEvent(LockEvent.KILL, new LockEvent(this, slideToken, namespace, subjectUri));
293     }
294     
295     public Enumeration JavaDoc enumerateLocks(SlideToken slideToken, String JavaDoc objectUri)
296         throws ServiceAccessException, ObjectNotFoundException,
297         LockTokenNotFoundException {
298         
299         return enumerateLocks(slideToken, objectUri, true);
300     }
301     
302     public Enumeration JavaDoc enumerateLocks(SlideToken slideToken, String JavaDoc objectUri,
303                                       boolean inherited)
304         throws ServiceAccessException, ObjectNotFoundException,
305         LockTokenNotFoundException {
306         
307         // We retrieve the LockStore service from the namespace.
308
Uri subjectUri = namespace.getUri(slideToken, objectUri);
309         
310         Enumeration JavaDoc scopes = null;
311         if (inherited) {
312             // traverse the namespace up to the root node, and add any locks
313
// found in the process
314
scopes = subjectUri.getScopes();
315         } else {
316             // only return the locks that explicitly and directly lock the
317
// given subject
318
Vector JavaDoc scopeVector = new Vector JavaDoc();
319             scopeVector.add(subjectUri.toString());
320             scopes = scopeVector.elements();
321         }
322         Vector JavaDoc locksVector = new Vector JavaDoc();
323         while (scopes.hasMoreElements()) {
324             String JavaDoc currentScope = (String JavaDoc) scopes.nextElement();
325             Uri currentScopeUri =
326                 namespace.getUri(slideToken, currentScope);
327             Enumeration JavaDoc currentLocks =
328                 currentScopeUri.getStore().enumerateLocks(currentScopeUri);
329             while (currentLocks.hasMoreElements()) {
330                 NodeLock currentLockToken =
331                     (NodeLock) currentLocks.nextElement();
332                 if (currentLockToken.hasExpired()) {
333                     // FIXME: do cleanup of locks someplace else
334
try {
335                         currentScopeUri.getStore()
336                             .removeLock(currentScopeUri, currentLockToken);
337                     }
338                     catch (LockTokenNotFoundException ex) {
339                         // ignore
340
}
341                 } else {
342                     locksVector.addElement(currentLockToken);
343                 }
344             }
345         }
346         return locksVector.elements();
347     }
348     
349     
350     /**
351      * Utility function for lock checking. Mirrors Security.checkCredentials.
352      *
353      * @param token Slide token
354      * @param object Object on which the action is performed
355      * @param action Action to test
356      * @exception ObjectLockedException Can't perform specified action
357      * on object
358      * @exception ServiceAccessException Low level service access exception
359      * @exception ObjectNotFoundException One of the objects referenced
360      * in the lock token were not found
361      */

362     public void checkLock(SlideToken token,
363                           ObjectNode object, ActionNode action)
364         throws ServiceAccessException, ObjectNotFoundException,
365         ObjectLockedException {
366         
367         if (!token.isForceLock()) {
368             return;
369         }
370         
371         if (Configuration.useIntegratedLocking()) {
372             
373             Boolean JavaDoc locked = token.checkLockCache(object, action);
374             if (locked != null) {
375                 if (locked.booleanValue()) {
376                     throw new ObjectLockedException(object.getUri());
377                 }
378                 else {
379                     return;
380                 }
381             }
382             
383             Uri objectUri = namespace.getUri(token, object.getUri());
384             ObjectNode realObject = objectUri.getStore()
385                 .retrieveObject(objectUri);
386             try {
387                 checkLock(token, realObject, (SubjectNode)securityHelper.getPrincipal(token), action);
388                 token.cacheLock(object, action, false);
389             }
390             catch (ObjectLockedException e) {
391                 token.cacheLock(object, action, true);
392                 throw e;
393             }
394         }
395     }
396     
397     
398     /**
399      * Check locking for a specific action and credential.
400      *
401      * @param slideToken Credetials token
402      * @param subject Subject to test
403      * @param user User to test
404      * @param action Action to test
405      * @exception ObjectLockedException Can't perform specified action
406      * on object
407      * @exception ServiceAccessException Low level service access exception
408      * @exception ObjectNotFoundException One of the objects referenced
409      * in the lock token were not found
410      */

411     public void checkLock(SlideToken slideToken,
412                           ObjectNode subject, SubjectNode user,
413                           ActionNode action)
414         throws ServiceAccessException, ObjectNotFoundException,
415         ObjectLockedException {
416         
417         // no check for default action (server intitialization)
418
if (action.equals(ActionNode.DEFAULT)) {
419             return;
420         }
421         
422         if (Configuration.useIntegratedLocking()) {
423             if (isLocked(slideToken, subject, user, action, false)) {
424                 throw new ObjectLockedException(subject.getUri());
425             }
426         }
427     }
428     
429     
430     /**
431      * Tests if an element is locked.
432      *
433      * @param slideToken Slide token
434      * @param subject Subject to test
435      * @param user User to test
436      * @param action Locked for action
437      * @return boolean True if the subject is locked
438      * @exception ServiceAccessException Low level service access exception
439      * @exception ObjectNotFoundException One of the objects referenced
440      * in the lock token were not found
441      */

442     public boolean isLocked(SlideToken slideToken,
443                             ObjectNode subject, SubjectNode user,
444                             ActionNode action, boolean tryToLock)
445         throws ServiceAccessException, ObjectNotFoundException {
446         NodeLock token = new NodeLock(subject, user, action,
447                                       new Date JavaDoc(), false);
448         return isLocked(slideToken, token, tryToLock);
449     }
450     
451     
452     /**
453      * Tests if an element is locked.
454      *
455      * @param slideToken Slide token
456      * @param subject Subject to test
457      * @param user User to test
458      * @param action Locked for action
459      * @param inheritance Set to true if we want to check if any children
460      * is locked with an incompatible lock
461      * @param tryToLock True if the check is intended to check whether
462      * or not we can put a new lock
463      * @return boolean True if the subject is locked
464      * @exception ServiceAccessException Low level service access exception
465      * @exception ObjectNotFoundException One of the objects referenced
466      * in the lock token were not found
467      */

468     public boolean isLocked(SlideToken slideToken,
469                             ObjectNode subject, SubjectNode user,
470                             ActionNode action, boolean inheritance,
471                             boolean tryToLock)
472         throws ServiceAccessException, ObjectNotFoundException {
473         NodeLock token = new NodeLock(subject, user, action, new Date JavaDoc(),
474                                       inheritance);
475         return isLocked(slideToken, token, tryToLock);
476     }
477     
478     
479     /**
480      * Tests if an element is locked.
481      *
482      * @param token Lock token to test
483      * @return boolean True if locked
484      * @exception ServiceAccessException Low level service access exception
485      * @exception ObjectNotFoundException One of the objects referenced
486      * in the lock token were not found
487      */

488     public boolean isLocked(SlideToken slideToken, NodeLock token,
489                             boolean tryToLock)
490         throws ServiceAccessException, ObjectNotFoundException {
491         
492         return isLockedInternal(slideToken, token, tryToLock, null);
493         
494     }
495     
496     /**
497      * Clears the expired locks from the specified resource.
498      *
499      * @param slideToken a SlideToken
500      * @param objectUri a String
501      * @param listener an UnlockListener
502      * @throws SlideException
503      */

504     public void clearExpiredLocks( SlideToken slideToken, String JavaDoc objectUri, UnlockListener listener ) throws SlideException {
505         
506         Uri uri =
507             namespace.getUri(slideToken, objectUri);
508         Enumeration JavaDoc currentLocks =
509             uri.getStore().enumerateLocks(uri);
510         while (currentLocks.hasMoreElements()) {
511             NodeLock currentLockToken =
512                 (NodeLock) currentLocks.nextElement();
513             if (currentLockToken.hasExpired()) {
514                 try {
515                     uri.getStore().removeLock(uri, currentLockToken);
516                     if( listener != null )
517                         listener.afterUnlock( objectUri );
518                 }
519                 catch (LockTokenNotFoundException ex) {
520                     // ignore
521
}
522             }
523         }
524     }
525     
526     /**
527      * Return true if a lock token for this lock has been given in the
528      * credentials token.
529      *
530      * @param slideToken Current credentials token
531      * @param token Lock token
532      */

533     public boolean checkLockToken(SlideToken slideToken, NodeLock token) {
534         if (!slideToken.isEnforceLockTokens())
535             return true;
536         
537         // if org.apache.slide.principalIdentifiedLocks is true the lock-owner
538
// must not provide the lock token (i.e. it must not be present in the
539
// slide token)
540
if (Configuration.usePrincipalIdentifiedLocks()) {
541             SubjectNode principalNode = null;
542             SubjectNode ownerNode = SubjectNode.getSubjectNode(token.getSubjectUri());
543             try {
544                 principalNode = (SubjectNode)securityHelper.getPrincipal(slideToken);
545             }
546             catch (SlideException e) {}
547             
548             if (ownerNode != null &&
549                 !ownerNode.equals(SubjectNode.UNAUTHENTICATED) &&
550                 ownerNode.equals(principalNode))
551             {
552                 return true;
553             }
554             return (slideToken.checkLockToken(token.getLockId()));
555         } else {
556             return (slideToken.checkLockToken(token.getLockId()));
557         }
558     }
559     
560     /**
561      * Returns true, if the principal from the credentials token is either the
562      * lock-owner of has kill-lock privileges
563      *
564      * @param slideToken a SlideToken
565      * @param token a NodeLock
566      * @return a boolean
567      *
568      * @throws ObjectNotFoundException
569      * @throws ServiceAccessException
570      */

571     public boolean checkLockOwner(SlideToken slideToken, NodeLock token) throws ObjectNotFoundException, ServiceAccessException {
572         boolean canUnlock = true;
573         SubjectNode principal = (SubjectNode)securityHelper.getPrincipal(slideToken);
574         SubjectNode lockOwner = SubjectNode.getSubjectNode(token.getSubjectUri());
575         if (!securityHelper.matchPrincipal(slideToken, principal, lockOwner)) {
576             // it is not the lock owner ... now check the kill-lock privilege
577
try {
578                 securityHelper.checkCredentials(slideToken,
579                                                 SubjectNode.getSubjectNode(token.getObjectUri()),
580                                                 namespaceConfig.getKillLockAction());
581             }
582             catch (AccessDeniedException e) {
583                 canUnlock = false;
584             }
585         }
586         return canUnlock;
587     }
588     
589     // -------------------------------------------------------- Private Methods
590

591     
592     /**
593      * Tests if an element is locked.
594      *
595      * @param token Lock token to test
596      * @return boolean True if locked
597      * @exception ServiceAccessException Low level service access exception
598      * @exception ObjectNotFoundException One of the objects referenced
599      * in the lock token were not found
600      */

601     private boolean isLockedInternal
602         (SlideToken slideToken, NodeLock token,
603          boolean tryToLock, ObjectIsAlreadyLockedException nestedException)
604         throws ServiceAccessException, ObjectNotFoundException {
605         
606         Uri objectUri = namespace.getUri(slideToken, token.getObjectUri());
607         ObjectNode initialObject = objectUri.getStore()
608             .retrieveObject(objectUri);
609         Enumeration JavaDoc scopes = objectUri.getScopes();
610         
611         // At the end of the test, this boolean's value is true if we can
612
// actually put the lock on the desired subject.
613
boolean isLocked = false;
614         
615         // We parse all of the scopes which encompass the subject we want
616
// to lock.
617
// First, we parse all the parents of the subject.
618
while (!isLocked && scopes.hasMoreElements()) {
619             String JavaDoc currentScope = (String JavaDoc) scopes.nextElement();
620             Uri currentScopeUri = namespace.getUri(slideToken, currentScope);
621             Enumeration JavaDoc locks = currentScopeUri.getStore()
622                 .enumerateLocks(currentScopeUri);
623             
624             while (locks.hasMoreElements()) {
625                 NodeLock currentLockToken = (NodeLock) locks.nextElement();
626                 if (!isCompatible(slideToken, token, currentLockToken,
627                                   tryToLock)) {
628                     isLocked = true;
629                     if (nestedException != null) {
630                         nestedException.addException
631                             (new ObjectLockedException
632                                  (currentScopeUri.toString()));
633                     }
634                 }
635             }
636         }
637         
638         // Then, if the desired scope is inheritable, we parse the
639
// locked subject's children to see if any of them has been
640
// locked with an incompatible lock.
641
if (token.isInheritable()) {
642             Stack JavaDoc childrenStack = new Stack JavaDoc();
643             childrenStack.push(initialObject);
644             while (!isLocked && !childrenStack.empty()) {
645                 ObjectNode currentObject = (ObjectNode) childrenStack.pop();
646                 Uri currentObjectUri =
647                     namespace.getUri(slideToken, currentObject.getUri());
648                 // We test the compatibility of the child
649
Enumeration JavaDoc locks = currentObjectUri.getStore()
650                     .enumerateLocks(currentObjectUri);
651                 
652                 while (locks.hasMoreElements()) {
653                     NodeLock currentLockToken = (NodeLock) locks.nextElement();
654                     if (!isCompatible(slideToken, token,
655                                       currentLockToken, tryToLock)) {
656                         isLocked = true;
657                         if (nestedException != null) {
658                             nestedException.addException
659                                 (new ObjectLockedException
660                                      (currentObjectUri.toString()));
661                         }
662                     }
663                 }
664                 
665                 // We get the children and add them to the Stack.
666

667                 Vector JavaDoc childrenVector = new Vector JavaDoc();
668                 Enumeration JavaDoc childrenUri = currentObject.enumerateChildren();
669                 while (childrenUri.hasMoreElements()) {
670                     String JavaDoc childUri = (String JavaDoc) childrenUri.nextElement();
671                     Uri tempUri = namespace.getUri(slideToken, childUri);
672                     ObjectNode child = tempUri.getStore()
673                         .retrieveObject(tempUri);
674                     childrenVector.addElement(child);
675                 }
676                 
677                 Enumeration JavaDoc children = childrenVector.elements();
678                 while (children.hasMoreElements()) {
679                     ObjectNode tempObject =
680                         (ObjectNode) children.nextElement();
681                     childrenStack.push(tempObject);
682                 }
683             }
684         }
685         
686         return isLocked;
687         
688     }
689     
690     
691     /**
692      * Method isCompatibe
693      *
694      * @param slideToken a SlideToken
695      * @param checkToken the "current" token
696      * @param matchToken the token to check against
697      * (from a resource)
698      * @param tryToLock a boolean
699      *
700      * @return a boolean
701      *
702      */

703     private boolean isCompatible(SlideToken slideToken,
704                                  NodeLock checkToken,
705                                  NodeLock matchToken,
706                                  boolean tryToLock)
707         throws ServiceAccessException {
708         
709         boolean compatible = true;
710         
711         if (matchToken.hasExpired()) {
712             // Since the lock has expired, it is removed
713
try {
714                 if (slideToken.isForceStoreEnlistment()) {
715                     Uri token2Uri = namespace.getUri(slideToken,
716                                                      matchToken.getObjectUri());
717                     token2Uri.getStore().removeLock(token2Uri, matchToken);
718                 }
719             }
720             catch (LockTokenNotFoundException e) {} // ignore silently
721
return true;
722         }
723         
724         // same-object-uri-OR-matchToken-is-inheritable
725
boolean condition0 = matchToken.getObjectUri().equals(checkToken.getObjectUri()) || matchToken.isInheritable();
726         if (!condition0) {
727             return true;
728         }
729         
730         // lock-types-are-equal
731
boolean condition1 = matchToken.getTypeUri().equals(checkToken.getTypeUri());
732         
733         // user-of-tokens-are-equal OR user-of-matchToken-is-parent
734
SubjectNode checkSubject = SubjectNode.getSubjectNode(checkToken.getSubjectUri());
735         SubjectNode matchSubject = SubjectNode.getSubjectNode(matchToken.getSubjectUri());
736         boolean condition2 =
737             securityHelper.matchPrincipal(slideToken, checkSubject, matchSubject);
738         
739         // lock-types-are-equal OR lock-type-of-matchToken-is-parent
740
ActionNode checkAction = ActionNode.getActionNode(checkToken.getTypeUri());
741         ActionNode matchAction = ActionNode.getActionNode(matchToken.getTypeUri());
742         boolean condition3 =
743             securityHelper.matchAction(slideToken, checkAction, matchAction);
744         
745         // checkToken-is-exclusive
746
boolean condition4 = checkToken.isExclusive();
747         
748         // check-matchToken-against-slidetoken
749
boolean condition5 = checkLockToken(slideToken, matchToken);
750         
751         // enforced-locktokens
752
boolean condition6 = slideToken.isEnforceLockTokens();
753         
754         if ((tryToLock && condition1 && condition4)
755             || (condition3 && !condition2 && !condition6)
756             || (condition3 && !condition5 && condition6)
757             || (condition5 && !condition2) // FIXES 30982
758
){
759             compatible = false;
760         }
761         
762         // sharing the lock is OK
763
if (checkToken.isShared() && matchToken.isShared() && tryToLock && condition1 ) {
764             compatible = true;
765         }
766         
767         // System.out.println();
768
// System.out.println("SlideToken:");
769
// System.out.println(" .showLockTokens = " + slideToken.showLockTokens());
770
// System.out.println(" .isEnforceLockTokens = " + slideToken.isEnforceLockTokens());
771
// System.out.println("Token1 = " + checkToken);
772
// System.out.println(" .getLockId = " + checkToken.getLockId());
773
// System.out.println(" .getTypeUri = " + checkToken.getTypeUri());
774
// System.out.println(" .getSubjectUri = " + checkToken.getSubjectUri());
775
// System.out.println(" .getObjectUri = " + checkToken.getObjectUri());
776
// System.out.println(" .isExclusive = " + checkToken.isExclusive());
777
// System.out.println(" .isInheritable() = " + checkToken.isInheritable());
778
// System.out.println("Token2 = " + matchToken);
779
// System.out.println(" .getLockId = " + matchToken.getLockId());
780
// System.out.println(" .getTypeUri = " + matchToken.getTypeUri());
781
// System.out.println(" .getSubjectUri = " + matchToken.getSubjectUri());
782
// System.out.println(" .getObjectUri = " + matchToken.getObjectUri());
783
// System.out.println(" .isExclusive = " + matchToken.isExclusive());
784
// System.out.println(" .isInheritable() = " + matchToken.isInheritable());
785
// System.out.println("Parameter tryToLock: "+tryToLock);
786
// System.out.println("C1 (lock-types-are-equal).................................: " + condition1);
787
// System.out.println("C2 (user-of-tokens-are-equal OR user-of-matchToken-is-parent).: " + condition2);
788
// System.out.println("C3 (lock-types-are-equal OR lock-type-of-checkToken-is-parent): " + condition3);
789
// System.out.println("C4 (checkToken-is-exclusive)..................................: " + condition4);
790
// System.out.println("C5 (check-matchToken-against-slidetoken)......................: " + condition5);
791
// System.out.println("C6 (enforced-locktokens)..................................: " + condition6);
792
// System.out.println("C7 (same-object-uri-OR-matchToken-is-inheritable).............: " + condition7);
793
// System.out.println("Compatible (tryToLock && C1 && C4 && C7 || C3 && !C2 && !C6 && C7 || C3 && !C5 && C6 && C7): " + compatible);
794

795         return compatible;
796     }
797     
798     /**
799      * Tells whether or not two locks are compatible.
800      *
801      * @param token1 First token
802      * @param token2 Second token : object's lock
803      * @param tryToLock True if we want to check for a lock creation
804      * @return boolean True if the locks are compatible
805      */

806     private boolean isCompatible_OLD(SlideToken slideToken,
807                                      NodeLock token1, NodeLock token2,
808                                      boolean tryToLock) {
809         /*
810          System.out.println("**** Check lock ****");
811          System.out.println("Lock 1 : " + token1.getSubjectUri() + " action "
812          + token1.getTypeUri());
813          System.out.println("Lock 2 : " + token2.getSubjectUri() + " on "
814          + token2.getObjectUri() + " action "
815          + token2.getTypeUri());
816          */

817         
818         boolean compatible = true;
819         
820         // We first check whether or not the lock is still valid
821
if (token2.hasExpired()) {
822             // Since the lock has expired, it is removed
823
try {
824                 if (slideToken.isForceStoreEnlistment()) {
825                     Uri token2Uri = namespace.getUri(slideToken,
826                                                      token2.getObjectUri());
827                     token2Uri.getStore().removeLock(token2Uri, token2);
828                 }
829             } catch (SlideException e) {
830                 e.printStackTrace();
831             }
832         } else {
833             // lock-types-are-equal
834
boolean condition1 = token2.getTypeUri().equals(token1.getTypeUri());
835             // user-of-tokens-are-equal OR user-of-token2-is-parent
836
boolean condition2 =
837                 (token1.getSubjectUri().startsWith(token2.getSubjectUri()));
838             // lock-types-are-equal OR lock-type-of-token1-is-parent
839
boolean condition3 = token2.getTypeUri()
840                 .startsWith(token1.getTypeUri());
841             // token1-is-exclusive
842
boolean condition4 = token1.isExclusive();
843             // check-token2-against-slidetoken
844
boolean condition5 = checkLockToken(slideToken, token2);
845             // enforced-locktokens
846
boolean condition6 = slideToken.isEnforceLockTokens();
847             // same-object-uri-OR-token2-is-inheritable
848
boolean condition7 = token2.getObjectUri().equals(token1.getObjectUri()) || token2.isInheritable();
849             
850             if ((tryToLock && condition1 && condition4 && condition7)
851                 || (condition3 && !condition2 && !condition6 && condition7)
852                 || (condition3 && !condition5 && condition6 && condition7)
853                ){
854                 compatible = false;
855             }
856             
857             // sharing the lock is OK
858
if (token1.isShared() && token2.isShared() && tryToLock && condition1 ) {
859                 compatible = true;
860             }
861             
862             // System.out.println();
863
// System.out.println("SlideToken:");
864
// System.out.println(" .showLockTokens = " + slideToken.showLockTokens());
865
// System.out.println(" .isEnforceLockTokens = " + slideToken.isEnforceLockTokens());
866
// System.out.println("Token1 = " + token1);
867
// System.out.println(" .getLockId = " + token1.getLockId());
868
// System.out.println(" .getTypeUri = " + token1.getTypeUri());
869
// System.out.println(" .getSubjectUri = " + token1.getSubjectUri());
870
// System.out.println(" .getObjectUri = " + token1.getObjectUri());
871
// System.out.println(" .isExclusive = " + token1.isExclusive());
872
// System.out.println(" .isInheritable() = " + token1.isInheritable());
873
// System.out.println("Token2 = " + token2);
874
// System.out.println(" .getLockId = " + token2.getLockId());
875
// System.out.println(" .getTypeUri = " + token2.getTypeUri());
876
// System.out.println(" .getSubjectUri = " + token2.getSubjectUri());
877
// System.out.println(" .getObjectUri = " + token2.getObjectUri());
878
// System.out.println(" .isExclusive = " + token2.isExclusive());
879
// System.out.println(" .isInheritable() = " + token2.isInheritable());
880
// System.out.println("Parameter tryToLock: "+tryToLock);
881
// System.out.println("C1 (lock-types-are-equal).................................: " + condition1);
882
// System.out.println("C2 (user-of-tokens-are-equal OR user-of-token2-is-parent).: " + condition2);
883
// System.out.println("C3 (lock-types-are-equal OR lock-type-of-token1-is-parent): " + condition3);
884
// System.out.println("C4 (token1-is-exclusive)..................................: " + condition4);
885
// System.out.println("C5 (check-token2-against-slidetoken)......................: " + condition5);
886
// System.out.println("C6 (enforced-locktokens)..................................: " + condition6);
887
// System.out.println("C7 (same-object-uri-OR-token2-is-inheritable).............: " + condition7);
888
// System.out.println("Compatible (tryToLock && C1 && C4 && C7 || C3 && !C2 && !C6 && C7 || C3 && !C5 && C6 && C7): " + compatible);
889

890         }
891         return compatible;
892     }
893 }
894
895
896
897
898
Popular Tags