Skip to content

Commit eec89bb

Browse files
committed
Cleaned up patch version comparison to simplify code complexity
1 parent 6d5713a commit eec89bb

File tree

12 files changed

+321
-262
lines changed

12 files changed

+321
-262
lines changed

imagetool/src/main/java/com/oracle/weblogic/imagetool/aru/AruPatch.java

+5-33
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
// Copyright (c) 2020, 2022, Oracle and/or its affiliates.
1+
// Copyright (c) 2020, 2024, Oracle and/or its affiliates.
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33

44
package com.oracle.weblogic.imagetool.aru;
55

66
import java.util.List;
77
import java.util.Map;
8-
import java.util.Objects;
98
import java.util.stream.Collectors;
109
import java.util.stream.Stream;
1110
import javax.xml.xpath.XPathExpressionException;
@@ -22,11 +21,11 @@
2221
* Metadata for a patch, as defined by ARU.
2322
* Simple bean for holding metadata obtained from ARU for a given patch ID and version.
2423
*/
25-
public class AruPatch implements Comparable<AruPatch> {
24+
public class AruPatch {
2625
private static final LoggingFacade logger = LoggingFactory.getLogger(AruPatch.class);
2726

2827
private String patchId;
29-
private Version version;
28+
private String version;
3029
private String description;
3130
private String product;
3231
private String release;
@@ -51,15 +50,11 @@ public AruPatch patchId(String value) {
5150
* @return The string value of the version found in ARU.
5251
*/
5352
public String version() {
54-
if (version != null) {
55-
return version.toString();
56-
} else {
57-
return null;
58-
}
53+
return version;
5954
}
6055

6156
public AruPatch version(String value) {
62-
version = new Version(value);
57+
version = value;
6358
return this;
6459
}
6560

@@ -294,27 +289,4 @@ private static AruPatch selectPatchOffline(List<AruPatch> patches, String provid
294289
public String toString() {
295290
return patchId + " - " + description;
296291
}
297-
298-
@Override
299-
public int compareTo(AruPatch obj) {
300-
return version.compareTo(obj.version);
301-
}
302-
303-
@Override
304-
public boolean equals(Object o) {
305-
if (this == o) {
306-
return true;
307-
}
308-
if (o == null || getClass() != o.getClass()) {
309-
return false;
310-
}
311-
AruPatch aruPatch = (AruPatch) o;
312-
return Objects.equals(patchId, aruPatch.patchId) && Objects.equals(version, aruPatch.version)
313-
&& Objects.equals(release, aruPatch.release);
314-
}
315-
316-
@Override
317-
public int hashCode() {
318-
return Objects.hash(patchId, version, release);
319-
}
320292
}

imagetool/src/main/java/com/oracle/weblogic/imagetool/aru/Version.java

-91
This file was deleted.

imagetool/src/main/java/com/oracle/weblogic/imagetool/cachestore/OPatchFile.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020, 2022, Oracle and/or its affiliates.
1+
// Copyright (c) 2020, 2024, Oracle and/or its affiliates.
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33

44
package com.oracle.weblogic.imagetool.cachestore;
@@ -110,8 +110,11 @@ private static AruPatch getAruPatchOnline(String patchNumber, String providedVer
110110
throw new VersionNotFoundException(patchNumber, providedVersion, patches);
111111
}
112112
} else {
113-
// Select the newest (highest numbered) patch
114-
selectedPatch = patches.stream().max(Comparator.naturalOrder()).orElse(null);
113+
// Compare the ARU OPatch patches using the patch version field, like 12.2.1.4.0
114+
Comparator<AruPatch> patchVersionComparator =
115+
Comparator.comparing(AruPatch::version, Utils::compareVersionsNullsFirst);
116+
// Select the newest (highest version) OPatch install/patch
117+
selectedPatch = patches.stream().max(patchVersionComparator).orElse(null);
115118
}
116119
return selectedPatch;
117120
}

imagetool/src/main/java/com/oracle/weblogic/imagetool/util/HttpUtil.java

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2019, 2022, Oracle and/or its affiliates.
1+
// Copyright (c) 2019, 2024, Oracle and/or its affiliates.
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33

44
package com.oracle.weblogic.imagetool.util;
@@ -182,18 +182,12 @@ private static HttpRequestRetryHandler retryHandler() {
182182
// Do not retry if over max retries
183183
return false;
184184
}
185-
if (exception instanceof InterruptedIOException) {
186-
// Timeout
187-
return false;
188-
}
189-
if (exception instanceof UnknownHostException) {
190-
// Unknown host
191-
return false;
192-
}
193-
if (exception instanceof SSLException) {
194-
// SSL handshake failed
185+
if (exception instanceof InterruptedIOException // Timeout
186+
|| exception instanceof UnknownHostException // Unknown Host
187+
|| exception instanceof SSLException) { // SSL handshake failed
195188
return false;
196189
}
190+
197191
HttpClientContext clientContext = HttpClientContext.adapt(context);
198192
HttpRequest request = clientContext.getRequest();
199193
// return true if it is okay to retry this request type

imagetool/src/main/java/com/oracle/weblogic/imagetool/util/Utils.java

+53-35
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2019, 2021, Oracle and/or its affiliates.
1+
// Copyright (c) 2019, 2024, Oracle and/or its affiliates.
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33

44
package com.oracle.weblogic.imagetool.util;
@@ -303,6 +303,27 @@ public static void deleteFilesRecursively(String pathDir) throws IOException {
303303
logger.exiting();
304304
}
305305

306+
/**
307+
* Compares two version strings. A null-friendly comparator that considers null to be less than non-null.
308+
* Any qualifiers are treated as older than the same version without
309+
* a qualifier. If both versions have qualifiers and are otherwise equal, they are compared using
310+
* String.compareTo() to determine the result.
311+
*
312+
* @param thisVersion - first version
313+
* @param otherVersion - second version
314+
* @return returns 0 if the versions are equal, greater than zero if thisVersion is newer,
315+
* and less than zero if thisVersion is older.
316+
*/
317+
public static int compareVersionsNullsFirst(String thisVersion, String otherVersion) {
318+
if (isEmptyString(thisVersion)) {
319+
return (isEmptyString(otherVersion)) ? 0 : -1;
320+
} else if (isEmptyString(otherVersion)) {
321+
return 1;
322+
} else {
323+
return compareVersions(thisVersion, otherVersion);
324+
}
325+
}
326+
306327
/**
307328
* Compares two version strings. Any qualifiers are treated as older than the same version without
308329
* a qualifier. If both versions have qualifiers and are otherwise equal, they are compared using
@@ -314,8 +335,6 @@ public static void deleteFilesRecursively(String pathDir) throws IOException {
314335
* and less than zero if thisVersion is older.
315336
*/
316337
public static int compareVersions(String thisVersion, String otherVersion) {
317-
int result = 0;
318-
319338
if (isEmptyString(thisVersion) || isEmptyString(otherVersion)) {
320339
throw new IllegalArgumentException("cannot compare null strings");
321340
}
@@ -330,61 +349,60 @@ public static int compareVersions(String thisVersion, String otherVersion) {
330349

331350
int fieldsToCompare = Math.min(thisVersionElements.length, otherVersionElements.length);
332351

352+
int result = 0;
333353
int idx;
334354
for (idx = 0; idx < fieldsToCompare; idx++) {
335355
int thisVersionNumber = Integer.parseInt(thisVersionElements[idx]);
336356
int otherVersionNumber = Integer.parseInt(otherVersionElements[idx]);
337357

338-
if (thisVersionNumber > otherVersionNumber) {
339-
result = 1;
340-
break;
341-
} else if (thisVersionNumber < otherVersionNumber) {
342-
result = -1;
343-
break;
358+
result = Integer.compare(thisVersionNumber, otherVersionNumber);
359+
if (result != 0) {
360+
return result;
344361
}
345362
}
346363

347364
// Version fields compared so far are equal so check to see if one version number
348365
// has more fields than the other.
349366
//
350-
if (result == 0 && thisVersionElements.length != otherVersionElements.length) {
367+
if (thisVersionElements.length != otherVersionElements.length) {
351368
if (thisVersionElements.length > otherVersionElements.length) {
352369
result = 1;
353370
} else {
354371
result = -1;
355372
}
356373
}
357374

375+
if (result != 0) {
376+
return result;
377+
}
378+
358379
// Finally, look to see if one or both versions have a qualifier if they are otherwise the same.
359380
//
360-
if (result == 0) {
361-
int useCase = 0;
362-
if (thisVersion.indexOf('-') != -1) {
363-
useCase += 1;
364-
}
365-
if (otherVersion.indexOf('-') != -1) {
366-
useCase += 2;
367-
}
368-
switch (useCase) {
369-
case 0:
370-
break;
381+
int useCase = 0;
382+
if (thisVersion.indexOf('-') != -1) {
383+
useCase += 1;
384+
}
385+
if (otherVersion.indexOf('-') != -1) {
386+
useCase += 2;
387+
}
388+
switch (useCase) {
389+
case 1:
390+
result = -1;
391+
break;
371392

372-
case 1:
373-
result = -1;
374-
break;
393+
case 2:
394+
result = 1;
395+
break;
375396

376-
case 2:
377-
result = 1;
378-
break;
397+
case 3:
398+
String thisQualifier = thisVersion.substring(thisVersion.indexOf('-'));
399+
String otherQualifier = otherVersion.substring(otherVersion.indexOf('-'));
400+
result = thisQualifier.compareTo(otherQualifier);
401+
break;
379402

380-
case 3:
381-
String thisQualifier = thisVersion.substring(thisVersion.indexOf('-'));
382-
String otherQualifier = otherVersion.substring(otherVersion.indexOf('-'));
383-
result = thisQualifier.compareTo(otherQualifier);
384-
break;
385-
default:
386-
break;
387-
}
403+
case 0:
404+
default:
405+
break;
388406
}
389407
return result;
390408
}

0 commit comments

Comments
 (0)