1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.springframework.extensions.surf.studio;
20
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Iterator;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.json.JSONException;
28 import org.json.JSONObject;
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.Config;
35 import org.springframework.extensions.config.WebStudioConfigElement;
36 import org.springframework.extensions.config.WebStudioConfigElement.ApplicationDescriptor;
37 import org.springframework.extensions.config.WebStudioConfigElement.PanelDescriptor;
38 import org.springframework.extensions.surf.FrameworkUtil;
39 import org.springframework.extensions.surf.WebFrameworkServiceRegistry;
40 import org.springframework.extensions.surf.task.TaskManager;
41 import org.springframework.extensions.webscripts.Container;
42 import org.springframework.extensions.webscripts.SearchPath;
43 import org.springframework.extensions.webscripts.Store;
44
45
46
47
48
49
50 public class WebStudioService implements ApplicationListener, ApplicationContextAware
51 {
52 private static String WEB_STUDIO_SERVICE_BEAN_ID = "webstudio.service";
53
54 private static Log logger = LogFactory.getLog(WebStudioService.class);
55
56 protected ApplicationContext applicationContext;
57 protected WebFrameworkServiceRegistry serviceRegistry;
58 protected WebStudioStateProvider clientStateProvider;
59 protected TaskManager taskManager;
60 protected Container container;
61
62 protected JSONObject overlayConfiguration;
63
64 public static WebStudioService getInstance()
65 {
66 return (WebStudioService) FrameworkUtil.getCurrentRequestContext().getServiceRegistry().getApplicationContext().getBean(WEB_STUDIO_SERVICE_BEAN_ID);
67 }
68
69
70
71
72
73
74 public void setApplicationContext(ApplicationContext applicationContext)
75 {
76 this.applicationContext = applicationContext;
77 }
78
79
80
81
82
83
84 public ApplicationContext getApplicationContext()
85 {
86 return this.applicationContext;
87 }
88
89
90
91
92
93
94 public void setServiceRegistry(WebFrameworkServiceRegistry serviceRegistry)
95 {
96 this.serviceRegistry = serviceRegistry;
97 }
98
99
100
101
102
103
104 public void setClientStateProvider(WebStudioStateProvider clientStateProvider)
105 {
106 this.clientStateProvider = clientStateProvider;
107 }
108
109
110
111
112
113
114 public void setTaskManager(TaskManager taskManager)
115 {
116 this.taskManager = taskManager;
117 }
118
119
120
121
122
123
124 public void setContainer(Container container)
125 {
126 this.container = container;
127 }
128
129
130
131
132
133
134 public WebStudioStateProvider getClientStateProvider()
135 {
136 return this.clientStateProvider;
137 }
138
139
140
141
142
143
144 public TaskManager getTaskManager()
145 {
146 return this.taskManager;
147 }
148
149
150
151
152
153
154 public WebStudioConfigElement getWebStudioConfiguration()
155 {
156 Config config = this.serviceRegistry.getConfigService().getConfig("WebFramework");
157 return (WebStudioConfigElement) config.getConfigElement("web-studio");
158 }
159
160
161
162
163
164
165 public JSONObject getOverlayConfiguration()
166 {
167 return this.overlayConfiguration;
168 }
169
170
171
172
173 public void onApplicationEvent(ApplicationEvent event)
174 {
175 if (event instanceof ContextRefreshedEvent)
176 {
177 ContextRefreshedEvent refreshEvent = (ContextRefreshedEvent)event;
178 ApplicationContext refreshContext = refreshEvent.getApplicationContext();
179 if (refreshContext != null && refreshContext.equals(getApplicationContext()))
180 {
181 onBootstrap();
182 }
183 }
184 }
185
186
187
188
189
190 protected void onBootstrap()
191 {
192 try
193 {
194 autowire();
195 initConfiguration();
196
197 if (logger.isInfoEnabled())
198 logger.info("Initialised Web Studio Preview Extensions");
199 }
200 catch (Exception ex)
201 {
202 logger.fatal("Unable to parse Web Studio configuration into JSON config", ex);
203 }
204 }
205
206
207
208
209 protected void autowire()
210 {
211
212 appendSearchPath("webframework.webscripts.searchpath", "webstudio.webscripts.store.system");
213
214
215 appendSearchPath("webframework.templates.searchpath", "webstudio.webscripts.store.system");
216
217
218 this.container.reset();
219 }
220
221 protected void appendSearchPath(String searchPathId, String storeId)
222 {
223
224 Store additionalStore = (Store) this.getApplicationContext().getBean(storeId);
225 if (additionalStore == null)
226 {
227 return;
228 }
229
230 SearchPath searchPath = (SearchPath) this.getApplicationContext().getBean(searchPathId);
231 if (searchPath != null)
232 {
233 ArrayList<Store> searchPathList = new ArrayList<Store>(16);
234
235 Collection<Store> collection = searchPath.getStores();
236 Iterator<Store> it = collection.iterator();
237 while (it.hasNext())
238 {
239 Store store = (Store) it.next();
240 if (store == additionalStore)
241 {
242 return;
243 }
244 searchPathList.add(store);
245 }
246
247
248 searchPathList.add(additionalStore);
249 searchPath.setSearchPath(searchPathList);
250 }
251 }
252
253 protected void initConfiguration()
254 throws JSONException
255 {
256
257 JSONObject config = new JSONObject();
258
259 JSONObject applications = new JSONObject();
260 config.put("applications", applications);
261
262
263 String[] applicationIds = getWebStudioConfiguration().getApplicationIds();
264 for(int z = 0; z < applicationIds.length; z++)
265 {
266 ApplicationDescriptor appDescriptor = getWebStudioConfiguration().getApplication(applicationIds[z]);
267 String appClassName = appDescriptor.getClassName();
268 String appObjectId = appDescriptor.getId();
269 String appTitle = appDescriptor.getTitle();
270 if(appTitle == null)
271 {
272 appTitle = appObjectId;
273 }
274
275 JSONObject applicationConfig = new JSONObject();
276 applicationConfig.put("title", appTitle);
277 applicationConfig.put("classname", appClassName);
278
279 applications.put(appObjectId, applicationConfig);
280
281
282 JSONObject panels = new JSONObject();
283 applicationConfig.put("panels", panels);
284
285 String[] panelIds = appDescriptor.getPanelIds();
286 for(int y = 0; y < panelIds.length; y++)
287 {
288 String panelId = (String) panelIds[y];
289
290 PanelDescriptor panelDescriptor = appDescriptor.getPanelDescriptor(panelId);
291 String panelClassName = panelDescriptor.getClassName();
292 String panelObjectId = panelDescriptor.getId();
293 String panelTitle = panelDescriptor.getTitle();
294 if(panelTitle == null)
295 {
296 panelTitle = panelObjectId;
297 }
298
299 JSONObject panelConfig = new JSONObject();
300 panelConfig.put("title", panelTitle);
301 panelConfig.put("classname", panelClassName);
302
303 panels.put(panelObjectId, panelConfig);
304 }
305 }
306
307 this.overlayConfiguration = config;
308 }
309
310 }