View Javadoc

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