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.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   * Encapsulates a Container within which the Web Script Runtime executes.
40   * 
41   * Container examples - presentation (web tier), repository (server tier)
42   * 
43   * @author dcaruana
44   */
45  public abstract class AbstractRuntimeContainer
46      implements RuntimeContainer, ApplicationListener, ApplicationContextAware
47  {
48      // Logger
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       * @param name
64       */
65      public void setName(String name)
66      {
67          this.name = name;
68      }
69  
70      /**
71       * @param formatRegistry
72       */
73      public void setFormatRegistry(FormatRegistry formatRegistry)
74      {
75          this.formatRegistry = formatRegistry;
76      }
77  
78      /**
79       * @param registry
80       */
81      public void setRegistry(Registry registry)
82      {
83          this.registry = registry;
84      }
85      
86      /**
87       * @param scriptProcessorRegistry
88       */
89      public void setScriptProcessorRegistry(ScriptProcessorRegistry scriptProcessorRegistry)
90      {
91          this.scriptProcessorRegistry = scriptProcessorRegistry;
92      }
93      
94      /**
95       * @param templateProcessorRegistry
96       */
97      public void setTemplateProcessorRegistry(TemplateProcessorRegistry templateProcessorRegistry)
98      {
99          this.templateProcessorRegistry = templateProcessorRegistry;
100     }
101     
102     /**
103      * @param searchPath
104      */
105     public void setSearchPath(SearchPath searchPath)
106     {
107        this.searchPath = searchPath;
108     }
109     
110     /**
111      * @param configService
112      */
113     public void setConfigService(ConfigService configService)
114     {
115         this.configService = configService;
116     }
117     
118     /**
119      * @param scriptObjects
120      */
121     public void setScriptObjects(Map<String, Object> scriptObjects)
122     {
123         this.scriptObjects = scriptObjects;
124     }
125     
126     /**
127      * @param templateObjects
128      */
129     public void setTemplateObjects(Map<String, Object> templateObjects)
130     {
131         this.templateObjects = templateObjects;
132     }
133 
134     
135     /* (non-Javadoc)
136      * @see org.alfresco.web.scripts.RuntimeContainer#getName()
137      */
138     public String getName()
139     {
140         return name;
141     }
142     
143     /* (non-Javadoc)
144      * @see org.alfresco.web.scripts.Container#getScriptParameters()
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     /* (non-Javadoc)
156      * @see org.alfresco.web.scripts.Container#getTemplateParameters()
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     /* (non-Javadoc)
169      * @see org.alfresco.web.scripts.Container#getFormatRegistry()
170      */
171     public FormatRegistry getFormatRegistry()
172     {
173         return formatRegistry;
174     }
175 
176     /* (non-Javadoc)
177      * @see org.alfresco.web.scripts.Container#getRegistry()
178      */
179     public Registry getRegistry()
180     {
181         return registry;
182     }
183     
184     /* (non-Javadoc)
185      * @see org.alfresco.web.scripts.Container#getConfigService()
186      */
187     public ConfigService getConfigService()
188     {
189         return configService;
190     }
191 
192     /* (non-Javadoc)
193      * @see org.alfresco.web.scripts.Container#getScriptProcessorRegistry()
194      */
195     public ScriptProcessorRegistry getScriptProcessorRegistry()
196     {
197         return scriptProcessorRegistry;
198     }
199 
200     /* (non-Javadoc)
201      * @see org.alfresco.web.scripts.Container#getTemplateProcessorRegistry()
202      */
203     public TemplateProcessorRegistry getTemplateProcessorRegistry()
204     {
205         return templateProcessorRegistry;
206     }
207     
208     /* (non-Javadoc)
209      * @see org.alfresco.web.scripts.Container#getSearchPath()
210      */
211     public SearchPath getSearchPath()
212     {
213         return searchPath;
214     }
215 
216     /* (non-Javadoc)
217      * @see org.alfresco.web.scripts.Container#reset()
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     /* (non-Javadoc)
237      * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
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     /* (non-Javadoc)
253      * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
254      */
255     public void setApplicationContext(ApplicationContext applicationContext)
256         throws BeansException
257     {
258         this.applicationContext = applicationContext;
259     }
260 
261     /**
262      * Gets the Application Context
263      * 
264      * @return  application context
265      */
266     protected ApplicationContext getApplicationContext()
267     {
268         return this.applicationContext;
269     }
270     
271     /* (non-Javadoc)
272      * @see org.alfresco.web.scripts.RuntimeContainer#getRequiredAuthentication()
273      */
274     public RequiredAuthentication getRequiredAuthentication()
275     {
276         return RequiredAuthentication.none;
277     }
278     
279     /* (non-Javadoc)
280      * @see org.alfresco.web.scripts.RuntimeContainer#authenticate(org.alfresco.web.scripts.Authenticator, org.alfresco.web.scripts.Description.RequiredAuthentication)
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 }