View Javadoc
1   /**
2    * Copyright 2005-2016 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.lang.StringUtils;
19  import org.apache.struts.util.MessageResources;
20  import org.apache.struts.util.PropertyMessageResourcesFactory;
21  import org.kuali.rice.core.api.config.property.ConfigContext;
22  import org.kuali.rice.krad.util.KRADConstants;
23  
24  /**
25   * A custom MessageResourceFactory that delegates to the ConfigurationService's pre-loaded properties.
26   *
27   * <p>
28   * This factory can be used in struts-config.xml files by specifying a factory attribute in the <message-resources/>
29   * tag
30   *
31   * Example:
32   * <message-resources
33   * factory="KualiPropertyMessageResourcesFactory"
34   * parameter="SampleApplicationResources" />
35   * </p>
36   *
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   *
39   * @deprecated KNS Struts deprecated, use KRAD and the Spring MVC framework.
40   */
41  @Deprecated
42  public class KualiPropertyMessageResourcesFactory extends PropertyMessageResourcesFactory {
43      private static final long serialVersionUID = 9045578011738154255L;
44  
45      /**
46       * Uses KualiPropertyMessageResources, which allows multiple property files to be loaded into the default message
47       * set
48       *
49       * @see org.apache.struts.util.MessageResourcesFactory#createResources(java.lang.String)
50       */
51      @Override
52      public MessageResources createResources(String config) {
53          if (StringUtils.isBlank(config)) {
54              final String propertyConfig = (String) ConfigContext.getCurrentContextConfig().getProperties().get(
55                      KRADConstants.MESSAGE_RESOURCES);
56              config = removeSpacesAround(propertyConfig);
57          }
58  
59          return new KualiPropertyMessageResources(this, config, this.returnNull);
60      }
61  
62      /**
63       * Removes the spaces around the elements on a csv list of elements
64       *
65       * <p>
66       * A null input will return a null output.
67       * </p>
68       *
69       * @param csv a list of elements in csv format e.g. foo, bar, baz
70       * @return a list of elements in csv format without spaces e.g. foo,bar,baz
71       */
72      private String removeSpacesAround(String csv) {
73          if (csv == null) {
74              return null;
75          }
76  
77          final StringBuilder result = new StringBuilder();
78          for (final String value : csv.split(",")) {
79              if (!"".equals(value.trim())) {
80                  result.append(value.trim());
81                  result.append(",");
82              }
83          }
84  
85          //remove trailing comma
86          int i = result.lastIndexOf(",");
87          if (i != -1) {
88              result.deleteCharAt(i);
89          }
90  
91          return result.toString();
92      }
93  
94  }