KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.lenya.cms.ac;
19
20 import org.apache.avalon.framework.logger.AbstractLogEnabled;
21 import org.apache.avalon.framework.service.ServiceException;
22 import org.apache.avalon.framework.service.ServiceManager;
23 import org.apache.avalon.framework.service.Serviceable;
24 import org.apache.excalibur.source.Source;
25 import org.apache.excalibur.source.SourceResolver;
26 import org.apache.lenya.ac.AccessControlException;
27 import org.apache.lenya.ac.Accreditable;
28 import org.apache.lenya.ac.AccreditableManager;
29 import org.apache.lenya.ac.Policy;
30 import org.apache.lenya.ac.PolicyManager;
31 import org.apache.lenya.ac.impl.PolicyBuilder;
32 import org.apache.lenya.xml.DocumentHelper;
33 import org.w3c.dom.Document JavaDoc;
34
35 /**
36  * Policy manager based on Cocoon sitemaps.
37  * @version $Id: SitemapPolicyManager.java 43242 2004-08-16 16:42:32Z andreas $
38  */

39 public class SitemapPolicyManager extends AbstractLogEnabled implements PolicyManager, Serviceable {
40
41     /**
42      * @see org.apache.lenya.ac.PolicyManager#getPolicy(org.apache.lenya.ac.AccreditableManager,
43      * java.lang.String)
44      */

45     public Policy getPolicy(AccreditableManager accreditableManager, String JavaDoc url)
46             throws AccessControlException {
47
48         url = url.substring(1);
49
50         int slashIndex = url.indexOf("/");
51         if (slashIndex == -1) {
52             slashIndex = url.length();
53         }
54
55         String JavaDoc publicationId = url.substring(0, slashIndex);
56         url = url.substring(publicationId.length());
57
58         SourceResolver resolver = null;
59         Policy policy = null;
60         Source source = null;
61         try {
62             resolver = (SourceResolver) getManager().lookup(SourceResolver.ROLE);
63
64             String JavaDoc policyUrl = publicationId + "/policies" + url + ".acml";
65             getLogger().debug("Policy URL: " + policyUrl);
66             source = resolver.resolveURI("cocoon://" + policyUrl);
67             Document JavaDoc document = DocumentHelper.readDocument(source.getInputStream());
68             policy = new PolicyBuilder(accreditableManager).buildPolicy(document);
69
70         } catch (Exception JavaDoc e) {
71             throw new AccessControlException(e);
72         } finally {
73             if (resolver != null) {
74                 if (source != null) {
75                     resolver.release(source);
76                 }
77                 getManager().release(resolver);
78             }
79         }
80         return policy;
81     }
82
83     private ServiceManager manager;
84
85     /**
86      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
87      */

88     public void service(ServiceManager manager) throws ServiceException {
89         this.manager = manager;
90     }
91
92     /**
93      * Returns the service manager.
94      * @return A service manager.
95      */

96     public ServiceManager getManager() {
97         return manager;
98     }
99
100     /**
101      * @see org.apache.lenya.ac.PolicyManager#accreditableRemoved(org.apache.lenya.ac.AccreditableManager,
102      * org.apache.lenya.ac.Accreditable)
103      */

104     public void accreditableRemoved(AccreditableManager manager, Accreditable accreditable)
105             throws AccessControlException {
106     }
107
108     /**
109      * @see org.apache.lenya.ac.PolicyManager#accreditableAdded(org.apache.lenya.ac.AccreditableManager,
110      * org.apache.lenya.ac.Accreditable)
111      */

112     public void accreditableAdded(AccreditableManager manager, Accreditable accreditable)
113             throws AccessControlException {
114     }
115
116 }
Popular Tags