001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.kns.web.struts.action;
017
018import org.apache.commons.lang.StringUtils;
019import org.apache.struts.util.MessageResources;
020import org.apache.struts.util.PropertyMessageResourcesFactory;
021import org.kuali.rice.core.api.config.property.ConfigContext;
022import org.kuali.rice.krad.util.KRADConstants;
023
024/**
025 * A custom MessageResourceFactory that delegates to the ConfigurationService's pre-loaded properties.
026 * 
027 * This factory can be used in struts-config.xml files by specifying a factory attribute in the <message-resources/> tag.  
028 * Example: 
029 *   <message-resources 
030 *       factory="KualiPropertyMessageResourcesFactory"
031 *       parameter="SampleApplicationResources" />
032 */
033public class KualiPropertyMessageResourcesFactory extends PropertyMessageResourcesFactory {
034
035    private static final long serialVersionUID = 9045578011738154255L;
036
037    /**
038     * Uses KualiPropertyMessageResources, which allows multiple property files to be loaded into the defalt message set.
039     * 
040     * @see org.apache.struts.util.MessageResourcesFactory#createResources(java.lang.String)
041     */
042    @Override
043    public MessageResources createResources(String config) {
044        if (StringUtils.isBlank(config)) {
045            final String propertyConfig = (String) ConfigContext.getCurrentContextConfig().getProperties().get(
046                    KRADConstants.MESSAGE_RESOURCES);
047            config = removeSpacesAround(propertyConfig);
048        }
049        return new KualiPropertyMessageResources(this, config, this.returnNull);
050    }
051    
052    /**
053     * Removes the spaces around the elements on a csv list of elements.
054     * <p>
055     * A null input will return a null output.
056     * </p>
057     * 
058     * @param csv a list of elements in csv format e.g. foo, bar, baz
059     * @return a list of elements in csv format without spaces e.g. foo,bar,baz
060     */
061    private String removeSpacesAround(String csv) {
062        if (csv == null) {
063                return null;
064        }
065        
066        final StringBuilder result = new StringBuilder();
067                for (final String value : csv.split(",")) {
068                        if (!"".equals(value.trim())) {
069                                result.append(value.trim());
070                                result.append(",");
071                        }
072                }
073                
074                //remove trailing comma
075                int i = result.lastIndexOf(",");
076                if (i != -1) {
077                        result.deleteCharAt(i);
078                }
079                
080                return result.toString();
081    }
082    
083}