1 package org.apache.velocity.tools;
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 import org.apache.velocity.tools.config.Data;
25 import org.apache.velocity.tools.config.FactoryConfiguration;
26 import org.apache.velocity.tools.Scope;
27 import org.apache.velocity.tools.config.ToolboxConfiguration;
28 import org.apache.velocity.tools.config.ToolConfiguration;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 public class ToolboxFactory
62 {
63 public static final String DEFAULT_SCOPE = Scope.REQUEST;
64
65 private final Map<String,Map<String,ToolInfo>> scopedToolInfo;
66 private final Map<String,Map<String,Object>> scopedProperties;
67 private Map<String,Object> data;
68 private Map<String,Object> globalProperties;
69
70 public ToolboxFactory()
71 {
72 this.scopedToolInfo = new HashMap<String,Map<String,ToolInfo>>();
73 this.scopedProperties = new HashMap<String,Map<String,Object>>();
74 }
75
76
77 public synchronized void configure(FactoryConfiguration config)
78 {
79
80 config.validate();
81
82
83 for (Data datum : config.getData())
84 {
85 putData(datum.getKey(), datum.getConvertedValue());
86 }
87
88
89
90
91
92
93 for (ToolboxConfiguration toolbox : config.getToolboxes())
94 {
95 String scope = toolbox.getScope();
96
97
98 for (ToolConfiguration tool : toolbox.getTools())
99 {
100 addToolInfo(scope, tool.createInfo());
101 }
102
103
104 Map<String,Object> newToolboxProps = toolbox.getPropertyMap();
105 putProperties(scope, newToolboxProps);
106
107
108 for (ToolInfo info : getToolInfo(scope).values())
109 {
110
111
112 info.addProperties(newToolboxProps);
113 }
114 }
115
116
117
118 Map<String,Object> newGlobalProps = config.getPropertyMap();
119 putGlobalProperties(newGlobalProps);
120
121
122 for (Map<String,ToolInfo> toolbox : scopedToolInfo.values())
123 {
124
125 for (ToolInfo info : toolbox.values())
126 {
127
128
129 info.addProperties(newGlobalProps);
130 }
131 }
132 }
133
134
135
136 protected synchronized Object putData(String key, Object value)
137 {
138 if (data == null)
139 {
140 data = new HashMap<String,Object>();
141 }
142 return data.put(key, value);
143 }
144
145 protected void addToolInfo(String scope, ToolInfo tool)
146 {
147
148
149
150 getToolInfo(scope).put(tool.getKey(), tool);
151 }
152
153 protected synchronized Map<String,ToolInfo> getToolInfo(String scope)
154 {
155 Map<String,ToolInfo> tools = scopedToolInfo.get(scope);
156 if (tools == null)
157 {
158 tools = new HashMap<String,ToolInfo>();
159 scopedToolInfo.put(scope, tools);
160 }
161 return tools;
162 }
163
164 protected synchronized void putGlobalProperties(Map<String,Object> props)
165 {
166 if (props != null && !props.isEmpty())
167 {
168 if (globalProperties == null)
169 {
170 globalProperties = new HashMap<String,Object>(props);
171 }
172 else
173 {
174 globalProperties.putAll(props);
175 }
176 }
177 }
178
179 protected synchronized void putProperties(String scope, Map<String,Object> props)
180 {
181 if (props != null && !props.isEmpty())
182 {
183 Map<String,Object> properties = scopedProperties.get(scope);
184 if (properties == null)
185 {
186 properties = new HashMap<String,Object>(props);
187 scopedProperties.put(scope, properties);
188 }
189 else
190 {
191 properties.putAll(props);
192 }
193 }
194 }
195
196
197
198 public Object getGlobalProperty(String name)
199 {
200 if (globalProperties == null)
201 {
202 return null;
203 }
204 return globalProperties.get(name);
205 }
206
207 public Map<String,Object> getData()
208 {
209 return data;
210 }
211
212 public boolean hasTools(String scope)
213 {
214 Map<String,ToolInfo> tools = scopedToolInfo.get(scope);
215 if (tools != null && !tools.isEmpty())
216 {
217 return true;
218 }
219 else if (data != null && Scope.APPLICATION.equals(scope))
220 {
221 return true;
222 }
223 return false;
224 }
225
226 public Toolbox createToolbox(String scope)
227 {
228 Map<String,ToolInfo> tools = scopedToolInfo.get(scope);
229 Map properties = scopedProperties.get(scope);
230
231 Toolbox toolbox;
232 if (properties == null)
233 {
234 if (globalProperties == null)
235 {
236 toolbox = new Toolbox(tools);
237 }
238 else
239 {
240 toolbox = new Toolbox(tools, globalProperties);
241 }
242 }
243 else
244 {
245
246
247 if (globalProperties != null)
248 {
249 properties.putAll(globalProperties);
250 }
251 toolbox = new Toolbox(tools, properties);
252 }
253
254
255
256 if (data != null &&
257 (scopedToolInfo.size() == 1 || scope.equals(Scope.APPLICATION)))
258 {
259 toolbox.cacheData(getData());
260 }
261 return toolbox;
262 }
263
264 }