KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > security > implementation > context > ContextCache


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.security.implementation.context;
11
12 import java.util.HashMap JavaDoc;
13 import java.util.Map JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.mmbase.util.logging.Logger;
17 import org.mmbase.util.logging.Logging;
18
19 /**
20  * ContextCache class
21  * @javadoc
22  *
23  * @author Eduard Witteveen
24  * @version $Id: ContextCache.java,v 1.9 2006/07/18 12:46:50 michiel Exp $
25  */

26 public class ContextCache {
27     private static Logger log = Logging.getLoggerInstance(ContextCache.class.getName());
28
29     private org.mmbase.cache.Cache globalRightCache = new org.mmbase.cache.Cache(50) {
30         public String JavaDoc getName() { return "ContextRight"; }
31             public String JavaDoc getDescription() { return "Context Security Implementation Rights Cache"; }
32         };
33
34     private long rightTries = 0;
35     private long rightSucces = 0;
36     private long rightSize = 0;
37
38     public void rightAdd(String JavaDoc operation, String JavaDoc context, String JavaDoc user, boolean value) {
39         Map JavaDoc operationCache = (Map JavaDoc) globalRightCache.get(operation);
40         // when operation not known, create
41
if(operationCache == null) {
42             operationCache = new HashMap JavaDoc();
43             globalRightCache.put(operation, operationCache);
44         }
45         Map JavaDoc contextCache = (Map JavaDoc) operationCache.get(context);
46         // when context not known, create
47
if(contextCache == null) {
48             contextCache = new HashMap JavaDoc();
49             operationCache.put(context, contextCache);
50         }
51         if(contextCache.containsKey(user)) {
52             log.warn("rights context cache already contained this entry");
53         }
54         contextCache.put(user, Boolean.valueOf(value));
55         log.debug("added to cache the operation: " + operation + " for context: " + context + " with user: " + user + " with value: " + value );
56         rightSize++;
57     }
58
59     public Boolean JavaDoc rightGet(String JavaDoc operation, String JavaDoc context, String JavaDoc user) {
60         HashMap JavaDoc operationCache = (HashMap JavaDoc)globalRightCache.get(operation);
61         rightTries ++;
62         if(operationCache == null) {
63             if (log.isDebugEnabled()) {
64                 log.debug("operation not found in cache (" + info(rightTries, rightSucces, rightSize) + ")");
65             }
66             return null;
67         }
68
69         HashMap JavaDoc contextCache = (HashMap JavaDoc)operationCache.get(context);
70
71         if(contextCache == null) {
72             if (log.isDebugEnabled()) {
73                 log.debug("rights context not found in cache (" + info(rightTries, rightSucces, rightSize)+")");
74             }
75             return null;
76         }
77
78         if(contextCache.containsKey(user)) {
79             rightSucces ++;
80             if (log.isDebugEnabled()) {
81                 log.debug("user found in cache ("+info(rightTries, rightSucces, rightSize)+")");
82                 log.debug("the operation: " + operation + " for context: " + context + " with user: " + user + " returned: " + contextCache.get(user) );
83             }
84         }
85         return (Boolean JavaDoc)contextCache.get(user);
86     }
87
88     private org.mmbase.cache.Cache globalContextCache = new org.mmbase.cache.Cache(50) {
89             public String JavaDoc getName() { return "ContextContext"; }
90             public String JavaDoc getDescription() { return "Context Security Implementation Context Cache"; }
91         };
92
93     private long contextTries = 0;
94     private long contextSucces = 0;
95     private long contextSize = 0;
96
97     public void contextAdd(String JavaDoc context, Set JavaDoc possible) {
98         // when context was already known....
99
if(globalContextCache.containsKey(context)) {
100             log.warn("context cache already contained this entry");
101         }
102         globalContextCache.put(context, possible);
103         log.debug("added possible list to context with name : " + context);
104         contextSize++;
105     }
106
107     public Set JavaDoc contextGet(String JavaDoc context) {
108         contextTries++;
109
110         if(globalContextCache.containsKey(context)) {
111             contextSucces++;
112             log.debug("context found in cache ("+info(contextTries, contextSucces, contextSize)+")");
113         }
114         return (Set JavaDoc)globalContextCache.get(context);
115     }
116
117     private String JavaDoc info(long tries, long succes, long size) {
118         return "hit of #"+succes+ " access of #"+tries+" ("+ succes/(tries/100.0)+" %) with a number of entries #"+size;
119     }
120
121     ContextCache() {
122         globalContextCache.putCache();
123         globalRightCache.putCache();
124     }
125 }
126
Popular Tags