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