KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > ac > DocumentPolicyManagerWrapper


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: DocumentPolicyManagerWrapper.java 43242 2004-08-16 16:42:32Z andreas $ */
19
20 package org.apache.lenya.cms.ac;
21
22 import java.io.File JavaDoc;
23
24 import org.apache.avalon.framework.activity.Disposable;
25 import org.apache.avalon.framework.configuration.Configurable;
26 import org.apache.avalon.framework.configuration.Configuration;
27 import org.apache.avalon.framework.configuration.ConfigurationException;
28 import org.apache.avalon.framework.logger.AbstractLogEnabled;
29 import org.apache.avalon.framework.service.ServiceException;
30 import org.apache.avalon.framework.service.ServiceManager;
31 import org.apache.avalon.framework.service.ServiceSelector;
32 import org.apache.avalon.framework.service.Serviceable;
33 import org.apache.excalibur.source.Source;
34 import org.apache.excalibur.source.SourceResolver;
35 import org.apache.excalibur.source.SourceUtil;
36 import org.apache.lenya.ac.AccessControlException;
37 import org.apache.lenya.ac.Accreditable;
38 import org.apache.lenya.ac.AccreditableManager;
39 import org.apache.lenya.ac.Policy;
40 import org.apache.lenya.ac.PolicyManager;
41 import org.apache.lenya.ac.impl.DefaultAccessController;
42 import org.apache.lenya.ac.impl.DefaultPolicy;
43 import org.apache.lenya.ac.impl.InheritingPolicyManager;
44 import org.apache.lenya.cms.publication.Document;
45 import org.apache.lenya.cms.publication.DocumentBuilder;
46 import org.apache.lenya.cms.publication.Publication;
47 import org.apache.lenya.cms.publication.PublicationFactory;
48
49 /**
50  * A PolicyManager which is capable of mapping all URLs of a document to the appropriate canonical
51  * URL, e.g. <code>/foo/bar_de.print.html</code> is mapped to <code>/foo/bar</code>.
52  */

