Author: michiel Date: 2010-04-12 17:25:06 +0200 (Mon, 12 Apr 2010) New Revision: 41846
Removed: mmbase/trunk/core/src/main/java/org/mmbase/cache/BlobCache.java mmbase/trunk/core/src/main/java/org/mmbase/cache/Cache.java mmbase/trunk/core/src/main/java/org/mmbase/cache/CacheConfigurationException.java mmbase/trunk/core/src/main/java/org/mmbase/cache/CacheImplementationInterface.java mmbase/trunk/core/src/main/java/org/mmbase/cache/CacheMBean.java mmbase/trunk/core/src/main/java/org/mmbase/cache/CacheManager.java mmbase/trunk/core/src/main/java/org/mmbase/cache/CacheManagerMBean.java mmbase/trunk/core/src/main/java/org/mmbase/cache/CachePolicy.java mmbase/trunk/core/src/main/java/org/mmbase/cache/Cacheable.java mmbase/trunk/core/src/main/java/org/mmbase/cache/implementation/ mmbase/trunk/core/src/main/java/org/mmbase/cache/resources/ mmbase/trunk/core/src/main/java/org/mmbase/cache/xslt/ Log: moved to bridge jar Deleted: mmbase/trunk/core/src/main/java/org/mmbase/cache/BlobCache.java =================================================================== --- mmbase/trunk/core/src/main/java/org/mmbase/cache/BlobCache.java 2010-04-12 15:21:43 UTC (rev 41845) +++ mmbase/trunk/core/src/main/java/org/mmbase/cache/BlobCache.java 2010-04-12 15:25:06 UTC (rev 41846) @@ -1,67 +0,0 @@ -/* - -This software is OSI Certified Open Source Software. -OSI Certified is a certification mark of the Open Source Initiative. - -The license (Mozilla version 1.0) can be read at the MMBase site. -See http://www.MMBase.org/license - -*/ -package org.mmbase.cache; - -import org.mmbase.util.logging.*; - -/** - * The 'blob cache' is used in MMObjectNode to cache small byte-array field values. it is a - * replacement for the 'handle cache' which was present in MMBase <1.8. - * - * @author Michiel Meeuwissen - * @version $Id$ - * @since MMBase 1.8 - */ -public abstract class BlobCache extends Cache<String, Object> { - - private static final Logger log = Logging.getLoggerInstance(BlobCache.class); - - public BlobCache(int size) { - super(size); - } - - @Override - protected int getDefaultMaxEntrySize() { - return 100 * 1024; - - } - - @Override - public String getName() { - return "A Blob Cache"; - } - - @Override - public String getDescription() { - return "Node number - field Name-> ByteArray"; - } - - public final String getKey(int nodeNumber, String fieldName) { - return "" + nodeNumber + '-' + fieldName; - } - - @Override - public Object put(String key, Object value) { - if (!checkCachePolicy(key)) return null; - if (value instanceof byte[]) { - int max = getMaxEntrySize(); - byte[] b = (byte[]) value; - log.trace(" max " + max + " " + b.length); - if (max > 0 && b.length > max) return null; - } else if (value instanceof String) { - int max = getMaxEntrySize(); - String s = (String) value; - if (max > 0 && s.length() > getMaxEntrySize()) return null; - } - return super.put(key, value); - } - - -} Deleted: mmbase/trunk/core/src/main/java/org/mmbase/cache/Cache.java =================================================================== --- mmbase/trunk/core/src/main/java/org/mmbase/cache/Cache.java 2010-04-12 15:21:43 UTC (rev 41845) +++ mmbase/trunk/core/src/main/java/org/mmbase/cache/Cache.java 2010-04-12 15:25:06 UTC (rev 41846) @@ -1,473 +0,0 @@ -/* - -This software is OSI Certified Open Source Software. -OSI Certified is a certification mark of the Open Source Initiative. - -The license (Mozilla version 1.0) can be read at the MMBase site. -See http://www.MMBase.org/license - -*/ -package org.mmbase.cache; - -import java.util.*; - -import org.mmbase.util.*; -import org.mmbase.cache.implementation.*; -import org.mmbase.util.logging.Logger; -import org.mmbase.util.logging.Logging; - -/** - * A base class for all Caches. Extend this class for other caches. - * - * @author Michiel Meeuwissen - * @version $Id$ - */ -abstract public class Cache<K, V> implements SizeMeasurable, Map<K, V>, CacheMBean { - - private static final Logger log = Logging.getLoggerInstance(Cache.class); - - private boolean active = true; - protected int maxEntrySize = -1; // no maximum/ implementation does not support; - - /** - * @since MMBase-1.8 - */ - private CacheImplementationInterface<K, V> implementation; - - protected volatile Object lock; - - /** - * The number of times an element was succesfully retrieved from this cache. - */ - private long hits = 0; - - /** - * The number of times an element could not be retrieved from this cache. - */ - private long misses = 0; - - /** - * The number of times an element was committed to this cache. - */ - private long puts = 0; - - public Cache(int size) { - // See: http://www.mmbase.org/jira/browse/MMB-1486 - implementation = new LRUCache<K, V>(size); - lock = implementation.getLock(); - //implementation = new LRUHashtable<K, V>(size); - - log.service("Creating cache " + getName() + ": " + getDescription()); - } - - @SuppressWarnings("unchecked") - void setImplementation(String clazz, Map<String,String> configValues) { - synchronized(lock) { - clear(); - try { - - Class<?> clas = Class.forName(clazz); - if (implementation == null || (! clas.equals(implementation.getClass()))) { - log.info("Setting implementation of " + this + " to " + clas); - implementation = (CacheImplementationInterface<K,V>) clas.newInstance(); - implementation.config(configValues); - lock = implementation.getLock(); - } - } catch (ClassNotFoundException cnfe) { - log.error("For cache " + this + " " + cnfe.getClass().getName() + ": " + cnfe.getMessage()); - } catch (InstantiationException ie) { - log.error("For cache " + this + " " + ie.getClass().getName() + ": " + ie.getMessage()); - } catch (IllegalAccessException iae) { - log.error("For cache " + this + " " + iae.getClass().getName() + ": " + iae.getMessage()); - } - } - } - - /** - * If you want to structurally modify this cache, synchronize on this object. - * - * @since MMBase-1.8.6 - */ - public final Object getLock() { - return lock; - } - - /** - * Returns a name for this cache type. Default it is the class - * name, but this normally will be overriden. - */ - public String getName() { - return getClass().getName(); - } - - /** - * Gives a description for this cache type. This can be used in - * cache overviews. - */ - public String getDescription() { - return "An all purpose Cache"; - } - - - - /** - * Return the maximum entry size for the cache in bytes. If the - * cache-type supports it (default no), then no values bigger then - * this will be stored in the cache. - */ - public int getMaxEntrySize() { - if (getDefaultMaxEntrySize() > 0) { - return maxEntrySize; - } else { - return -1; - } - } - - /** - * @since MMBase-1.9.2 - */ - public void setMaxEntrySize(int i) { - if (getDefaultMaxEntrySize() > 0) { - maxEntrySize = i; - } else { - throw new UnsupportedOperationException(); - } - } - - /** - * Returns the average 'length' of the values in the cache. Whatever that may mean. For a {...@link - * QueryResultCache} the length obviously is the length of the cached lists. - * - * May return <code>NaN</code> if unknown or undetermined. - * @since MMBase-1.9.2 - */ - public double getAvarageValueLength() { - return Double.NaN; - } - - /** - * This has to be overridden by Caches which support max entry size. - */ - - protected int getDefaultMaxEntrySize() { - return -1; - } - - public Set<Map.Entry<K,V>> entrySet() { - if (! active) { - return new HashSet<Map.Entry<K,V>>(); - } - return implementation.entrySet(); - } - - /** - * @since MMBase-1.8.6 - */ - public Class<?> getImplementation() { - return implementation.getClass(); - } - - /** - * Checks whether the key object should be cached. - * This method returns <code>false</code> if either the current cache is inactive, or the object to cache - * has a cache policy associated that prohibits caching of the object. - * @param key the object to be cached - * @return <code>true</code> if the object can be cached - * @since MMBase-1.8 - */ - protected boolean checkCachePolicy(Object key) { - CachePolicy policy = null; - if (active) { - if (key instanceof Cacheable) { - policy = ((Cacheable)key).getCachePolicy(); - if (policy != null) { - return policy.checkPolicy(key); - } - } - return true; - } - return false; - } - - /** - * Like 'get' of Maps but considers if the cache is active or not, and the cache policy of the key. - */ - public V get(Object key) { - if (!checkCachePolicy(key)) { - return null; - } - V res = implementation.get(key); - if (res != null) { - hits++; - } else { - misses++; - } - return res; - } - - /** - * Like 'put' of LRUHashtable but considers if the cache is active or not. - * - */ - public V put(K key, V value) { - if (!checkCachePolicy(key)) { - return null; - } - puts++; - return implementation.put(key, value); - } - - /** - * Returns the number of times an element was succesfully retrieved - * from the table. - */ - public long getHits() { - return hits; - } - - /** - * Returns the number of times an element cpould not be retrieved - * from the table. - */ - public long getMisses() { - return misses; - } - - /** - * Returns the number of times an element was committed to the table. - */ - public long getPuts() { - return puts; - } - - /** - * Reset 'puts', 'misses' and 'puts' to 0. - * @since MMBase-1.9.2 - */ - public void reset() { - hits = 0; misses = 0; puts = 0; - } - - public void setMaxSize(int size) { - implementation.setMaxSize(size); - } - public int maxSize() { - return implementation.maxSize(); - } - public int getMaxSize() { - return maxSize(); - } - - /** - * @see java.util.Map#size() - */ - public int size() { - return implementation.size(); - } - - public int getSize() { - return size(); - } - public boolean contains(Object key) { - return implementation.containsKey(key); - } - - public int getCount(K key) { - return implementation.getCount(key); - } - - /** - * Returns the ratio of hits and misses. - * The higher the ratio, the more succesfull the table retrieval is. - * A value of 1 means every attempt to retrieve data is succesfull, - * while a value nearing 0 means most times the object requested it is - * not available. - * Generally a high ratio means the table can be shrunk, while a low ratio - * means its size needs to be increased. - * - * @return A double between 0 and 1 or NaN. - */ - public double getRatio() { - return ((double) hits) / ( hits + misses ); - } - - - /** - * Returns statistics on this table. - * The information shown includes number of accesses, ratio of misses and hits, - * current size, and number of puts. - */ - public String getStats() { - return "Access "+ (hits + misses) + " Ratio " + getRatio() + " Size " + size() + " Puts " + puts; - } - - - /** - * Sets this cache to active or passive. - * TODO: Writing back to caches.xml if necessary (if this call was nog caused by change of caches.xml itself) - */ - public void setActive(boolean a) { - active = a; - if (! active) { - implementation.clear(); - } - // inactive caches cannot contain anything - // another option would be to override also the 'contains' methods (which you problable should not use any way) - } - - @Override - public String toString() { - return "Cache " + getName() + ", Ratio: " + getRatio() + " " + implementation; - } - - /** - * Wether this cache is active or not. - */ - public final boolean isActive() { - return active; - } - - public int getByteSize() { - return getByteSize(new SizeOf()); - } - - public int getByteSize(SizeOf sizeof) { - int size = 26; - if (implementation instanceof SizeMeasurable) { - size += ((SizeMeasurable) implementation).getByteSize(sizeof); - } else { - // sizeof.sizeof(implementation) does not work because this.equals(implementation) - synchronized(lock) { - for (Map.Entry<K, V> entry : implementation.entrySet()) { - size += sizeof.sizeof(entry.getKey()); - size += sizeof.sizeof(entry.getValue()); - } - } - } - return size; - } - - /** - * Returns the sum of bytesizes of every key and value. This may count too much, because objects - * (like Nodes) may occur in more then one value, but this is considerably cheaper then {...@link - * #getByteSize()}, which has to keep a Collection of every counted object. - * @since MMBase-1.8 - */ - public int getCheapByteSize() { - int size = 0; - SizeOf sizeof = new SizeOf(); - synchronized(lock) { - for (Map.Entry<K, V> entry : implementation.entrySet()) { - size += sizeof.sizeof(entry.getKey()); - size += sizeof.sizeof(entry.getValue()); - sizeof.clear(); - } - } - return size; - } - - - /** - * @see java.util.Map#clear() - */ - public void clear() { - implementation.clear(); - } - - - /** - * @see java.util.Map#containsKey(java.lang.Object) - */ - public boolean containsKey(Object key) { - return implementation.containsKey(key); - } - - - /** - * @see java.util.Map#containsValue(java.lang.Object) - */ - public boolean containsValue(Object value) { - return implementation.containsValue(value); - } - - - /** - * @see java.util.Map#equals(java.lang.Object) - */ - @Override - public boolean equals(Object o) { - // odd, but this is accordinding to javadoc of Map. - if (o == this) - return true; - - if (!(o instanceof Cache)) - return false; - Cache<?,?> c = (Cache<?,?>) o; - if (!c.getName().equals(getName())) { - return false; - } - return implementation.equals(o); - } - - - /** - * @see java.util.Map#hashCode() - */ - @Override - public int hashCode() { - int hash = getName().hashCode(); - hash = HashCodeUtil.hashCode(hash, implementation.hashCode()); - return hash; - } - - - /** - * @see java.util.Map#isEmpty() - */ - public boolean isEmpty() { - return implementation.isEmpty(); - } - - - /** - * @see java.util.Map#keySet() - */ - public Set<K> keySet() { - return implementation.keySet(); - } - - - /** - * @see java.util.Map#putAll(java.util.Map) - */ - public void putAll(Map<? extends K,? extends V> t) { - implementation.putAll(t); - } - - - /** - * @see java.util.Map#remove(java.lang.Object) - */ - public V remove(Object key) { - return implementation.remove(key); - } - - - /** - * @see java.util.Map#values() - */ - public Collection<V> values() { - return implementation.values(); - } - - - /** - * Puts this cache in the caches repository. - * @see CacheManager#putCache(Cache) - */ - - public Cache<K,V> putCache() { - return CacheManager.putCache(this); - } - -} Deleted: mmbase/trunk/core/src/main/java/org/mmbase/cache/CacheConfigurationException.java =================================================================== --- mmbase/trunk/core/src/main/java/org/mmbase/cache/CacheConfigurationException.java 2010-04-12 15:21:43 UTC (rev 41845) +++ mmbase/trunk/core/src/main/java/org/mmbase/cache/CacheConfigurationException.java 2010-04-12 15:25:06 UTC (rev 41846) @@ -1,30 +0,0 @@ -/* - -This software is OSI Certified Open Source Software. -OSI Certified is a certification mark of the Open Source Initiative. - -The license (Mozilla version 1.0) can be read at the MMBase site. -See http://www.MMBase.org/license - -*/ -package org.mmbase.cache; - -/** - * @javadoc - * - * @author Ernst Bunders - * @since MMBase-1.8 - * @version $Id$ - */ -public class CacheConfigurationException extends Exception { - private static final long serialVersionUID = 0L; - - public CacheConfigurationException(String string) { - super(string); - } - - public CacheConfigurationException(String string, Exception cause) { - super(string, cause); - } - -} Deleted: mmbase/trunk/core/src/main/java/org/mmbase/cache/CacheImplementationInterface.java =================================================================== --- mmbase/trunk/core/src/main/java/org/mmbase/cache/CacheImplementationInterface.java 2010-04-12 15:21:43 UTC (rev 41845) +++ mmbase/trunk/core/src/main/java/org/mmbase/cache/CacheImplementationInterface.java 2010-04-12 15:25:06 UTC (rev 41846) @@ -1,51 +0,0 @@ -/* - -This software is OSI Certified Open Source Software. -OSI Certified is a certification mark of the Open Source Initiative. - -The license (Mozilla version 1.0) can be read at the MMBase site. -See http://www.MMBase.org/license - -*/ -package org.mmbase.cache; - -import java.util.*; - - -/** - * Classes which can be used as a cache implementation need to implement this interface. - * An implementation of this interface has to be thread-safe to guarantee correctness. - * - * @author Michiel Meeuwissen - * @version $Id$ - * @since MMBase-1.8 - */ -public interface CacheImplementationInterface<K, V> extends Map<K, V> { - - /** - * Sets the (maximal) size of the cache (if implementable). - */ - void setMaxSize(int size); - - /** - * Gets the (maximal) size of the cache (if implementable) - */ - int maxSize(); - - /** - * Returns the hit-count on a certain key (if implementable, -1 otherwise). - */ - int getCount(K key); - - /** - * Configure the implementation with the given configuration values - */ - void config(Map<String, String> configuration); - - /** - * The cache implementation must be somehow thread-safe. This method should - * return the object on which to synchronize, e.g. when looping over entrySet. - * @since MMBase-1.8.6 - */ - Object getLock(); -} Deleted: mmbase/trunk/core/src/main/java/org/mmbase/cache/CacheMBean.java =================================================================== --- mmbase/trunk/core/src/main/java/org/mmbase/cache/CacheMBean.java 2010-04-12 15:21:43 UTC (rev 41845) +++ mmbase/trunk/core/src/main/java/org/mmbase/cache/CacheMBean.java 2010-04-12 15:25:06 UTC (rev 41846) @@ -1,84 +0,0 @@ -/* - -This software is OSI Certified Open Source Software. -OSI Certified is a certification mark of the Open Source Initiative. - -The license (Mozilla version 1.0) can be read at the MMBase site. -See http://www.MMBase.org/license - -*/ -package org.mmbase.cache; - - -/** - * See http://java.sun.com/docs/books/tutorial/jmx/mbeans/standard.html - * @author Michiel Meeuwissen - * @version $Id$ - * @since MMBase-1.9 - */ -public interface CacheMBean { - - - String getName(); - String getDescription(); - - /** - * @see Cache#clear() - */ - void clear(); - - /** - * @see Cache#reset() - */ - void reset(); - - /** - * @see Cache#getSize() - */ - int getSize(); - /** - * @see Cache#getHits() - */ - long getHits(); - /** - * @see Cache#getMisses() - */ - long getMisses(); - /** - * @see Cache#getPuts() - */ - long getPuts(); - /** - * @see Cache#getMaxSize() - */ - int getMaxSize(); - void setMaxSize(int i); - /** - * @see Cache#isActive - */ - void setActive(boolean b); - boolean isActive(); - /** - * @see Cache#getMaxEntrySize - */ - int getMaxEntrySize(); - - /** - * @see Cache#setMaxEntrySize - */ - void setMaxEntrySize(int m); - - /** - * @see Cache#getRatio - */ - double getRatio(); - //Class getImplementation(); - - int getByteSize(); - - double getAvarageValueLength(); - - - - -} Deleted: mmbase/trunk/core/src/main/java/org/mmbase/cache/CacheManager.java =================================================================== --- mmbase/trunk/core/src/main/java/org/mmbase/cache/CacheManager.java 2010-04-12 15:21:43 UTC (rev 41845) +++ mmbase/trunk/core/src/main/java/org/mmbase/cache/CacheManager.java 2010-04-12 15:25:06 UTC (rev 41846) @@ -1,574 +0,0 @@ -/* - -This software is OSI Certified Open Source Software. -OSI Certified is a certification mark of the Open Source Initiative. - -The license (Mozilla version 1.0) can be read at the MMBase site. -See http://www.MMBase.org/license - -*/ -package org.mmbase.cache; - -import java.util.*; -import java.util.regex.*; - -import org.mmbase.util.*; -import org.mmbase.util.logging.Logger; -import org.mmbase.util.logging.Logging; -import org.mmbase.util.xml.DocumentReader; -import org.w3c.dom.Element; - -import java.util.concurrent.*; -import java.lang.management.*; -import javax.management.*; - - -/** - * Cache manager manages the static methods of {...@link Cache}. If you prefer you can call them on - * this in stead. - * - * Since 1.9.1 this class represents a singleton. Actually most methods should more logically not be - * static any more. - * - * @since MMBase-1.8 - * @version $Id$ - */ -public class CacheManager implements CacheManagerMBean { - - private static final Logger log = Logging.getLoggerInstance(CacheManager.class); - - /** - * All registered caches - */ - //private static final NavigableMap<String, Cache<?,?>> caches = new ConcurrentSkipListMap<String, Cache<?,?>>(); - private final Map<String, Cache<?,?>> caches = new ConcurrentHashMap<String, Cache<?,?>>(); - - private static CacheManager instance = null; - - - private CacheManager() { - // singleton - } - - private static String getMachineName(boolean assertUp) { - String machineName; - try { - if (assertUp) { - org.mmbase.bridge.ContextProvider.getDefaultCloudContext().assertUp(); - } - machineName = org.mmbase.module.core.MMBaseContext.getMachineName(); - } catch (NoClassDefFoundError ncfde) { - //happens when RMMCI - machineName = "localhost"; - } - return machineName; - } - - /** - * @since MMBase-1.9.1 - */ - - public static CacheManager getInstance() { - if (instance == null) { - instance = new CacheManager(); - ThreadPools.jobsExecutor.execute(new Runnable() { - public void run() { - - ObjectName on; - final Hashtable<String, String> props = new Hashtable<String, String>(); - - try { - props.put("type", "Caches"); - try { - String machineName = getMachineName(true); - - if (machineName != null) { - props.put("type", machineName); - } - } catch (Throwable t) { - log.error(t.getMessage(), t); - } - on = new ObjectName("org.mmbase", props); - } catch (MalformedObjectNameException mfone) { - log.warn("" + props + " " + mfone); - return; - } - try { - MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); - mbs.registerMBean(instance, on); - log.service("Registered " + on); - } catch (JMException jmo) { - log.warn("" + on + " " + jmo.getClass() + " " + jmo.getMessage()); - } catch (Throwable t) { - log.error("" + on + " " + t.getClass() + " " + t.getMessage()); - } - } - }); - - } - return instance; - } - - - - - /** - * Returns the Cache with a certain name. To be used in combination with getCaches(). If you - * need a certain cache, you can just as well call the non-static 'getCache' which is normally - * in cache singletons. - * - * @see #getCaches - */ - public static Cache getCache(String name) { - return getInstance().caches.get(name); - } - - /** - * Returns a cache wrapped in a 'Bean', so it is not a Map any more. This makes it easier - * accesible by tools which want that (like EL). - * @since MMBase-1.9 - */ - public static Bean getBean(String name) { - return new Bean(getCache(name)); - } - public static Set<Bean> getCaches(String className) { - SortedSet<Bean> result = new TreeSet<Bean>(); - for (Cache c : getInstance().caches.values()) { - try { - if (className == null || "".equals(className) || Class.forName(className).isInstance(c)) { - result.add(new Bean(c)); - } - } catch (Exception e) { - throw new RuntimeException(e); - } - } - return result; - } - - /** - * Returns the names of all caches. - * - * @return A Set containing the names of all caches. - */ - public static Set<String> getCaches() { - return Collections.unmodifiableSet(getInstance().caches.keySet()); - } - - /** - * @since MMBase-1.8.6 - */ - public static Map<String, Cache<?, ?>> getMap() { - return Collections.unmodifiableMap(getInstance().caches); - } - - - private static ThreadPoolExecutor cachePutter = new ThreadPoolExecutor(0, 1, 2 , TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new ThreadFactory() { - public Thread newThread(Runnable r) { - return ThreadPools.newThread(r, "CachePutter"); - } - }); -/* static { - cachePutter.allowCoreThreadTimeOut(true); - } -*/ - /** - * Puts a cache in the caches repository. This function will be - * called in the static of childs, therefore it is protected. - * - * @param cache A cache. - * @return The previous cache of the same type (stored under the same name) - */ - public static <K,V> Cache<K,V> putCache(final Cache<K,V> cache) { - Cache old = getInstance().caches.put(cache.getName(), cache); - try { - configure(configReader, cache.getName()); - } catch (Throwable t) { - log.error(t.getMessage(), t); - } - Runnable run = new Runnable() { - public void run() { - ObjectName name = getObjectName(cache, true); - try { - MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); - mbs.registerMBean(cache, name); - } catch (JMException jmo) { - log.warn("" + name + " " + jmo.getClass() + " " + jmo.getMessage()); - } catch (Throwable t) { - log.error("" + name + " " + t.getClass() + " " + t.getMessage()); - } - } - }; - if (org.mmbase.bridge.ContextProvider.getDefaultCloudContext().isUp()) { - run.run(); - } else { - cachePutter.execute(run); - } - - return old; - } - - - /** - * @since MMBase-1.9 - */ - private static ObjectName getObjectName(Cache cache, boolean assertUp) { - // Not using the Constructor with Hashtable, because you can't influence the order of keys - // with that. Which is relevant, e.g. when presented in a tree by jconsole. - StringBuilder buf = new StringBuilder("org.mmbase:"); - try { - buf.append("type=Caches"); - org.mmbase.util.transformers.CharTransformer identifier = new org.mmbase.util.transformers.Identifier(); - String machineName = getMachineName(assertUp); - if (machineName != null) { - buf.append(",mmb=").append(machineName); - } else { - } - if (cache != null) { - buf.append(",name=").append(identifier.transform(cache.getName())); - } else { - //props.put("name", "*"); // WTF, this does not work in java 5. - } - return new ObjectName(buf.toString()); - } catch (MalformedObjectNameException mfone) { - log.warn("" + buf + " " + mfone); - return null; - } - } - - /** - * Configures the caches using a config File. There is only one - * config file now so the argument is a little overdone, but it - * doesn't harm. - */ - - private static void configure(DocumentReader file) { - configure(file, null); - } - - private static DocumentReader configReader = null; - - /** - * As configure, but it only changes the configuration of the cache 'only'. - * This is called on first use of a cache. - */ - private static void configure(DocumentReader xmlReader, String only) { - if (xmlReader == null) { - return; // nothing can be done... - } - - if (only == null) { - log.service("Configuring caches with " + xmlReader.getSystemId()); - } else { - if (log.isDebugEnabled()) log.debug("Configuring cache " + only + " with file " + xmlReader.getSystemId()); - } - - for (Element cacheElement: xmlReader.getChildElements("caches", "cache")) { - String cacheName = cacheElement.getAttribute("name"); - if (only != null && ! only.equals(cacheName)) { - continue; - } - // TODO: fix again when everybody runs 1.5.0_08, because of - // generics bug http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4916620 - Cache cache = getCache(cacheName); - if (cache == null) { - log.service("No cache " + cacheName + " is present (perhaps not used yet?)"); - } else { - String clazz = xmlReader.getElementValue(xmlReader.getElementByPath(cacheElement, "cache.implementation.class")); - if(!"".equals(clazz)) { - Element cacheImpl = xmlReader.getElementByPath(cacheElement, "cache.implementation"); - Map<String,String> configValues = new HashMap<String,String>(); - for (Element attrNode: xmlReader.getChildElements(cacheImpl, "param")) { - String paramName = xmlReader.getElementAttributeValue(attrNode, "name"); - String paramValue = xmlReader.getElementValue(attrNode); - configValues.put(paramName, paramValue); - } - cache.setImplementation(clazz, configValues); - } - String status = xmlReader.getElementValue(xmlReader.getElementByPath(cacheElement, "cache.status")); - cache.setActive(status.equalsIgnoreCase("active")); - try { - Integer size = Integer.valueOf(xmlReader.getElementValue(xmlReader.getElementByPath(cacheElement, "cache.size"))); - cache.setMaxSize(size.intValue()); - log.service("Setting " + cacheName + " " + status + " with size " + size); - } catch (NumberFormatException nfe) { - log.error("Could not configure cache " + cacheName + " because the size was wrong: " + nfe.toString()); - } catch (Throwable t) { - log.error(" " + cacheName + " maxsize " + t.getMessage()); - } - String maxSize = xmlReader.getElementValue(xmlReader.getElementByPath(cacheElement, "cache.maxEntrySize")); - if (!"".equals(maxSize)) { - try { - cache.maxEntrySize = Integer.parseInt(maxSize); - log.service("Setting maximum entry size on " + cacheName + ": " + cache.maxEntrySize + " bytes "); - } catch (NumberFormatException nfe2) { - log.error("Could not set max entry size cache of " + cacheName + " because " + nfe2.toString()); - } catch (Throwable t) { - log.error(" " + cacheName + " maxentrysize " + t.getMessage()); - } - } else { - if (cache.getDefaultMaxEntrySize() > 0) { - log.service("No max entry size specified for this cache taking default " + cache.getDefaultMaxEntrySize() + " bytes"); - } - cache.maxEntrySize = cache.getDefaultMaxEntrySize(); - //now see if we have to load cache release strategies for this lovely cache... - if(cache instanceof QueryResultCache){ - QueryResultCache queryCache = (QueryResultCache) cache; - //first remove all present strategies (this might be a reconfiguration) - queryCache.getReleaseStrategy().removeAllStrategies(); - log.debug("found a SearchQueryCache: " + cacheName); - //see if there are globally configured release strategies - Element releaseStrategies = xmlReader.getElementByPath("caches.releaseStrategies"); - if (releaseStrategies != null) { - queryCache.getReleaseStrategy().fillFromXml(releaseStrategies); - } - queryCache.getReleaseStrategy().fillFromXml(cacheElement); - - if (queryCache.getReleaseStrategy().size() == 0) { - log.warn("No release-strategies configured for cache " + queryCache + " (nor globally configured); falling back to basic release strategy"); - queryCache.addReleaseStrategy(new BasicReleaseStrategy()); - } - log.service("Release strategies for " + queryCache.getName() + ": " + queryCache.getReleaseStrategy()); - } - } - } - } - } - - - - /** - * The caches can be configured with an XML file, this file can - * be changed which causes the caches to be reconfigured automaticly. - */ - private static ResourceWatcher configWatcher = new ResourceWatcher () { - public void onChange(String resource) { - try { - org.xml.sax.InputSource is = ResourceLoader.getConfigurationRoot().getInputSource(resource); - log.service("Reading " + is.getSystemId()); - configReader = new DocumentReader(is, Cache.class); - } catch (Exception e) { - log.warn(e.getClass() + " " + e.getMessage()); - return; - } - configure(configReader); - } - }; - - static { // configure - try { - log.debug("Static init of Caches"); - configWatcher.add("caches.xml"); - configWatcher.onChange("caches.xml"); - configWatcher.setDelay(10 * 1000); // check every 10 secs if config changed - configWatcher.start(); - } catch (Throwable t) { - log.error(t); - } - - - } - - - public static int getTotalByteSize() { - int len = 0; - SizeOf sizeof = new SizeOf(); - for (Map.Entry<String, Cache<?, ?>> entry : getInstance().caches.entrySet()) { - len += sizeof.sizeof(entry.getKey()) + sizeof.sizeof(entry.getValue()); - } - return len; - } - - /** - * Clears and dereferences all caches. To be used on shutdown of MMBase. - * @since MMBase-1.8.1 - */ - public static void shutdown() { - MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); - log.info("Clearing and unregistering all caches"); - log.debug(mbs.queryNames(getObjectName(null, false), null)); - for(Cache<?,?> cache : getInstance().caches.values()) { - cache.clear(); - ObjectName name = getObjectName(cache, false); - if (mbs.isRegistered(name)) { - try { - mbs.unregisterMBean(name); - } catch (JMException jmo) { - log.warn("" + name + " " + jmo.getClass() + " " + jmo.getMessage() + " " + mbs.queryNames(null, null)); - } - } - } - { - final Hashtable<String, String> props = new Hashtable<String, String>(); - props.put("type", "Caches"); - String machineName = org.mmbase.module.core.MMBaseContext.getMachineName(); - if (machineName != null) { - props.put("type", machineName); - } - try { - ObjectName name = new ObjectName("org.mmbase", props); - if (mbs.isRegistered(name)) { - mbs.unregisterMBean(name); - } - } catch (JMException jmo) { - - } - } - if(mbs.queryNames(getObjectName(null, false), null).size() > 0) { - log.warn("Didn't unregister all caches" + mbs.queryNames(getObjectName(null, false), null)); - } - getInstance().caches.clear(); - instance = null; - } - - - /** - * Used in config/functions/caches.xml - */ - public static Object remove(String name, Object key) { - Cache cache = getCache(name); - if (cache == null) { - throw new IllegalArgumentException(); - } - log.service("Removing " + key + " from " + cache); - return cache.remove(key); - } - - /** - * @since MMBase-1.9.1 - */ - public String clear(String pattern) { - if (pattern == null) pattern = ".*"; - StringBuilder buf = new StringBuilder(); - Pattern p = Pattern.compile(pattern); - for (Map.Entry<String, Cache<?, ?>> entry : caches.entrySet()) { - if (p.matcher(entry.getKey()).matches()) { - buf.append("Clearing " + entry.getValue() + "\n"); - entry.getValue().clear(); - } - } - if (buf.length() == 0) buf.append("The regular expression '" + pattern + "' matched no cache at all"); - return buf.toString(); - } - /** - * @since MMBase-1.9.1 - */ - public String enable(String pattern) { - if (pattern == null) pattern = ".*"; - StringBuilder buf = new StringBuilder(); - Pattern p = Pattern.compile(pattern); - for (Map.Entry<String, Cache<?, ?>> entry : caches.entrySet()) { - if (p.matcher(entry.getKey()).matches()) { - Cache c = entry.getValue(); - if(c.isActive()) { - buf.append("Already active " + c + "\n"); - } else { - c.setActive(true); - buf.append("Making active " + c + "\n"); - } - - } - } - if (buf.length() == 0) buf.append("The regular expression '" + pattern + "' matched no cache at all"); - return buf.toString(); - } - /** - * @since MMBase-1.9.1 - */ - public String disable(String pattern) { - if (pattern == null) pattern = ".*"; - StringBuilder buf = new StringBuilder(); - Pattern p = Pattern.compile(pattern); - for (Map.Entry<String, Cache<?, ?>> entry : caches.entrySet()) { - if (p.matcher(entry.getKey()).matches()) { - Cache c = entry.getValue(); - if(c.isActive()) { - c.setActive(false); - buf.append("Making inactive " + c + "\n"); - } else { - buf.append("Already inactive " + c + "\n"); - } - - } - } - if (buf.length() == 0) buf.append("The regular expression '" + pattern + "' matched no cache at all"); - return buf.toString(); - } - /** - * @since MMBase-1.9.1 - */ - public String readConfiguration() { - configWatcher.onChange("caches.xml"); - return "Read " + ResourceLoader.getConfigurationRoot().getResource("caches.xml"); - } - - - public static class Bean<K, V> implements Comparable<Bean<?, ?>> { - /* private final Cache<K, V> cache; // this line prevents building in Java 1.5.0_07 probably because of http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4916620 */ - private final Cache cache; - public Bean(Cache<K, V> c) { - cache = c; - } - public String getName() { return cache.getName(); } - public String getDescription() { return cache.getDescription(); } - public int getMaxEntrySize() { return cache.getMaxEntrySize(); } - public Set<Map.Entry<K, V>> getEntrySet() { - synchronized (cache.getLock()) { - return new HashSet<Map.Entry<K, V>>(cache.entrySet()); - } - } - public Set<K> getKeySet() { - synchronized (cache.getLock()) { - return new HashSet<K>(cache.keySet()); - } - } - public long getHits() { return cache.getHits(); } - public long getMisses() { return cache.getMisses(); } - public long getPuts() { return cache.getPuts(); } - public int getMaxSize() { return cache.maxSize(); } - public int getSize() { return cache.size(); } - public double getRatio() { return cache.getRatio(); } - public String getStats() { return cache.getStats(); } - public String toString() { return cache.toString(); } - public boolean isActive() { return cache.isActive(); } - public int getByteSize() { return cache.getByteSize(); } - public int getCheapByteSize() { return cache.getCheapByteSize(); } - public boolean isEmpty() { return cache.isEmpty(); } - public ReleaseStrategy getReleaseStrategy() { return cache instanceof QueryResultCache ? ((QueryResultCache) cache).getReleaseStrategy() : null;} - public Map<K, V> getMap() { return cache; } - public Map<K, Integer> getCounts() { - return new AbstractMap<K, Integer>() { - public Set<Map.Entry<K, Integer>> entrySet() { - return new AbstractSet<Map.Entry<K, Integer>>() { - public int size() { - return cache.size(); - } - public Iterator<Map.Entry<K, Integer>> iterator() { - return new Iterator<Map.Entry<K, Integer>>() { - private Iterator<K> iterator = Bean.this.getKeySet().iterator(); - public boolean hasNext() { - return iterator.hasNext(); - } - public Map.Entry<K, Integer> next() { - K key = iterator.next(); - return new org.mmbase.util.Entry<K, Integer>(key, cache.getCount(key)); - } - public void remove() { - throw new UnsupportedOperationException(); - } - }; - } - }; - } - }; - } - public boolean equals(Object o) { - return o instanceof Bean && ((Bean) o).cache.equals(cache); - } - public int hashCode() { - return cache.hashCode(); - } - public int compareTo(Bean<?, ?> bean) { - return getName().compareTo(bean.getName()); - } - } -} Deleted: mmbase/trunk/core/src/main/java/org/mmbase/cache/CacheManagerMBean.java =================================================================== --- mmbase/trunk/core/src/main/java/org/mmbase/cache/CacheManagerMBean.java 2010-04-12 15:21:43 UTC (rev 41845) +++ mmbase/trunk/core/src/main/java/org/mmbase/cache/CacheManagerMBean.java 2010-04-12 15:25:06 UTC (rev 41846) @@ -1,25 +0,0 @@ -/* - -This software is OSI Certified Open Source Software. -OSI Certified is a certification mark of the Open Source Initiative. - -The license (Mozilla version 1.0) can be read at the MMBase site. -See http://www.MMBase.org/license - -*/ -package org.mmbase.cache; - - -/** - * See http://java.sun.com/docs/books/tutorial/jmx/mbeans/standard.html - * @author Michiel Meeuwissen - * @version $Id$ - * @since MMBase-1.9.1 - */ -public interface CacheManagerMBean { - - String clear(String regex); - String disable(String regex); - String enable(String regex); - String readConfiguration(); -} Deleted: mmbase/trunk/core/src/main/java/org/mmbase/cache/CachePolicy.java =================================================================== --- mmbase/trunk/core/src/main/java/org/mmbase/cache/CachePolicy.java 2010-04-12 15:21:43 UTC (rev 41845) +++ mmbase/trunk/core/src/main/java/org/mmbase/cache/CachePolicy.java 2010-04-12 15:25:06 UTC (rev 41846) @@ -1,108 +0,0 @@ -/* - -This software is OSI Certified Open Source Software. -OSI Certified is a certification mark of the Open Source Initiative. - -The license (Mozilla version 1.0) can be read at the MMBase site. -See http://www.MMBase.org/license - -*/ -package org.mmbase.cache; - -import java.io.Serializable; -import java.util.Map; -import java.util.HashMap; - -/** - * A CachePolicy object determines for a given object whether it should be cached or not, and how. - * Code that makes use of a cache should use a CachePolicy object, when available, to determine if the - * object should be cached or not. - * - * @since MMBase 1.8 - * @author Pierre van Rooden - * @version $Id$ - */ -abstract public class CachePolicy implements Serializable { - - // map with all known policies - static private Map<Object,CachePolicy> policies = new HashMap<Object,CachePolicy>(); - - /** - * Standard cache policy that advises to never cache a passed object. - * Accessible with the key "never". - */ - static final public CachePolicy NEVER = new CachePolicy("never") { - private static final long serialVersionUID = 0; - public boolean checkPolicy(Object o) { - return false; - } - - @Override - public String getDescription() { - return "CACHE NEVER"; - } - }; - - /** - * Standard cache policy that advises to always cache a passed object. - * Accessible with the key "always". - */ - static final public CachePolicy ALWAYS = new CachePolicy("always") { - private static final long serialVersionUID = 0L; - public boolean checkPolicy(Object o) { - return true; - } - - @Override - public String getDescription() { - return "CACHE ALWAYS"; - } - }; - - /** - * Obtains a cache policy given a policy key. - * @param policyKey the key of the cache policy - * @return the policy key - * @throws IllegalArgumentException if the policy does not exist - */ - static public CachePolicy getPolicy(Object policyKey) { - CachePolicy policy = policies.get(policyKey); - if (policy == null) { - throw new IllegalArgumentException("There is no cache policy known with key '"+policyKey+"'"); - } - return policy; - } - - - static public void putPolicy(Object policyKey, CachePolicy policy) { - policies.put(policyKey, policy); - } - - /** - * Instantiates a new cache policy, and registers it with the given policy key. - */ - protected CachePolicy(Object policyKey) { - CachePolicy.putPolicy(policyKey, this); - } - - /** - * Instantiates a new cache policy without registering it - */ - protected CachePolicy() { - } - - /** - * Checks whether the policy advises to cache the passed object. - * @param o the object to check the cache for - * @return <code>true</code> if the policy advises to cache this object, <code>false</code> otherwise. - */ - abstract public boolean checkPolicy(Object o); - - /** - * Returns a description of the policy. - */ - public String getDescription() { - return getClass().getName(); - } - -} Deleted: mmbase/trunk/core/src/main/java/org/mmbase/cache/Cacheable.java =================================================================== --- mmbase/trunk/core/src/main/java/org/mmbase/cache/Cacheable.java 2010-04-12 15:21:43 UTC (rev 41845) +++ mmbase/trunk/core/src/main/java/org/mmbase/cache/Cacheable.java 2010-04-12 15:25:06 UTC (rev 41846) @@ -1,36 +0,0 @@ -/* - -This software is OSI Certified Open Source Software. -OSI Certified is a certification mark of the Open Source Initiative. - -The license (Mozilla version 1.0) can be read at the MMBase site. -See http://www.MMBase.org/license - -*/ - -package org.mmbase.cache; - -/** - * A Cacheable object contains information on cache policies, which determines whether the object should be cached or not. - * The code that handles the caching should verify this for a cacheable object. - * - * @author Pierre van Rooden - * @version $Id$ - * @since MMBase-1.8 - * @see org.mmbase.cache.CachePolicy - */ -public interface Cacheable { - - /** - * Returns the CachePolicy of the object. - * @return the {...@link org.mmbase.cache.CachePolicy} object. - */ - CachePolicy getCachePolicy(); - - /** - * Sets the CachePolicy of the query. - * @param policy the {...@link org.mmbase.cache.CachePolicy} object. - */ - void setCachePolicy(CachePolicy policy); - -} _______________________________________________ Cvs mailing list [email protected] http://lists.mmbase.org/mailman/listinfo/cvs
