001    /**
002     * Copyright 2005-2014 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kns.web.struts.action;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.apache.struts.util.MessageResources;
020    import org.apache.struts.util.PropertyMessageResourcesFactory;
021    import org.kuali.rice.core.api.config.property.ConfigContext;
022    import org.kuali.rice.krad.util.KRADConstants;
023    
024    /**
025     * A custom MessageResourceFactory that delegates to the ConfigurationService's pre-loaded properties.
026     *
027     * <p>
028     * This factory can be used in struts-config.xml files by specifying a factory attribute in the <message-resources/>
029     * tag
030     *
031     * Example:
032     * <message-resources
033     * factory="KualiPropertyMessageResourcesFactory"
034     * parameter="SampleApplicationResources" />
035     * </p>
036     *
037     * @author Kuali Rice Team (rice.collab@kuali.org)
038     */
039    public class KualiPropertyMessageResourcesFactory extends PropertyMessageResourcesFactory {
040        private static final long serialVersionUID = 9045578011738154255L;
041    
042        /**
043         * Uses KualiPropertyMessageResources, which allows multiple property files to be loaded into the default message
044         * set
045         *
046         * @see org.apache.struts.util.MessageResourcesFactory#createResources(java.lang.String)
047         */
048        @Override
049        public MessageResources createResources(String config) {
050            if (StringUtils.isBlank(config)) {
051                final String propertyConfig = (String) ConfigContext.getCurrentContextConfig().getProperties().get(
052                        KRADConstants.MESSAGE_RESOURCES);
053                config = removeSpacesAround(propertyConfig);
054            }
055    
056            return new KualiPropertyMessageResources(this, config, this.returnNull);
057        }
058    
059        /**
060         * Removes the spaces around the elements on a csv list of elements
061         *
062         * <p>
063         * A null input will return a null output.
064         * </p>
065         *
066         * @param csv a list of elements in csv format e.g. foo, bar, baz
067         * @return a list of elements in csv format without spaces e.g. foo,bar,baz
068         */
069        private String removeSpacesAround(String csv) {
070            if (csv == null) {
071                return null;
072            }
073    
074            final StringBuilder result = new StringBuilder();
075            for (final String value : csv.split(",")) {
076                if (!"".equals(value.trim())) {
077                    result.append(value.trim());
078                    result.append(",");
079                }
080            }
081    
082            //remove trailing comma
083            int i = result.lastIndexOf(",");
084            if (i != -1) {
085                result.deleteCharAt(i);
086            }
087    
088            return result.toString();
089        }
090    
091    }