Skip to content

Commit f27387b

Browse files
authored
allow new OPatch versions in local cache to be located when working online (#362)
1 parent d0c38cd commit f27387b

File tree

11 files changed

+201
-90
lines changed

11 files changed

+201
-90
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ private String getReleaseNumber(AruProduct product, String version, String userI
344344
throw new AruException("Could not extract release number with XPath", xpe);
345345
}
346346
if (Utils.isEmptyString(result)) {
347-
String msg = Utils.getMessage("IMG-0082", version, product);
347+
String msg = Utils.getMessage("IMG-0082", version, product.description());
348348
logger.info(msg);
349349
throw new ReleaseNotFoundException(msg);
350350
}

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

Lines changed: 3 additions & 5 deletions
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, 2022, 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;
@@ -31,14 +31,12 @@ public interface CacheStore {
3131
String getValueFromCache(String key);
3232

3333
/**
34-
* Checks if cache has certain key, value combination. This is used to check if a certain artifact
35-
* is in the desired location if it has been downloaded previously.
34+
* Checks if cache contains the specified key.
3635
*
3736
* @param key artifact identifier
38-
* @param value location on disk
3937
* @return true if found
4038
*/
41-
boolean hasMatchingKeyValue(String key, String value);
39+
boolean containsKey(String key);
4240

4341
/**
4442
* Add an entry to the cache metadata file.

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

Lines changed: 5 additions & 5 deletions
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, 2022, 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;
@@ -31,7 +31,7 @@ public class FileCacheStore implements CacheStore {
3131
private static final LoggingFacade logger = LoggingFactory.getLogger(FileCacheStore.class);
3232

3333
private final Properties properties = new Properties();
34-
private String metadataPath;
34+
private final String metadataPath;
3535

3636
FileCacheStore() throws CacheStoreException {
3737
try {
@@ -74,11 +74,11 @@ public String getValueFromCache(String key) {
7474
}
7575

7676
@Override
77-
public boolean hasMatchingKeyValue(String key, String value) {
78-
if (key == null || value == null) {
77+
public boolean containsKey(String key) {
78+
if (key == null) {
7979
return false;
8080
}
81-
return value.equals(properties.getProperty(key.toLowerCase()));
81+
return properties.containsKey(key.toLowerCase());
8282
}
8383

8484
@Override

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

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020, 2021, Oracle and/or its affiliates.
1+
// Copyright (c) 2020, 2022, 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;
@@ -52,30 +52,61 @@ public static OPatchFile getInstance(String patchId, String userid, String passw
5252
logger.fine("User provided OPatch version {0} {1}", patchNumber, providedVersion);
5353
}
5454

55-
List<AruPatch> patches = AruUtil.rest().getPatches(patchNumber, userid, password);
56-
if (!isOffline(userid, password)) {
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);
55+
AruPatch selectedPatch;
56+
if (isOffline(userid, password)) {
57+
selectedPatch = getAruPatchOffline(patchNumber, providedVersion, cache);
6058
} 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);
63-
if (providedVersion == null) {
64-
offlinePatch.version(getLatestCachedVersion(cache, patchNumber));
65-
} else {
66-
offlinePatch.version(providedVersion);
59+
try {
60+
selectedPatch = getAruPatchOnline(patchNumber, providedVersion, userid, password);
61+
} catch (VersionNotFoundException notFound) {
62+
// Could not find the user requested OPatch version on ARU, checking local cache before giving up
63+
if (cache.containsKey(patchId)) {
64+
logger.info("OPatch version {0} is not available online, using cached copy.", providedVersion);
65+
selectedPatch = new AruPatch().patchId(patchNumber).version(providedVersion);
66+
} else {
67+
logger.severe("IMG-0101", providedVersion);
68+
throw notFound;
69+
}
6770
}
6871
}
6972

73+
logger.exiting(selectedPatch);
74+
return new OPatchFile(selectedPatch, userid, password);
75+
}
76+
77+
private static boolean isOffline(String userid, String password) {
78+
return userid == null || password == null;
79+
}
80+
81+
private static AruPatch getAruPatchOffline(String patchNumber, String providedVersion, CacheStore cache) {
82+
// if working offline, update the placeholder in the list to have the provided version or the latest cached
83+
AruPatch offlinePatch = new AruPatch().patchId(patchNumber);
84+
if (Utils.isEmptyString(providedVersion)) {
85+
// user did not request a specific version, find the latest version of OPatch in the cache
86+
offlinePatch.version(getLatestCachedVersion(cache, patchNumber));
87+
} else {
88+
offlinePatch.version(providedVersion);
89+
}
90+
91+
return offlinePatch;
92+
}
93+
94+
private static AruPatch getAruPatchOnline(String patchNumber, String providedVersion,
95+
String userid, String password)
96+
throws XPathExpressionException, IOException, AruException {
97+
98+
List<AruPatch> patches = AruUtil.rest().getPatches(patchNumber, userid, password);
99+
// filter ARU results based on access flag (discard protected versions)
100+
patches = patches.stream().filter(AruPatch::isOpenAccess).collect(Collectors.toList());
101+
logger.fine("Found {0} OPatch versions for id {1}", patches.size(), patchNumber);
102+
70103
AruPatch selectedPatch;
71104
if (patches.isEmpty()) {
72105
throw new NoPatchesFoundException(Utils.getMessage("IMG-0057", patchNumber));
73106
} else if (providedVersion != null) {
74-
String finalProvidedVersion = providedVersion;
75107
selectedPatch = patches.stream()
76-
.filter(p -> finalProvidedVersion.equals(p.version())).findAny().orElse(null);
108+
.filter(p -> providedVersion.equals(p.version())).findAny().orElse(null);
77109
if (selectedPatch == null) {
78-
logger.severe("IMG-0101", providedVersion);
79110
throw new VersionNotFoundException(patchNumber, providedVersion, patches);
80111
}
81112
} else {
@@ -84,12 +115,7 @@ public static OPatchFile getInstance(String patchId, String userid, String passw
84115
// Select the newest (highest numbered) patch
85116
selectedPatch = sortedList.stream().findFirst().orElse(null);
86117
}
87-
logger.exiting(selectedPatch);
88-
return new OPatchFile(selectedPatch, userid, password);
89-
}
90-
91-
private static boolean isOffline(String userid, String password) {
92-
return userid == null || password == null;
118+
return selectedPatch;
93119
}
94120

95121
/**

imagetool/src/main/resources/ImageTool.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ IMG-0078=Starting build: {0}
8080
IMG-0079=OS Package Manager override, changed from {0} to {1}
8181
IMG-0080=NOT USED
8282
IMG-0081=Unable to retrieve list of Oracle releases from Oracle Updates (ARU). Try again later.
83-
IMG-0082=Version {0} did not contain {1}, skipping {1}
83+
IMG-0082=Version {0} did not contain {1}, skipping
8484
IMG-0083=Version {0} for patch ID {1} was not found in the available versions: {2}
8585
IMG-0084=No recommended patches found for {0}
8686
IMG-0085=The recommended ADR patch was skipped for the WLS installer, use --patch {0} to apply this patch

imagetool/src/test/java/com/oracle/weblogic/imagetool/aru/AruUtilTest.java

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package com.oracle.weblogic.imagetool.aru;
55

66
import java.io.IOException;
7-
import java.lang.reflect.Field;
87
import java.util.ArrayList;
98
import java.util.List;
109
import java.util.logging.Level;
@@ -34,33 +33,19 @@ static void setUp() throws NoSuchFieldException, IllegalAccessException {
3433
oldLevel = logger.getLevel();
3534
logger.setLevel(Level.SEVERE);
3635
// insert test class into AruUtil to intercept REST calls to ARU
37-
Field aruRest = AruUtil.class.getDeclaredField("instance");
38-
aruRest.setAccessible(true);
39-
aruRest.set(aruRest, new TestAruUtil());
36+
MockAruUtil.insertMockAruInstance(new TestAruUtil());
4037
}
4138

4239
@AfterAll
43-
static void tearDown() {
40+
static void tearDown() throws NoSuchFieldException, IllegalAccessException {
4441
logger.setLevel(oldLevel);
42+
MockAruUtil.removeMockAruInstance();
4543
}
4644

4745
/**
4846
* Intercept calls to the ARU REST API during unit testing.
4947
*/
50-
public static class TestAruUtil extends AruUtil {
51-
public TestAruUtil() {
52-
}
53-
54-
@Override
55-
Document getAllReleases(String userId, String password) {
56-
try {
57-
return ResourceUtils.getXmlFromResource("/releases.xml");
58-
} catch (IOException e) {
59-
e.printStackTrace();
60-
throw new RuntimeException("failed to load releases.xml from resources", e);
61-
}
62-
}
63-
48+
public static class TestAruUtil extends MockAruUtil {
6449
@Override
6550
Document getRecommendedPatchesMetadata(AruProduct product, String releaseNumber, String userId,
6651
String password) {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) 2022, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package com.oracle.weblogic.imagetool.aru;
5+
6+
import java.io.IOException;
7+
import java.lang.reflect.Field;
8+
import java.util.Collections;
9+
import java.util.List;
10+
import javax.xml.xpath.XPathExpressionException;
11+
12+
import com.oracle.weblogic.imagetool.ResourceUtils;
13+
import org.w3c.dom.Document;
14+
15+
public class MockAruUtil extends AruUtil {
16+
/**
17+
* Intercept calls to the ARU REST API during unit testing.
18+
*/
19+
20+
@Override
21+
Document getAllReleases(String userId, String password) {
22+
try {
23+
return ResourceUtils.getXmlFromResource("/releases.xml");
24+
} catch (IOException e) {
25+
e.printStackTrace();
26+
throw new RuntimeException("failed to load releases.xml from resources", e);
27+
}
28+
}
29+
30+
@Override
31+
public List<AruPatch> getPatches(String bugNumber, String user, String password)
32+
throws IOException, XPathExpressionException {
33+
if (user == null || password == null) {
34+
// running in offline mode (no credentials to connect to ARU)
35+
return Collections.singletonList(new AruPatch().patchId(bugNumber));
36+
} else {
37+
return AruPatch.getPatches(
38+
ResourceUtils.getXmlFromResource("/patches/patch-" + bugNumber + ".xml"));
39+
}
40+
}
41+
42+
public static void insertMockAruInstance(AruUtil mockInstance) throws NoSuchFieldException, IllegalAccessException {
43+
// insert test class into AruUtil to intercept REST calls to ARU
44+
Field aruRest = AruUtil.class.getDeclaredField("instance");
45+
aruRest.setAccessible(true);
46+
aruRest.set(aruRest, mockInstance);
47+
}
48+
49+
public static void removeMockAruInstance() throws NoSuchFieldException, IllegalAccessException {
50+
// remove test class from AruUtil instance
51+
Field aruRest = AruUtil.class.getDeclaredField("instance");
52+
aruRest.setAccessible(true);
53+
aruRest.set(aruRest, null);
54+
}
55+
56+
57+
}

imagetool/src/test/java/com/oracle/weblogic/imagetool/cachestore/CacheStoreTestImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020, 2021, Oracle and/or its affiliates.
1+
// Copyright (c) 2020, 2022, 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;
@@ -27,11 +27,11 @@ public String getValueFromCache(String key) {
2727
}
2828

2929
@Override
30-
public boolean hasMatchingKeyValue(String key, String value) {
31-
if (key == null || value == null) {
30+
public boolean containsKey(String key) {
31+
if (key == null) {
3232
return false;
3333
}
34-
return value.equals(cache.get(key.toLowerCase()));
34+
return cache.containsKey(key.toLowerCase());
3535
}
3636

3737
@Override

imagetool/src/test/java/com/oracle/weblogic/imagetool/cachestore/FileCacheStoreTest.java

Lines changed: 3 additions & 3 deletions
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, 2022, 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;
@@ -45,8 +45,8 @@ void addingValueToCache() {
4545
void checkValueInCache() {
4646
// check to see if the key that was just added is there, and value matches expected value
4747
assertDoesNotThrow(() ->
48-
assertTrue(cache().hasMatchingKeyValue(testKey, testVal)),
49-
"hasMatchingKeyValue failed to find key or value that was just added");
48+
assertTrue(cache().containsKey(testKey)),
49+
"containsKey failed to find key or value that was just added");
5050

5151
// check (another way) that the value that was just added
5252
assertDoesNotThrow(() ->

0 commit comments

Comments
 (0)