Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,28 @@ public boolean matches(String otherUri) {
return match;
}

/**
* Returns {@code true} if this location pattern strictly matches other pattern.
* <p>
* In a strict match, if this instance doesn't contain a classifier,
* it won't match against an otherUri which contains one.
*/
public boolean strictlyMatches(String otherUri) {
boolean match = matches(otherUri);
if (!match) return false;
// refine the loose match
LocationPattern other = null;
try {
other = new LocationPattern(otherUri);
} catch (IllegalArgumentException ignored) {
return true; // not a "mvn:" uri
}
if (classifierPattern == null && classifier == null) {
match = other.classifierPattern == null && other.classifier == null;
}
return match;
}

@Override
public String toString() {
return originalUri;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;

Expand Down Expand Up @@ -222,24 +222,22 @@ private void staticOverrideBundle(Bundle bundle) {
bundle.setOverriden(BundleInfo.BundleOverrideMode.NONE);

String originalLocation = bundle.getLocation();
final List<BundleReplacements.OverrideBundle> candidates = new ArrayList<>();
for (BundleReplacements.OverrideBundle override : this.getInstructions().getBundleReplacements().getOverrideBundles()) {
if (override.getOriginalUriPattern().matches(originalLocation)) {
candidates.add(override);
}
}
if (!candidates.isEmpty()) {
BundleReplacements.OverrideBundle bestMatch = candidates.stream()
.max((o1, o2) -> Integer.compare(o1.getReplacement().length(), o2.getReplacement().length())).get();
LOG.debug("Overriding bundle location \"" + originalLocation + "\" with \"" + bestMatch.getReplacement() + "\"");
bundle.setOriginalLocation(originalLocation);
if (bestMatch.getMode() == BundleReplacements.BundleOverrideMode.MAVEN) {
bundle.setOverriden(BundleInfo.BundleOverrideMode.MAVEN);
} else {
bundle.setOverriden(BundleInfo.BundleOverrideMode.OSGI);
}
bundle.setLocation(bestMatch.getReplacement());
Optional<BundleReplacements.OverrideBundle> bestMatch = this.getInstructions().getBundleReplacements().getOverrideBundles().stream()
.filter(overrideBundle -> overrideBundle.getOriginalUriPattern().strictlyMatches(originalLocation))
.max((o1, o2) -> Integer.compare(o1.getReplacement().length(), o2.getReplacement().length()));
bestMatch.ifPresent(o -> doOverrideBundle(bundle, o));
}

private void doOverrideBundle(Bundle bundle, BundleReplacements.OverrideBundle bestMatch) {
String originalLocation = bundle.getLocation();
LOG.debug("Overriding bundle location \"" + originalLocation + "\" with \"" + bestMatch.getReplacement() + "\"");
bundle.setOriginalLocation(originalLocation);
if (bestMatch.getMode() == BundleReplacements.BundleOverrideMode.MAVEN) {
bundle.setOverriden(BundleInfo.BundleOverrideMode.MAVEN);
} else {
bundle.setOverriden(BundleInfo.BundleOverrideMode.OSGI);
}
bundle.setLocation(bestMatch.getReplacement());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,12 @@ public void matchingMavenUrisWithVersionRangesInUri() throws MalformedURLExcepti
assertFalse(new LocationPattern("mvn:g/a/[1,1]").matches("mvn:g/a/[1,1]"));
}

@Test
public void strictlyMatchingMavenUris() {
assertFalse(new LocationPattern("mvn:*/*").strictlyMatches("mvn:g/a/1//c"));
assertFalse(new LocationPattern("mvn:*/*").strictlyMatches("mvn:g/a/1/t/c"));
assertTrue(new LocationPattern("mvn:*/*").strictlyMatches("mvn:g/a/1/t"));
assertTrue(new LocationPattern("mvn:*/*").strictlyMatches("mvn:g/a/1"));
assertTrue(new LocationPattern("mvn:*/*").strictlyMatches("mvn:g/a"));
}
}