|
9 | 9 | import java.util.List;
|
10 | 10 | import java.util.Set;
|
11 | 11 | import java.util.stream.Collectors;
|
12 |
| -import java.util.stream.Stream; |
13 | 12 | import javax.xml.xpath.XPathExpressionException;
|
14 | 13 |
|
15 | 14 | import com.oracle.weblogic.imagetool.aru.AruException;
|
16 | 15 | import com.oracle.weblogic.imagetool.aru.AruPatch;
|
17 | 16 | import com.oracle.weblogic.imagetool.aru.AruUtil;
|
| 17 | +import com.oracle.weblogic.imagetool.aru.NoPatchesFoundException; |
| 18 | +import com.oracle.weblogic.imagetool.aru.VersionNotFoundException; |
18 | 19 | import com.oracle.weblogic.imagetool.logging.LoggingFacade;
|
19 | 20 | import com.oracle.weblogic.imagetool.logging.LoggingFactory;
|
20 | 21 | import com.oracle.weblogic.imagetool.util.Utils;
|
@@ -53,32 +54,44 @@ public static OPatchFile getInstance(String patchId, String userid, String passw
|
53 | 54 |
|
54 | 55 | List<AruPatch> patches = AruUtil.rest().getPatches(patchNumber, userid, password);
|
55 | 56 | if (!isOffline(userid, password)) {
|
56 |
| - // if working online with ARU metadata, filter results based on availability and ARU recommendation |
57 |
| - Stream<AruPatch> filteredList = patches.stream().filter(AruPatch::isOpenAccess); |
| 57 | + // if working online with ARU metadata, filter results based on access flag (discard protected versions) |
| 58 | + patches = patches.stream().filter(AruPatch::isOpenAccess).collect(Collectors.toList()); |
| 59 | + logger.fine("Found {0} OPatch versions for id {1}", patches.size(), patchNumber); |
| 60 | + } else { |
| 61 | + // if working offline, update the placeholder in the list to have the provided version or the latest cached |
| 62 | + AruPatch offlinePatch = patches.get(0); |
58 | 63 | if (providedVersion == null) {
|
59 |
| - // When the user did not provide a specific version, try to reduce the patch list to one patch. |
60 |
| - // There should only be ONE recommended patch/install for OPatch |
61 |
| - filteredList = filteredList.filter(AruPatch::isRecommended); |
| 64 | + offlinePatch.version(getLatestCachedVersion(cache, patchNumber)); |
| 65 | + } else { |
| 66 | + offlinePatch.version(providedVersion); |
62 | 67 | }
|
63 |
| - patches = filteredList.collect(Collectors.toList()); |
64 | 68 | }
|
65 | 69 |
|
66 | 70 | // For the OPatch use case, only the provided version (--opatchBugNumber) is used to "select" a patch.
|
67 | 71 | // selectPatch will return null if there is more than one patch in the patches list.
|
68 |
| - AruPatch selectedPatch = AruPatch.selectPatch(patches, providedVersion, null, null); |
69 |
| - |
70 |
| - if (selectedPatch != null) { |
71 |
| - // if offline, find the latest OPatch version available in the cache |
72 |
| - if (isOffline(userid, password) && providedVersion == null) { |
73 |
| - selectedPatch.version(getLatestCachedVersion(cache, patchNumber)); |
| 72 | + //AruPatch selectedPatch = AruPatch.selectPatch(patches, providedVersion, null, null); |
| 73 | + |
| 74 | + AruPatch selectedPatch; |
| 75 | + if (patches.isEmpty()) { |
| 76 | + throw new NoPatchesFoundException(Utils.getMessage("IMG-0057", patchNumber)); |
| 77 | + } else if (providedVersion != null) { |
| 78 | + String finalProvidedVersion = providedVersion; |
| 79 | + selectedPatch = patches.stream() |
| 80 | + .filter(p -> finalProvidedVersion.equals(p.version())).findAny().orElse(null); |
| 81 | + if (selectedPatch == null) { |
| 82 | + logger.severe("IMG-0101", providedVersion); |
| 83 | + throw new VersionNotFoundException(patchNumber, providedVersion, patches); |
74 | 84 | }
|
75 | 85 | } else {
|
76 |
| - // select first recommended patch |
| 86 | + // Sort the patches list from highest to lowest (newest to oldest) |
77 | 87 | List<AruPatch> sortedList = patches.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
|
78 |
| - selectedPatch = sortedList.get(0); |
79 |
| - logger.warning("IMG-0057", selectedPatch.version(), sortedList.stream() |
80 |
| - .map(s -> s.patchId() + "_" + s.version()) |
81 |
| - .collect(Collectors.joining(", "))); |
| 88 | + // Select the first recommended patch, if there is one marked as recommended |
| 89 | + selectedPatch = sortedList.stream().filter(AruPatch::isRecommended).findFirst().orElse(null); |
| 90 | + if (selectedPatch == null) { |
| 91 | + // nothing is marked as recommended, return the newest (highest numbered) patch |
| 92 | + selectedPatch = sortedList.stream().findFirst().orElse(null); |
| 93 | + // since patches list cannot be empty here, this findFirst should never return null |
| 94 | + } |
82 | 95 | }
|
83 | 96 | logger.exiting(selectedPatch);
|
84 | 97 | return new OPatchFile(selectedPatch, userid, password);
|
|
0 commit comments