1 package org.apache.turbine.om;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.util.HashMap;
58 import java.util.Map;
59 import org.apache.turbine.services.pull.ApplicationTool;
60 import org.apache.turbine.services.resources.TurbineResources;
61 import org.apache.turbine.util.pool.Recyclable;
62
63 /***
64 * A Pull tool to make om objects available to a template
65 *
66 * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
67 * @version $Id: OMTool.java,v 1.2 2002/07/11 14:28:45 mpoeschl Exp $
68 */
69 public class OMTool implements ApplicationTool, Recyclable
70 {
71 // private RunData data;
72 private HashMap omMap;
73
74 // note the following could be a static attribute to reduce memory
75 // footprint. Might require a service to front load the
76 // PullHelpers to avoid MT issues. A multiple write is not so bad
77 // though
78
79 /*** The cache of PullHelpers. **/
80 private static Map pullMap = new HashMap();
81
82 /***
83 * The Factory responsible for retrieving the
84 * objects from storage
85 */
86 private RetrieverFactory omFactory;
87
88 public OMTool()throws Exception
89 {
90 omMap = new HashMap();
91 String className = TurbineResources.getString("tool.om.factory");
92 // RetrieverFactory omFactory =
93 // (RetrieverFactory)Class.forName(className).newInstance();
94 }
95
96 /***
97 * Prepares tool for a single request
98 */
99 public void init(Object runData)
100 {
101 // data = (RunData)runData;
102 }
103
104 /***
105 * Implementation of ApplicationTool interface is not needed for this
106 * method as the tool is request scoped
107 */
108 public void refresh()
109 {
110 // empty
111 }
112
113 /***
114 * Inner class to present a nice interface to the template designer
115 */
116 private class PullHelper
117 {
118 String omName;
119
120 private PullHelper(String omName)
121 {
122 this.omName = omName;
123 }
124
125 public Object setKey(String key)
126 throws Exception
127 {
128 Object om = null;
129
130 String inputKey = omName + key;
131 if (omMap.containsKey(inputKey))
132 {
133 om = omMap.get(inputKey);
134 }
135 else
136 {
137 om = omFactory.getInstance(omName).retrieve(key);
138 omMap.put(inputKey, om);
139 }
140
141 return om;
142 }
143 }
144
145 public Object get(String omName) throws Exception
146 {
147 if (!pullMap.containsKey(omName))
148 {
149 // MT could overwrite a PullHelper, but that is not a problem
150 // should still synchronize to avoid two threads adding at
151 // same time
152 synchronized (this.getClass())
153 {
154 pullMap.put(omName, new OMTool.PullHelper(omName));
155 }
156 }
157
158 return pullMap.get(omName);
159 }
160
161 public Object get(String omName, String key) throws Exception
162 {
163 return ((OMTool.PullHelper) get(omName)).setKey(key);
164 }
165
166
167 public String getName()
168 {
169 return "om";
170 }
171
172
173 // ****************** Recyclable implementation ************************
174
175 private boolean disposed;
176
177 /***
178 * Recycles the object for a new client. Recycle methods with
179 * parameters must be added to implementing object and they will be
180 * automatically called by pool implementations when the object is
181 * taken from the pool for a new client. The parameters must
182 * correspond to the parameters of the constructors of the object.
183 * For new objects, constructors can call their corresponding recycle
184 * methods whenever applicable.
185 * The recycle methods must call their super.
186 */
187 public void recycle()
188 {
189 disposed = false;
190 }
191
192 /***
193 * Disposes the object after use. The method is called
194 * when the object is returned to its pool.
195 * The dispose method must call its super.
196 */
197 public void dispose()
198 {
199 omMap.clear();
200 // data = null;
201 disposed = true;
202 }
203
204 /***
205 * Checks whether the recyclable has been disposed.
206 * @return true, if the recyclable is disposed.
207 */
208 public boolean isDisposed()
209 {
210 return disposed;
211 }
212 }
213
214
215
216
217
218
219
220
This page was automatically generated by Maven