KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > ac > file > FileAccreditableManager


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: FileAccreditableManager.java 169196 2005-05-09 00:02:50Z gregor $ */
19
20 package org.apache.lenya.ac.file;
21
22 import java.io.File JavaDoc;
23 import java.net.URI JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import org.apache.avalon.framework.configuration.Configuration;
29 import org.apache.avalon.framework.configuration.ConfigurationException;
30 import org.apache.avalon.framework.configuration.Configurable;
31 import org.apache.avalon.framework.parameters.ParameterException;
32 import org.apache.avalon.framework.parameters.Parameterizable;
33 import org.apache.avalon.framework.parameters.Parameters;
34 import org.apache.avalon.framework.service.ServiceException;
35 import org.apache.avalon.framework.service.ServiceManager;
36 import org.apache.avalon.framework.service.Serviceable;
37 import org.apache.excalibur.source.Source;
38 import org.apache.excalibur.source.SourceResolver;
39 import org.apache.lenya.ac.AccessControlException;
40 import org.apache.lenya.ac.GroupManager;
41 import org.apache.lenya.ac.IPRangeManager;
42 import org.apache.lenya.ac.RoleManager;
43 import org.apache.lenya.ac.UserManager;
44 import org.apache.lenya.ac.UserType;
45 import org.apache.lenya.ac.impl.AbstractAccreditableManager;
46
47 import org.apache.cocoon.util.NetUtils;
48
49 /**
50  * File-based accreditable manager.
51  */

