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