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: package org.apache.lenya.ac.file;
020:
021: import java.io.File;
022: import java.util.Arrays;
023: import java.util.HashMap;
024: import java.util.HashSet;
025: import java.util.Map;
026: import java.util.Set;
027:
028: import org.apache.avalon.framework.logger.Logger;
029: import org.apache.lenya.ac.AccessControlException;
030: import org.apache.lenya.ac.AccreditableManager;
031: import org.apache.lenya.ac.Item;
032: import org.apache.lenya.ac.User;
033: import org.apache.lenya.ac.UserManager;
034: import org.apache.lenya.ac.UserType;
035:
036: /**
037: * File-based user manager implementation.
038: * @version $Id: FileUserManager.java 485769 2006-12-11 17:41:23Z andreas $
039: */
040: public class FileUserManager extends FileItemManager implements
041: UserManager {
042:
043: private static Map instances = new HashMap();
044: private Set userTypes;
045:
046: /**
047: * Create a UserManager
048: *
049: * @param mgr The accreditable manager.
050: * @param _userTypes The supported user types.
051: * @throws AccessControlException if the UserManager could not be instantiated.
052: */
053: private FileUserManager(AccreditableManager mgr,
054: UserType[] _userTypes) throws AccessControlException {
055: super (mgr);
056: this .userTypes = new HashSet(Arrays.asList(_userTypes));
057: }
058:
059: /**
060: * Describe <code>instance</code> method here.
061: *
062: * @param mgr The accreditable manager.
063: * @param configurationDirectory a directory
064: * @param userTypes The supported user types.
065: * @param logger The logger.
066: * @return an <code>UserManager</code> value
067: * @exception AccessControlException if an error occurs
068: */
069: public static FileUserManager instance(AccreditableManager mgr,
070: File configurationDirectory, UserType[] userTypes,
071: Logger logger) throws AccessControlException {
072:
073: assert configurationDirectory != null;
074: if (!configurationDirectory.isDirectory()) {
075: throw new AccessControlException(
076: "Configuration directory ["
077: + configurationDirectory
078: + "] does not exist!");
079: }
080:
081: if (!instances.containsKey(configurationDirectory)) {
082: FileUserManager manager = new FileUserManager(mgr,
083: userTypes);
084: manager.enableLogging(logger);
085: manager.configure(configurationDirectory);
086: instances.put(configurationDirectory, manager);
087: }
088:
089: return (FileUserManager) instances.get(configurationDirectory);
090: }
091:
092: /**
093: * Get all users.
094: *
095: * @return an Iterator to iterate over all users
096: */
097: public User[] getUsers() {
098: Item[] items = super .getItems();
099: User[] users = new User[items.length];
100: for (int i = 0; i < users.length; i++) {
101: users[i] = (User) items[i];
102: }
103: return users;
104: }
105:
106: /**
107: * @see org.apache.lenya.ac.UserManager#add(org.apache.lenya.ac.User)
108: */
109: public void add(User user) throws AccessControlException {
110: super .add(user);
111: }
112:
113: /**
114: * @see org.apache.lenya.ac.UserManager#remove(org.apache.lenya.ac.User)
115: */
116: public void remove(User user) throws AccessControlException {
117: super .remove(user);
118: }
119:
120: /**
121: * Get the user with the given user id.
122: *
123: * @param userId user id of requested user
124: * @return the requested user or null if there is no user with the given user id
125: */
126: public User getUser(String userId) {
127: return (User) getItem(userId);
128: }
129:
130: /**
131: * @see org.apache.lenya.ac.UserManager#getUserTypes()
132: */
133: public UserType[] getUserTypes() {
134: return (UserType[]) this .userTypes
135: .toArray(new UserType[this .userTypes.size()]);
136: }
137:
138: protected static final String SUFFIX = ".iml";
139:
140: /**
141: * @see org.apache.lenya.ac.file.FileItemManager#getSuffix()
142: */
143: protected String getSuffix() {
144: return SUFFIX;
145: }
146:
147: }
|