52 public class FileAccreditableManager extends AbstractAccreditableManager implements Serviceable,
53         Configurable, Parameterizable {
54
55     /**
56      * Creates a new FileAccreditableManager. If you use this constructor, you have to set the
57      * configuration directory either by calling {@link #setConfigurationDirectory(File)}or by
58      * calling {@link #parameterize(Parameters)}.
59      */

60     public FileAccreditableManager() {
61     }
62
63     /**
64      * Creates a new FileAccessController based on a configuration directory.
65      *
66      * @param configurationDirectory The configuration directory.
67      * @param userTypes The supported user types.
68      */

69     public FileAccreditableManager(File JavaDoc configurationDirectory, UserType[] userTypes) {
70         assert configurationDirectory != null;
71         assert configurationDirectory.exists();
72         assert configurationDirectory.isDirectory();
73         this.configurationDirectory = configurationDirectory;
74         this.userTypes = new HashSet JavaDoc(Arrays.asList(userTypes));
75     }
76
77     private File JavaDoc configurationDirectory;
78     private Set JavaDoc userTypes;
79
80     /**
81      * Returns the supported user types.
82      * @return An array of user types.
83      * @throws AccessControlException if an error occurs.
84      */

85     public UserType[] getUserTypes() throws AccessControlException {
86         if (userTypes == null)
87             throw new AccessControlException("User types not initialized");
88         return (UserType[]) userTypes.toArray(new UserType[userTypes.size()]);
89     }
90
91     /**
92      * Returns the configuration directory.
93      *
94      * @return The configuration directory.
95      * @throws AccessControlException when something went wrong.
96      */

97     public File JavaDoc getConfigurationDirectory() throws AccessControlException {
98
99         if (configurationDirectory == null) {
100
101             if (configurationDirectoryPath == null) {
102                 throw new AccessControlException("Configuration directory not set!");
103             }
104
105             Source source = null;
106             SourceResolver resolver = null;
107             File JavaDoc directory;
108             try {
109
110                 getLogger().debug(
111                         "Configuration directory Path: [" + configurationDirectoryPath + "]");
112
113                 resolver = (SourceResolver) getManager().lookup(SourceResolver.ROLE);
114                 source = resolver.resolveURI(configurationDirectoryPath);
115
116                 getLogger().debug("Configuration directory URI: " + source.getURI());
117                 directory = new File JavaDoc(new URI JavaDoc(NetUtils.encodePath(source.getURI())));
118             } catch (Exception JavaDoc e) {
119                 throw new AccessControlException(e);
120             } finally {
121                 if (resolver != null) {
122                     if (source != null) {
123                         resolver.release(source);
124                     }
125                     getManager().release(resolver);
126                 }
127             }
128             setConfigurationDirectory(directory);
129         }
130
131         return configurationDirectory;
132     }
133
134     protected static final String JavaDoc DIRECTORY = "directory";
135
136     /**
137      * @see org.apache.avalon.framework.parameters.Parameterizable#parameterize(org.apache.avalon.framework.parameters.Parameters)
138      */

139     public void parameterize(Parameters parameters) throws ParameterException {
140         if (parameters.isParameter(DIRECTORY)) {
141             configurationDirectoryPath = parameters.getParameter(DIRECTORY);
142             getLogger().debug("Configuration directory: [" + configurationDirectoryPath + "]");
143         }
144     }
145
146     protected static final String JavaDoc A_M_TAG = "accreditable-manager";
147     protected static final String JavaDoc U_M_CHILD_TAG = "user-manager";
148     protected static final String JavaDoc U_T_CHILD_TAG = "user-type";
149     protected static final String JavaDoc U_T_CLASS_ATTRIBUTE = "class";
150     protected static final String JavaDoc U_T_CREATE_ATTRIBUTE = "create-use-case";
151     // provided for backward compatibility
152
protected static final String JavaDoc DEFAULT_USER_TYPE_CLASS = FileUser.class.getName();
153     protected static final String JavaDoc DEFAULT_USER_TYPE_KEY = "Local User";
154     protected static final String JavaDoc DEFAULT_USER_CREATE_USE_CASE = "userAddUser";
155
156     /**
157      * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
158      * added to read new user-manager block within accreditable-manager
159      */

160     public void configure(Configuration configuration) throws ConfigurationException {
161         // note:
162
// we need to distinguish the case where configure is called from
163
// somewhere else (i.e. not due to the reading of ac.xconf),
164
// from the case where the child user-manager does not exist,
165
// for instance when reading an old ac.xconf which does not yet
166
// have this configuration. So for backward compatibility, we need
167
// to distinguish the 2 cases
168
if (A_M_TAG.equals(configuration.getName())) {
169             userTypes = new HashSet JavaDoc();
170             Configuration umConf = configuration.getChild(U_M_CHILD_TAG, false);
171             if (umConf != null) {
172                 Configuration[] typeConfs = umConf.getChildren();
173                 for (int i = 0; i < typeConfs.length; i++) {
174                     userTypes.add(new UserType(typeConfs[i].getValue(), typeConfs[i]
175                             .getAttribute(U_T_CLASS_ATTRIBUTE), typeConfs[i]
176                             .getAttribute(U_T_CREATE_ATTRIBUTE)));
177                 }
178             } else {
179                 getLogger().debug(
180                         "FileAccreditableManager: using default configuration for user types");
181                 // no "user-manager" block in access control: provide
182
// a default for backward compatibility
183
userTypes.add(getDefaultUserType());
184             }
185             // maybe todo (or is it overkill?) : validate the parametrized user
186
// types, for example, check if the classes are in the classpath ?
187
}
188     }
189
190     /**
191      * Returns the default user type.
192      * @return A user type.
193      */

194     public static UserType getDefaultUserType() {
195         return new UserType(DEFAULT_USER_TYPE_KEY, DEFAULT_USER_TYPE_CLASS,
196                 DEFAULT_USER_CREATE_USE_CASE);
197     }
198
199     private String JavaDoc configurationDirectoryPath;
200
201     /**
202      * Sets the configuration directory.
203      *
204      * @param file The configuration directory.
205      *
206      * @throws AccessControlException if an error occurs
207      */

208     public void setConfigurationDirectory(File JavaDoc file) throws AccessControlException {
209         if (file == null || !file.isDirectory()) {
210             throw new AccessControlException("Configuration directory [" + file
211                     + "] does not exist!");
212         }
213         configurationDirectory = file;
214     }
215
216     private ServiceManager manager;
217
218     /**
219      * Set the global component manager.
220      *
221      * @param manager The global component manager
222      * @throws ServiceException when something went wrong.
223      */

224     public void service(ServiceManager manager) throws ServiceException {
225         this.manager = manager;
226     }
227
228     /**
229      * Returns the service manager.
230      *
231      * @return A service manager.
232      */

233     protected ServiceManager getManager() {
234         return manager;
235     }
236
237     /**
238      * @see org.apache.lenya.ac.impl.AbstractAccreditableManager#initializeGroupManager()
239      */

240     protected GroupManager initializeGroupManager() throws AccessControlException {
241         return FileGroupManager.instance(getConfigurationDirectory());
242     }
243
244     /**
245      * @see org.apache.lenya.ac.impl.AbstractAccreditableManager#initializeIPRangeManager()
246      */

247     protected IPRangeManager initializeIPRangeManager() throws AccessControlException {
248         return FileIPRangeManager.instance(getConfigurationDirectory());
249     }
250
251     /**
252      * @see org.apache.lenya.ac.impl.AbstractAccreditableManager#initializeRoleManager()
253      */

254     protected RoleManager initializeRoleManager() throws AccessControlException {
255         return FileRoleManager.instance(getConfigurationDirectory());
256     }
257
258     /**
259      * @see org.apache.lenya.ac.impl.AbstractAccreditableManager#initializeUserManager()
260      */

261     protected UserManager initializeUserManager() throws AccessControlException {
262         return FileUserManager.instance(getConfigurationDirectory(), getUserTypes());
263     }
264
265 }
Popular Tags