This commit is contained in:
Phillip Webb
2020-03-23 20:03:44 -07:00
parent 9a33a723fe
commit 0717de723f
30 changed files with 252 additions and 198 deletions

View File

@@ -37,10 +37,10 @@ public class Library {
private final LibraryScope scope;
private final boolean unpackRequired;
private final LibraryCoordinates coordinates;
private final boolean unpackRequired;
/**
* Create a new {@link Library}.
* @param file the source file
@@ -69,15 +69,24 @@ public class Library {
* @param unpackRequired if the library needs to be unpacked before it can be used
*/
public Library(String name, File file, LibraryScope scope, boolean unpackRequired) {
this(name, file, scope, unpackRequired, null);
this(name, file, scope, null, unpackRequired);
}
public Library(String name, File file, LibraryScope scope, boolean unpackRequired, LibraryCoordinates coordinates) {
/**
* Create a new {@link Library}.
* @param name the name of the library as it should be written or {@code null} to use
* the file name
* @param file the source file
* @param scope the scope of the library
* @param coordinates the library coordinates or {@code null}
* @param unpackRequired if the library needs to be unpacked before it can be used
*/
public Library(String name, File file, LibraryScope scope, LibraryCoordinates coordinates, boolean unpackRequired) {
this.name = (name != null) ? name : file.getName();
this.file = file;
this.scope = scope;
this.unpackRequired = unpackRequired;
this.coordinates = coordinates;
this.unpackRequired = unpackRequired;
}
/**
@@ -113,6 +122,14 @@ public class Library {
return this.scope;
}
/**
* Return the {@linkplain LibraryCoordinates coordinates} of the library.
* @return the coordinates
*/
public LibraryCoordinates getCoordinates() {
return this.coordinates;
}
/**
* Return if the file cannot be used directly as a nested jar and needs to be
* unpacked.
@@ -122,14 +139,6 @@ public class Library {
return this.unpackRequired;
}
/**
* Return the {@linkplain LibraryCoordinates coordinates} of the library.
* @return the coordinates
*/
public LibraryCoordinates getCoordinates() {
return this.coordinates;
}
long getLastModified() {
return this.file.lastModified();
}

View File

@@ -54,24 +54,45 @@ public final class LibraryCoordinates {
Assert.isTrue(elements.length >= 2, "Coordinates must contain at least 'groupId:artifactId'");
this.groupId = elements[0];
this.artifactId = elements[1];
if (elements.length > 2) {
this.version = elements[2];
}
else {
this.version = null;
}
this.version = (elements.length > 2) ? elements[2] : null;
}
/**
* Return the group ID of the coordinates.
* @return the group ID
*/
public String getGroupId() {
return this.groupId;
}
/**
* Return the artifact ID of the coordinates.
* @return the artifact ID
*/
public String getArtifactId() {
return this.artifactId;
}
/**
* Return the version of the coordinates.
* @return the version
*/
public String getVersion() {
return this.version;
}
/**
* Return the coordinates in the form {@code groupId:artifactId:version}.
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append((this.groupId != null) ? this.groupId : "");
builder.append(":");
builder.append((this.artifactId != null) ? this.artifactId : "");
builder.append(":");
builder.append((this.version != null) ? this.version : "");
return builder.toString();
}
}

View File

@@ -16,9 +16,9 @@
package org.springframework.boot.loader.tools.layer.library;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.springframework.boot.loader.tools.Library;
import org.springframework.boot.loader.tools.LibraryCoordinates;
@@ -32,58 +32,21 @@ import org.springframework.boot.loader.tools.LibraryCoordinates;
*/
public class CoordinateFilter implements LibraryFilter {
private final List<String> includes = new ArrayList<>();
private static final String EMPTY_COORDINATES = "::";
private final List<String> excludes = new ArrayList<>();
private final List<Pattern> includes;
private final List<Pattern> excludes;
public CoordinateFilter(List<String> includes, List<String> excludes) {
this.includes.addAll(includes);
this.excludes.addAll(excludes);
this.includes = includes.stream().map(this::asPattern).collect(Collectors.toList());
this.excludes = excludes.stream().map(this::asPattern).collect(Collectors.toList());
}
@Override
public boolean isLibraryIncluded(Library library) {
return isMatch(library, this.includes);
}
@Override
public boolean isLibraryExcluded(Library library) {
return isMatch(library, this.excludes);
}
private boolean isMatch(Library library, List<String> toMatch) {
private Pattern asPattern(String string) {
StringBuilder builder = new StringBuilder();
LibraryCoordinates coordinates = library.getCoordinates();
if (coordinates != null) {
if (coordinates.getGroupId() != null) {
builder.append(coordinates.getGroupId());
}
builder.append(":");
if (coordinates.getArtifactId() != null) {
builder.append(coordinates.getArtifactId());
}
builder.append(":");
if (coordinates.getVersion() != null) {
builder.append(coordinates.getVersion());
}
}
else {
builder.append("::");
}
String input = builder.toString();
for (String patternString : toMatch) {
Pattern pattern = buildPatternForString(patternString);
if (pattern.matcher(input).matches()) {
return true;
}
}
return false;
}
private Pattern buildPatternForString(String pattern) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < pattern.length(); i++) {
char c = pattern.charAt(i);
for (int i = 0; i < string.length(); i++) {
char c = string.charAt(i);
if (c == '.') {
builder.append("\\.");
}
@@ -97,4 +60,25 @@ public class CoordinateFilter implements LibraryFilter {
return Pattern.compile(builder.toString());
}
@Override
public boolean isLibraryIncluded(Library library) {
return isMatch(library, this.includes);
}
@Override
public boolean isLibraryExcluded(Library library) {
return isMatch(library, this.excludes);
}
private boolean isMatch(Library library, List<Pattern> patterns) {
LibraryCoordinates coordinates = library.getCoordinates();
String input = (coordinates != null) ? coordinates.toString() : EMPTY_COORDINATES;
for (Pattern pattern : patterns) {
if (pattern.matcher(input).matches()) {
return true;
}
}
return false;
}
}

View File

@@ -65,4 +65,10 @@ class LibraryCoordinatesTests {
assertThatIllegalArgumentException().isThrownBy(() -> new LibraryCoordinates("com.acme"));
}
@Test
void toStringReturnsString() {
assertThat(new LibraryCoordinates("com.acme:my-library:1.0.0")).hasToString("com.acme:my-library:1.0.0");
assertThat(new LibraryCoordinates("com.acme:my-library")).hasToString("com.acme:my-library:");
}
}