KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > ac > impl > ComposableAccessControllerResolver


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.ac.impl;
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.avalon.framework.activity.Disposable;
24 import org.apache.avalon.framework.configuration.Configurable;
25 import org.apache.avalon.framework.configuration.Configuration;
26 import org.apache.avalon.framework.configuration.ConfigurationException;
27 import org.apache.avalon.framework.service.ServiceException;
28 import org.apache.avalon.framework.service.ServiceSelector;
29 import org.apache.lenya.ac.AccessControlException;
30 import org.apache.lenya.ac.AccessController;
31 import org.apache.lenya.ac.AccessControllerResolver;
32
33 /**
34  * Access controller resolver composed of other access controller resolvers.
35  * The member resolvers are called one after the other to resolve the access controllers.
36  *
37  * @version $Id: ComposableAccessControllerResolver.java 43241 2004-08-16 16:36:57Z andreas $
38  */

39 public class ComposableAccessControllerResolver
40     extends AbstractAccessControllerResolver
41     implements Configurable, Disposable {
42
43     /**
44      * @see org.apache.lenya.ac.impl.AbstractAccessControllerResolver#doResolveAccessController(java.lang.String)
45      */

46     public AccessController doResolveAccessController(String JavaDoc url) throws AccessControlException {
47
48         AccessController controller = null;
49
50         try {
51             
52             if (selector == null) {
53                 selector =
54                     (ServiceSelector) getManager().lookup(AccessControllerResolver.ROLE + "Selector");
55             }
56
57             String JavaDoc[] types = getResolverTypes();
58             int i = 0;
59             while (controller == null && i < types.length) {
60
61                 getLogger().debug("Trying to resolve AC resolver for type [" + types[i] + "]");
62                 AccessControllerResolver resolver =
63                     (AccessControllerResolver) selector.select(types[i]);
64                 controller = resolver.resolveAccessController(url);
65                 setResolver(controller, resolver);
66                 getLogger().debug("Resolved access controller [" + controller + "]");
67                 i++;
68             }
69
70         } catch (ServiceException e) {
71             throw new AccessControlException(e);
72         }
73
74         return controller;
75     }
76
77     private Map JavaDoc controllerToResolver = new HashMap JavaDoc();
78
79     /**
80      * @see org.apache.lenya.ac.AccessControllerResolver#release(org.apache.lenya.ac.AccessController)
81      */

82     public void release(AccessController controller) {
83         assert controller != null;
84         AccessControllerResolver resolver = getResolver(controller);
85         resolver.release(controller);
86         selector.release(resolver);
87     }
88
89     /**
90      * Returns the access controller resolver that was used to resolve a
91      * specific access controller.
92      * @param controller The access controller.
93      * @return An AC resolver.
94      */

95     protected AccessControllerResolver getResolver(AccessController controller) {
96         AccessControllerResolver resolver =
97             (AccessControllerResolver) controllerToResolver.get(controller);
98         return resolver;
99     }
100     
101     /**
102      * Sets the access controller resolver that was used to resolve a
103      * specific access controller.
104      * @param controller The access controller.
105      * @param resolver An AC resolver.
106      */

107     protected void setResolver(AccessController controller, AccessControllerResolver resolver) {
108         controllerToResolver.put(controller, resolver);
109     }
110
111     protected static final String JavaDoc RESOLVER_ELEMENT = "resolver";
112     protected static final String JavaDoc TYPE_ATTRIBUTE = "type";
113
114     private String JavaDoc[] resolverTypes;
115     private ServiceSelector selector;
116
117     /**
118      * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
119      */

120     public void configure(Configuration configuration) throws ConfigurationException {
121         Configuration[] accessControllerConfigs = configuration.getChildren(RESOLVER_ELEMENT);
122         resolverTypes = new String JavaDoc[accessControllerConfigs.length];
123         for (int i = 0; i < accessControllerConfigs.length; i++) {
124             resolverTypes[i] = accessControllerConfigs[i].getAttribute(TYPE_ATTRIBUTE);
125         }
126     }
127
128     /**
129      * Returns the access controller types.
130      * @return A string array.
131      */

132     protected String JavaDoc[] getResolverTypes() {
133         return resolverTypes;
134     }
135
136     /**
137      * @see org.apache.avalon.framework.activity.Disposable#dispose()
138      */

139     public void dispose() {
140         if (selector != null) {
141             getManager().release(selector);
142         }
143     }
144
145 }
146
Popular Tags