Skip to content

Commit

Permalink
Make ResourceArrayPropertyEditor supports comma delimited location pa…
Browse files Browse the repository at this point in the history
  • Loading branch information
quaff committed Nov 28, 2023
1 parent 4e2d357 commit e1f7e12
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
Expand All @@ -18,10 +18,12 @@

import java.beans.PropertyEditorSupport;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
Expand All @@ -33,6 +35,7 @@
import org.springframework.core.io.Resource;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
* Editor for {@link org.springframework.core.io.Resource} arrays, to
Expand All @@ -50,6 +53,7 @@
*
* @author Juergen Hoeller
* @author Chris Beams
* @author Yanming Zhou
* @since 1.1.2
* @see org.springframework.core.io.Resource
* @see ResourcePatternResolver
Expand Down Expand Up @@ -108,17 +112,33 @@ public ResourceArrayPropertyEditor(ResourcePatternResolver resourcePatternResolv


/**
* Treat the given text as a location pattern and convert it to a Resource array.
* Treat the given text as a location pattern or comma delimited location patterns and convert it to a Resource array.
*/
@Override
public void setAsText(String text) {
String pattern = resolvePath(text).trim();
try {
setValue(this.resourcePatternResolver.getResources(pattern));
if (pattern.indexOf(',') > 0) {
List<Resource> resources = new ArrayList<>();
for (String locationPattern : StringUtils.commaDelimitedListToStringArray(pattern)) {
try {
for (Resource resource : this.resourcePatternResolver.getResources(locationPattern)) {
resources.add(resource);
}
}
catch (IOException ex) {
throw new IllegalArgumentException(
"Could not resolve resource location pattern [" + locationPattern + "]: " + ex.getMessage());
}
}
setValue(resources.toArray());
}
catch (IOException ex) {
throw new IllegalArgumentException(
"Could not resolve resource location pattern [" + pattern + "]: " + ex.getMessage());
else {
try {
setValue(this.resourcePatternResolver.getResources(pattern));
} catch (IOException ex) {
throw new IllegalArgumentException(
"Could not resolve resource location pattern [" + pattern + "]: " + ex.getMessage());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2013 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.
Expand All @@ -21,6 +21,8 @@
import org.junit.jupiter.api.Test;

import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileUrlResource;
import org.springframework.core.io.Resource;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -29,6 +31,7 @@
/**
* @author Dave Syer
* @author Juergen Hoeller
* @author Yanming Zhou
*/
class ResourceArrayPropertyEditorTests {

Expand Down Expand Up @@ -83,4 +86,13 @@ void strictSystemPropertyReplacementWithUnresolvablePlaceholder() {
}
}

@Test
void commaDelimitedResources() {
PropertyEditor editor = new ResourceArrayPropertyEditor();
editor.setAsText("classpath:org/springframework/core/io/support/ResourceArrayPropertyEditor.class,file:/foo/bar.txt");
Resource[] resources = (Resource[]) editor.getValue();
assertThat(resources).isNotNull();
assertThat(resources[0]).isInstanceOf(ClassPathResource.class);
assertThat(resources[1]).isInstanceOf(FileUrlResource.class);
}
}

0 comments on commit e1f7e12

Please sign in to comment.