KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > macro > MacroPropertyUpdater


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/macro/MacroPropertyUpdater.java,v 1.5.2.1 2004/09/01 10:40:18 unico Exp $
3  * $Revision: 1.5.2.1 $
4  * $Date: 2004/09/01 10:40:18 $
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 package org.apache.slide.macro;
24
25 import org.apache.slide.common.Domain;
26 import org.apache.slide.common.Uri;
27 import org.apache.slide.content.Content;
28 import org.apache.slide.content.ContentImpl;
29 import org.apache.slide.content.NodeRevisionDescriptor;
30 import org.apache.slide.content.NodeRevisionDescriptors;
31 import org.apache.slide.event.MacroEvent;
32 import org.apache.slide.event.MacroListener;
33 import org.apache.slide.event.VetoException;
34 import org.apache.slide.lock.Lock;
35 import org.apache.slide.lock.LockImpl;
36 import org.apache.slide.security.ACLSecurityImpl;
37 import org.apache.slide.security.Security;
38 import org.apache.slide.structure.Structure;
39 import org.apache.slide.structure.StructureImpl;
40 import org.apache.slide.structure.SubjectNode;
41 import org.apache.slide.util.conf.Configurable;
42 import org.apache.slide.util.conf.Configuration;
43 import org.apache.slide.util.conf.ConfigurationException;
44
45 /**
46  * Event handler (listener) that updates some DAV: properties
47  * on COPY or MOVE actions.
48  *
49  * <p>The DAV: properties currently updated are: displayname and owner.
50  *
51  */

52 public class MacroPropertyUpdater
53    implements MacroListener, Configurable
54 {
55    private boolean updateDisplayName = true;
56    private boolean updateOwnerOnMove = false;
57    private boolean updateOwnerOnCopy = true;
58    
59    /*
60     * @see org.apache.slide.util.conf.Configurable#configure(org.apache.slide.util.conf.Configuration)
61     */

62    public void configure(Configuration configuration)
63          throws ConfigurationException
64    {
65      
66      try {
67         this.updateDisplayName = configuration.getConfiguration("update-displayname")
68            .getValueAsBoolean(this.updateDisplayName);
69      } catch (Exception JavaDoc e) {
70         // ignore, maintain default
71
}
72      
73      try {
74         this.updateOwnerOnMove = configuration.getConfiguration("update-owner-on-move")
75            .getValueAsBoolean(this.updateOwnerOnMove);
76      } catch (Exception JavaDoc e) {
77         // ignore, maintain default
78
}
79
80      try {
81         this.updateOwnerOnCopy = configuration.getConfiguration("update-owner-on-copy")
82            .getValueAsBoolean(this.updateOwnerOnCopy);
83      } catch (Exception JavaDoc e) {
84         // ignore, maintain default
85
}
86    }
87    
88    /*
89     * @see org.apache.slide.event.MacroListener#copy(org.apache.slide.event.MacroEvent)
90     */

91    public void copy(MacroEvent event) throws VetoException
92    {
93       try {
94          Content helper = getContentHelper(event);
95          
96          NodeRevisionDescriptors destNrds = helper.retrieve(event.getToken(),
97                                                             event.getTargetURI());
98          NodeRevisionDescriptor destNrd = helper.retrieve(event.getToken(),
99                                                           destNrds);
100          
101          boolean anyThingUpdated = false;
102          
103          // update display name (if changed)
104
if (this.updateDisplayName) {
105             String JavaDoc displayName = getFilename(event.getTargetURI());
106             if (!displayName.equals(destNrd.getName())) {
107                destNrd.setName(displayName);
108                anyThingUpdated = true;
109             }
110          }
111
112          // update owner (if changed)
113
if (this.updateOwnerOnCopy) {
114             String JavaDoc newOwner = event.getToken().getCredentialsToken().getPublicCredentials();
115             if (newOwner == null || newOwner.equals("") || newOwner.equals("/")) {
116                 newOwner = SubjectNode.UNAUTHENTICATED_URI;
117             }
118             if (!destNrd.getOwner().equals(newOwner)) {
119                destNrd.setOwner(newOwner);
120                anyThingUpdated = true;
121             }
122          }
123
124          if (anyThingUpdated) {
125             Uri target = event.getNamespace().getUri(event.getToken(),
126                   event.getTargetURI());
127             target.getStore().storeRevisionDescriptor(target, destNrd);
128          }
129          
130       }
131       catch (Exception JavaDoc e) {
132          Domain.error("Exception while copy", e);
133       }
134       
135    }
136
137    /*
138     * @see org.apache.slide.event.MacroListener#move(org.apache.slide.event.MacroEvent)
139     */

140    public void move(MacroEvent event) throws VetoException
141    {
142       try {
143          Content helper = getContentHelper(event);
144          
145          NodeRevisionDescriptors destNrds = helper.retrieve(event.getToken(),
146                                                             event.getTargetURI());
147          NodeRevisionDescriptor destNrd = helper.retrieve(event.getToken(),
148                                                           destNrds);
149          
150          boolean anyThingUpdated = false;
151          
152          // update owner (if changed)
153
if (this.updateOwnerOnMove) {
154             String JavaDoc newOwner = event.getToken().getCredentialsToken().getPublicCredentials();
155             if (newOwner == null || newOwner.equals("") || newOwner.equals("/")) {
156                 newOwner = SubjectNode.UNAUTHENTICATED_URI;
157             }
158             if (!destNrd.getOwner().equals(newOwner)) {
159                anyThingUpdated = true;
160                destNrd.setOwner(newOwner);
161             }
162          }
163
164          if (anyThingUpdated) {
165             Uri target = event.getNamespace().getUri(event.getToken(), event.getTargetURI());
166             target.getStore().storeRevisionDescriptor(target, destNrd);
167          }
168       }
169       catch (Exception JavaDoc e) {
170          Domain.error("Exception while move", e);
171       }
172    }
173    
174    
175    /*
176     * @see org.apache.slide.event.MacroListener#delete(org.apache.slide.event.MacroEvent)
177     */

178    public void delete(MacroEvent event) throws VetoException
179    {
180    }
181    
182
183    /**
184     * Returns the last part of an URI that is normaly called the name of
185     * the collection of file.
186     * <p>I bet there is anywhere such a method but I havn't found it.
187     * @param uri
188     * @return the name of the resource given by uri
189     */

190   private static String JavaDoc getFilename(String JavaDoc uri) {
191       if (uri.indexOf('/') == -1) {
192          return uri;
193       }
194       if (uri.equals("/")) {
195          return "";
196       }
197       if (uri.endsWith("/")) {
198          return uri.substring(1+uri.lastIndexOf('/', uri.length() - 2),
199                               uri.length() - 1);
200       } else {
201          return uri.substring(1+uri.lastIndexOf('/'));
202       }
203   }
204
205   private Content getContentHelper(MacroEvent event) {
206      Security security = new ACLSecurityImpl(event.getNamespace(),
207                                              event.getNamespace().getConfig());
208      Lock lock = new LockImpl(event.getNamespace(),
209                               event.getNamespace().getConfig(),
210                               security);
211      Structure structure = new StructureImpl(event.getNamespace(),
212                               event.getNamespace().getConfig(),
213                               security,
214                               lock);
215      Content helper = new ContentImpl(
216            event.getNamespace(),
217            event.getNamespace().getConfig(),
218            security,
219            structure,
220            lock);
221      return helper;
222   }
223 }
224
Popular Tags