53 public class DocumentPolicyManagerWrapper
54     extends AbstractLogEnabled
55     implements InheritingPolicyManager, Serviceable, Configurable, Disposable {
56
57     /**
58      * Ctor.
59      */

60     public DocumentPolicyManagerWrapper() {
61     }
62
63     private InheritingPolicyManager policyManager;
64     private ServiceSelector policyManagerSelector;
65
66     /**
67      * Returns the URI which is used to obtain the policy for a webapp URL.
68      *
69      * @param webappUrl The webapp URL.
70      * @return A string.
71      * @throws AccessControlException when something went wrong.
72      */

73     protected String JavaDoc getPolicyURL(String JavaDoc webappUrl) throws AccessControlException {
74
75         if (getLogger().isDebugEnabled()) {
76             getLogger().debug("Resolving policy for webapp URL [" + webappUrl + "]");
77         }
78
79         Publication publication = getPublication(webappUrl);
80         DocumentBuilder builder = publication.getDocumentBuilder();
81         String JavaDoc url = null;
82         try {
83             if (builder.isDocument(publication, webappUrl)) {
84                 Document document = builder.buildDocument(publication, webappUrl);
85                 if (document.existsInAnyLanguage()) {
86                     url = "/" + document.getArea() + document.getId();
87                     if (getLogger().isDebugEnabled()) {
88                         getLogger().debug(" Document exists");
89                         getLogger().debug(" Document ID: [" + document.getId() + "]");
90                     }
91                 }
92             }
93         } catch (Exception JavaDoc e) {
94             throw new AccessControlException(e);
95         }
96
97         if (url == null) {
98             if (getLogger().isDebugEnabled()) {
99                 getLogger().debug(" Document does not exist.");
100             }
101             url = webappUrl.substring(("/" + publication.getId()).length());
102         }
103
104         if (getLogger().isDebugEnabled()) {
105             getLogger().debug(" Using URL: [" + url + "]");
106         }
107         return url;
108     }
109
110     /**
111      * Returns the publication for a certain URL.
112      *
113      * @param url The webapp url.
114      * @return A publication.
115      * @throws AccessControlException when the publication could not be created.
116      */

117     protected Publication getPublication(String JavaDoc url) throws AccessControlException {
118         getLogger().debug("Building publication");
119
120         Publication publication;
121         Source source = null;
122         SourceResolver resolver = null;
123
124         try {
125             resolver = (SourceResolver) serviceManager.lookup(SourceResolver.ROLE);
126             source = resolver.resolveURI("context:///");
127             File JavaDoc servletContext = SourceUtil.getFile(source);
128             getLogger().debug(" Webapp URL: [" + url + "]");
129             getLogger().debug(" Serlvet context: [" + servletContext.getAbsolutePath() + "]");
130             publication = PublicationFactory.getPublication(url, servletContext);
131         } catch (Exception JavaDoc e) {
132             throw new AccessControlException(e);
133         } finally {
134             if (resolver != null) {
135                 if (source != null) {
136                     resolver.release(source);
137                 }
138                 serviceManager.release(resolver);
139             }
140         }
141         return publication;
142     }
143
144     private ServiceManager serviceManager;
145
146     /**
147      * Returns the service manager.
148      *
149      * @return A service manager.
150      */

151     protected ServiceManager getServiceManager() {
152         return serviceManager;
153     }
154
155     /**
156      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
157      */

158     public void service(ServiceManager manager) throws ServiceException {
159         this.serviceManager = manager;
160     }
161
162     /**
163      * @return Returns the policyManager.
164      */

165     public InheritingPolicyManager getPolicyManager() {
166         return policyManager;
167     }
168
169     /**
170      * @param policyManager The policyManager to set.
171      */

172     public void setPolicyManager(InheritingPolicyManager policyManager) {
173         this.policyManager = policyManager;
174     }
175
176     /**
177      * @see org.apache.lenya.ac.impl.InheritingPolicyManager#buildURLPolicy(org.apache.lenya.ac.AccreditableManager,
178      * java.lang.String)
179      */

180     public DefaultPolicy buildURLPolicy(AccreditableManager controller, String JavaDoc url)
181         throws AccessControlException {
182         return getPolicyManager().buildURLPolicy(controller, getPolicyURL(url));
183     }
184
185     /**
186      * @see org.apache.lenya.ac.impl.InheritingPolicyManager#buildSubtreePolicy(org.apache.lenya.ac.AccreditableManager,
187      * java.lang.String)
188      */

189     public DefaultPolicy buildSubtreePolicy(AccreditableManager controller, String JavaDoc url)
190         throws AccessControlException {
191         return getPolicyManager().buildSubtreePolicy(controller, getPolicyURL(url));
192     }
193
194     /**
195      * @see org.apache.lenya.ac.impl.InheritingPolicyManager#getPolicies(org.apache.lenya.ac.AccreditableManager,
196      * java.lang.String)
197      */

198     public DefaultPolicy[] getPolicies(AccreditableManager controller, String JavaDoc url)
199         throws AccessControlException {
200         return getPolicyManager().getPolicies(controller, getPolicyURL(url));
201     }
202
203     /**
204      * @see org.apache.lenya.ac.impl.InheritingPolicyManager#saveURLPolicy(java.lang.String,
205      * org.apache.lenya.ac.impl.DefaultPolicy)
206      */

207     public void saveURLPolicy(String JavaDoc url, DefaultPolicy policy) throws AccessControlException {
208         getPolicyManager().saveURLPolicy(getPolicyURL(url), policy);
209
210     }
211
212     /**
213      * @see org.apache.lenya.ac.impl.InheritingPolicyManager#saveSubtreePolicy(java.lang.String,
214      * org.apache.lenya.ac.impl.DefaultPolicy)
215      */

216     public void saveSubtreePolicy(String JavaDoc url, DefaultPolicy policy) throws AccessControlException {
217         getPolicyManager().saveSubtreePolicy(getPolicyURL(url), policy);
218     }
219
220     /**
221      * @see org.apache.lenya.ac.PolicyManager#getPolicy(org.apache.lenya.ac.AccreditableManager,
222      * java.lang.String)
223      */

224     public Policy getPolicy(AccreditableManager controller, String JavaDoc url)
225         throws AccessControlException {
226         return getPolicyManager().getPolicy(controller, getPolicyURL(url));
227     }
228
229     /**
230      * @see org.apache.lenya.ac.PolicyManager#accreditableRemoved(org.apache.lenya.ac.AccreditableManager,
231      * org.apache.lenya.ac.Accreditable)
232      */

233     public void accreditableRemoved(AccreditableManager manager, Accreditable accreditable)
234         throws AccessControlException {
235         getPolicyManager().accreditableRemoved(manager, accreditable);
236
237     }
238
239     String JavaDoc ELEMENT_POLICY_MANAGER = "policy-manager";
240     String JavaDoc ATTRIBUTE_TYPE = "type";
241
242     /**
243      * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
244      */

245     public void configure(Configuration configuration) throws ConfigurationException {
246         Configuration policyManagerConfiguration =
247             configuration.getChild(ELEMENT_POLICY_MANAGER, false);
248         if (policyManagerConfiguration != null) {
249             String JavaDoc type = policyManagerConfiguration.getAttribute(ATTRIBUTE_TYPE);
250             try {
251                 policyManagerSelector =
252                     (ServiceSelector) getServiceManager().lookup(PolicyManager.ROLE + "Selector");
253
254                 PolicyManager policyManager = (PolicyManager) policyManagerSelector.select(type);
255
256                 if (!(policyManager instanceof InheritingPolicyManager)) {
257                     throw new AccessControlException(
258                         "The "
259                             + getClass().getName()
260                             + " can only be used with an "
261                             + InheritingPolicyManager.class.getName()
262                             + ".");
263                 }
264
265                 DefaultAccessController.configureOrParameterize(policyManager, policyManagerConfiguration);
266                 setPolicyManager((InheritingPolicyManager) policyManager);
267             } catch (Exception JavaDoc e) {
268                 throw new ConfigurationException(
269                     "Obtaining policy manager for type [" + type + "] failed: ",
270                     e);
271             }
272         }
273     } /**
274        * @see org.apache.avalon.framework.activity.Disposable#dispose()
275        */

276     public void dispose() {
277         if (policyManagerSelector != null) {
278             if (getPolicyManager() != null) {
279                 policyManagerSelector.release(getPolicyManager());
280             }
281             getServiceManager().release(policyManagerSelector);
282         }
283         if (getLogger().isDebugEnabled()) {
284             getLogger().debug("Disposing [" + this +"]");
285         }
286
287     }
288
289     /**
290      * @see org.apache.lenya.ac.PolicyManager#accreditableAdded(org.apache.lenya.ac.AccreditableManager, org.apache.lenya.ac.Accreditable)
291      */

292     public void accreditableAdded(AccreditableManager manager, Accreditable accreditable) throws AccessControlException {
293         getPolicyManager().accreditableAdded(manager, accreditable);
294     }
295
296 }
297
Popular Tags