KickJava   Java API By Example, From Geeks To Geeks.

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


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 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.avalon.framework.thread.ThreadSafe;
25 import org.apache.excalibur.source.SourceResolver;
26 import org.apache.lenya.ac.AccessControlException;
27 import org.apache.lenya.ac.AccessController;
28 import org.apache.lenya.ac.AccessControllerResolver;
29 import org.apache.lenya.ac.cache.URLKeyUtil;
30 import org.apache.lenya.util.CacheMap;
31
32 /**
33  * Abstract implementation for access controller resolvers.
34  * @version $Id: AbstractAccessControllerResolver.java 43241 2004-08-16 16:36:57Z andreas $
35  */

36 public abstract class AbstractAccessControllerResolver
37     extends AbstractLogEnabled
38     implements AccessControllerResolver, Serviceable, ThreadSafe {
39
40     protected static final int CAPACITY = 1000;
41     private CacheMap cache = new CacheMap(CAPACITY);
42
43     /**
44      * @see org.apache.lenya.ac.AccessControllerResolver#resolveAccessController(java.lang.String)
45      */

46     public AccessController resolveAccessController(String JavaDoc webappUrl)
47         throws AccessControlException {
48
49         SourceResolver resolver = null;
50         AccessController controller = null;
51         Object JavaDoc key = null;
52
53         try {
54             resolver = (SourceResolver) getManager().lookup(SourceResolver.ROLE);
55             key = generateCacheKey(webappUrl, resolver);
56             getLogger().debug("Access controller cache key: [" + key + "]");
57
58         } catch (Exception JavaDoc e) {
59             throw new AccessControlException(e);
60         } finally {
61             if (resolver != null) {
62                 getManager().release(resolver);
63             }
64         }
65
66         synchronized (cache) {
67             controller = (AccessController) cache.get(key);
68             if (controller == null) {
69                 getLogger().debug("No access controller in cache.");
70                 controller = doResolveAccessController(webappUrl);
71                 cache.put(key, controller);
72             } else {
73                 getLogger().debug("Getting access controller from cache.");
74             }
75         }
76
77         return controller;
78     }
79
80     /**
81      * Generates a cache key for the access controller.
82      * @param webappUrl The webapp URL.
83      * @param resolver The source resolver.
84      * @return An object.
85      * @throws AccessControlException when something went wrong.
86      */

87     protected Object JavaDoc generateCacheKey(String JavaDoc webappUrl, SourceResolver resolver)
88         throws AccessControlException {
89         Object JavaDoc key;
90         try {
91             key = URLKeyUtil.generateKey(resolver, webappUrl);
92         } catch (Exception JavaDoc e) {
93             throw new AccessControlException(e);
94         }
95         return key;
96     }
97
98     /**
99      * The actual resolving method.
100      * @param webappUrl The URL within the web application.
101      * @return An access controller.
102      * @throws AccessControlException when something went wrong.
103      */

104     protected abstract AccessController doResolveAccessController(String JavaDoc webappUrl)
105         throws AccessControlException;
106
107     /**
108      * @see org.apache.lenya.ac.AccessControllerResolver#release(org.apache.lenya.ac.AccessController)
109      */

110     public void release(AccessController controller) {
111         /*
112         if (controller != null) {
113             getManager().release(controller);
114         }
115         */

116     }
117
118     private ServiceManager manager;
119
120     /**
121      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
122      */

123     public void service(ServiceManager manager) throws ServiceException {
124         getLogger().debug("Servicing [" + getClass().getName() + "]");
125         this.manager = manager;
126     }
127
128     /**
129      * Returns the service manager of this Serviceable.
130      * @return A service manager.
131      */

132     public ServiceManager getManager() {
133         return manager;
134     }
135
136 }
137
Popular Tags