001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. 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: package org.apache.naming;
019:
020: import java.util.Hashtable;
021:
022: /**
023: * Handles the access control on the JNDI contexts.
024: *
025: * @author Remy Maucherat
026: * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
027: */
028:
029: public class ContextAccessController {
030:
031: // -------------------------------------------------------------- Variables
032:
033: /**
034: * Catalina context names on which writing is not allowed.
035: */
036: private static Hashtable readOnlyContexts = new Hashtable();
037:
038: /**
039: * Security tokens repository.
040: */
041: private static Hashtable securityTokens = new Hashtable();
042:
043: // --------------------------------------------------------- Public Methods
044:
045: /**
046: * Set a security token for a context. Can be set only once.
047: *
048: * @param name Name of the context
049: * @param token Security token
050: */
051: public static void setSecurityToken(Object name, Object token) {
052: if ((!securityTokens.containsKey(name)) && (token != null)) {
053: securityTokens.put(name, token);
054: }
055: }
056:
057: /**
058: * Remove a security token for a context.
059: *
060: * @param name Name of the context
061: * @param token Security token
062: */
063: public static void unsetSecurityToken(Object name, Object token) {
064: if (checkSecurityToken(name, token)) {
065: securityTokens.remove(name);
066: }
067: }
068:
069: /**
070: * Check a submitted security token. The submitted token must be equal to
071: * the token present in the repository. If no token is present for the
072: * context, then returns true.
073: *
074: * @param name Name of the context
075: * @param token Submitted security token
076: */
077: public static boolean checkSecurityToken(Object name, Object token) {
078: Object refToken = securityTokens.get(name);
079: if (refToken == null)
080: return (true);
081: if ((refToken != null) && (refToken.equals(token)))
082: return (true);
083: return (false);
084: }
085:
086: /**
087: * Allow writing to a context.
088: *
089: * @param name Name of the context
090: * @param token Security token
091: */
092: public static void setWritable(Object name, Object token) {
093: if (checkSecurityToken(name, token))
094: readOnlyContexts.remove(name);
095: }
096:
097: /**
098: * Set whether or not a context is writable.
099: *
100: * @param name Name of the context
101: */
102: public static void setReadOnly(Object name) {
103: readOnlyContexts.put(name, name);
104: }
105:
106: /**
107: * Returns if a context is writable.
108: *
109: * @param name Name of the context
110: */
111: public static boolean isWritable(Object name) {
112: return !(readOnlyContexts.containsKey(name));
113: }
114:
115: }
|