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.HashMap;
023: import java.util.Map;
024:
025: import org.apache.avalon.framework.logger.Logger;
026: import org.apache.lenya.ac.AccessControlException;
027: import org.apache.lenya.ac.AccreditableManager;
028: import org.apache.lenya.ac.Item;
029: import org.apache.lenya.ac.Role;
030: import org.apache.lenya.ac.RoleManager;
031:
032: /**
033: * File-based role manager implementation.
034: * @version $Id: FileRoleManager.java 485769 2006-12-11 17:41:23Z andreas $
035: */
036: public final class FileRoleManager extends FileItemManager implements
037: RoleManager {
038: protected static final String SUFFIX = ".rml";
039: private static Map instances = new HashMap();
040:
041: /**
042: * Return the <code>RoleManager</code> for this configuration directory. The
043: * <code>RoleManager</code> is a singleton.
044: * @param mgr The accreditable manager.
045: */
046: protected FileRoleManager(AccreditableManager mgr) {
047: super (mgr);
048: }
049:
050: /**
051: * Returns the role manager for this configuration directory.
052: * @param mgr The accreditable manager.
053: * @param configurationDirectory The configuration directory.
054: * @param logger The logger.
055: * @return A role manager.
056: * @throws AccessControlException when something went wrong.
057: */
058: public static FileRoleManager instance(AccreditableManager mgr,
059: File configurationDirectory, Logger logger)
060: throws AccessControlException {
061: if (!instances.containsKey(configurationDirectory)) {
062: FileRoleManager manager = new FileRoleManager(mgr);
063: manager.enableLogging(logger);
064: manager.configure(configurationDirectory);
065: instances.put(configurationDirectory, manager);
066: }
067:
068: return (FileRoleManager) instances.get(configurationDirectory);
069: }
070:
071: /**
072: * Get the role for the given ID.
073: * @param roleId The name of the role requested.
074: * @return a <code>Role</code> or null if no role with the given name found
075: */
076: public Role getRole(String roleId) {
077: return (Role) getItem(roleId);
078: }
079:
080: /**
081: * @see org.apache.lenya.ac.file.FileItemManager#getSuffix()
082: */
083: protected String getSuffix() {
084: return SUFFIX;
085: }
086:
087: /**
088: * Get all roles
089: * @return an array of roles.
090: */
091: public Role[] getRoles() {
092: Item[] items = super .getItems();
093: Role[] roles = new Role[items.length];
094: for (int i = 0; i < roles.length; i++) {
095: roles[i] = (Role) items[i];
096: }
097: return roles;
098: }
099:
100: /**
101: * Add a role
102: * @param role The role to add.
103: * @throws AccessControlException if an error occurs.
104: */
105: public void add(Role role) throws AccessControlException {
106: super .add(role);
107: }
108:
109: /**
110: * Remove a role
111: * @param role The role to remove.
112: * @throws AccessControlException if an error occurs.
113: */
114: public void remove(Role role) throws AccessControlException {
115: super .remove(role);
116: }
117:
118: protected Item createItem() {
119: return new FileRole(this, getLogger());
120: }
121: }
|