001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.util;
021:
022: import java.util.Enumeration;
023: import java.util.Hashtable;
024:
025: import com.salmonllc.properties.Props;
026:
027: /**
028: * This class is used to cache images for the objectstore.
029: */
030: public class ObjectstoreCache {
031: static Hashtable _entries;
032: static long _bytesUsed = 0;
033: static long _maxBytes = 0;
034: static long _lastCleared = -1;
035: static long _cacheTimeout = 17500000;
036:
037: private static class CacheEntry {
038: byte[] data;
039: long lastUsed;
040: }
041:
042: /**
043: * This method adds an item to the cache
044: */
045: public static synchronized boolean addEntry(String key, byte[] data) {
046: init();
047: if (_entries.containsKey(key)) {
048: CacheEntry e = (CacheEntry) _entries.get(key);
049: _bytesUsed -= e.data.length;
050: _bytesUsed += data.length;
051: e.data = data;
052: e.lastUsed = System.currentTimeMillis();
053: return true;
054: } else {
055: if (_bytesUsed >= _maxBytes)
056: return false;
057: CacheEntry e = new CacheEntry();
058: _bytesUsed += data.length;
059: e.data = data;
060: e.lastUsed = System.currentTimeMillis();
061: _entries.put(key, e);
062: return true;
063: }
064: }
065:
066: /**
067: * This method will clear old entries from the cache
068: */
069: public static void cleanCache() {
070: long t = System.currentTimeMillis() - _cacheTimeout;
071: if (_entries != null) {
072: Enumeration enum = _entries.keys();
073: while (enum.hasMoreElements()) {
074: String key = (String) enum.nextElement();
075: CacheEntry ent = (CacheEntry) _entries.get(key);
076: if (ent != null) {
077: if (ent.lastUsed < t)
078: removeEntry(key);
079:
080: }
081: }
082: }
083:}
084:
085: /**
086: * This clears the ObjectStore cache of all entries
087: */
088: public static synchronized void clearCache() {
089: init();
090: _entries.clear();
091: _bytesUsed = 0;
092: initProperties();
093: _lastCleared = System.currentTimeMillis();
094: }
095:
096: /**
097: * This method gets an item from the cache
098: */
099: public static byte[] getEntry(String key) {
100: init();
101: if (_entries.containsKey(key)) {
102: CacheEntry e = (CacheEntry) _entries.get(key);
103: return e.data;
104: }
105: return null;
106: }
107:
108: /**
109: * This method returns the time the cache was last cleared
110: */
111: public static long getLastCleared() {
112: return _lastCleared;
113: }
114:
115: /**
116: * This method was created in VisualAge.
117: */
118: private static void init() {
119: if (_entries == null) {
120: _entries = new Hashtable();
121: _bytesUsed = 0;
122: _lastCleared = -1;
123: initProperties();
124: }
125: }
126:
127: private static void initProperties() {
128: Props p = Props.getSystemProps();
129: _maxBytes = p.getIntProperty(Props.SYS_OBJECTSTORE_CACHE_SIZE);
130: if (_maxBytes == -1)
131: _maxBytes = 1000000;
132: _cacheTimeout = p
133: .getIntProperty(Props.SYS_OBJECTSTORE_CACHE_IDLE_TIME_OUT);
134: if (_cacheTimeout == -1)
135: _cacheTimeout = 1750000;
136: }
137:
138: private static synchronized void removeEntry(String key) {
139: init();
140: if (_entries.containsKey(key)) {
141: CacheEntry e = (CacheEntry) _entries.get(key);
142: _bytesUsed -= e.data.length;
143: _entries.remove(key);
144: }
145: }
146: }
|