KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > webdav > util > UnlockListenerImpl


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/util/UnlockListenerImpl.java,v 1.3.2.1 2004/09/17 15:38:42 luetzkendorf Exp $
3  * $Revision: 1.3.2.1 $
4  * $Date: 2004/09/17 15:38:42 $
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.webdav.util;
25
26 import java.util.Enumeration JavaDoc;
27 import java.util.HashSet JavaDoc;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31
32 import org.apache.slide.common.NamespaceAccessToken;
33 import org.apache.slide.common.SlideException;
34 import org.apache.slide.common.SlideToken;
35 import org.apache.slide.content.Content;
36 import org.apache.slide.content.NodeProperty;
37 import org.apache.slide.content.NodeRevisionDescriptor;
38 import org.apache.slide.content.NodeRevisionDescriptors;
39 import org.apache.slide.content.RevisionDescriptorNotFoundException;
40 import org.apache.slide.lock.UnlockListener;
41 import org.apache.slide.structure.ObjectNotFoundException;
42 import org.apache.slide.structure.Structure;
43 import org.apache.slide.util.Configuration;
44 import org.apache.slide.webdav.WebdavServletConfig;
45 import org.apache.slide.webdav.util.resourcekind.AbstractResourceKind;
46 import org.apache.slide.webdav.util.resourcekind.CheckedOutVersionControlled;
47 import org.apache.slide.webdav.util.resourcekind.ResourceKind;
48
49 /**
50  * Implements UnlockListener
51  */

52 public class UnlockListenerImpl implements UnlockListener {
53     SlideToken slideToken;
54     NamespaceAccessToken token;
55     WebdavServletConfig config;
56     HttpServletRequest JavaDoc req;
57     HttpServletResponse JavaDoc resp;
58     // counts the locks this listner is called for
59
int unlockCount = 0;
60     // set ot uris that are Lock-Null resources anre removed by this listener
61
HashSet JavaDoc removedLockNullResources = new HashSet JavaDoc();
62     
63     /**
64      * Constructor
65      */

66     public UnlockListenerImpl( SlideToken slideToken, NamespaceAccessToken token, WebdavServletConfig config, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp ) {
67         this.slideToken = slideToken;
68         this.token = token;
69         this.config = config;
70         this.req = req;
71         this.resp = resp;
72     }
73     
74     /**
75      * This method is called before unlocking the resource.
76      *
77      * @param uri the uri of the resource that will be unlocked.
78      * @throws SlideException
79      */

80     public void beforeUnlock(String JavaDoc uri) throws SlideException {
81     }
82     
83     /**
84      * This method is called after unlocking the resource.
85      *
86      * @param uri the uri of the resource that has been unlocked.
87      * @throws SlideException
88      */

89     public void afterUnlock(String JavaDoc uri) throws SlideException {
90         unlockCount++;
91         
92         // Check whether the resource must be checked-in due to auto-versioning semantics.
93
Content content = token.getContentHelper();
94         Structure structure = token.getStructureHelper();
95         NodeRevisionDescriptors revisionDescriptors =
96             content.retrieve(slideToken, uri);
97         NodeRevisionDescriptor revisionDescriptor =
98             content.retrieve(slideToken, revisionDescriptors);
99         ResourceKind resourceKind = AbstractResourceKind.determineResourceKind(token, uri, revisionDescriptor);
100         if( Configuration.useVersionControl() &&
101                (resourceKind instanceof CheckedOutVersionControlled) ) {
102             NodeProperty checkinLocktokenProperty =
103                 revisionDescriptor.getProperty(DeltavConstants.I_CHECKIN_LOCKTOKEN,
104                                                NodeProperty.NamespaceCache.SLIDE_URI);
105             if (checkinLocktokenProperty == null) {
106                 // retry with default (DAV:) namespace which was the
107
// former namespace of this property
108
checkinLocktokenProperty =
109                     revisionDescriptor.getProperty(DeltavConstants.I_CHECKIN_LOCKTOKEN);
110             }
111             if ( (checkinLocktokenProperty != null) && (checkinLocktokenProperty.getValue() != null)
112                 // && slideToken.checkLockToken(checkinLocktokenProperty.getValue().toString())
113
) {
114                 VersioningHelper versionHelper = VersioningHelper.getVersioningHelper(slideToken, token, req, resp, config);
115                 try {
116                     versionHelper.checkin(revisionDescriptors, revisionDescriptor, false, false, true);
117                 }
118                 catch (java.io.IOException JavaDoc e) {}
119                 catch (org.jdom.JDOMException e) {}
120             }
121         }
122         
123         // if the URI has no more locks associated to it and is
124
// a lock-null resource, we must attempt to delete it
125
try {
126             Enumeration JavaDoc locks = token.getLockHelper().enumerateLocks(slideToken, uri);
127             if (!locks.hasMoreElements() && isLockNull(revisionDescriptor)) {
128                 this.removedLockNullResources.add(uri);
129                 content.remove(slideToken, uri, revisionDescriptor);
130                 content.remove(slideToken, revisionDescriptors);
131                 structure.remove(slideToken, structure.retrieve(slideToken, uri));
132             }
133         } catch (ObjectNotFoundException onfe) {
134         } catch (RevisionDescriptorNotFoundException e) {
135            // this happens e.g. if some one tries to create a resource
136
// in the history that looks like a version
137
// e.g. PUT /history/221/7.1 (cf VcPutVHR)
138
}
139     }
140     
141     /**
142      * Returns the number on unlocks processed be this listener.
143      */

144     public int getUnlockCount() {
145         return this.unlockCount;
146     }
147     public boolean isRemovedLockResource(String JavaDoc uri) {
148         return this.removedLockNullResources.contains(uri);
149     }
150     
151     private boolean isLockNull( NodeRevisionDescriptor nrd ) {
152         return nrd.propertyValueContains(WebdavConstants.P_RESOURCETYPE,
153                 WebdavConstants.E_LOCKNULL);
154     }
155 }
156
157
Popular Tags