001: /*
002: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
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: package de.odysseus.calyxo.base.access;
017:
018: import java.util.HashMap;
019: import java.util.Map;
020:
021: import javax.servlet.http.HttpServletRequest;
022:
023: import de.odysseus.calyxo.base.util.MapFacade;
024:
025: /**
026: * An accessor map is a container for other accessors. It produces a map
027: * which uses its contained accessors to produce objects. The
028: * object produced by a contained accessor is cached if that accessor
029: * states that it is cachable. Otherwise, a new object is served
030: * by the accessor each time the accessor's key is requested.
031: *
032: * @author Christoph Beck
033: */
034: public class AccessorMap implements Accessor {
035: private Map map = new HashMap();
036:
037: /**
038: * This method produces a map. This map implements the
039: * <code>get(Object)</code> method as follows:
040: * It gets the accessor stored in the outer class under the specified
041: * key and uses that accessor to produce the result object.
042: * For subsequent request of the same key, the object is cached
043: * if the accessor states that it is cachable.
044: *
045: * @see Accessor#isCacheable()
046: */
047: public Object get(final HttpServletRequest request) {
048: return new MapFacade() {
049: private Map cache;
050:
051: public Object get(Object key) {
052: Accessor accessor = AccessorMap.this .get(key);
053: if (accessor == null) {
054: throw new AccessException("No such accessor: "
055: + key);
056: }
057: if (accessor.isCacheable()) {
058: if (cache == null) {
059: cache = new HashMap();
060: } else if (cache.containsKey(key)) {
061: return cache.get(key);
062: }
063: Object value = accessor.get(request);
064: cache.put(key, value);
065: return value;
066: } else {
067: return accessor.get(request);
068: }
069: }
070: };
071: }
072:
073: /**
074: * The produced lookup map may be cached in a containing accessor map.
075: */
076: public boolean isCacheable() {
077: return true;
078: }
079:
080: /**
081: * Save accessor under specified key
082: */
083: public void put(Object key, Accessor value) {
084: map.put(key, value);
085: }
086:
087: /**
088: * Get accessor for specified key
089: */
090: public Accessor get(Object key) {
091: return (Accessor) map.get(key);
092: }
093:
094: /**
095: * Remove accessor
096: */
097: public void remove(Object key) {
098: map.remove(key);
099: }
100: }
|