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