Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
OMTool |
|
| 1.1818181818181819;1,182 | ||||
OMTool$PullHelper |
|
| 1.1818181818181819;1,182 |
1 | package org.apache.turbine.om; | |
2 | ||
3 | ||
4 | /* | |
5 | * Licensed to the Apache Software Foundation (ASF) under one | |
6 | * or more contributor license agreements. See the NOTICE file | |
7 | * distributed with this work for additional information | |
8 | * regarding copyright ownership. The ASF licenses this file | |
9 | * to you under the Apache License, Version 2.0 (the | |
10 | * "License"); you may not use this file except in compliance | |
11 | * with the License. You may obtain a copy of the License at | |
12 | * | |
13 | * http://www.apache.org/licenses/LICENSE-2.0 | |
14 | * | |
15 | * Unless required by applicable law or agreed to in writing, | |
16 | * software distributed under the License is distributed on an | |
17 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
18 | * KIND, either express or implied. See the License for the | |
19 | * specific language governing permissions and limitations | |
20 | * under the License. | |
21 | */ | |
22 | ||
23 | ||
24 | import java.util.HashMap; | |
25 | import java.util.Map; | |
26 | ||
27 | import org.apache.fulcrum.pool.Recyclable; | |
28 | import org.apache.turbine.services.pull.ApplicationTool; | |
29 | ||
30 | /** | |
31 | * A Pull tool to make om objects available to a template | |
32 | * | |
33 | * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a> | |
34 | * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> | |
35 | * @version $Id: OMTool.java 1078552 2011-03-06 19:58:46Z tv $ | |
36 | */ | |
37 | public class OMTool implements ApplicationTool, Recyclable | |
38 | { | |
39 | // private RunData data; | |
40 | protected HashMap<String, Object> omMap; | |
41 | ||
42 | // note the following could be a static attribute to reduce memory | |
43 | // footprint. Might require a service to front load the | |
44 | // PullHelpers to avoid MT issues. A multiple write is not so bad | |
45 | // though | |
46 | ||
47 | /** The cache of PullHelpers. **/ | |
48 | 0 | private static Map<String, OMTool.PullHelper> pullMap = new HashMap<String, OMTool.PullHelper>(); |
49 | ||
50 | /** | |
51 | * The Factory responsible for retrieving the | |
52 | * objects from storage | |
53 | */ | |
54 | protected RetrieverFactory omFactory; | |
55 | ||
56 | public OMTool()throws Exception | |
57 | 0 | { |
58 | 0 | omMap = new HashMap<String, Object>(); |
59 | //String className = Turbine.getConfiguration() | |
60 | // .getString("tool.om.factory"); | |
61 | // RetrieverFactory omFactory = | |
62 | // (RetrieverFactory)Class.forName(className).newInstance(); | |
63 | 0 | } |
64 | ||
65 | /** | |
66 | * Prepares tool for a single request | |
67 | */ | |
68 | public void init(Object runData) | |
69 | { | |
70 | // data = (RunData)runData; | |
71 | 0 | } |
72 | ||
73 | /** | |
74 | * Implementation of ApplicationTool interface is not needed for this | |
75 | * method as the tool is request scoped | |
76 | */ | |
77 | public void refresh() | |
78 | { | |
79 | // empty | |
80 | 0 | } |
81 | ||
82 | /** | |
83 | * Inner class to present a nice interface to the template designer | |
84 | */ | |
85 | protected class PullHelper | |
86 | { | |
87 | String omName; | |
88 | ||
89 | protected PullHelper(String omName) | |
90 | 0 | { |
91 | 0 | this.omName = omName; |
92 | 0 | } |
93 | ||
94 | public Object setKey(String key) | |
95 | throws Exception | |
96 | { | |
97 | 0 | Object om = null; |
98 | ||
99 | 0 | String inputKey = omName + key; |
100 | 0 | if (omMap.containsKey(inputKey)) |
101 | { | |
102 | 0 | om = omMap.get(inputKey); |
103 | } | |
104 | else | |
105 | { | |
106 | 0 | om = omFactory.getInstance(omName).retrieve(key); |
107 | 0 | omMap.put(inputKey, om); |
108 | } | |
109 | ||
110 | 0 | return om; |
111 | } | |
112 | } | |
113 | ||
114 | public Object get(String omName) throws Exception | |
115 | { | |
116 | 0 | if (!pullMap.containsKey(omName)) |
117 | { | |
118 | // MT could overwrite a PullHelper, but that is not a problem | |
119 | // should still synchronize to avoid two threads adding at | |
120 | // same time | |
121 | 0 | synchronized (this.getClass()) |
122 | { | |
123 | 0 | pullMap.put(omName, new OMTool.PullHelper(omName)); |
124 | 0 | } |
125 | } | |
126 | ||
127 | 0 | return pullMap.get(omName); |
128 | } | |
129 | ||
130 | public Object get(String omName, String key) throws Exception | |
131 | { | |
132 | 0 | return ((OMTool.PullHelper) get(omName)).setKey(key); |
133 | } | |
134 | ||
135 | ||
136 | public String getName() | |
137 | { | |
138 | 0 | return "om"; |
139 | } | |
140 | ||
141 | ||
142 | // ****************** Recyclable implementation ************************ | |
143 | ||
144 | private boolean disposed; | |
145 | ||
146 | /** | |
147 | * Recycles the object for a new client. Recycle methods with | |
148 | * parameters must be added to implementing object and they will be | |
149 | * automatically called by pool implementations when the object is | |
150 | * taken from the pool for a new client. The parameters must | |
151 | * correspond to the parameters of the constructors of the object. | |
152 | * For new objects, constructors can call their corresponding recycle | |
153 | * methods whenever applicable. | |
154 | * The recycle methods must call their super. | |
155 | */ | |
156 | public void recycle() | |
157 | { | |
158 | 0 | disposed = false; |
159 | 0 | } |
160 | ||
161 | /** | |
162 | * Disposes the object after use. The method is called | |
163 | * when the object is returned to its pool. | |
164 | * The dispose method must call its super. | |
165 | */ | |
166 | public void dispose() | |
167 | { | |
168 | 0 | omMap.clear(); |
169 | // data = null; | |
170 | 0 | disposed = true; |
171 | 0 | } |
172 | ||
173 | /** | |
174 | * Checks whether the recyclable has been disposed. | |
175 | * @return true, if the recyclable is disposed. | |
176 | */ | |
177 | public boolean isDisposed() | |
178 | { | |
179 | 0 | return disposed; |
180 | } | |
181 | } | |
182 | ||
183 | ||
184 | ||
185 | ||
186 | ||
187 | ||
188 | ||
189 |