KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > naming > ContextAccessController


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 package org.apache.naming;
20
21 import java.util.Hashtable JavaDoc;
22
23 /**
24  * Handles the access control on the JNDI contexts.
25  *
26  * @author Remy Maucherat
27  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
28  */

29
30 public class ContextAccessController {
31
32
33     // -------------------------------------------------------------- Variables
34

35
36     /**
37      * Catalina context names on which writing is not allowed.
38      */

39     private static Hashtable JavaDoc readOnlyContexts = new Hashtable JavaDoc();
40
41
42     /**
43      * Security tokens repository.
44      */

45     private static Hashtable JavaDoc securityTokens = new Hashtable JavaDoc();
46
47
48     // --------------------------------------------------------- Public Methods
49

50
51     /**
52      * Set a security token for a context. Can be set only once.
53      *
54      * @param name Name of the context
55      * @param token Security token
56      */

57     public static void setSecurityToken(Object JavaDoc name, Object JavaDoc token) {
58         if ((!securityTokens.containsKey(name)) && (token != null)) {
59             securityTokens.put(name, token);
60         }
61     }
62
63
64     /**
65      * Remove a security token for a context.
66      *
67      * @param name Name of the context
68      * @param token Security token
69      */

70     public static void unsetSecurityToken(Object JavaDoc name, Object JavaDoc token) {
71         if (checkSecurityToken(name, token)) {
72             securityTokens.remove(name);
73         }
74     }
75
76
77     /**
78      * Check a submitted security token. The submitted token must be equal to
79      * the token present in the repository. If no token is present for the
80      * context, then returns true.
81      *
82      * @param name Name of the context
83      * @param token Submitted security token
84      */

85     public static boolean checkSecurityToken
86         (Object JavaDoc name, Object JavaDoc token) {
87         Object JavaDoc refToken = securityTokens.get(name);
88         if (refToken == null)
89             return (true);
90         if ((refToken != null) && (refToken.equals(token)))
91             return (true);
92         return (false);
93     }
94
95
96     /**
97      * Allow writing to a context.
98      *
99      * @param name Name of the context
100      * @param token Security token
101      */

102     public static void setWritable(Object JavaDoc name, Object JavaDoc token) {
103         if (checkSecurityToken(name, token))
104             readOnlyContexts.remove(name);
105     }
106
107
108     /**
109      * Set whether or not a context is writable.
110      *
111      * @param name Name of the context
112      */

113     public static void setReadOnly(Object JavaDoc name) {
114         readOnlyContexts.put(name, name);
115     }
116
117
118     /**
119      * Returns if a context is writable.
120      *
121      * @param name Name of the context
122      */

123     public static boolean isWritable(Object JavaDoc name) {
124         return !(readOnlyContexts.containsKey(name));
125     }
126
127
128 }
129
130
Popular Tags