001: package org.apache.turbine.om;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.HashMap;
023: import java.util.Map;
024:
025: import org.apache.turbine.services.pull.ApplicationTool;
026: import org.apache.turbine.util.pool.Recyclable;
027:
028: /**
029: * A Pull tool to make om objects available to a template
030: *
031: * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
032: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
033: * @version $Id: OMTool.java 534527 2007-05-02 16:10:59Z tv $
034: */
035: public class OMTool implements ApplicationTool, Recyclable {
036: // private RunData data;
037: private HashMap omMap;
038:
039: // note the following could be a static attribute to reduce memory
040: // footprint. Might require a service to front load the
041: // PullHelpers to avoid MT issues. A multiple write is not so bad
042: // though
043:
044: /** The cache of PullHelpers. **/
045: private static Map pullMap = new HashMap();
046:
047: /**
048: * The Factory responsible for retrieving the
049: * objects from storage
050: */
051: private RetrieverFactory omFactory;
052:
053: public OMTool() throws Exception {
054: omMap = new HashMap();
055: // String className = Turbine.getConfiguration()
056: // .getString("tool.om.factory");
057: // RetrieverFactory omFactory =
058: // (RetrieverFactory)Class.forName(className).newInstance();
059: }
060:
061: /**
062: * Prepares tool for a single request
063: */
064: public void init(Object runData) {
065: // data = (RunData)runData;
066: }
067:
068: /**
069: * Implementation of ApplicationTool interface is not needed for this
070: * method as the tool is request scoped
071: */
072: public void refresh() {
073: // empty
074: }
075:
076: /**
077: * Inner class to present a nice interface to the template designer
078: */
079: private class PullHelper {
080: String omName;
081:
082: private PullHelper(String omName) {
083: this .omName = omName;
084: }
085:
086: public Object setKey(String key) throws Exception {
087: Object om = null;
088:
089: String inputKey = omName + key;
090: if (omMap.containsKey(inputKey)) {
091: om = omMap.get(inputKey);
092: } else {
093: om = omFactory.getInstance(omName).retrieve(key);
094: omMap.put(inputKey, om);
095: }
096:
097: return om;
098: }
099: }
100:
101: public Object get(String omName) throws Exception {
102: if (!pullMap.containsKey(omName)) {
103: // MT could overwrite a PullHelper, but that is not a problem
104: // should still synchronize to avoid two threads adding at
105: // same time
106: synchronized (this .getClass()) {
107: pullMap.put(omName, new OMTool.PullHelper(omName));
108: }
109: }
110:
111: return pullMap.get(omName);
112: }
113:
114: public Object get(String omName, String key) throws Exception {
115: return ((OMTool.PullHelper) get(omName)).setKey(key);
116: }
117:
118: public String getName() {
119: return "om";
120: }
121:
122: // ****************** Recyclable implementation ************************
123:
124: private boolean disposed;
125:
126: /**
127: * Recycles the object for a new client. Recycle methods with
128: * parameters must be added to implementing object and they will be
129: * automatically called by pool implementations when the object is
130: * taken from the pool for a new client. The parameters must
131: * correspond to the parameters of the constructors of the object.
132: * For new objects, constructors can call their corresponding recycle
133: * methods whenever applicable.
134: * The recycle methods must call their super.
135: */
136: public void recycle() {
137: disposed = false;
138: }
139:
140: /**
141: * Disposes the object after use. The method is called
142: * when the object is returned to its pool.
143: * The dispose method must call its super.
144: */
145: public void dispose() {
146: omMap.clear();
147: // data = null;
148: disposed = true;
149: }
150:
151: /**
152: * Checks whether the recyclable has been disposed.
153: * @return true, if the recyclable is disposed.
154: */
155: public boolean isDisposed() {
156: return disposed;
157: }
158: }
|