Package org.internna.iwebmvc.spring.i18n

Source Code of org.internna.iwebmvc.spring.i18n.PathMatcherResourceBundleMessageSource

/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache license, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.internna.iwebmvc.spring.i18n;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

/**
* Resource bundle that accepts widlcarding.
*
* @author Jose Noheda
* @since 2.0
*/
public class PathMatcherResourceBundleMessageSource extends ReloadableResourceBundleMessageSource implements InitializingBean {

    private List<String> basePaths;
    private PathMatchingResourcePatternResolver resolver;

    public void setBasePath(final String path) throws IOException {
        setBasePaths(Arrays.asList(path));
    }

    public void setBasePaths(List<String> paths) throws IOException {
        this.basePaths = paths;
    }

    @Override public void setResourceLoader(ResourceLoader resourceLoader) {
        super.setResourceLoader(resourceLoader);
        if (logger.isDebugEnabled()) logger.debug("Creating pattern matcher with resource loader [" + resourceLoader.getClass() + "]");
        resolver = new PathMatchingResourcePatternResolver(resourceLoader);
    }

    @Override public void afterPropertiesSet() throws Exception {
        List<String> baseNames = new ArrayList<String>();
        if ((basePaths != null) && (!basePaths.isEmpty())) {
            for (String path : basePaths) {
                if (!path.contains("WEB-INF")) path = "classpath*:" + path;
                String actualPath = path + "/**/*.properties";
                if (logger.isDebugEnabled()) logger.debug("Processing path location [" + actualPath + "]");
                try {
                    for (Resource resource: resolver.getResources(actualPath)) {
                        if (!resource.getFilename().contains("_")) {
                            if (logger.isDebugEnabled()) logger.debug("Found resource [" + resource.getFilename() + "] URI [" + resource.getURI() + "] URL [" + resource.getURL() + "]");
                            baseNames.add(path.replace("*", "") + "/" + resource.getFilename().replace(".properties", ""));
                        }
                    }
                } catch(Exception ex) {
                    if (logger.isInfoEnabled()) logger.info("Could not load i18n resources from [" + actualPath + "]: " + ex.getMessage());
                }
            }
        }
        setBasenames(baseNames.toArray(new String[baseNames.size()]));
    }

}
TOP

Related Classes of org.internna.iwebmvc.spring.i18n.PathMatcherResourceBundleMessageSource

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.