Skip to content

Commit 1fac270

Browse files
committed
Manually merge #535 - improve error message for duplicate fragments
Now lists the JARs that contain the duplicates. PR by Mads Rolsdorph
1 parent 6b71575 commit 1fac270

5 files changed

Lines changed: 52 additions & 6 deletions

File tree

java/org/apache/tomcat/util/descriptor/web/FragmentJarScannerCallback.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private void addFragment(WebXml fragment, URL url) {
136136
// this name as having a duplicate so Tomcat can handle it
137137
// correctly when the fragments are being ordered.
138138
String duplicateName = fragment.getName();
139-
fragments.get(duplicateName).setDuplicated(true);
139+
fragments.get(duplicateName).addDuplicate(url.toString());
140140
// Rename the current fragment so it doesn't clash
141141
fragment.setName(url.toString());
142142
}

java/org/apache/tomcat/util/descriptor/web/LocalStrings.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ webRuleSet.relativeOrderingCount=<ordering> element is limited to 1 occurrence
3232

3333
webXml.duplicateEnvEntry=Duplicate env-entry name [{0}]
3434
webXml.duplicateFilter=Duplicate filter name [{0}]
35-
webXml.duplicateFragment=More than one fragment with the name [{0}] was found. This is not legal with relative ordering. See section 8.2.2 2c of the Servlet specification for details. Consider using absolute ordering.
35+
webXml.duplicateFragment=More than one fragment with the name [{0}] was found. This is not legal with relative ordering. See section 8.2.2 2c of the Servlet specification for details. Consider using absolute ordering. Duplicate fragments found in [{1}].
3636
webXml.duplicateMessageDestination=Duplicate message-destination name [{0}]
3737
webXml.duplicateMessageDestinationRef=Duplicate message-destination-ref name [{0}]
3838
webXml.duplicateResourceEnvRef=Duplicate resource-env-ref name [{0}]

java/org/apache/tomcat/util/descriptor/web/WebXml.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,23 @@ public void setOverridable(boolean overridable) {
8484
* to know as the action that the specification requires (see 8.2.2 1.e and
8585
* 2.c) varies depending on the ordering method used.
8686
*/
87-
private boolean duplicated = false;
87+
private final List<String> duplicates = new ArrayList<>();
8888
public boolean isDuplicated() {
89-
return duplicated;
89+
return !duplicates.isEmpty();
9090
}
91+
@Deprecated
9192
public void setDuplicated(boolean duplicated) {
92-
this.duplicated = duplicated;
93+
if (duplicated) {
94+
duplicates.add("unknown");
95+
} else {
96+
duplicates.clear();
97+
}
98+
}
99+
public void addDuplicate(String duplicate) {
100+
this.duplicates.add(duplicate);
101+
}
102+
public List<String> getDuplicates() {
103+
return new ArrayList<>(this.duplicates);
93104
}
94105

95106
/**
@@ -2264,8 +2275,10 @@ private Set<WebXml> orderWebFragments(Map<String,WebXml> fragments,
22642275
// Stage 0. Check there were no fragments with duplicate names
22652276
for (WebXml fragment : fragments.values()) {
22662277
if (fragment.isDuplicated()) {
2278+
List<String> duplicates = fragment.getDuplicates();
2279+
duplicates.add(0, fragment.getURL().toString());
22672280
throw new IllegalArgumentException(
2268-
sm.getString("webXml.duplicateFragment", fragment.getName()));
2281+
sm.getString("webXml.duplicateFragment", fragment.getName(), duplicates));
22692282
}
22702283
}
22712284
// Stage 1. Make all dependencies bi-directional - this makes the

test/org/apache/tomcat/util/descriptor/web/TestWebXmlOrdering.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package org.apache.tomcat.util.descriptor.web;
1818

19+
import java.net.MalformedURLException;
20+
import java.net.URL;
1921
import java.util.ArrayList;
2022
import java.util.HashSet;
2123
import java.util.Iterator;
@@ -348,6 +350,31 @@ public void testOrderWebFragmentsrelativeCircular2() {
348350
WebXml.orderWebFragments(app, fragments, null);
349351
}
350352

353+
@Test(expected=IllegalArgumentException.class)
354+
public void testOrderWebFragmentsRelativeDuplicate() throws MalformedURLException {
355+
WebXml withDuplicate = new WebXml();
356+
withDuplicate.setURL(new URL("https://example.com/original.jar"));
357+
withDuplicate.addDuplicate("https://example.com/duplicate.jar");
358+
359+
Map<String,WebXml> fragmentsWithDuplicate = new LinkedHashMap<>();
360+
fragmentsWithDuplicate.put("has-duplicate", withDuplicate);
361+
362+
WebXml.orderWebFragments(app, fragmentsWithDuplicate, null);
363+
}
364+
365+
@SuppressWarnings("deprecation")
366+
@Test(expected=IllegalArgumentException.class)
367+
public void testOrderWebFragmentsRelativeDuplicateDeprecated() throws MalformedURLException {
368+
WebXml withDuplicate = new WebXml();
369+
withDuplicate.setURL(new URL("https://example.com/original.jar"));
370+
withDuplicate.setDuplicated(true);
371+
372+
Map<String,WebXml> fragmentsWithDuplicate = new LinkedHashMap<>();
373+
fragmentsWithDuplicate.put("has-duplicate", withDuplicate);
374+
375+
WebXml.orderWebFragments(app, fragmentsWithDuplicate, null);
376+
}
377+
351378
private interface RelativeOrderingTestRunner {
352379
void init();
353380
void validate(String order);

webapps/docs/changelog.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@
152152
<bug>66233</bug>: Include an error message when sending a 400 response
153153
because a request has too many cookies. (markt)
154154
</fix>
155+
<fix>
156+
When web application deployment fails due to JARs with duplicate
157+
fragment names, improve the error message by listing the JARs that
158+
contain the duplicates. Based on pull request <pr>535</pr> by Mads
159+
Rolsdorph. (markt)
160+
</fix>
155161
</changelog>
156162
</subsection>
157163
<subsection name="Coyote">

0 commit comments

Comments
 (0)