View Javadoc

1   /**
2    *
3    * Copyright 2003-2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  package org.apache.geronimo.security.jacc;
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  import javax.security.jacc.PolicyConfiguration;
23  import javax.security.jacc.PolicyConfigurationFactory;
24  import javax.security.jacc.PolicyContextException;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.geronimo.security.GeronimoSecurityPermission;
29  
30  
31  /**
32   * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
33   */
34  public class GeronimoPolicyConfigurationFactory extends PolicyConfigurationFactory {
35  
36      private final Log log = LogFactory.getLog(GeronimoPolicyConfigurationFactory.class);
37      private static GeronimoPolicyConfigurationFactory singleton;
38      private Map configurations = new HashMap();
39  
40      public GeronimoPolicyConfigurationFactory() {
41          synchronized (GeronimoPolicyConfigurationFactory.class) {
42              if (singleton != null) {
43                  log.error("Singleton already assigned.  There may be more than one GeronimoPolicyConfigurationFactory being used.");
44                  throw new IllegalStateException("Singleton already assigned");
45              }
46              singleton = this;
47          }
48      }
49  
50      public void setPolicyConfiguration(String contextID, GeronimoPolicyConfiguration configuration) {
51          SecurityManager sm = System.getSecurityManager();
52          if (sm != null) sm.checkPermission(new GeronimoSecurityPermission("setPolicyConfiguration"));
53  
54          configurations.put(contextID, configuration);
55  
56          log.trace("Set policy configuration " + contextID);
57      }
58  
59      public GeronimoPolicyConfiguration getGeronimoPolicyConfiguration(String contextID) throws PolicyContextException {
60          GeronimoPolicyConfiguration configuration = (GeronimoPolicyConfiguration) configurations.get(contextID);
61  
62          if (configuration == null) {
63              throw new PolicyContextException("No policy configuration registered for contextID: " + contextID);
64          }
65  
66          log.trace("Get policy configuration " + contextID);
67          return configuration;
68      }
69  
70      public PolicyConfiguration getPolicyConfiguration(String contextID, boolean remove) throws PolicyContextException {
71          GeronimoPolicyConfiguration configuration = (GeronimoPolicyConfiguration) configurations.get(contextID);
72  
73          if (configuration == null) {
74              configuration = new PolicyConfigurationGeneric(contextID);
75              configurations.put(contextID, configuration);
76          } else {
77              configuration.open(remove);
78          }
79  
80          log.trace("Get " + (remove ? "CLEANED" : "") + " policy configuration " + contextID);
81          return configuration;
82      }
83  
84      public boolean inService(String contextID) throws PolicyContextException {
85          PolicyConfiguration configuration = getPolicyConfiguration(contextID, false);
86  
87          log.trace("Policy configuration " + contextID + " put into service");
88          return configuration.inService();
89      }
90  
91      static GeronimoPolicyConfigurationFactory getSingleton() {
92          return singleton;
93      }
94  }