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.geronimo.security.jacc;
019    
020    import java.util.HashMap;
021    import java.util.Map;
022    import javax.security.jacc.PolicyConfiguration;
023    import javax.security.jacc.PolicyConfigurationFactory;
024    import javax.security.jacc.PolicyContextException;
025    
026    import org.apache.commons.logging.Log;
027    import org.apache.commons.logging.LogFactory;
028    import org.apache.geronimo.security.GeronimoSecurityPermission;
029    
030    
031    /**
032     * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
033     */
034    public class GeronimoPolicyConfigurationFactory extends PolicyConfigurationFactory {
035    
036        private final Log log = LogFactory.getLog(GeronimoPolicyConfigurationFactory.class);
037        private static GeronimoPolicyConfigurationFactory singleton;
038        private Map configurations = new HashMap();
039    
040        public GeronimoPolicyConfigurationFactory() {
041            synchronized (GeronimoPolicyConfigurationFactory.class) {
042                if (singleton != null) {
043                    log.error("Singleton already assigned.  There may be more than one GeronimoPolicyConfigurationFactory being used.");
044                    throw new IllegalStateException("Singleton already assigned");
045                }
046                singleton = this;
047            }
048        }
049    
050        public void setPolicyConfiguration(String contextID, GeronimoPolicyConfiguration configuration) {
051            SecurityManager sm = System.getSecurityManager();
052            if (sm != null) sm.checkPermission(new GeronimoSecurityPermission("setPolicyConfiguration"));
053    
054            configurations.put(contextID, configuration);
055    
056            log.trace("Set policy configuration " + contextID);
057        }
058    
059        public GeronimoPolicyConfiguration getGeronimoPolicyConfiguration(String contextID) throws PolicyContextException {
060            GeronimoPolicyConfiguration configuration = (GeronimoPolicyConfiguration) configurations.get(contextID);
061    
062            if (configuration == null) {
063                throw new PolicyContextException("No policy configuration registered for contextID: " + contextID);
064            }
065    
066            log.trace("Get policy configuration " + contextID);
067            return configuration;
068        }
069    
070        public PolicyConfiguration getPolicyConfiguration(String contextID, boolean remove) throws PolicyContextException {
071            GeronimoPolicyConfiguration configuration = (GeronimoPolicyConfiguration) configurations.get(contextID);
072    
073            if (configuration == null) {
074                configuration = new PolicyConfigurationGeneric(contextID);
075                configurations.put(contextID, configuration);
076            } else {
077                configuration.open(remove);
078            }
079    
080            log.trace("Get " + (remove ? "CLEANED" : "") + " policy configuration " + contextID);
081            return configuration;
082        }
083    
084        public boolean inService(String contextID) throws PolicyContextException {
085            PolicyConfiguration configuration = getPolicyConfiguration(contextID, false);
086    
087            log.trace("Policy configuration " + contextID + " put into service");
088            return configuration.inService();
089        }
090    
091        static GeronimoPolicyConfigurationFactory getSingleton() {
092            return singleton;
093        }
094    }