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.woody;
018:
019: import java.io.IOException;
020:
021: import org.apache.avalon.framework.activity.Disposable;
022: import org.apache.avalon.framework.component.Component;
023: import org.apache.avalon.framework.configuration.Configurable;
024: import org.apache.avalon.framework.configuration.Configuration;
025: import org.apache.avalon.framework.configuration.ConfigurationException;
026: import org.apache.avalon.framework.logger.AbstractLogEnabled;
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.commons.collections.FastHashMap;
032: import org.apache.excalibur.source.Source;
033: import org.apache.excalibur.source.SourceValidity;
034:
035: /**
036: * Component implementing the {@link CacheManager} role.
037: *
038: * @version $Id: DefaultCacheManager.java 433543 2006-08-22 06:22:54Z crossley $
039: */
040: public class DefaultCacheManager extends AbstractLogEnabled implements
041: CacheManager, ThreadSafe, Serviceable, Disposable,
042: Configurable, Component {
043:
044: protected ServiceManager manager;
045: protected Configuration configuration;
046: protected FastHashMap cache = new FastHashMap();
047:
048: public void service(ServiceManager serviceManager)
049: throws ServiceException {
050: this .manager = serviceManager;
051: }
052:
053: /**
054: * Configurable
055: */
056: public void configure(Configuration configuration)
057: throws ConfigurationException {
058: this .configuration = configuration;
059: }
060:
061: public Object get(Source source, String prefix) {
062: String key = prefix + source.getURI();
063: SourceValidity newValidity = source.getValidity();
064:
065: if (newValidity == null) {
066: cache.remove(key);
067: return null;
068: }
069:
070: Object[] objectAndValidity = (Object[]) cache.get(key);
071: if (objectAndValidity == null)
072: return null;
073:
074: SourceValidity storedValidity = (SourceValidity) objectAndValidity[1];
075: int valid = storedValidity.isValid();
076: boolean isValid;
077: if (valid == 0) {
078: valid = storedValidity.isValid(newValidity);
079: isValid = (valid == 1);
080: } else {
081: isValid = (valid == 1);
082: }
083:
084: if (!isValid) {
085: cache.remove(key);
086: return null;
087: }
088:
089: return objectAndValidity[0];
090: }
091:
092: public void set(Object object, Source source, String prefix)
093: throws IOException {
094: String key = prefix + source.getURI();
095: SourceValidity validity = source.getValidity();
096: if (validity != null) {
097: Object[] objectAndValidity = { object, validity };
098: cache.put(key, objectAndValidity);
099: }
100: }
101:
102: /**
103: * Disposable
104: */
105: public void dispose() {
106: this.manager = null;
107: this.cache = null;
108: }
109: }
|