001/*
002 * Copyright 2002-2012 the original author or authors.
003 *
004 * Licensed under the Apache 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 *      https://www.apache.org/licenses/LICENSE-2.0
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
017package org.springframework.jdbc.config;
018
019import java.io.IOException;
020import java.util.ArrayList;
021import java.util.Arrays;
022import java.util.Collections;
023import java.util.Comparator;
024import java.util.List;
025
026import org.springframework.beans.factory.FactoryBean;
027import org.springframework.beans.factory.config.AbstractFactoryBean;
028import org.springframework.context.ResourceLoaderAware;
029import org.springframework.core.io.Resource;
030import org.springframework.core.io.ResourceLoader;
031import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
032import org.springframework.core.io.support.ResourcePatternResolver;
033import org.springframework.core.io.support.ResourcePatternUtils;
034
035/**
036 * {@link FactoryBean} implementation that takes a list of location Strings
037 * and creates a sorted array of {@link Resource} instances.
038 *
039 * @author Dave Syer
040 * @author Juergen Hoeller
041 * @author Christian Dupuis
042 * @since 3.0
043 */
044public class SortedResourcesFactoryBean extends AbstractFactoryBean<Resource[]> implements ResourceLoaderAware {
045
046        private final List<String> locations;
047
048        private ResourcePatternResolver resourcePatternResolver;
049
050
051        public SortedResourcesFactoryBean(List<String> locations) {
052                this.locations = locations;
053                this.resourcePatternResolver = new PathMatchingResourcePatternResolver();
054        }
055
056        public SortedResourcesFactoryBean(ResourceLoader resourceLoader, List<String> locations) {
057                this.locations = locations;
058                this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
059        }
060
061
062        @Override
063        public void setResourceLoader(ResourceLoader resourceLoader) {
064                this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
065        }
066
067
068        @Override
069        public Class<? extends Resource[]> getObjectType() {
070                return Resource[].class;
071        }
072
073        @Override
074        protected Resource[] createInstance() throws Exception {
075                List<Resource> scripts = new ArrayList<Resource>();
076                for (String location : this.locations) {
077                        List<Resource> resources = new ArrayList<Resource>(
078                                        Arrays.asList(this.resourcePatternResolver.getResources(location)));
079                        Collections.sort(resources, new Comparator<Resource>() {
080                                @Override
081                                public int compare(Resource r1, Resource r2) {
082                                        try {
083                                                return r1.getURL().toString().compareTo(r2.getURL().toString());
084                                        }
085                                        catch (IOException ex) {
086                                                return 0;
087                                        }
088                                }
089                        });
090                        for (Resource resource : resources) {
091                                scripts.add(resource);
092                        }
093                }
094                return scripts.toArray(new Resource[scripts.size()]);
095        }
096
097}