001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: /* $Id: FileAccreditableManager.java 508055 2007-02-15 18:04:30Z andreas $ */
020:
021: package org.apache.lenya.ac.file;
022:
023: import java.io.File;
024: import java.io.IOException;
025: import java.net.URI;
026: import java.util.Arrays;
027: import java.util.HashSet;
028: import java.util.Set;
029:
030: import org.apache.avalon.framework.logger.Logger;
031: import org.apache.avalon.framework.service.ServiceManager;
032: import org.apache.cocoon.util.NetUtils;
033: import org.apache.excalibur.source.Source;
034: import org.apache.excalibur.source.SourceResolver;
035: import org.apache.lenya.ac.AccessControlException;
036: import org.apache.lenya.ac.GroupManager;
037: import org.apache.lenya.ac.IPRangeManager;
038: import org.apache.lenya.ac.RoleManager;
039: import org.apache.lenya.ac.UserManager;
040: import org.apache.lenya.ac.UserType;
041: import org.apache.lenya.ac.impl.AbstractAccreditableManager;
042: import org.apache.lenya.util.Assert;
043:
044: /**
045: * File-based accreditable manager.
046: */
047: public class FileAccreditableManager extends
048: AbstractAccreditableManager {
049:
050: private ServiceManager manager;
051:
052: /**
053: * Creates a new FileAccessController based on a configuration directory.
054: * @param manager The service manager.
055: * @param logger The logger.
056: * @param configurationUri The configuration directory URI.
057: * @param _userTypes The supported user types.
058: */
059: public FileAccreditableManager(ServiceManager manager,
060: Logger logger, String configurationUri,
061: UserType[] _userTypes) {
062: super (logger);
063:
064: Assert.notNull("service manager", manager);
065: this .manager = manager;
066:
067: Assert.notNull("configuration directory", configurationUri);
068: this .configurationDirectoryUri = configurationUri;
069:
070: this .userTypes = new HashSet(Arrays.asList(_userTypes));
071: }
072:
073: private File configurationDirectory;
074: private Set userTypes;
075:
076: /**
077: * Returns the supported user types.
078: * @return An array of user types.
079: * @throws AccessControlException if an error occurs.
080: */
081: public UserType[] getUserTypes() throws AccessControlException {
082: if (this .userTypes == null)
083: throw new AccessControlException(
084: "User types not initialized");
085: return (UserType[]) this .userTypes
086: .toArray(new UserType[this .userTypes.size()]);
087: }
088:
089: /**
090: * Returns the configuration directory.
091: * @return The configuration directory.
092: * @throws AccessControlException when something went wrong.
093: */
094: public File getConfigurationDirectory()
095: throws AccessControlException {
096:
097: if (this .configurationDirectory == null) {
098:
099: if (this .configurationDirectoryUri == null) {
100: throw new AccessControlException(
101: "Configuration directory not set!");
102: }
103:
104: Source source = null;
105: SourceResolver resolver = null;
106: File directory;
107: try {
108:
109: getLogger().debug(
110: "Configuration directory Path: ["
111: + this .configurationDirectoryUri + "]");
112:
113: resolver = (SourceResolver) getManager().lookup(
114: SourceResolver.ROLE);
115: source = resolver
116: .resolveURI(this .configurationDirectoryUri);
117:
118: getLogger().debug(
119: "Configuration directory URI: "
120: + source.getURI());
121: directory = new File(new URI(NetUtils.encodePath(source
122: .getURI())));
123: } catch (Exception e) {
124: throw new AccessControlException(e);
125: } finally {
126: if (resolver != null) {
127: if (source != null) {
128: resolver.release(source);
129: }
130: getManager().release(resolver);
131: }
132: }
133: this .configurationDirectory = directory;
134: }
135:
136: return this .configurationDirectory;
137: }
138:
139: // provided for backward compatibility
140: protected static final String DEFAULT_USER_TYPE_CLASS = FileUser.class
141: .getName();
142: protected static final String DEFAULT_USER_TYPE_KEY = "Local User";
143: protected static final String DEFAULT_USER_CREATE_USE_CASE = "userAddUser";
144:
145: /**
146: * Returns the default user type.
147: * @return A user type.
148: */
149: public static UserType getDefaultUserType() {
150: return new UserType(DEFAULT_USER_TYPE_KEY,
151: DEFAULT_USER_TYPE_CLASS, DEFAULT_USER_CREATE_USE_CASE);
152: }
153:
154: private String configurationDirectoryUri;
155:
156: /**
157: * Returns the service manager.
158: * @return A service manager.
159: */
160: protected ServiceManager getManager() {
161: return this .manager;
162: }
163:
164: /**
165: * @see org.apache.lenya.ac.impl.AbstractAccreditableManager#initializeGroupManager()
166: */
167: protected GroupManager initializeGroupManager()
168: throws AccessControlException {
169: FileGroupManager _manager = FileGroupManager.instance(this ,
170: getConfigurationDirectory(), getLogger());
171: return _manager;
172: }
173:
174: /**
175: * @see org.apache.lenya.ac.impl.AbstractAccreditableManager#initializeIPRangeManager()
176: */
177: protected IPRangeManager initializeIPRangeManager()
178: throws AccessControlException {
179: FileIPRangeManager _manager = FileIPRangeManager.instance(this ,
180: getConfigurationDirectory(), getLogger());
181: return _manager;
182: }
183:
184: /**
185: * @see org.apache.lenya.ac.impl.AbstractAccreditableManager#initializeRoleManager()
186: */
187: protected RoleManager initializeRoleManager()
188: throws AccessControlException {
189: FileRoleManager _manager = FileRoleManager.instance(this ,
190: getConfigurationDirectory(), getLogger());
191: return _manager;
192: }
193:
194: /**
195: * @see org.apache.lenya.ac.impl.AbstractAccreditableManager#initializeUserManager()
196: */
197: protected UserManager initializeUserManager()
198: throws AccessControlException {
199: FileUserManager _manager = FileUserManager.instance(this ,
200: getConfigurationDirectory(), getUserTypes(),
201: getLogger());
202: return _manager;
203: }
204:
205: public String getConfigurationCollectionUri() {
206: try {
207: return "file://"
208: + getConfigurationDirectory().getCanonicalPath();
209: } catch (Exception e) {
210: throw new RuntimeException(e);
211: }
212: }
213:
214: public String getId() {
215: try {
216: Assert.notNull("configuration directory", this
217: .getConfigurationDirectory());
218: return this .getConfigurationDirectory().getCanonicalPath();
219: } catch (IOException e) {
220: throw new RuntimeException(e);
221: } catch (AccessControlException e) {
222: throw new RuntimeException(e);
223: }
224: }
225:
226: }
|