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.core;
018:
019: import java.util.Hashtable;
020:
021: /**
022: * Handles the access control on the JNDI contexts. All
023: * contexts implementations should use this.
024: *
025: * @author Remy Maucherat
026: */
027: public class ContextAccessController {
028:
029: // -------------------------------------------------------------- Variables
030:
031: /**
032: * Catalina context names on which writing is not allowed.
033: */
034: private static Hashtable readOnlyContexts = new Hashtable();
035:
036: /**
037: * Security tokens repository.
038: */
039: private static Hashtable securityTokens = new Hashtable();
040:
041: // --------------------------------------------------------- Public Methods
042:
043: /**
044: * Set a security token for a context. Can be set only once.
045: *
046: * @param name Name of the context
047: * @param context Security token
048: */
049: public static void setSecurityToken(Object name, Object token) {
050: if ((!securityTokens.containsKey(name)) && (token != null)) {
051: securityTokens.put(name, token);
052: }
053: }
054:
055: /**
056: * Remove a security token for a context.
057: *
058: * @param name Name of the context
059: * @param context Security token
060: */
061: public static void unsetSecurityToken(Object name, Object token) {
062: if (checkSecurityToken(name, token)) {
063: securityTokens.remove(name);
064: }
065: }
066:
067: /**
068: * Check a submitted security token. The submitted token must be equal to
069: * the token present in the repository. If no token is present for the
070: * context, then returns true.
071: *
072: * @param name Name of the context
073: * @param context Submitted security token
074: */
075: public static boolean checkSecurityToken(Object name, Object token) {
076: Object refToken = securityTokens.get(name);
077: if (refToken == null)
078: return (true);
079: if ((refToken != null) && (refToken.equals(token)))
080: return (true);
081: return (false);
082: }
083:
084: /**
085: * Allow writing to a context.
086: *
087: * @param name Name of the context
088: * @param token Security token
089: */
090: public static void setWritable(Object name, Object token) {
091: if (checkSecurityToken(name, token))
092: readOnlyContexts.remove(name);
093: }
094:
095: /**
096: * Set whether or not a context is writable.
097: *
098: * @param name Name of the context
099: */
100: public static void setReadOnly(Object name) {
101: readOnlyContexts.put(name, name);
102: }
103:
104: /**
105: * Returns if a context is writable.
106: *
107: * @param name Name of the context
108: */
109: public static boolean isWritable(Object name) {
110: return !(readOnlyContexts.containsKey(name));
111: }
112: }
|