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  //
19  // This source code implements specifications defined by the Java
20  // Community Process. In order to remain compliant with the specification
21  // DO NOT add / change / or delete method signatures!
22  //
23  
24  package javax.security.jacc;
25  
26  import java.security.SecurityPermission;
27  import java.util.Hashtable;
28  import java.util.Set;
29  
30  
31  /**
32   *
33   * @version $Rev: 54183 $ $Date: 2004-10-09 15:04:29 -0700 (Sat, 09 Oct 2004) $
34   */
35  public final class PolicyContext {
36  
37      private static ThreadLocal contextId = new ThreadLocal();
38      private static ThreadLocal handlerData = new ThreadLocal();
39      private static Hashtable handlers = new Hashtable();
40      private final static SecurityPermission SET_POLICY = new SecurityPermission("setPolicy");
41  
42      private PolicyContext() {
43      }
44  
45      public static void setContextID(String contextID) {
46          SecurityManager sm = System.getSecurityManager();
47          if (sm != null) sm.checkPermission(SET_POLICY);
48  
49          contextId.set(contextID);
50      }
51  
52      public static String getContextID() {
53          return (String) contextId.get();
54      }
55  
56      public static void setHandlerData(Object data) {
57          SecurityManager sm = System.getSecurityManager();
58          if (sm != null) sm.checkPermission(SET_POLICY);
59  
60          handlerData.set(data);
61      }
62  
63      public static void registerHandler(String key, PolicyContextHandler handler, boolean replace) throws PolicyContextException {
64          if (key == null) throw new IllegalArgumentException("Key must not be null");
65          if (handler == null) throw new IllegalArgumentException("Handler must not be null");
66          if (!replace && handlers.containsKey(key)) throw new IllegalArgumentException("A handler has already been registered under '" + key + "' and replace is false.");
67  
68          SecurityManager sm = System.getSecurityManager();
69          if (sm != null) sm.checkPermission(SET_POLICY);
70  
71          handlers.put(key, handler);
72      }
73  
74      public static Set getHandlerKeys() {
75          return handlers.keySet();
76      }
77  
78      public static Object getContext(String key) throws PolicyContextException {
79          if (key == null) throw new IllegalArgumentException("Key must not be null");
80  
81          PolicyContextHandler handler = (PolicyContextHandler) handlers.get(key);
82  
83          if (handler == null) throw new IllegalArgumentException("No handler can be found for the key '" + key + "'");
84          if (!handler.supports(key)) throw new IllegalArgumentException("Registered handler no longer supports the key '" + key + "'");
85  
86          SecurityManager sm = System.getSecurityManager();
87          if (sm != null) sm.checkPermission(SET_POLICY);
88  
89          return handler.getContext(key, handlerData.get());
90      }
91  }