001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.naming;
018:
019: import java.util.Hashtable;
020:
021: /**
022: * Handles the access control on the JNDI contexts.
023: *
024: * @author Remy Maucherat
025: * @version $Revision: 1.3 $ $Date: 2004/02/27 14:58:53 $
026: */
027:
028: public class ContextAccessController {
029:
030: // -------------------------------------------------------------- Variables
031:
032: /**
033: * Catalina context names on which writing is not allowed.
034: */
035: private static Hashtable readOnlyContexts = new Hashtable();
036:
037: /**
038: * Security tokens repository.
039: */
040: private static Hashtable securityTokens = new Hashtable();
041:
042: // --------------------------------------------------------- Public Methods
043:
044: /**
045: * Set a security token for a context. Can be set only once.
046: *
047: * @param name Name of the context
048: * @param context Security token
049: */
050: public static void setSecurityToken(Object name, Object token) {
051: if ((!securityTokens.containsKey(name)) && (token != null)) {
052: securityTokens.put(name, token);
053: }
054: }
055:
056: /**
057: * Remove a security token for a context.
058: *
059: * @param name Name of the context
060: * @param context Security token
061: */
062: public static void unsetSecurityToken(Object name, Object token) {
063: if (checkSecurityToken(name, token)) {
064: securityTokens.remove(name);
065: }
066: }
067:
068: /**
069: * Check a submitted security token. The submitted token must be equal to
070: * the token present in the repository. If no token is present for the
071: * context, then returns true.
072: *
073: * @param name Name of the context
074: * @param context Submitted security token
075: */
076: public static boolean checkSecurityToken(Object name, Object token) {
077: Object refToken = securityTokens.get(name);
078: if (refToken == null)
079: return (true);
080: if ((refToken != null) && (refToken.equals(token)))
081: return (true);
082: return (false);
083: }
084:
085: /**
086: * Allow writing to a context.
087: *
088: * @param name Name of the context
089: * @param token Security token
090: */
091: public static void setWritable(Object name, Object token) {
092: if (checkSecurityToken(name, token))
093: readOnlyContexts.remove(name);
094: }
095:
096: /**
097: * Set whether or not a context is writable.
098: *
099: * @param name Name of the context
100: */
101: public static void setReadOnly(Object name) {
102: readOnlyContexts.put(name, name);
103: }
104:
105: /**
106: * Returns if a context is writable.
107: *
108: * @param name Name of the context
109: */
110: public static boolean isWritable(Object name) {
111: return !(readOnlyContexts.containsKey(name));
112: }
113:
114: }
|