1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.springframework.extensions.webscripts;
20
21 import java.util.Collections;
22 import java.util.Date;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.springframework.beans.BeansException;
29 import org.springframework.context.ApplicationContext;
30 import org.springframework.context.ApplicationContextAware;
31 import org.springframework.context.ApplicationEvent;
32 import org.springframework.context.ApplicationListener;
33 import org.springframework.context.event.ContextRefreshedEvent;
34 import org.springframework.extensions.config.ConfigService;
35 import org.springframework.extensions.webscripts.Description.RequiredAuthentication;
36
37
38
39
40
41
42
43
44
45 public abstract class AbstractRuntimeContainer
46 implements RuntimeContainer, ApplicationListener, ApplicationContextAware
47 {
48
49 private static final Log logger = LogFactory.getLog(AbstractRuntimeContainer.class);
50
51 protected ApplicationContext applicationContext = null;
52 private String name = "<undefined>";
53 private Registry registry;
54 private FormatRegistry formatRegistry;
55 private ScriptProcessorRegistry scriptProcessorRegistry;
56 private TemplateProcessorRegistry templateProcessorRegistry;
57 private SearchPath searchPath;
58 private ConfigService configService;
59 private Map<String, Object> scriptObjects;
60 private Map<String, Object> templateObjects;
61
62
63
64
65 public void setName(String name)
66 {
67 this.name = name;
68 }
69
70
71
72
73 public void setFormatRegistry(FormatRegistry formatRegistry)
74 {
75 this.formatRegistry = formatRegistry;
76 }
77
78
79
80
81 public void setRegistry(Registry registry)
82 {
83 this.registry = registry;
84 }
85
86
87
88
89 public void setScriptProcessorRegistry(ScriptProcessorRegistry scriptProcessorRegistry)
90 {
91 this.scriptProcessorRegistry = scriptProcessorRegistry;
92 }
93
94
95
96
97 public void setTemplateProcessorRegistry(TemplateProcessorRegistry templateProcessorRegistry)
98 {
99 this.templateProcessorRegistry = templateProcessorRegistry;
100 }
101
102
103
104
105 public void setSearchPath(SearchPath searchPath)
106 {
107 this.searchPath = searchPath;
108 }
109
110
111
112
113 public void setConfigService(ConfigService configService)
114 {
115 this.configService = configService;
116 }
117
118
119
120
121 public void setScriptObjects(Map<String, Object> scriptObjects)
122 {
123 this.scriptObjects = scriptObjects;
124 }
125
126
127
128
129 public void setTemplateObjects(Map<String, Object> templateObjects)
130 {
131 this.templateObjects = templateObjects;
132 }
133
134
135
136
137
138 public String getName()
139 {
140 return name;
141 }
142
143
144
145
146 public Map<String, Object> getScriptParameters()
147 {
148 Map<String, Object> params = new HashMap<String, Object>(8, 1.0f);
149 params.put("server", getDescription());
150 params.putAll(scriptObjects);
151
152 return Collections.unmodifiableMap(params);
153 }
154
155
156
157
158 public Map<String, Object> getTemplateParameters()
159 {
160 Map<String, Object> params = new HashMap<String, Object>(8, 1.0f);
161 params.put("server", getDescription());
162 params.put("date", new Date());
163 params.putAll(templateObjects);
164
165 return Collections.unmodifiableMap(params);
166 }
167
168
169
170
171 public FormatRegistry getFormatRegistry()
172 {
173 return formatRegistry;
174 }
175
176
177
178
179 public Registry getRegistry()
180 {
181 return registry;
182 }
183
184
185
186
187 public ConfigService getConfigService()
188 {
189 return configService;
190 }
191
192
193
194
195 public ScriptProcessorRegistry getScriptProcessorRegistry()
196 {
197 return scriptProcessorRegistry;
198 }
199
200
201
202
203 public TemplateProcessorRegistry getTemplateProcessorRegistry()
204 {
205 return templateProcessorRegistry;
206 }
207
208
209
210
211 public SearchPath getSearchPath()
212 {
213 return searchPath;
214 }
215
216
217
218
219 public void reset()
220 {
221 long startTime = System.nanoTime();
222 try
223 {
224 scriptProcessorRegistry.reset();
225 templateProcessorRegistry.reset();
226 getRegistry().reset();
227 configService.reset();
228 }
229 finally
230 {
231 if (logger.isInfoEnabled())
232 logger.info("Initialised " + getName() + " Web Script Container (in " + (System.nanoTime() - startTime)/1000000f + "ms)");
233 }
234 }
235
236
237
238
239 public void onApplicationEvent(ApplicationEvent event)
240 {
241 if (event instanceof ContextRefreshedEvent)
242 {
243 ContextRefreshedEvent refreshEvent = (ContextRefreshedEvent)event;
244 ApplicationContext refreshContext = refreshEvent.getApplicationContext();
245 if (refreshContext != null && refreshContext.equals(applicationContext))
246 {
247 reset();
248 }
249 }
250 }
251
252
253
254
255 public void setApplicationContext(ApplicationContext applicationContext)
256 throws BeansException
257 {
258 this.applicationContext = applicationContext;
259 }
260
261
262
263
264
265
266 protected ApplicationContext getApplicationContext()
267 {
268 return this.applicationContext;
269 }
270
271
272
273
274 public RequiredAuthentication getRequiredAuthentication()
275 {
276 return RequiredAuthentication.none;
277 }
278
279
280
281
282 public boolean authenticate(Authenticator auth, RequiredAuthentication required)
283 {
284 if (! ((required == null) || (required.equals(RequiredAuthentication.none))))
285 {
286 logger.error("Unexpected - required authentication = "+required);
287 return false;
288 }
289
290 return true;
291 }
292 }