KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > common > SlideTokenImpl


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/common/SlideTokenImpl.java,v 1.13.2.2 2004/09/22 13:34:17 luetzkendorf Exp $
3  * $Revision: 1.13.2.2 $
4  * $Date: 2004/09/22 13:34:17 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.common;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.util.List JavaDoc;
30 import org.apache.slide.authenticate.CredentialsToken;
31 import org.apache.slide.store.ResourceId;
32 import org.apache.slide.structure.ActionNode;
33 import org.apache.slide.structure.ObjectNode;
34 import org.apache.slide.structure.SubjectNode;
35
36 /**
37  * Slide token class.
38  *
39  */

40 public final class SlideTokenImpl implements SlideToken {
41     
42     
43     // ------------------------------------------------------------ Constructor
44

45     
46     /**
47      * Constructor.
48      *
49      * @param credentialsToken Credentials stored in this token
50      */

51     public SlideTokenImpl(CredentialsToken credentialsToken) {
52         this.credentialsToken = credentialsToken;
53     }
54     
55     
56     /**
57      * Constructor.
58      */

59     public SlideTokenImpl() {
60     }
61     
62     
63     /**
64      * Constructor.
65      *
66      * @param credentialsToken Credentials stored in this token
67      * @param parameters Token parameters
68      */

69     public SlideTokenImpl(CredentialsToken credentialsToken,
70                           Hashtable JavaDoc parameters) {
71         this.credentialsToken = credentialsToken;
72         this.parameters = parameters;
73     }
74     
75     
76     // ----------------------------------------------------- Instance Variables
77

78     
79     /**
80      * Credentials.
81      */

82     private CredentialsToken credentialsToken;
83     
84     
85     /**
86      * CacheInfo.
87      */

88     private CacheInfoToken cacheInfoToken;
89     
90     
91     /**
92      * Use lock tokens for lock resolution.
93      */

94     private boolean enforceLockTokens = false;
95     
96     
97     /**
98      * Always use a transaction for all operations. That will cause Slide to
99      * enlist the store in the current transaction for all operations,
100      * to be able to prevent dirty reads when doing complex updates.
101      */

102     private boolean forceStoreEnlistment = false;
103     
104     
105     /**
106      * Lock tokens.
107      */

108     private Hashtable JavaDoc lockTokens = new Hashtable JavaDoc();
109     
110     
111     /**
112      * Parameters.
113      */

114     private Hashtable JavaDoc parameters = new Hashtable JavaDoc();
115     
116     /**
117      * Caches
118      */

119     private Hashtable JavaDoc permissionCache = new Hashtable JavaDoc();
120     private Hashtable JavaDoc lockCache = new Hashtable JavaDoc();
121     private Hashtable JavaDoc resolveCache = new Hashtable JavaDoc();
122     private Hashtable JavaDoc matchPrincipalCache = new Hashtable JavaDoc();
123
124     /**
125      * Determines if this request is part of an externally controlled transaction.
126      */

127     private boolean isExternalTransaction = false;
128     
129     
130     // ------------------------------------------------------------- Properties
131

132     
133     /**
134      * Returns the credentials token.
135      *
136      * @return String
137      */

138     public CredentialsToken getCredentialsToken() {
139         return credentialsToken;
140     }
141     
142     
143     /**
144      * Credentials token mutator.
145      */

146     public void setCredentialsToken(CredentialsToken credentialsToken) {
147         this.credentialsToken = credentialsToken;
148     }
149     
150     
151     /**
152      * Returns the CacheInfo token.
153      *
154      * @return CacheInfoToken
155      */

156     public CacheInfoToken getCacheInfoToken() {
157         return cacheInfoToken;
158     }
159     
160     
161     /**
162      * CacheInfo token mutator.
163      */

164     public void setCacheInfoToken(CacheInfoToken cacheInfoToken) {
165         this.cacheInfoToken = cacheInfoToken;
166     }
167     
168     
169     /**
170      * Use lock tokens in lock resolution ?
171      *
172      * @return boolean
173      */

174     public boolean isEnforceLockTokens() {
175         return enforceLockTokens;
176     }
177     
178     
179     /**
180      * Enforce lock tokens flag mutator.
181      *
182      * @param enforceLockTokens New flag value
183      */

184     public void setEnforceLockTokens(boolean enforceLockTokens) {
185         this.enforceLockTokens = enforceLockTokens;
186     }
187     
188     
189     /**
190      * Force store enlistment flag accessor. If true, that will cause Slide to
191      * enlist the store in the current transaction for all operations,
192      * to be able to prevent dirty reads when doing complex updates.
193      *
194      * @return boolean
195      */

196     public boolean isForceStoreEnlistment() {
197         return forceStoreEnlistment;
198     }
199     
200     
201     /**
202      * Force store enlistment flag mutator. If set to true, that will cause
203      * Slide to enlist the store in the current transaction for all
204      * operations, to be able to prevent dirty reads when doing complex
205      * updates. That value should be set to true only in some very specific
206      * critical sections of the code, as this would greatly decrease the
207      * ability of Slide to handle multiple concurrent requests.
208      *
209      * @param forceStoreEnlistment New flag value
210      */

211     public void setForceStoreEnlistment(boolean forceStoreEnlistment) {
212         this.forceStoreEnlistment = forceStoreEnlistment;
213     }
214     
215     
216     /**
217      * Add a new lock token to the lock token list.
218      *
219      * @param lockId Lock token to add
220      */

221     public void addLockToken(String JavaDoc lockId) {
222         lockTokens.put(lockId, lockId);
223     }
224     
225     
226     /**
227      * Removes a lock token from the lock token list.
228      *
229      * @param lockId Lock token to remove
230      */

231     public void removeLockToken(String JavaDoc lockId) {
232         lockTokens.remove(lockId);
233     }
234     
235     
236     /**
237      * Clears the lock token list.
238      */

239     public void clearLockTokens() {
240         lockTokens.clear();
241     }
242     
243     public List JavaDoc showLockTokens() {
244         return new ArrayList JavaDoc(lockTokens.keySet());
245     }
246     
247     /**
248      * Checks if the given lock token is present.
249      *
250      * @param lockToken Lock token to check
251      * @return boolean True if the given lock token is present
252      */

253     public boolean checkLockToken(String JavaDoc lockToken) {
254         return lockTokens.containsKey(lockToken);
255     }
256     
257     
258     /**
259      * Add a new parameter to the parameter list.
260      *
261      * @param parameterName Parameter to add
262      * @param parameterValue Parameter value
263      */

264     public void addParameter(String JavaDoc parameterName, Object JavaDoc parameterValue) {
265         parameters.put(parameterName, parameterValue);
266     }
267     
268     
269     /**
270      * Removes a parameter from the parameter list.
271      *
272      * @param parameterName Parameter to remove
273      */

274     public void removeParameter(String JavaDoc parameterName) {
275         parameters.remove(parameterName);
276     }
277     
278     
279     /**
280      * Clears the parameter list.
281      */

282     public void clearParameters() {
283         parameters.clear();
284     }
285     
286     
287     /**
288      * Return parameter list.
289      */

290     public Enumeration JavaDoc getParameterNames() {
291         return parameters.keys();
292     }
293     
294     /**
295      * Returns a named parameter.
296      */

297     public Object JavaDoc getParameter(String JavaDoc name) {
298         return parameters.get(name);
299     }
300     
301     /**
302      * allows to cache the result of a permission check
303      *
304      * @return true if successful added to cache, false else
305      */

306     public void cachePermission(ObjectNode object, ActionNode action, boolean permission){
307         String JavaDoc key = new String JavaDoc (object.getUri()+ action.getUri());
308         Boolean JavaDoc perm = new Boolean JavaDoc(permission);
309         permissionCache.put(key,perm);
310     }
311     
312     /**
313      * checks if the permission cache contains an entry for the ObjectNode and
314      * ActionNode combination.
315      *
316      * @return true if granted, false if denied, null if nothing in the cache.
317      */

318     public Boolean JavaDoc checkPermissionCache(ObjectNode object, ActionNode action){
319         String JavaDoc key = new String JavaDoc (object.getUri()+ action.getUri());
320         return (Boolean JavaDoc) permissionCache.get(key);
321     }
322     
323     /**
324      * Force security check. Always TRUE for tokens.
325      * User SlideTokenWrapper to obtain temporaryly insecure tokens.
326      *
327      * @return a boolean
328      */

329     public boolean isForceSecurity() {
330         return true;
331     }
332     
333     /**
334      * Force lock check. If false, checkLock of LockImpl will
335      * return immediately.
336      *
337      * @return a boolean
338      */

339     public boolean isForceLock() {
340         return true;
341     }
342     
343     /**
344      * allows to cache the result of a lock check
345      */

346     public void cacheLock(ObjectNode object, ActionNode action, boolean lock) {
347         String JavaDoc key = new String JavaDoc (object.getUri()+ action.getUri());
348         Boolean JavaDoc locked = new Boolean JavaDoc(lock);
349         lockCache.put(key,locked);
350     }
351     
352     /**
353      * checks if the lock cache contains an entry for the ObjectNode and
354      * ActionNode combination.
355      *
356      * @return true if locked, false otherwise
357      */

358     public Boolean JavaDoc checkLockCache(ObjectNode object, ActionNode action) {
359         String JavaDoc key = new String JavaDoc (object.getUri()+ action.getUri());
360         return (Boolean JavaDoc) lockCache.get(key);
361     }
362     
363     /**
364      * Allows to cache the result of a resolve operation
365      */

366     public void cacheResolve(Uri uri, ResourceId resourceId) {
367         if (resourceId != null) {
368             resolveCache.put(uri, resourceId);
369         } else {
370             resolveCache.remove(uri);
371         }
372     }
373     
374     /**
375      * Checks if the resolve cache contains an entry for the specified uri.
376      * @return the cached resourceId or null
377      */

378     public ResourceId checkResolveCache(Uri uri) {
379         return (ResourceId)resolveCache.get(uri);
380     }
381     
382     /**
383      * Allows to cache the result of a matchPrincipal operation
384      */

385     public void cacheMatchPrincipal(SubjectNode checkSubject, SubjectNode matchSubject, boolean match) {
386         String JavaDoc key = String.valueOf(checkSubject)+String.valueOf(matchSubject);
387         matchPrincipalCache.put(key, new Boolean JavaDoc(match));
388     }
389     
390     /**
391      * Checks if the matchPrincipal cache
392      * @return the cached Boolean or null
393      */

394     public Boolean JavaDoc checkMatchPrincipalCache(SubjectNode checkSubject, SubjectNode matchSubject) {
395         String JavaDoc key = String.valueOf(checkSubject)+String.valueOf(matchSubject);
396         return (Boolean JavaDoc)matchPrincipalCache.get(key);
397     }
398     
399     public void setForceLock(boolean forceLock) {
400         throw new UnsupportedOperationException JavaDoc();
401     }
402     
403     public void setForceSecurity(boolean forceSecurity) {
404         throw new UnsupportedOperationException JavaDoc();
405     }
406
407     /**
408      * Checks if this request is part of an externally controlled transaction.
409      */

410     public boolean isExternalTransaction() {
411         return isExternalTransaction;
412     }
413
414     /**
415      * Sets if this request is part of an externally controlled transaction.
416      */

417     public void setExternalTx() {
418         isExternalTransaction = true;
419     }
420     
421 }
422
423
Popular Tags