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: package org.apache.lenya.ac.file;
019:
020: import java.util.HashMap;
021: import java.util.HashSet;
022: import java.util.Map;
023: import java.util.Set;
024:
025: import org.apache.avalon.framework.configuration.Configuration;
026: import org.apache.avalon.framework.configuration.ConfigurationException;
027: import org.apache.avalon.framework.logger.AbstractLogEnabled;
028: import org.apache.avalon.framework.service.ServiceException;
029: import org.apache.avalon.framework.service.ServiceManager;
030: import org.apache.avalon.framework.service.Serviceable;
031: import org.apache.avalon.framework.thread.ThreadSafe;
032: import org.apache.lenya.ac.AccreditableManager;
033: import org.apache.lenya.ac.AccreditableManagerFactory;
034: import org.apache.lenya.ac.UserType;
035:
036: /**
037: * Factory for file-based accreditable managers.
038: */
039: public class FileAccreditableManagerFactory extends AbstractLogEnabled
040: implements AccreditableManagerFactory, ThreadSafe, Serviceable {
041:
042: private Map id2manager = new HashMap();
043:
044: protected static final String U_M_CHILD_TAG = "user-manager";
045: protected static final String U_T_CHILD_TAG = "user-type";
046: protected static final String U_T_CLASS_ATTRIBUTE = "class";
047: protected static final String U_T_CREATE_ATTRIBUTE = "create-use-case";
048:
049: public AccreditableManager getAccreditableManager(
050: Configuration config) throws ConfigurationException {
051:
052: try {
053: String configUri = null;
054: Configuration[] paramConfigs = config
055: .getChildren("parameter");
056: for (int i = 0; i < paramConfigs.length; i++) {
057: if (paramConfigs[i].getAttribute("name").equals(
058: "directory")) {
059: configUri = paramConfigs[i].getAttribute("value");
060: }
061: }
062:
063: if (configUri == null) {
064: throw new RuntimeException(
065: "No <parameter name=\"directory\"> element found!");
066: }
067:
068: if (this .id2manager.containsKey(configUri)) {
069: return (AccreditableManager) this .id2manager
070: .get(configUri);
071: } else {
072: Set userTypes = new HashSet();
073: Configuration umConf = config.getChild(U_M_CHILD_TAG,
074: false);
075: if (umConf != null) {
076: Configuration[] typeConfs = umConf.getChildren();
077: for (int i = 0; i < typeConfs.length; i++) {
078: userTypes
079: .add(new UserType(
080: typeConfs[i].getValue(),
081: typeConfs[i]
082: .getAttribute(U_T_CLASS_ATTRIBUTE),
083: typeConfs[i]
084: .getAttribute(U_T_CREATE_ATTRIBUTE)));
085: }
086: } else {
087: getLogger()
088: .debug(
089: "FileAccreditableManager: using default configuration for user types");
090: // no "user-manager" block in access control: provide
091: // a default for backward compatibility
092: userTypes.add(FileAccreditableManager
093: .getDefaultUserType());
094: }
095: UserType[] types = (UserType[]) userTypes
096: .toArray(new UserType[userTypes.size()]);
097: AccreditableManager mgr = new FileAccreditableManager(
098: this .manager, getLogger(), configUri, types);
099: this .id2manager.put(configUri, mgr);
100: return mgr;
101: }
102:
103: } catch (Exception e) {
104: throw new RuntimeException(e);
105: }
106: }
107:
108: protected ServiceManager manager;
109:
110: public void service(ServiceManager manager) throws ServiceException {
111: this.manager = manager;
112: }
113:
114: }
|