001: /*
002: * Copyright 2004-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */
017:
018: package org.jpublish.util;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022: import org.jpublish.JPublishCacheException;
023:
024: import java.util.Arrays;
025: import java.util.HashMap;
026: import java.util.List;
027: import java.util.Map;
028:
029: /**
030: * @author <a href="mailto:florin.patrascu@gmail.com">Florin T.PATRASCU</a>
031: * @since $Revision$ (created: 8-Jul-2006 8:06:33 PM)
032: */
033: public class JPublishSimpleCacheImpl implements JPublishCache {
034: protected static final Log log = LogFactory
035: .getLog(JPublishSimpleCacheImpl.class);
036: private Map cache;
037:
038: /**
039: * Add or creates a new cache with a given name
040: *
041: * @param cacheName the name of the cache
042: * @throws org.jpublish.JPublishCacheException
043: *
044: */
045: public void addCache(String cacheName)
046: throws JPublishCacheException {
047: log.info(cacheName + " created.");
048: cache = new HashMap(10);
049: }
050:
051: /**
052: * Gets a value of an element which matches the given key.
053: *
054: * @param key the key of the element to return.
055: * @return The value placed into the cache with an earlier put, or null if not found or expired
056: * @throws org.jpublish.JPublishCacheException
057: *
058: */
059: public Object get(Object key) throws JPublishCacheException {
060: if (key == null)
061: throw new JPublishCacheException(
062: "Invalid key specification: null");
063:
064: return cache.get(key);
065: }
066:
067: /**
068: * Puts an object into the cache.
069: *
070: * @param key a serializable} key
071: * @param value a Serializable value
072: * @throws org.jpublish.JPublishCacheException
073: * if the parameters are not {@link java.io.Serializable} or another {@link Exception} occurs.
074: */
075: public void put(Object key, Object value)
076: throws JPublishCacheException {
077: if (key != null && value != null)
078: cache.put(key, value);
079:
080: }
081:
082: /**
083: * Removes the element which matches the key.
084: * <p/>
085: * If no element matches, nothing is removed and no Exception is thrown.
086: *
087: * @param key the key of the element to remove
088: * @throws org.jpublish.JPublishCacheException
089: *
090: */
091: public void remove(Object key) throws JPublishCacheException {
092: if (cache.containsKey(key))
093: cache.remove(key);
094: }
095:
096: /**
097: * Remove all elements in the cache, but leave the cache
098: * <p/>
099: * in a useable state.
100: *
101: * @throws org.jpublish.JPublishCacheException
102: *
103: */
104: public void clear() throws JPublishCacheException {
105: cache.clear();
106: }
107:
108: /**
109: * Remove the cache and make it unuseable.
110: *
111: * @throws org.jpublish.JPublishCacheException
112: *
113: */
114: public void destroy() throws JPublishCacheException {
115: cache = null;
116: }
117:
118: /**
119: * define the cache flush interval
120: *
121: * @param interval
122: * @throws org.jpublish.JPublishCacheException
123: *
124: */
125: public void setFlushInterval(long interval)
126: throws JPublishCacheException {
127: }
128:
129: /**
130: * retrieves the flushing interval
131: *
132: * @throws org.jpublish.JPublishCacheException
133: *
134: */
135: public long getFlushInterval() throws JPublishCacheException {
136: return 0;
137: }
138:
139: /**
140: * @return
141: * @throws org.jpublish.JPublishCacheException
142: *
143: */
144: public List getKeys() throws JPublishCacheException {
145: if (cache != null)
146: return Arrays.asList(cache.keySet().toArray());
147: else
148: return null;
149: }
150: }
|