View Javadoc

1   /**
2    *
3    * Copyright 2003-2005 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.security.CodeSource;
21  import java.security.Permission;
22  import java.security.PermissionCollection;
23  import java.security.Policy;
24  import java.security.ProtectionDomain;
25  import javax.security.jacc.PolicyContext;
26  import javax.security.jacc.PolicyContextException;
27  
28  
29  /**
30   * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
31   */
32  public class GeronimoPolicy extends Policy {
33      private final Policy root;
34      private GeronimoPolicyConfigurationFactory factory;
35      private boolean loaded;
36  
37      public GeronimoPolicy() {
38          String provider = System.getProperty("org.apache.geronimo.jacc.policy.provider");
39  
40          if (provider == null) {
41              root = Policy.getPolicy();
42          } else {
43              try {
44                  Object obj = Class.forName(provider).newInstance();
45                  if (obj instanceof Policy) {
46                      root = (Policy) obj;
47                  } else {
48                      throw new RuntimeException(provider + "is not a type of java.security.Policy");
49                  }
50              } catch (InstantiationException e) {
51                  throw new RuntimeException("Unable to create an instance of " + provider, e);
52              } catch (IllegalAccessException e) {
53                  throw new RuntimeException("Unable to create an instance of " + provider, e);
54              } catch (ClassNotFoundException e) {
55                  throw new RuntimeException("Unable to create an instance of " + provider, e);
56              }
57          }
58          root.refresh();
59      }
60  
61      public PermissionCollection getPermissions(CodeSource codesource) {
62  
63          if (root != null) return root.getPermissions(codesource);
64  
65          return null;
66      }
67  
68      public void refresh() {
69      }
70  
71      public boolean implies(ProtectionDomain domain, Permission permission) {
72  
73          if (!loaded) {
74              factory = GeronimoPolicyConfigurationFactory.getSingleton();
75              loaded = true;
76          }
77  
78          if (factory != null) {
79              String contextID = PolicyContext.getContextID();
80              if (contextID != null) {
81                  try {
82                      GeronimoPolicyConfiguration configuration = factory.getGeronimoPolicyConfiguration(contextID);
83  
84                      if (configuration.inService()) {
85                          if (configuration.implies(domain, permission)) return true;
86                      } else {
87                          return false;
88                      }
89                  } catch (PolicyContextException e) {
90                  }
91              }
92          }
93          if (root != null) return root.implies(domain, permission);
94  
95          return false;
96      }
97  }