reintroduced two-arg constructor (making STS warning disappear); always use a ResourcePatternResolver (through ResourcePatternUtils)

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3212 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
Juergen Hoeller 2010-04-01 10:31:15 +00:00
parent a3756edf15
commit 7efec864e9
1 changed files with 32 additions and 26 deletions

View File

@ -28,11 +28,14 @@ import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.springframework.context.ResourceLoaderAware; import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;
/** /**
* {@link FactoryBean} implementation that takes a list of location strings and creates a sorted array * {@link FactoryBean} implementation that takes a list of location Strings
* of {@link Resource} instances. * and creates a sorted array of {@link Resource} instances.
*
* @author Dave Syer * @author Dave Syer
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Christian Dupuis * @author Christian Dupuis
@ -42,45 +45,48 @@ public class SortedResourcesFactoryBean extends AbstractFactoryBean<Resource[]>
private final List<String> locations; private final List<String> locations;
private ResourceLoader resourceLoader; private ResourcePatternResolver resourcePatternResolver;
public SortedResourcesFactoryBean(List<String> locations) { public SortedResourcesFactoryBean(List<String> locations) {
this.locations = locations; this.locations = locations;
setSingleton(true); this.resourcePatternResolver = new PathMatchingResourcePatternResolver();
} }
public SortedResourcesFactoryBean(ResourceLoader resourceLoader, List<String> locations) {
this.locations = locations;
this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
}
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
}
@Override @Override
public Class<? extends Resource[]> getObjectType() { public Class<? extends Resource[]> getObjectType() {
return Resource[].class; return Resource[].class;
} }
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
@Override @Override
protected Resource[] createInstance() throws Exception { protected Resource[] createInstance() throws Exception {
List<Resource> scripts = new ArrayList<Resource>(); List<Resource> scripts = new ArrayList<Resource>();
for (String location : locations) { for (String location : this.locations) {
if (resourceLoader instanceof ResourcePatternResolver) { List<Resource> resources = new ArrayList<Resource>(
List<Resource> resources = new ArrayList<Resource>(Arrays Arrays.asList(this.resourcePatternResolver.getResources(location)));
.asList(((ResourcePatternResolver) resourceLoader).getResources(location))); Collections.sort(resources, new Comparator<Resource>() {
Collections.sort(resources, new Comparator<Resource>() { public int compare(Resource r1, Resource r2) {
public int compare(Resource r1, Resource r2) { try {
try { return r1.getURL().toString().compareTo(r2.getURL().toString());
return r1.getURL().toString().compareTo(r2.getURL().toString()); }
} catch (IOException ex) {
catch (IOException ex) { return 0;
return 0;
}
} }
});
for (Resource resource : resources) {
scripts.add(resource);
} }
} });
else { for (Resource resource : resources) {
scripts.add(resourceLoader.getResource(location)); scripts.add(resource);
} }
} }
return scripts.toArray(new Resource[scripts.size()]); return scripts.toArray(new Resource[scripts.size()]);