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  public class KualiPropertyMessageResourcesFactory extends PropertyMessageResourcesFactory {
40      private static final long serialVersionUID = 9045578011738154255L;
41  
42      /**
43       * Uses KualiPropertyMessageResources, which allows multiple property files to be loaded into the default message
44       * set
45       *
46       * @see org.apache.struts.util.MessageResourcesFactory#createResources(java.lang.String)
47       */
48      @Override
49      public MessageResources createResources(String config) {
50          if (StringUtils.isBlank(config)) {
51              final String propertyConfig = (String) ConfigContext.getCurrentContextConfig().getProperties().get(
52                      KRADConstants.MESSAGE_RESOURCES);
53              config = removeSpacesAround(propertyConfig);
54          }
55  
56          return new KualiPropertyMessageResources(this, config, this.returnNull);
57      }
58  
59      /**
60       * Removes the spaces around the elements on a csv list of elements
61       *
62       * <p>
63       * A null input will return a null output.
64       * </p>
65       *
66       * @param csv a list of elements in csv format e.g. foo, bar, baz
67       * @return a list of elements in csv format without spaces e.g. foo,bar,baz
68       */
69      private String removeSpacesAround(String csv) {
70          if (csv == null) {
71              return null;
72          }
73  
74          final StringBuilder result = new StringBuilder();
75          for (final String value : csv.split(",")) {
76              if (!"".equals(value.trim())) {
77                  result.append(value.trim());
78                  result.append(",");
79              }
80          }
81  
82          //remove trailing comma
83          int i = result.lastIndexOf(",");
84          if (i != -1) {
85              result.deleteCharAt(i);
86          }
87  
88          return result.toString();
89      }
90  
91  }