Skip to content

Commit d6db2ce

Browse files
committed
The storage of cache.dir in the metadata file was removed
1 parent fcf1680 commit d6db2ce

File tree

19 files changed

+161
-165
lines changed

19 files changed

+161
-165
lines changed

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

Lines changed: 14 additions & 35 deletions
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.cachestore;
@@ -13,7 +13,6 @@
1313
import java.nio.file.Path;
1414
import java.nio.file.Paths;
1515
import java.time.LocalDateTime;
16-
import java.util.HashSet;
1716
import java.util.Map;
1817
import java.util.Objects;
1918
import java.util.Properties;
@@ -31,29 +30,20 @@ public class FileCacheStore implements CacheStore {
3130
private static final LoggingFacade logger = LoggingFactory.getLogger(FileCacheStore.class);
3231

3332
private final Properties properties = new Properties();
34-
private final String metadataPath;
33+
private final File metadataFile;
34+
private final String cacheDir;
3535

3636
FileCacheStore() throws CacheStoreException {
3737
try {
38-
String userCacheDir = initCacheDir();
39-
metadataPath = userCacheDir + File.separator + Constants.DEFAULT_META_FILE;
40-
File metadataFile = new File(metadataPath);
38+
cacheDir = initCacheDir();
39+
metadataFile = Paths.get(cacheDir, Constants.DEFAULT_META_FILE).toFile();
4140
if (metadataFile.exists() && metadataFile.isFile()) {
4241
loadProperties(metadataFile);
4342
} else {
4443
if (!metadataFile.createNewFile()) {
4544
throw new IOException("Failed to create file cache metadata file " + metadataFile.getName());
4645
}
4746
}
48-
if (properties.getProperty(Constants.CACHE_DIR_KEY) == null) {
49-
properties.put(Constants.CACHE_DIR_KEY, userCacheDir);
50-
persistToDisk();
51-
}
52-
File cacheDir = new File(properties.getProperty(Constants.CACHE_DIR_KEY));
53-
if (!cacheDir.exists() && !cacheDir.mkdirs()) {
54-
// the cache directory did not exist, and the mkdirs failed to create it
55-
throw new IOException("Failed to create cache directory: " + cacheDir.getName());
56-
}
5747
} catch (IOException e) {
5848
CacheStoreException error =
5949
new CacheStoreException("Failed to establish a cache store on the filesystem", e);
@@ -64,7 +54,7 @@ public class FileCacheStore implements CacheStore {
6454

6555
@Override
6656
public String getCacheDir() {
67-
return properties.getProperty(Constants.CACHE_DIR_KEY);
57+
return cacheDir;
6858
}
6959

7060
@Override
@@ -92,9 +82,6 @@ public void addToCache(String key, String value) throws CacheStoreException {
9282
@Override
9383
public String deleteFromCache(String key) throws CacheStoreException {
9484
Objects.requireNonNull(key, Utils.getMessage("IMG-0066"));
95-
if (Constants.CACHE_DIR_KEY.equalsIgnoreCase(key)) {
96-
return properties.getProperty(Constants.CACHE_DIR_KEY, null);
97-
}
9885
String oldValue = (String) properties.remove(key.toLowerCase());
9986
if (oldValue != null) {
10087
persistToDisk();
@@ -104,12 +91,7 @@ public String deleteFromCache(String key) throws CacheStoreException {
10491

10592
@Override
10693
public void clearCache() throws CacheStoreException {
107-
// remove all cache entries except the cache directory
108-
for (Object key: new HashSet<>(properties.keySet())) {
109-
if (!key.equals(Constants.CACHE_DIR_KEY)) {
110-
properties.remove(key);
111-
}
112-
}
94+
properties.clear();
11395
persistToDisk();
11496
}
11597

@@ -124,7 +106,7 @@ public Map<String, String> getCacheItems() {
124106
private void persistToDisk() throws CacheStoreException {
125107
logger.entering();
126108
synchronized (properties) {
127-
try (FileOutputStream outputStream = new FileOutputStream(metadataPath)) {
109+
try (FileOutputStream outputStream = new FileOutputStream(metadataFile)) {
128110
properties.store(outputStream, "changed on:" + LocalDateTime.now());
129111
} catch (IOException e) {
130112
CacheStoreException error = new CacheStoreException("Could not persist cache file", e);
@@ -143,8 +125,6 @@ private void loadProperties(File propsFile) {
143125
} else {
144126
Properties tmpProperties = new Properties();
145127
tmpProperties.load(bufferedReader);
146-
// Do not let cache.dir to be modified outside setCacheDir
147-
tmpProperties.remove(Constants.CACHE_DIR_KEY);
148128
tmpProperties.forEach((key, value) -> properties.put(((String) key).toLowerCase(), value));
149129
}
150130
} catch (IOException e) {
@@ -154,19 +134,18 @@ private void loadProperties(File propsFile) {
154134
logger.exiting();
155135
}
156136

137+
private static String defaultCacheDir() {
138+
return System.getProperty("user.home") + File.separator + "cache";
139+
}
140+
157141
/**
158142
* Initialize the cache store directory.
159143
*
160144
* @return cache directory
161145
*/
162146
private static String initCacheDir() throws IOException {
163-
String cacheDirStr = System.getenv(CACHEDIR);
164-
if (cacheDirStr == null) {
165-
cacheDirStr = System.getProperty(CACHEDIR);
166-
}
167-
if (cacheDirStr == null) {
168-
cacheDirStr = System.getProperty("user.home") + File.separator + "cache";
169-
}
147+
String cacheDirStr = Utils.getEnvironmentProperty(CACHEDIR, FileCacheStore::defaultCacheDir);
148+
170149
Path cacheDir = Paths.get(cacheDirStr);
171150

172151
boolean pathExists = Files.exists(cacheDir, LinkOption.NOFOLLOW_LINKS);

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/cache/DeleteEntry.java

Lines changed: 2 additions & 4 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, 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.cli.cache;
@@ -20,9 +20,7 @@ public class DeleteEntry extends CacheOperation {
2020
@Override
2121
public CommandResponse call() throws Exception {
2222
if (!Utils.isEmptyString(key)) {
23-
if (Constants.CACHE_DIR_KEY.equalsIgnoreCase(key)) {
24-
return CommandResponse.success("Cannot delete key: " + key);
25-
} else if (Constants.DELETE_ALL_FOR_SURE.equalsIgnoreCase(key)) {
23+
if (Constants.DELETE_ALL_FOR_SURE.equalsIgnoreCase(key)) {
2624
cache().clearCache();
2725
return CommandResponse.success("IMG-0046");
2826
} else {

imagetool/src/main/java/com/oracle/weblogic/imagetool/cli/cache/ListCacheItems.java

Lines changed: 1 addition & 8 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, 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.cli.cache;
@@ -8,8 +8,6 @@
88

99
import com.oracle.weblogic.imagetool.api.model.CommandResponse;
1010
import com.oracle.weblogic.imagetool.cachestore.CacheStoreException;
11-
import com.oracle.weblogic.imagetool.util.Constants;
12-
import com.oracle.weblogic.imagetool.util.Utils;
1311
import picocli.CommandLine.Command;
1412
import picocli.CommandLine.Option;
1513

@@ -28,11 +26,6 @@ public CommandResponse call() throws CacheStoreException {
2826
return CommandResponse.success("IMG-0047");
2927
} else {
3028
System.out.println("Cache contents");
31-
String cacheDir = resultMap.getOrDefault(Constants.CACHE_DIR_KEY, null);
32-
if (!Utils.isEmptyString(cacheDir)) {
33-
System.out.println(Constants.CACHE_DIR_KEY + "=" + cacheDir);
34-
resultMap.remove(Constants.CACHE_DIR_KEY);
35-
}
3629

3730
Pattern pattern = Pattern.compile(key == null ? ".*" : key);
3831
resultMap.entrySet().stream()

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

Lines changed: 3 additions & 4 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, 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;
@@ -10,14 +10,13 @@
1010
public final class Constants {
1111

1212
public static final String ARU_UPDATES_HOST =
13-
Utils.getEnvironmentProperty("WLSIMG_ARU_HOST", "updates.oracle.com");
13+
Utils.getEnvironmentProperty("WLSIMG_ARU_HOST", () -> "updates.oracle.com");
1414
public static final String ARU_REST_URL = "https://" + ARU_UPDATES_HOST + "/Orion/Services";
1515
public static final String REL_URL = ARU_REST_URL + "/metadata?table=aru_releases";
1616
public static final String RECOMMENDED_PATCHES_URL = ARU_REST_URL
1717
+ "/search?patch_type=all&life_cycle=Recommended&product=%s&release=%s";
1818
public static final String ARU_LANG_URL = ARU_REST_URL + "/metadata?table=aru_languages";
1919
public static final String CONFLICTCHECKER_URL = ARU_REST_URL + "/conflict_checks";
20-
public static final String CACHE_DIR_KEY = "cache.dir";
2120
public static final String DEFAULT_WLS_VERSION = "12.2.1.3.0";
2221
public static final String DEFAULT_JDK_VERSION = "8u202";
2322
public static final String DEFAULT_META_FILE = ".metadata";
@@ -29,7 +28,7 @@ public final class Constants {
2928
public static final String BUSYBOX = "busybox";
3029
public static final List<String> BUSYBOX_OS_IDS = Collections.unmodifiableList(Arrays.asList("bb", "alpine"));
3130
public static final String ORACLE_LINUX = "ghcr.io/oracle/oraclelinux:8-slim";
32-
public static final String BUILDER_DEFAULT = Utils.getEnvironmentProperty("WLSIMG_BUILDER", "docker");
31+
public static final String BUILDER_DEFAULT = Utils.getEnvironmentProperty("WLSIMG_BUILDER", () -> "docker");
3332

3433
private Constants() {
3534
//restrict access

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.ResourceBundle;
3232
import java.util.Set;
3333
import java.util.function.Predicate;
34+
import java.util.function.Supplier;
3435
import java.util.regex.Matcher;
3536
import java.util.regex.Pattern;
3637
import java.util.stream.Collectors;
@@ -545,13 +546,13 @@ public static String getPasswordFromInputs(String passwordStr, Path passwordFile
545546
* @param defaultValue if no environment variable is defined, nor system property, return this value
546547
* @return the value defined in the env or system property
547548
*/
548-
public static String getEnvironmentProperty(String name, String defaultValue) {
549+
public static String getEnvironmentProperty(String name, Supplier<String> defaultValue) {
549550
String result = System.getenv(name);
550551
if (isEmptyString(result)) {
551552
result = System.getProperty(name);
552553
}
553554
if (isEmptyString(result)) {
554-
return defaultValue;
555+
return defaultValue.get();
555556
}
556557
return result;
557558
}
@@ -562,7 +563,7 @@ public static String getEnvironmentProperty(String name, String defaultValue) {
562563
* @return working directory
563564
*/
564565
public static String getBuildWorkingDir() throws IOException {
565-
String workingDir = getEnvironmentProperty("WLSIMG_BLDDIR", System.getProperty("user.home"));
566+
String workingDir = getEnvironmentProperty("WLSIMG_BLDDIR", () -> System.getProperty("user.home"));
566567
Path path = Paths.get(workingDir);
567568

568569
boolean pathExists = Files.exists(path, LinkOption.NOFOLLOW_LINKS);

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
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.io.IOException;
77
import java.util.ArrayList;
88
import java.util.List;
9-
import java.util.logging.Level;
109
import java.util.stream.Collectors;
1110

1211
import com.oracle.weblogic.imagetool.ResourceUtils;
1312
import com.oracle.weblogic.imagetool.installer.FmwInstallerType;
14-
import com.oracle.weblogic.imagetool.logging.LoggingFacade;
15-
import com.oracle.weblogic.imagetool.logging.LoggingFactory;
13+
import com.oracle.weblogic.imagetool.test.annotations.ReduceTestLogging;
1614
import org.junit.jupiter.api.AfterAll;
1715
import org.junit.jupiter.api.BeforeAll;
1816
import org.junit.jupiter.api.Tag;
@@ -23,22 +21,17 @@
2321
import static org.junit.jupiter.api.Assertions.assertThrows;
2422
import static org.junit.jupiter.api.Assertions.assertTrue;
2523

24+
@ReduceTestLogging(loggerClass = AruUtil.class)
2625
@Tag("unit")
2726
class AruUtilTest {
28-
private static final LoggingFacade logger = LoggingFactory.getLogger(AruUtil.class);
29-
private static Level oldLevel;
30-
3127
@BeforeAll
3228
static void setUp() throws NoSuchFieldException, IllegalAccessException {
33-
oldLevel = logger.getLevel();
34-
logger.setLevel(Level.SEVERE);
3529
// insert test class into AruUtil to intercept REST calls to ARU
3630
MockAruUtil.insertMockAruInstance(new TestAruUtil());
3731
}
3832

3933
@AfterAll
4034
static void tearDown() throws NoSuchFieldException, IllegalAccessException {
41-
logger.setLevel(oldLevel);
4235
MockAruUtil.removeMockAruInstance();
4336
}
4437

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

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@
99
import java.nio.file.Path;
1010
import java.util.Arrays;
1111
import java.util.List;
12-
import java.util.logging.Level;
1312
import javax.xml.xpath.XPathExpressionException;
1413

1514
import com.oracle.weblogic.imagetool.api.model.CachedFile;
1615
import com.oracle.weblogic.imagetool.aru.AruException;
1716
import com.oracle.weblogic.imagetool.installer.InstallerType;
18-
import com.oracle.weblogic.imagetool.logging.LoggingFacade;
19-
import com.oracle.weblogic.imagetool.logging.LoggingFactory;
17+
import com.oracle.weblogic.imagetool.test.annotations.ReduceTestLogging;
2018
import com.oracle.weblogic.imagetool.util.BuildPlatform;
2119
import org.junit.jupiter.api.BeforeAll;
2220
import org.junit.jupiter.api.Tag;
@@ -31,6 +29,7 @@
3129
import static org.junit.jupiter.api.Assertions.assertThrows;
3230

3331
@Tag("unit")
32+
@ReduceTestLogging(loggerClass = CachedFile.class)
3433
class CachedFileTest {
3534

3635
static Path cacheDir;
@@ -139,19 +138,12 @@ void resolveFallbackToLocalArch() throws IOException {
139138

140139
@Test
141140
void copyFile(@TempDir Path contextDir) throws Exception {
142-
LoggingFacade logger = LoggingFactory.getLogger(CachedFile.class);
143-
Level oldLevel = logger.getLevel();
144-
logger.setLevel(Level.OFF);
145-
try {
146-
CachedFile wlsInstallerFile = new CachedFile(InstallerType.WLS, ver12213);
147-
// copy the file from the cache store to the fake build context directory
148-
Path result = wlsInstallerFile.copyFile(cacheStore, contextDir.toString());
149-
// check to see if the file was copied correctly by examining the contents of the resulting file
150-
assertLinesMatch(fileContents, Files.readAllLines(result),
151-
"copied file contents do not match source");
152-
} finally {
153-
logger.setLevel(oldLevel);
154-
}
141+
CachedFile wlsInstallerFile = new CachedFile(InstallerType.WLS, ver12213);
142+
// copy the file from the cache store to the fake build context directory
143+
Path result = wlsInstallerFile.copyFile(cacheStore, contextDir.toString());
144+
// check to see if the file was copied correctly by examining the contents of the resulting file
145+
assertLinesMatch(fileContents, Files.readAllLines(result),
146+
"copied file contents do not match source");
155147
}
156148

157149
@Test

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

Lines changed: 6 additions & 2 deletions
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.cachestore;
@@ -48,7 +48,7 @@ void checkValueInCache() {
4848
assertTrue(cache().containsKey(testKey)),
4949
"containsKey failed to find key or value that was just added");
5050

51-
// check (another way) that the value that was just added
51+
// check (another way) that the value was just added
5252
assertDoesNotThrow(() ->
5353
assertEquals(testVal, cache().getValueFromCache(testKey), "Found unexpected value in cache"),
5454
"Get from cache threw an exception");
@@ -73,5 +73,9 @@ void verifyCacheSize() {
7373
assertDoesNotThrow(() ->
7474
assertNotNull(cache().getCacheItems(), "Get cache items should never be null"),
7575
"getCacheItems threw an exception");
76+
77+
assertDoesNotThrow(() ->
78+
assertEquals(0, cache().getCacheItems().size(), "Get cache items should never be null"),
79+
"getCacheItems threw an exception");
7680
}
7781
}

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

Lines changed: 4 additions & 1 deletion
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;
@@ -11,6 +11,8 @@
1111

1212
import com.oracle.weblogic.imagetool.aru.AruException;
1313
import com.oracle.weblogic.imagetool.aru.MockAruUtil;
14+
import com.oracle.weblogic.imagetool.cli.menu.CommonOptions;
15+
import com.oracle.weblogic.imagetool.test.annotations.ReduceTestLogging;
1416
import org.junit.jupiter.api.AfterAll;
1517
import org.junit.jupiter.api.BeforeAll;
1618
import org.junit.jupiter.api.Tag;
@@ -22,6 +24,7 @@
2224
import static org.junit.jupiter.api.Assertions.assertNotNull;
2325
import static org.junit.jupiter.api.Assertions.assertTrue;
2426

27+
@ReduceTestLogging(loggerClass = CommonOptions.class)
2528
@Tag("unit")
2629
class OPatchFileTest {
2730
private static CacheStore cacheStore;

0 commit comments

Comments
 (0)