View Javadoc

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