1 package org.apache.turbine.om;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import org.apache.turbine.Turbine;
23 import org.apache.turbine.services.pull.ApplicationTool;
24 import org.apache.turbine.util.pool.Recyclable;
25
26 /***
27 * A Pull tool to make om objects available to a template
28 *
29 * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
30 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
31 * @version $Id: OMTool.java,v 1.4.2.2 2004/05/20 03:05:19 seade Exp $
32 */
33 public class OMTool implements ApplicationTool, Recyclable
34 {
35
36 private HashMap omMap;
37
38
39
40
41
42
43 /*** The cache of PullHelpers. **/
44 private static Map pullMap = new HashMap();
45
46 /***
47 * The Factory responsible for retrieving the
48 * objects from storage
49 */
50 private RetrieverFactory omFactory;
51
52 public OMTool()throws Exception
53 {
54 omMap = new HashMap();
55 String className = Turbine.getConfiguration()
56 .getString("tool.om.factory");
57
58
59 }
60
61 /***
62 * Prepares tool for a single request
63 */
64 public void init(Object runData)
65 {
66
67 }
68
69 /***
70 * Implementation of ApplicationTool interface is not needed for this
71 * method as the tool is request scoped
72 */
73 public void refresh()
74 {
75
76 }
77
78 /***
79 * Inner class to present a nice interface to the template designer
80 */
81 private class PullHelper
82 {
83 String omName;
84
85 private PullHelper(String omName)
86 {
87 this.omName = omName;
88 }
89
90 public Object setKey(String key)
91 throws Exception
92 {
93 Object om = null;
94
95 String inputKey = omName + key;
96 if (omMap.containsKey(inputKey))
97 {
98 om = omMap.get(inputKey);
99 }
100 else
101 {
102 om = omFactory.getInstance(omName).retrieve(key);
103 omMap.put(inputKey, om);
104 }
105
106 return om;
107 }
108 }
109
110 public Object get(String omName) throws Exception
111 {
112 if (!pullMap.containsKey(omName))
113 {
114
115
116
117 synchronized (this.getClass())
118 {
119 pullMap.put(omName, new OMTool.PullHelper(omName));
120 }
121 }
122
123 return pullMap.get(omName);
124 }
125
126 public Object get(String omName, String key) throws Exception
127 {
128 return ((OMTool.PullHelper) get(omName)).setKey(key);
129 }
130
131
132 public String getName()
133 {
134 return "om";
135 }
136
137
138
139
140 private boolean disposed;
141
142 /***
143 * Recycles the object for a new client. Recycle methods with
144 * parameters must be added to implementing object and they will be
145 * automatically called by pool implementations when the object is
146 * taken from the pool for a new client. The parameters must
147 * correspond to the parameters of the constructors of the object.
148 * For new objects, constructors can call their corresponding recycle
149 * methods whenever applicable.
150 * The recycle methods must call their super.
151 */
152 public void recycle()
153 {
154 disposed = false;
155 }
156
157 /***
158 * Disposes the object after use. The method is called
159 * when the object is returned to its pool.
160 * The dispose method must call its super.
161 */
162 public void dispose()
163 {
164 omMap.clear();
165
166 disposed = true;
167 }
168
169 /***
170 * Checks whether the recyclable has been disposed.
171 * @return true, if the recyclable is disposed.
172 */
173 public boolean isDisposed()
174 {
175 return disposed;
176 }
177 }
178
179
180
181
182
183
184
185