001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.caching.impl;
018:
019: import java.io.IOException;
020: import java.io.Serializable;
021:
022: import org.apache.avalon.framework.activity.Disposable;
023: import org.apache.avalon.framework.logger.AbstractLogEnabled;
024: import org.apache.avalon.framework.parameters.ParameterException;
025: import org.apache.avalon.framework.parameters.Parameterizable;
026: import org.apache.avalon.framework.parameters.Parameters;
027: import org.apache.avalon.framework.service.ServiceException;
028: import org.apache.avalon.framework.service.ServiceManager;
029: import org.apache.avalon.framework.service.Serviceable;
030: import org.apache.avalon.framework.thread.ThreadSafe;
031: import org.apache.cocoon.ProcessingException;
032: import org.apache.cocoon.caching.Cache;
033: import org.apache.cocoon.caching.CachedResponse;
034: import org.apache.excalibur.store.Store;
035:
036: /**
037: * This is the Cocoon cache. This component is responsible for storing
038: * and retrieving cached responses. It can be used to monitor the cache
039: * or the investigate which responses are cached etc.
040: * This component will grow!
041: *
042: * @since 2.1
043: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
044: * @version CVS $Id: CacheImpl.java 433543 2006-08-22 06:22:54Z crossley $
045: */
046: public class CacheImpl extends AbstractLogEnabled implements Cache,
047: ThreadSafe, Serviceable, Disposable, Parameterizable {
048:
049: /** The store containing the cached responses */
050: protected Store store;
051:
052: /** The service manager */
053: protected ServiceManager manager;
054:
055: /**
056: * Serviceable Interface
057: */
058: public void service(ServiceManager manager) throws ServiceException {
059: this .manager = manager;
060: }
061:
062: /**
063: * Disposable Interface
064: */
065: public void dispose() {
066: this .manager.release(this .store);
067: this .store = null;
068: this .manager = null;
069: }
070:
071: /**
072: * Store a cached response
073: * @param key the key used by the caching algorithm to identify the
074: * request
075: * @param response the cached response
076: */
077: public void store(Serializable key, CachedResponse response)
078: throws ProcessingException {
079: if (this .getLogger().isInfoEnabled()) {
080: this .getLogger().info("Caching new response for " + key);
081: }
082: try {
083: this .store.store(key, response);
084: } catch (IOException ioe) {
085: throw new ProcessingException("Unable to cache response.",
086: ioe);
087: }
088: }
089:
090: /**
091: * Get a cached response.
092: * If it is not available <code>null</code> is returned.
093: * @param key the key used by the caching algorithm to identify the
094: * request
095: */
096: public CachedResponse get(Serializable key) {
097: final CachedResponse r = (CachedResponse) this .store.get(key);
098: if (this .getLogger().isDebugEnabled()) {
099: this .getLogger().debug(
100: "Cached response for " + key + " : "
101: + (r == null ? "not found" : "found"));
102: }
103: return r;
104: }
105:
106: /**
107: * Remove a cached response.
108: * If it is not available no operation is performed.
109: * @param key the key used by the caching algorithm to identify the
110: * request
111: */
112: public void remove(Serializable key) {
113: if (this .getLogger().isInfoEnabled()) {
114: this .getLogger()
115: .info("Removing cached response for " + key);
116: }
117: this .store.remove(key);
118: }
119:
120: /**
121: * clear cache of all cached responses
122: */
123: public void clear() {
124: if (this .getLogger().isInfoEnabled()) {
125: this .getLogger().info("Clearing cache");
126: }
127: // FIXME this clears the whole store!
128: this .store.clear();
129: }
130:
131: /**
132: * See if a response is cached under this key
133: */
134: public boolean containsKey(Serializable key) {
135: return this .store.containsKey(key);
136: }
137:
138: /* (non-Javadoc)
139: * @see org.apache.avalon.framework.parameters.Parameterizable#parameterize(org.apache.avalon.framework.parameters.Parameters)
140: */
141: public void parameterize(Parameters parameters)
142: throws ParameterException {
143: String storeName = parameters.getParameter("store", Store.ROLE);
144: try {
145: this .store = (Store) this .manager.lookup(storeName);
146: } catch (ServiceException e) {
147: throw new ParameterException("Unable to lookup store: "
148: + storeName, e);
149: }
150: }
151:
152: }
|