001    /**
002     *
003     * Copyright 2003-2004 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  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    //
019    // This source code implements specifications defined by the Java
020    // Community Process. In order to remain compliant with the specification
021    // DO NOT add / change / or delete method signatures!
022    //
023    
024    package javax.security.jacc;
025    
026    import java.security.SecurityPermission;
027    import java.util.Hashtable;
028    import java.util.Set;
029    
030    
031    /**
032     *
033     * @version $Rev: 54183 $ $Date: 2004-10-09 15:04:29 -0700 (Sat, 09 Oct 2004) $
034     */
035    public final class PolicyContext {
036    
037        private static ThreadLocal contextId = new ThreadLocal();
038        private static ThreadLocal handlerData = new ThreadLocal();
039        private static Hashtable handlers = new Hashtable();
040        private final static SecurityPermission SET_POLICY = new SecurityPermission("setPolicy");
041    
042        private PolicyContext() {
043        }
044    
045        public static void setContextID(String contextID) {
046            SecurityManager sm = System.getSecurityManager();
047            if (sm != null) sm.checkPermission(SET_POLICY);
048    
049            contextId.set(contextID);
050        }
051    
052        public static String getContextID() {
053            return (String) contextId.get();
054        }
055    
056        public static void setHandlerData(Object data) {
057            SecurityManager sm = System.getSecurityManager();
058            if (sm != null) sm.checkPermission(SET_POLICY);
059    
060            handlerData.set(data);
061        }
062    
063        public static void registerHandler(String key, PolicyContextHandler handler, boolean replace) throws PolicyContextException {
064            if (key == null) throw new IllegalArgumentException("Key must not be null");
065            if (handler == null) throw new IllegalArgumentException("Handler must not be null");
066            if (!replace && handlers.containsKey(key)) throw new IllegalArgumentException("A handler has already been registered under '" + key + "' and replace is false.");
067    
068            SecurityManager sm = System.getSecurityManager();
069            if (sm != null) sm.checkPermission(SET_POLICY);
070    
071            handlers.put(key, handler);
072        }
073    
074        public static Set getHandlerKeys() {
075            return handlers.keySet();
076        }
077    
078        public static Object getContext(String key) throws PolicyContextException {
079            if (key == null) throw new IllegalArgumentException("Key must not be null");
080    
081            PolicyContextHandler handler = (PolicyContextHandler) handlers.get(key);
082    
083            if (handler == null) throw new IllegalArgumentException("No handler can be found for the key '" + key + "'");
084            if (!handler.supports(key)) throw new IllegalArgumentException("Registered handler no longer supports the key '" + key + "'");
085    
086            SecurityManager sm = System.getSecurityManager();
087            if (sm != null) sm.checkPermission(SET_POLICY);
088    
089            return handler.getContext(key, handlerData.get());
090        }
091    }