Skip to content

Commit 60a1c5a

Browse files
authored
Merge pull request #5 from TomasHofman/external-service
SET-218 Refactored logger configuration and HTML processor
2 parents e0709bf + 6cff9e1 commit 60a1c5a

File tree

6 files changed

+187
-127
lines changed

6 files changed

+187
-127
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.jboss.set.mavendependencyupdater.core.aggregation;
2+
3+
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
4+
import org.jboss.set.mavendependencyupdater.DependencyEvaluator.ComponentUpgrade;
5+
import org.jboss.set.mavendependencyupdater.LocatedDependency;
6+
import org.jboss.set.mavendependencyupdater.PomDependencyUpdater;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.net.URI;
11+
import java.util.ArrayList;
12+
import java.util.HashSet;
13+
import java.util.List;
14+
import java.util.Optional;
15+
import java.util.Set;
16+
17+
public class ComponentUpgradeAggregator {
18+
19+
/**
20+
* Aggregates component upgrades based on which properties they would update in the POM file.
21+
*
22+
* E.g. if a subset of component upgrades would lead to upgrading of the same property, only the first component
23+
* upgrade in this subset would be present in the resulting list.
24+
*
25+
* @param pomFile POM file being updated
26+
* @param upgrades list of component upgrades to be aggregated
27+
* @return aggregated list
28+
*/
29+
public static List<ComponentUpgrade> aggregateComponentUpgrades(File pomFile, List<ComponentUpgrade> upgrades)
30+
throws IOException, XmlPullParserException {
31+
final URI uri = pomFile.toURI();
32+
final Set<ModifiedProperty> modifiedProperties = new HashSet<>();
33+
final List<ComponentUpgrade> aggregatedUpgrades = new ArrayList<>();
34+
35+
for (ComponentUpgrade upgrade: upgrades) {
36+
Optional<LocatedDependency> locatedDependencyOpt =
37+
PomDependencyUpdater.locateDependency(pomFile, upgrade.getArtifact());
38+
if (locatedDependencyOpt.isPresent()) {
39+
LocatedDependency locatedDependency = locatedDependencyOpt.get();
40+
boolean added = modifiedProperties.add(
41+
new ModifiedProperty(uri, locatedDependency.getProfile(), locatedDependency.getVersionProperty(), upgrade.getNewVersion()));
42+
if (added) {
43+
aggregatedUpgrades.add(upgrade);
44+
}
45+
}
46+
}
47+
48+
return aggregatedUpgrades;
49+
}
50+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.jboss.set.mavendependencyupdater.core.aggregation;
2+
3+
import org.apache.commons.lang3.builder.EqualsBuilder;
4+
import org.apache.commons.lang3.builder.HashCodeBuilder;
5+
6+
import java.net.URI;
7+
8+
/**
9+
* Identifies a property in a particular POM file and a profile.
10+
*
11+
* It's used to record properties that would be changed by a component upgrade, to detect that given property would
12+
* have already been changed by a previous component upgrade.
13+
*/
14+
class ModifiedProperty {
15+
16+
final private URI pomUri;
17+
final private String profile;
18+
final private String propertyName;
19+
final private String newValue;
20+
21+
ModifiedProperty(URI pomUri, String profile, String propertyName, String newValue) {
22+
this.pomUri = pomUri;
23+
this.profile = profile;
24+
this.propertyName = propertyName;
25+
this.newValue = newValue;
26+
}
27+
28+
@Override
29+
public boolean equals(Object o) {
30+
if (this == o) return true;
31+
32+
if (o == null || getClass() != o.getClass()) return false;
33+
34+
ModifiedProperty that = (ModifiedProperty) o;
35+
36+
return new EqualsBuilder()
37+
.append(pomUri, that.pomUri)
38+
.append(profile, that.profile)
39+
.append(propertyName, that.propertyName)
40+
.append(newValue, that.newValue)
41+
.isEquals();
42+
}
43+
44+
@Override
45+
public int hashCode() {
46+
return new HashCodeBuilder(17, 37)
47+
.append(pomUri)
48+
.append(profile)
49+
.append(propertyName)
50+
.append(newValue)
51+
.toHashCode();
52+
}
53+
}

core/src/main/java/org/jboss/set/mavendependencyupdater/core/processingstrategies/HtmlReportProcessingStrategy.java

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.jboss.set.mavendependencyupdater.core.processingstrategies;
22

3+
import j2html.tags.DomContent;
34
import org.jboss.set.mavendependencyupdater.DependencyEvaluator.ComponentUpgrade;
45
import org.jboss.set.mavendependencyupdater.configuration.Configuration;
6+
import org.jboss.set.mavendependencyupdater.core.aggregation.ComponentUpgradeAggregator;
57

68
import java.io.File;
79
import java.io.PrintStream;
@@ -40,7 +42,7 @@ public class HtmlReportProcessingStrategy extends TextReportProcessingStrategy {
4042
private static final String CAPTION_STYLES = "text-align: left;" +
4143
"font-weight: bold;";
4244
private static final String TH_TD_STYLES = "border-bottom: 1px solid #ddd;" +
43-
"padding: 10px;" +
45+
"padding: 5px;" +
4446
"text-align: left;";
4547
private static final String GAV_STYLES = "font-family: \"Courier New\";";
4648
private static final String UL_STYLES = "list-style-type: circle;";
@@ -82,6 +84,8 @@ public boolean process(List<ComponentUpgrade> upgrades) {
8284
List<ComponentUpgrade> sortedUpgrades =
8385
upgrades.stream().sorted(new ComponentUpgradeComparator())
8486
.collect(Collectors.toList());
87+
List<ComponentUpgrade> aggregatedUpgrades = ComponentUpgradeAggregator.aggregateComponentUpgrades(pomFile, sortedUpgrades);
88+
8589

8690
String html = div().withStyle(BASIC_STYLES).with(
8791
h2("Component Upgrade Report"),
@@ -96,29 +100,9 @@ public boolean process(List<ComponentUpgrade> upgrades) {
96100
),
97101
table().withStyle(BASIC_STYLES + TABLE_STYLES).with(
98102
caption("Possible Component Upgrades").withStyle(CAPTION_STYLES),
99-
thead(tr().with(
100-
th("GAV").withStyle(TH_TD_STYLES),
101-
th("New Version").withStyle(TH_TD_STYLES),
102-
th("Repository").withStyle(TH_TD_STYLES),
103-
th("Since").withStyle(TH_TD_STYLES)
104-
)),
105-
each(sortedUpgrades, upgrade -> {
106-
boolean isNew = upgrade.getFirstSeen() == null;
107-
return tr().with(
108-
td(upgrade.getArtifact().getGroupId()
109-
+ ":" + upgrade.getArtifact().getArtifactId()
110-
+ ":" + upgrade.getArtifact().getVersionString())
111-
.withStyle(TH_TD_STYLES + GAV_STYLES + (isNew ? BG_NEW : "")),
112-
td(upgrade.getNewVersion())
113-
.withStyle(TH_TD_STYLES + (isNew ? BG_NEW : "")),
114-
td(span(upgrade.getRepository())
115-
.withStyle(REPO_LABEL_STYLES + repositoryColor(upgrade.getRepository())))
116-
.withStyle(TH_TD_STYLES + (isNew ? BG_NEW : "")),
117-
td(upgrade.getFirstSeen() == null ? "new" : upgrade.getFirstSeen().format(DATE_FORMATTER))
118-
.withStyle(TH_TD_STYLES + (isNew ? BG_NEW : ""))
119-
);
120-
}),
121-
tr(td(sortedUpgrades.size() + " items").withStyle(TH_TD_STYLES).attr("colspan", "4"))),
103+
thead(tr().with(tableHeaders())),
104+
each(aggregatedUpgrades, upgrade -> tr().with(tableRowData(upgrade))),
105+
tr(td(aggregatedUpgrades.size() + " items").withStyle(TH_TD_STYLES).attr("colspan", "4"))),
122106
p("Generated on " + DATE_FORMATTER.format(ZonedDateTime.now())),
123107
p().withStyle(FOOTER_STYLES).with(
124108
text("Report generated by "),
@@ -138,6 +122,37 @@ public boolean process(List<ComponentUpgrade> upgrades) {
138122
}
139123
}
140124

125+
private DomContent[] tableHeaders() {
126+
ArrayList<DomContent> headers = new ArrayList<>();
127+
headers.add(th("GAV").withStyle(TH_TD_STYLES));
128+
headers.add(th("New Version").withStyle(TH_TD_STYLES));
129+
headers.add(th("Repository").withStyle(TH_TD_STYLES));
130+
if (configuration.getLogger().isActive()) {
131+
headers.add(th("Since").withStyle(TH_TD_STYLES));
132+
}
133+
return headers.toArray(new DomContent[]{});
134+
}
135+
136+
private DomContent[] tableRowData(ComponentUpgrade upgrade) {
137+
boolean isNew = upgrade.getFirstSeen() == null && configuration.getLogger().isActive();
138+
139+
ArrayList<DomContent> cells = new ArrayList<>();
140+
cells.add(td(upgrade.getArtifact().getGroupId()
141+
+ ":" + upgrade.getArtifact().getArtifactId()
142+
+ ":" + upgrade.getArtifact().getVersionString())
143+
.withStyle(TH_TD_STYLES + GAV_STYLES + (isNew ? BG_NEW : "")));
144+
cells.add(td(upgrade.getNewVersion())
145+
.withStyle(TH_TD_STYLES + (isNew ? BG_NEW : "")));
146+
cells.add(td(span(upgrade.getRepository())
147+
.withStyle(REPO_LABEL_STYLES + repositoryColor(upgrade.getRepository())))
148+
.withStyle(TH_TD_STYLES + (isNew ? BG_NEW : "")));
149+
if (configuration.getLogger().isActive()) {
150+
cells.add(td(upgrade.getFirstSeen() == null ? "new" : upgrade.getFirstSeen().format(DATE_FORMATTER))
151+
.withStyle(TH_TD_STYLES + (isNew ? BG_NEW : "")));
152+
}
153+
return cells.toArray(new DomContent[]{});
154+
}
155+
141156
private void initRepositoryKeys() {
142157
repositoryKeys = new ArrayList<>(configuration.getRepositories().keySet());
143158
}

core/src/main/java/org/jboss/set/mavendependencyupdater/core/processingstrategies/TextReportProcessingStrategy.java

Lines changed: 17 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
package org.jboss.set.mavendependencyupdater.core.processingstrategies;
22

3-
import org.apache.commons.lang3.builder.EqualsBuilder;
4-
import org.apache.commons.lang3.builder.HashCodeBuilder;
53
import org.commonjava.maven.atlas.ident.ref.ArtifactRef;
64
import org.jboss.logging.Logger;
75
import org.jboss.set.mavendependencyupdater.DependencyEvaluator.ComponentUpgrade;
8-
import org.jboss.set.mavendependencyupdater.LocatedDependency;
9-
import org.jboss.set.mavendependencyupdater.PomDependencyUpdater;
106
import org.jboss.set.mavendependencyupdater.configuration.Configuration;
7+
import org.jboss.set.mavendependencyupdater.core.aggregation.ComponentUpgradeAggregator;
118

129
import java.io.File;
1310
import java.io.FileNotFoundException;
1411
import java.io.PrintStream;
15-
import java.net.URI;
1612
import java.time.ZonedDateTime;
1713
import java.time.format.DateTimeFormatter;
1814
import java.util.Comparator;
19-
import java.util.HashSet;
2015
import java.util.List;
2116
import java.util.Map;
22-
import java.util.Optional;
23-
import java.util.Set;
2417
import java.util.stream.Collectors;
2518

2619
/**
@@ -38,7 +31,6 @@ public class TextReportProcessingStrategy implements UpgradeProcessingStrategy {
3831

3932
final protected File pomFile;
4033
final protected Configuration configuration;
41-
final protected Set<ModifiedProperty> recordedUpdates = new HashSet<>();
4234
protected PrintStream outputStream;
4335
protected File outputFile;
4436

@@ -83,6 +75,7 @@ public boolean process(List<ComponentUpgrade> upgrades) {
8375
List<ComponentUpgrade> sortedUpgrades =
8476
upgrades.stream().sorted(new ComponentUpgradeComparator())
8577
.collect(Collectors.toList());
78+
List<ComponentUpgrade> aggregatedUpgrades = ComponentUpgradeAggregator.aggregateComponentUpgrades(pomFile, sortedUpgrades);
8679

8780
outputStream.println("Generated at " + DATE_TIME_FORMATTER.format(ZonedDateTime.now()));
8881
outputStream.println();
@@ -93,33 +86,18 @@ public boolean process(List<ComponentUpgrade> upgrades) {
9386
outputStream.println();
9487
outputStream.println("Possible upgrades:\n");
9588

96-
int counter = 0;
97-
URI uri = pomFile.toURI();
98-
for (ComponentUpgrade upgrade : sortedUpgrades) {
99-
Optional<LocatedDependency> locatedDependencyOpt =
100-
PomDependencyUpdater.locateDependency(pomFile, upgrade.getArtifact());
89+
for (ComponentUpgrade upgrade : aggregatedUpgrades) {
10190
ArtifactRef artifact = upgrade.getArtifact();
102-
String newVersion = upgrade.getNewVersion();
103-
String repoId = upgrade.getRepository();
104-
105-
if (locatedDependencyOpt.isPresent()) {
106-
LocatedDependency locatedDependency = locatedDependencyOpt.get();
107-
boolean added = recordedUpdates.add(
108-
new ModifiedProperty(uri, locatedDependency.getProfile(), locatedDependency.getVersionProperty(), newVersion));
109-
if (added) {
110-
counter++;
111-
outputStream.println(
112-
String.format("%s:%s:%s -> %s (%s) - %s",
113-
artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersionString(),
114-
newVersion, repoId,
115-
upgrade.getFirstSeen() == null ?
116-
"new" : "since " + upgrade.getFirstSeen().format(DATE_FORMATTER)
117-
));
118-
}
119-
}
91+
outputStream.println(
92+
String.format("%s:%s:%s -> %s (%s) - %s",
93+
artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersionString(),
94+
upgrade.getNewVersion(), upgrade.getRepository(),
95+
upgrade.getFirstSeen() == null ?
96+
"new" : "since " + upgrade.getFirstSeen().format(DATE_FORMATTER)
97+
));
12098
}
12199

122-
outputStream.println("\n" + counter + " items");
100+
outputStream.println("\n" + aggregatedUpgrades.size() + " items");
123101
outputStream.println("\nReport generated by Maven Dependency Updater");
124102
outputStream.println(PROJECT_URL);
125103
return true;
@@ -132,68 +110,24 @@ public boolean process(List<ComponentUpgrade> upgrades) {
132110
}
133111
}
134112

135-
/**
136-
* Identifies a property in a particular POM file and a profile.
137-
*
138-
* It's used to record properties that would be changed by a component upgrade, to detect that given property would
139-
* have already been changed by a previous component upgrade.
140-
*/
141-
protected static class ModifiedProperty {
142-
143-
final private URI pomUri;
144-
final private String profile;
145-
final private String propertyName;
146-
final private String newValue;
147-
148-
ModifiedProperty(URI pomUri, String profile, String propertyName, String newValue) {
149-
this.pomUri = pomUri;
150-
this.profile = profile;
151-
this.propertyName = propertyName;
152-
this.newValue = newValue;
153-
}
154-
155-
@Override
156-
public boolean equals(Object o) {
157-
if (this == o) return true;
158-
159-
if (o == null || getClass() != o.getClass()) return false;
160-
161-
ModifiedProperty that = (ModifiedProperty) o;
162-
163-
return new EqualsBuilder()
164-
.append(pomUri, that.pomUri)
165-
.append(profile, that.profile)
166-
.append(propertyName, that.propertyName)
167-
.append(newValue, that.newValue)
168-
.isEquals();
169-
}
170-
171-
@Override
172-
public int hashCode() {
173-
return new HashCodeBuilder(17, 37)
174-
.append(pomUri)
175-
.append(profile)
176-
.append(propertyName)
177-
.append(newValue)
178-
.toHashCode();
179-
}
180-
}
181-
182113
/**
183114
* Comparator for sorting component upgrades. Sort primarily by first seen date, then alphabetically.
184115
*/
185116
protected static class ComponentUpgradeComparator implements Comparator<ComponentUpgrade> {
186117
@Override
187118
public int compare(ComponentUpgrade o1, ComponentUpgrade o2) {
119+
// order null values first
188120
if (o1.getFirstSeen() == null && o2.getFirstSeen() == null) {
189121
return o1.getArtifact().compareTo(o2.getArtifact());
190122
} else if (o1.getFirstSeen() == null) {
191-
return 1;
192-
} else if (o2.getFirstSeen() == null) {
193123
return -1;
124+
} else if (o2.getFirstSeen() == null) {
125+
return 1;
194126
} else {
195-
int res = o1.getFirstSeen().toLocalDate().compareTo(o2.getFirstSeen().toLocalDate());
127+
// order by age from new to old
128+
int res = -o1.getFirstSeen().toLocalDate().compareTo(o2.getFirstSeen().toLocalDate());
196129
if (res == 0) {
130+
// when the date is the same, order alphabetically
197131
res = o1.getArtifact().toString().compareTo(o2.getArtifact().toString());
198132
}
199133
return res;

0 commit comments

Comments
 (0)