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