001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.security.implementation.context;
011:
012: import java.util.HashMap;
013: import java.util.Map;
014: import java.util.Set;
015:
016: import org.mmbase.util.logging.Logger;
017: import org.mmbase.util.logging.Logging;
018:
019: /**
020: * ContextCache class
021: * @javadoc
022: *
023: * @author Eduard Witteveen
024: * @version $Id: ContextCache.java,v 1.11 2007/02/11 19:45:04 nklasens Exp $
025: */
026: public class ContextCache {
027: private static Logger log = Logging
028: .getLoggerInstance(ContextCache.class.getName());
029:
030: private org.mmbase.cache.Cache<String, Map<String, Map<String, Boolean>>> globalRightCache = new org.mmbase.cache.Cache<String, Map<String, Map<String, Boolean>>>(
031: 50) {
032: public String getName() {
033: return "ContextRight";
034: }
035:
036: public String getDescription() {
037: return "Context Security Implementation Rights Cache";
038: }
039: };
040:
041: private long rightTries = 0;
042: private long rightSucces = 0;
043: private long rightSize = 0;
044:
045: public void rightAdd(String operation, String context, String user,
046: boolean value) {
047: Map<String, Map<String, Boolean>> operationCache = globalRightCache
048: .get(operation);
049: // when operation not known, create
050: if (operationCache == null) {
051: operationCache = new HashMap<String, Map<String, Boolean>>();
052: globalRightCache.put(operation, operationCache);
053: }
054: Map<String, Boolean> contextCache = operationCache.get(context);
055: // when context not known, create
056: if (contextCache == null) {
057: contextCache = new HashMap<String, Boolean>();
058: operationCache.put(context, contextCache);
059: }
060: if (contextCache.containsKey(user)) {
061: log
062: .warn("rights context cache already contained this entry");
063: }
064: contextCache.put(user, Boolean.valueOf(value));
065: log.debug("added to cache the operation: " + operation
066: + " for context: " + context + " with user: " + user
067: + " with value: " + value);
068: rightSize++;
069: }
070:
071: public Boolean rightGet(String operation, String context,
072: String user) {
073: Map<String, Map<String, Boolean>> operationCache = globalRightCache
074: .get(operation);
075: rightTries++;
076: if (operationCache == null) {
077: if (log.isDebugEnabled()) {
078: log.debug("operation not found in cache ("
079: + info(rightTries, rightSucces, rightSize)
080: + ")");
081: }
082: return null;
083: }
084:
085: Map<String, Boolean> contextCache = operationCache.get(context);
086:
087: if (contextCache == null) {
088: if (log.isDebugEnabled()) {
089: log.debug("rights context not found in cache ("
090: + info(rightTries, rightSucces, rightSize)
091: + ")");
092: }
093: return null;
094: }
095:
096: if (contextCache.containsKey(user)) {
097: rightSucces++;
098: if (log.isDebugEnabled()) {
099: log.debug("user found in cache ("
100: + info(rightTries, rightSucces, rightSize)
101: + ")");
102: log
103: .debug("the operation: " + operation
104: + " for context: " + context
105: + " with user: " + user + " returned: "
106: + contextCache.get(user));
107: }
108: }
109: return contextCache.get(user);
110: }
111:
112: private org.mmbase.cache.Cache<String, Set<String>> globalContextCache = new org.mmbase.cache.Cache<String, Set<String>>(
113: 50) {
114: public String getName() {
115: return "ContextContext";
116: }
117:
118: public String getDescription() {
119: return "Context Security Implementation Context Cache";
120: }
121: };
122:
123: private long contextTries = 0;
124: private long contextSucces = 0;
125: private long contextSize = 0;
126:
127: public void contextAdd(String context, Set<String> possible) {
128: // when context was already known....
129: if (globalContextCache.containsKey(context)) {
130: log.warn("context cache already contained this entry");
131: }
132: globalContextCache.put(context, possible);
133: log.debug("added possible list to context with name : "
134: + context);
135: contextSize++;
136: }
137:
138: public Set<String> contextGet(String context) {
139: contextTries++;
140:
141: if (globalContextCache.containsKey(context)) {
142: contextSucces++;
143: log.debug("context found in cache ("
144: + info(contextTries, contextSucces, contextSize)
145: + ")");
146: }
147: return globalContextCache.get(context);
148: }
149:
150: private String info(long tries, long succes, long size) {
151: return "hit of #" + succes + " access of #" + tries + " ("
152: + succes / (tries / 100.0)
153: + " %) with a number of entries #" + size;
154: }
155:
156: ContextCache() {
157: globalContextCache.putCache();
158: globalRightCache.putCache();
159: }
160: }
|