KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.naming.core;
18
19 import java.util.Hashtable JavaDoc;
20
21 /**
22  * Handles the access control on the JNDI contexts. All
23  * contexts implementations should use this.
24  *
25  * @author Remy Maucherat
26  */

27 public class ContextAccessController {
28
29
30     // -------------------------------------------------------------- Variables
31

32
33     /**
34      * Catalina context names on which writing is not allowed.
35      */

36     private static Hashtable JavaDoc readOnlyContexts = new Hashtable JavaDoc();
37
38
39     /**
40      * Security tokens repository.
41      */

42     private static Hashtable JavaDoc securityTokens = new Hashtable JavaDoc();
43
44
45     // --------------------------------------------------------- Public Methods
46

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

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

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

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

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

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

120     public static boolean isWritable(Object JavaDoc name) {
121         return !(readOnlyContexts.containsKey(name));
122     }
123 }
124
125
Popular Tags