View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.portals.bridges.frameworks.spring;
18  
19  import java.io.InputStream;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.Map;
23  import java.util.ResourceBundle;
24  
25  import javax.portlet.PortletConfig;
26  import javax.portlet.PortletException;
27  
28  import org.springframework.beans.PropertyValue;
29  import org.springframework.beans.factory.config.BeanDefinition;
30  import org.springframework.beans.factory.xml.XmlBeanFactory;
31  import org.springframework.core.io.FileSystemResource;
32  
33  import org.apache.commons.validator.Validator;
34  import org.apache.commons.validator.ValidatorException;
35  import org.apache.commons.validator.ValidatorResources;
36  import org.apache.commons.validator.ValidatorResults;
37  import org.apache.portals.bridges.frameworks.ExternalComponentSupport;
38  import org.apache.portals.bridges.frameworks.Lookup;
39  import org.apache.portals.bridges.frameworks.model.ModelBean;
40  import org.apache.portals.bridges.frameworks.model.PortletApplicationModel;
41  import org.apache.portals.bridges.frameworks.spring.ModelBeanImpl;
42  
43  
44  /***
45   * PortletApplicationModelImpl
46   * 
47   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
48   * @version $Id: PortletApplicationModelImpl.java 517068 2007-03-12 01:44:37Z ate $
49   */
50  public class PortletApplicationModelImpl implements PortletApplicationModel
51  {
52      /***
53       * Spring configuration: view to bean name map
54       */
55      private static final String PORTLET_VIEW_BEAN_MAP = "portlet-view-bean-map";
56      
57      /***
58       * Spring configuration: view to validator name map
59       */
60      private static final String PORTLET_VIEW_VALIDATOR_MAP = "portlet-view-validator-map";
61      
62      /***
63       * logical view to template map
64       */
65      private static final String PORTLET_LOGICAL_VIEW_MAP = "portlet-views";
66  
67      /***
68       * map for action forward definitions (success, failure)
69       */
70      private static final String PORTLET_ACTION_FORWARD_MAP = "portlet-action-forward-map";
71          
72      /***
73       * Spring Factory 
74       */
75      private XmlBeanFactory springFactory = null;
76  
77      /***
78       * View Bean Map
79       */
80      private Map viewBeanMap = null;
81      
82      /***
83       * Validation resources
84       */
85      private ValidatorResources validations = null;
86          
87      /***
88       * View Validation Map
89       */
90      private Map viewValidatorMap = null;
91  
92      /***
93       * Map from logical views to templates
94       */
95      private Map logicalViewMap = null;
96  
97      /***
98       * Map from view:status to view
99       */    
100     private Map actionForwardMap = null;
101     
102     private Map modelBeanMap = new HashMap();
103     
104     private Map externalSupportMap = new HashMap();
105     
106     private static Object semaphore = new Object();
107     
108     private String springConfig;
109     private String validatorConfig = null;
110     
111     public PortletApplicationModelImpl(String springConfig, String validatorConfig)
112     {
113         this.springConfig = springConfig;
114         this.validatorConfig = validatorConfig;
115     }
116     
117     public void setExternalSupport(Map map)
118     {
119         this.externalSupportMap = map;
120     }
121     
122     public void init(PortletConfig config)
123     throws PortletException
124     {
125         // load Spring
126         try 
127         {
128             synchronized (semaphore)
129             {
130                 if (null == springFactory)
131                 {
132                     springFactory = new XmlBeanFactory(new FileSystemResource(config.getPortletContext().getRealPath(springConfig)));
133                 }
134             }
135          } 
136          catch (Exception e) 
137          {
138              throw new PortletException("Failed to load spring configuration.", e);
139          }   
140                            
141          // load validator
142          synchronized (semaphore)
143          {             
144              if (validatorConfig != null && null == validations)
145              {
146                  InputStream is = null;
147                  
148                  try
149                  {
150                      // TODO: support extensible user-defined validator resources
151                      //is = this.getClass().getResourceAsStream("/org/apache/portals/bridges/velocity/validation/default-portlet-validation.xml");
152                      is = config.getPortletContext().getResourceAsStream(validatorConfig);                    
153                      
154                      validations = new ValidatorResources(is);
155                  }
156                  catch (Exception e)
157                  {
158                      throw new PortletException("Failed to load validator configuration.", e);
159                  }
160                  finally 
161                  {
162                      // Make sure we close the input stream.
163                      if (is != null) 
164                      {
165                          try
166                          {
167                              is.close();
168                          }
169                          catch (Exception e)
170                          {}
171                      }
172                  }                     
173              }
174          }
175 
176          // Logical Views to templates
177          synchronized (semaphore)
178          {
179              logicalViewMap = (Map)springFactory.getBean(PORTLET_LOGICAL_VIEW_MAP);
180              if (logicalViewMap == null)
181              {
182                  logicalViewMap = new HashMap(); 
183              }
184          }
185          
186          // View to Validator Map
187          synchronized (semaphore)
188          {             
189              viewValidatorMap = (Map)springFactory.getBean(PORTLET_VIEW_VALIDATOR_MAP);
190              if (viewValidatorMap == null)
191              {
192                  viewValidatorMap = new HashMap(); 
193              }
194          }
195          
196          // View to Bean Map
197          synchronized (semaphore)
198          {
199              viewBeanMap = (Map)springFactory.getBean(PORTLET_VIEW_BEAN_MAP);
200              if (viewBeanMap == null)
201              {
202                  viewBeanMap = new HashMap();              
203              }
204          }        
205 
206          // Action Forward map
207          synchronized (semaphore)
208          {
209              actionForwardMap = (Map)springFactory.getBean(PORTLET_ACTION_FORWARD_MAP);
210              if (actionForwardMap == null)
211              {
212                  actionForwardMap = new HashMap();              
213              }
214          }
215          
216          
217     }
218     
219     public ModelBean getModelBean(String view)
220     {
221         ModelBean modelBean;
222         String beanName = (String)viewBeanMap.get(view);
223         if (beanName != null)
224         {
225             modelBean = (ModelBean)modelBeanMap.get(beanName);
226             if (modelBean == null)
227             {
228                 BeanDefinition bd = springFactory.getBeanDefinition(beanName);
229                 Object bean = springFactory.getBean(beanName);
230                 if (bd == null || bean == null)
231                 {
232                     return new ModelBeanImpl(beanName, ModelBean.POJO);
233                 }                   
234                 String lookup = null;
235                 boolean requiresExternalSupport = false;
236                 PropertyValue value = bd.getPropertyValues().getPropertyValue("lookupKey");
237                 if (value != null)
238                 {
239                     lookup = (String)value.getValue();
240                 }                
241                 if (bean instanceof ExternalComponentSupport)
242                 {
243                     requiresExternalSupport = true;
244                 }
245                 modelBean = new ModelBeanImpl(beanName, ModelBean.POJO, lookup, requiresExternalSupport);
246                 modelBeanMap.put(beanName, modelBean);
247             }
248         }
249         else
250         {
251             modelBean = new ModelBeanImpl(beanName, ModelBean.PREFS_MAP);
252         }        
253         return modelBean;
254     }
255     
256     public String getTemplate(String view)
257     {
258         return (String)logicalViewMap.get(view);
259     }
260     
261     public Object lookupBean(ModelBean mb, String key)
262     {
263         Object bean = springFactory.getBean(mb.getBeanName());
264         if (bean != null)
265         {
266             if (mb.isRequiresExternalSupport())
267             {
268                 ExternalComponentSupport ecs = (ExternalComponentSupport)bean;
269                 ecs.setExternalSupport(externalSupportMap.get(mb.getBeanName()));
270             }
271             if (mb.isRequiresLookup())
272             {
273                 ((Lookup)bean).lookup(key);
274             }
275         }
276         return bean;
277     }
278 
279     public Object createBean(ModelBean mb)
280     {
281         Object bean = springFactory.getBean(mb.getBeanName());
282         if (bean != null)
283         {
284             if (mb.isRequiresExternalSupport())
285             {
286                 ExternalComponentSupport ecs = (ExternalComponentSupport)bean;
287                 ecs.setExternalSupport(externalSupportMap.get(mb.getBeanName()));
288             }
289         }
290         return bean;
291     }
292     
293     public Map createPrefsBean(ModelBean mb, Map original)
294     {
295         Map prefs = new HashMap();
296         Iterator it = original.entrySet().iterator();
297         while (it.hasNext())
298         {
299             Map.Entry entry = (Map.Entry)it.next();
300             String key = (String)entry.getKey();
301             Object value = entry.getValue();
302             if (value instanceof String)
303             {
304                 prefs.put(key, value);
305             }
306             else if (value instanceof String[])
307             {
308                 prefs.put(key, ((String[])value)[0]);
309             }
310         }
311         return prefs;        
312     }
313 
314     public Map validate(Object bean, String view, ResourceBundle bundle)
315     throws PortletException
316     {
317         Map result = new HashMap();
318         if (validations == null)
319         {
320             return result; // no validation configured
321         }
322         // Get the bean name from the bean-view map
323         String validatorFormName = (String)viewValidatorMap.get(view);
324         if (validatorFormName == null)
325         {
326             return result; // no validation for this bean
327         }
328 
329         Validator validator = new Validator(validations, validatorFormName);
330 
331         // Tell the validator which bean to validate against.
332         validator.setParameter(Validator.BEAN_PARAM, bean);
333         
334         // Parameters used by our validators
335         validator.setParameter("java.util.Map", result);
336         validator.setParameter("java.util.ResourceBundle", bundle);
337         
338         ValidatorResults results = null;
339                 
340         try
341         {
342             validator.setOnlyReturnErrors(true);
343             results = validator.validate();
344             if (results.isEmpty())
345             {
346                 return result;
347             }
348         }
349         catch (ValidatorException e)
350         {
351             throw new PortletException("Error in processing validation: ", e);            
352         }
353         
354         return result;        
355     }
356     
357     public String getForward(String view, String status)
358     {
359         return (String)actionForwardMap.get(view + ":" + status);
360     }
361 
362     public String getForward(String view)
363     {
364         return (String)actionForwardMap.get(view);
365     }
366     
367 }