View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kns.web.struts.action;
17  
18  import org.apache.commons.beanutils.ConvertUtils;
19  import org.apache.commons.beanutils.converters.BigDecimalConverter;
20  import org.apache.commons.beanutils.converters.BigIntegerConverter;
21  import org.apache.commons.beanutils.converters.BooleanConverter;
22  import org.apache.commons.beanutils.converters.ByteConverter;
23  import org.apache.commons.beanutils.converters.CharacterConverter;
24  import org.apache.commons.beanutils.converters.DoubleConverter;
25  import org.apache.commons.beanutils.converters.FloatConverter;
26  import org.apache.commons.beanutils.converters.IntegerConverter;
27  import org.apache.commons.beanutils.converters.LongConverter;
28  import org.apache.commons.beanutils.converters.ShortConverter;
29  import org.apache.commons.collections.iterators.IteratorEnumeration;
30  import org.apache.log4j.Logger;
31  import org.apache.struts.action.ActionServlet;
32  import org.kuali.rice.core.api.config.ConfigurationException;
33  import org.kuali.rice.core.api.config.property.ConfigContext;
34  import org.kuali.rice.core.framework.config.module.ModuleConfigurer;
35  import org.kuali.rice.core.framework.config.module.WebModuleConfiguration;
36  
37  import javax.servlet.ServletConfig;
38  import javax.servlet.ServletContext;
39  import javax.servlet.ServletException;
40  import java.math.BigDecimal;
41  import java.math.BigInteger;
42  import java.util.Collection;
43  import java.util.Enumeration;
44  import java.util.HashMap;
45  import java.util.Map;
46  
47  public class KualiActionServlet extends ActionServlet {
48      private static final Logger LOG = Logger.getLogger(KualiActionServlet.class);
49  
50      /**
51       * <p>Initialize other global characteristics of the controller servlet.</p>
52       * Overridden to remove the ConvertUtils.deregister() command that caused problems
53       * with the concurrent data dictionary load.  (KULRNE-4405)
54       *
55       * @exception ServletException if we cannot initialize these resources
56       */
57      @Override
58  	protected void initOther() throws ServletException {
59  
60          String value = null;
61          value = getServletConfig().getInitParameter("config");
62          if (value != null) {
63              config = value;
64          }
65  
66          // Backwards compatibility for form beans of Java wrapper classes
67          // Set to true for strict Struts 1.0 compatibility
68          value = getServletConfig().getInitParameter("convertNull");
69          if ("true".equalsIgnoreCase(value)
70              || "yes".equalsIgnoreCase(value)
71              || "on".equalsIgnoreCase(value)
72              || "y".equalsIgnoreCase(value)
73              || "1".equalsIgnoreCase(value)) {
74  
75              convertNull = true;
76          }
77  
78          if (convertNull) {
79              ConvertUtils.register(new BigDecimalConverter(null), BigDecimal.class);
80              ConvertUtils.register(new BigIntegerConverter(null), BigInteger.class);
81              ConvertUtils.register(new BooleanConverter(null), Boolean.class);
82              ConvertUtils.register(new ByteConverter(null), Byte.class);
83              ConvertUtils.register(new CharacterConverter(null), Character.class);
84              ConvertUtils.register(new DoubleConverter(null), Double.class);
85              ConvertUtils.register(new FloatConverter(null), Float.class);
86              ConvertUtils.register(new IntegerConverter(null), Integer.class);
87              ConvertUtils.register(new LongConverter(null), Long.class);
88              ConvertUtils.register(new ShortConverter(null), Short.class);
89          }
90  
91      }
92  
93      KualiActionServletConfig serverConfigOverride = null;
94  
95      @Override
96      public ServletConfig getServletConfig() {
97          if ( serverConfigOverride == null ) {
98              ServletConfig sConfig = super.getServletConfig();
99  
100             if ( sConfig == null ) {
101                 return null;
102             }
103             serverConfigOverride = new KualiActionServletConfig(sConfig);
104         }
105         return serverConfigOverride;
106     }
107 
108     /**
109      * A custom ServletConfig implementation which dynamically includes web content based on the installed modules in the RiceConfigurer object.
110      *   Accomplishes this by implementing custom
111      * {@link #getInitParameter(String)} and {@link #getInitParameterNames()} methods.
112      */
113     private class KualiActionServletConfig implements ServletConfig {
114 
115         private ServletConfig wrapped;
116         private Map<String,String> initParameters = new HashMap<String, String>();
117 
118         public KualiActionServletConfig(ServletConfig wrapped) {
119             this.wrapped = wrapped;
120             // copy out all the init parameters so they can be augmented
121             @SuppressWarnings("unchecked")
122 			final Enumeration<String> initParameterNames = wrapped.getInitParameterNames();
123             while ( initParameterNames.hasMoreElements() ) {
124                 String paramName = initParameterNames.nextElement();
125                 initParameters.put( paramName, wrapped.getInitParameter(paramName) );
126             }
127             // loop over the installed modules, adding their struts configuration to the servlet
128             // if they have a web interface
129             @SuppressWarnings("unchecked")
130 			final Collection<ModuleConfigurer> riceModules = (Collection<ModuleConfigurer>) ConfigContext.getCurrentContextConfig().getObject("ModuleConfigurers");
131             
132             if ( LOG.isInfoEnabled() ) {
133             	LOG.info( "Configuring init parameters of the KualiActionServlet from riceModules: " + riceModules );
134             }
135             for ( ModuleConfigurer module : riceModules ) {
136                 // only install the web configuration if the module has web content
137                 // and it is running in a "local" mode
138                 // in "embedded" or "remote" modes, the UIs are hosted on a central server
139                 if ( module.shouldRenderWebInterface() ) {
140                     WebModuleConfiguration webModuleConfiguration = module.getWebModuleConfiguration();
141                     if (webModuleConfiguration == null) {
142                         throw new ConfigurationException("Attempting to load WebModuleConfiguration for module '" + module.getModuleName() + "' but no configuration was provided!");
143                     }
144                 	if ( LOG.isInfoEnabled() ) {
145                 		LOG.info( "Configuring Web Content for Module: " + webModuleConfiguration.getModuleName()
146                 				+ " / " + webModuleConfiguration.getWebModuleStrutsConfigName()
147                 				+ " / " + webModuleConfiguration.getWebModuleStrutsConfigurationFiles()
148                 				+ " / Base URL: " + webModuleConfiguration.getWebModuleBaseUrl() );
149                 	}
150                     if ( !initParameters.containsKey( webModuleConfiguration.getWebModuleStrutsConfigName() ) ) {
151                         initParameters.put( webModuleConfiguration.getWebModuleStrutsConfigName(), webModuleConfiguration.getWebModuleStrutsConfigurationFiles() );
152                     }
153                 }
154             }
155         }
156 
157         @Override
158 		public String getInitParameter(String name) {
159             return initParameters.get(name);
160         }
161 
162         @Override
163 		@SuppressWarnings("unchecked")
164 		public Enumeration<String> getInitParameterNames() {
165             return new IteratorEnumeration( initParameters.keySet().iterator() );
166         }
167 
168         @Override
169 		public ServletContext getServletContext() {
170             return wrapped.getServletContext();
171         }
172         @Override
173 		public String getServletName() {
174             return wrapped.getServletName();
175         }
176     }
177 
178 }