View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * Web Studio spring service
47   * 
48   * @author muzquiano
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       * Sets the application context.
71       * 
72       * @param applicationContext the new application context
73       */
74      public void setApplicationContext(ApplicationContext applicationContext)
75      {
76          this.applicationContext = applicationContext;
77      }
78      
79      /**
80       * Gets the application context.
81       * 
82       * @return the application context
83       */
84      public ApplicationContext getApplicationContext()
85      {
86          return this.applicationContext;
87      }
88      
89      /**
90       * Sets the service registry.
91       * 
92       * @param serviceRegistry the new service registry
93       */
94      public void setServiceRegistry(WebFrameworkServiceRegistry serviceRegistry)
95      {
96          this.serviceRegistry = serviceRegistry;
97      }
98      
99      /**
100      * Sets the client state provider.
101      * 
102      * @param clientStateProvider the new web client state provider
103      */
104     public void setClientStateProvider(WebStudioStateProvider clientStateProvider)
105     {
106         this.clientStateProvider = clientStateProvider;
107     }
108     
109     /**
110      * Sets the task manager.
111      * 
112      * @param taskManager the new task manager
113      */
114     public void setTaskManager(TaskManager taskManager)
115     {
116         this.taskManager = taskManager;
117     }
118     
119     /**
120      * Sets the container.
121      * 
122      * @param container the new container
123      */
124     public void setContainer(Container container)
125     {
126         this.container = container;
127     }
128     
129     /**
130      * Gets the web studio state provider.
131      * 
132      * @return the web studio state provider
133      */
134     public WebStudioStateProvider getClientStateProvider()
135     {
136         return this.clientStateProvider;
137     }
138     
139     /**
140      * Gets the task manager.
141      * 
142      * @return the task manager
143      */
144     public TaskManager getTaskManager()
145     {
146         return this.taskManager;
147     }
148     
149     /**
150      * Gets the web studio configuration.
151      * 
152      * @return the web studio configuration
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      * Gets the overlay configuration.
162      * 
163      * @return the overlay configuration
164      */
165     public JSONObject getOverlayConfiguration()
166     {
167         return this.overlayConfiguration;
168     }
169     
170     /* (non-Javadoc)
171      * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
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      * Initialization of the Web Studio Service bean
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      * Ensures that Web Studio search path elements are in place
208      */
209     protected void autowire()
210     {
211         // add to web script search path
212         appendSearchPath("webframework.webscripts.searchpath", "webstudio.webscripts.store.system");
213         
214         // add to templates search path
215         appendSearchPath("webframework.templates.searchpath", "webstudio.webscripts.store.system");
216         
217         // reinitialize the web script container
218         this.container.reset();
219     }
220     
221     protected void appendSearchPath(String searchPathId, String storeId)
222     {
223         // additional store
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             // append in additional store
248             searchPathList.add(additionalStore);
249             searchPath.setSearchPath(searchPathList);
250         }        
251     }    
252     
253     protected void initConfiguration()
254         throws JSONException
255     {
256         // load application configuration and build json descriptor
257         JSONObject config = new JSONObject();
258         
259         JSONObject applications = new JSONObject();
260         config.put("applications", applications);
261     
262         // populate the application array
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             // set up the panels
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 }