001/**
002 * Copyright 2005-2017 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 * <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 * @deprecated KNS Struts deprecated, use KRAD and the Spring MVC framework.
040 */
041@Deprecated
042public class KualiPropertyMessageResourcesFactory extends PropertyMessageResourcesFactory {
043    private static final long serialVersionUID = 9045578011738154255L;
044
045    /**
046     * Uses KualiPropertyMessageResources, which allows multiple property files to be loaded into the default message
047     * set
048     *
049     * @see org.apache.struts.util.MessageResourcesFactory#createResources(java.lang.String)
050     */
051    @Override
052    public MessageResources createResources(String config) {
053        if (StringUtils.isBlank(config)) {
054            final String propertyConfig = (String) ConfigContext.getCurrentContextConfig().getProperties().get(
055                    KRADConstants.MESSAGE_RESOURCES);
056            config = removeSpacesAround(propertyConfig);
057        }
058
059        return new KualiPropertyMessageResources(this, config, this.returnNull);
060    }
061
062    /**
063     * Removes the spaces around the elements on a csv list of elements
064     *
065     * <p>
066     * A null input will return a null output.
067     * </p>
068     *
069     * @param csv a list of elements in csv format e.g. foo, bar, baz
070     * @return a list of elements in csv format without spaces e.g. foo,bar,baz
071     */
072    private String removeSpacesAround(String csv) {
073        if (csv == null) {
074            return null;
075        }
076
077        final StringBuilder result = new StringBuilder();
078        for (final String value : csv.split(",")) {
079            if (!"".equals(value.trim())) {
080                result.append(value.trim());
081                result.append(",");
082            }
083        }
084
085        //remove trailing comma
086        int i = result.lastIndexOf(",");
087        if (i != -1) {
088            result.deleteCharAt(i);
089        }
090
091        return result.toString();
092    }
093
094}