1 10 package org.mmbase.security.implementation.context; 11 12 import java.util.HashMap ; 13 import java.util.Map ; 14 import java.util.Set ; 15 16 import org.mmbase.util.logging.Logger; 17 import org.mmbase.util.logging.Logging; 18 19 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 getName() { return "ContextRight"; } 31 public String 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 operation, String context, String user, boolean value) { 39 Map operationCache = (Map ) globalRightCache.get(operation); 40 if(operationCache == null) { 42 operationCache = new HashMap (); 43 globalRightCache.put(operation, operationCache); 44 } 45 Map contextCache = (Map ) operationCache.get(context); 46 if(contextCache == null) { 48 contextCache = new HashMap (); 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 rightGet(String operation, String context, String user) { 60 HashMap operationCache = (HashMap )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 contextCache = (HashMap )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 )contextCache.get(user); 86 } 87 88 private org.mmbase.cache.Cache globalContextCache = new org.mmbase.cache.Cache(50) { 89 public String getName() { return "ContextContext"; } 90 public String 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 context, Set possible) { 98 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 contextGet(String 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 )globalContextCache.get(context); 115 } 116 117 private String 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 |