Skip to content

Commit

Permalink
Merge branch 'cache-dir-property' into 'main'
Browse files Browse the repository at this point in the history
The storage of cache.dir in the metadata file was removed

See merge request weblogic-cloud/weblogic-image-tool!474
  • Loading branch information
ddsharpe committed Apr 25, 2024
2 parents fcf1680 + d6db2ce commit e5f4c31
Show file tree
Hide file tree
Showing 19 changed files with 161 additions and 165 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019, 2022, Oracle and/or its affiliates.
// Copyright (c) 2019, 2024, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.

package com.oracle.weblogic.imagetool.cachestore;
Expand All @@ -13,7 +13,6 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
Expand All @@ -31,29 +30,20 @@ public class FileCacheStore implements CacheStore {
private static final LoggingFacade logger = LoggingFactory.getLogger(FileCacheStore.class);

private final Properties properties = new Properties();
private final String metadataPath;
private final File metadataFile;
private final String cacheDir;

FileCacheStore() throws CacheStoreException {
try {
String userCacheDir = initCacheDir();
metadataPath = userCacheDir + File.separator + Constants.DEFAULT_META_FILE;
File metadataFile = new File(metadataPath);
cacheDir = initCacheDir();
metadataFile = Paths.get(cacheDir, Constants.DEFAULT_META_FILE).toFile();
if (metadataFile.exists() && metadataFile.isFile()) {
loadProperties(metadataFile);
} else {
if (!metadataFile.createNewFile()) {
throw new IOException("Failed to create file cache metadata file " + metadataFile.getName());
}
}
if (properties.getProperty(Constants.CACHE_DIR_KEY) == null) {
properties.put(Constants.CACHE_DIR_KEY, userCacheDir);
persistToDisk();
}
File cacheDir = new File(properties.getProperty(Constants.CACHE_DIR_KEY));
if (!cacheDir.exists() && !cacheDir.mkdirs()) {
// the cache directory did not exist, and the mkdirs failed to create it
throw new IOException("Failed to create cache directory: " + cacheDir.getName());
}
} catch (IOException e) {
CacheStoreException error =
new CacheStoreException("Failed to establish a cache store on the filesystem", e);
Expand All @@ -64,7 +54,7 @@ public class FileCacheStore implements CacheStore {

@Override
public String getCacheDir() {
return properties.getProperty(Constants.CACHE_DIR_KEY);
return cacheDir;
}

@Override
Expand Down Expand Up @@ -92,9 +82,6 @@ public void addToCache(String key, String value) throws CacheStoreException {
@Override
public String deleteFromCache(String key) throws CacheStoreException {
Objects.requireNonNull(key, Utils.getMessage("IMG-0066"));
if (Constants.CACHE_DIR_KEY.equalsIgnoreCase(key)) {
return properties.getProperty(Constants.CACHE_DIR_KEY, null);
}
String oldValue = (String) properties.remove(key.toLowerCase());
if (oldValue != null) {
persistToDisk();
Expand All @@ -104,12 +91,7 @@ public String deleteFromCache(String key) throws CacheStoreException {

@Override
public void clearCache() throws CacheStoreException {
// remove all cache entries except the cache directory
for (Object key: new HashSet<>(properties.keySet())) {
if (!key.equals(Constants.CACHE_DIR_KEY)) {
properties.remove(key);
}
}
properties.clear();
persistToDisk();
}

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

private static String defaultCacheDir() {
return System.getProperty("user.home") + File.separator + "cache";
}

/**
* Initialize the cache store directory.
*
* @return cache directory
*/
private static String initCacheDir() throws IOException {
String cacheDirStr = System.getenv(CACHEDIR);
if (cacheDirStr == null) {
cacheDirStr = System.getProperty(CACHEDIR);
}
if (cacheDirStr == null) {
cacheDirStr = System.getProperty("user.home") + File.separator + "cache";
}
String cacheDirStr = Utils.getEnvironmentProperty(CACHEDIR, FileCacheStore::defaultCacheDir);

Path cacheDir = Paths.get(cacheDirStr);

boolean pathExists = Files.exists(cacheDir, LinkOption.NOFOLLOW_LINKS);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019, 2021, Oracle and/or its affiliates.
// Copyright (c) 2019, 2024, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.

package com.oracle.weblogic.imagetool.cli.cache;
Expand All @@ -20,9 +20,7 @@ public class DeleteEntry extends CacheOperation {
@Override
public CommandResponse call() throws Exception {
if (!Utils.isEmptyString(key)) {
if (Constants.CACHE_DIR_KEY.equalsIgnoreCase(key)) {
return CommandResponse.success("Cannot delete key: " + key);
} else if (Constants.DELETE_ALL_FOR_SURE.equalsIgnoreCase(key)) {
if (Constants.DELETE_ALL_FOR_SURE.equalsIgnoreCase(key)) {
cache().clearCache();
return CommandResponse.success("IMG-0046");
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019, 2021, Oracle and/or its affiliates.
// Copyright (c) 2019, 2024, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.

package com.oracle.weblogic.imagetool.cli.cache;
Expand All @@ -8,8 +8,6 @@

import com.oracle.weblogic.imagetool.api.model.CommandResponse;
import com.oracle.weblogic.imagetool.cachestore.CacheStoreException;
import com.oracle.weblogic.imagetool.util.Constants;
import com.oracle.weblogic.imagetool.util.Utils;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

Expand All @@ -28,11 +26,6 @@ public CommandResponse call() throws CacheStoreException {
return CommandResponse.success("IMG-0047");
} else {
System.out.println("Cache contents");
String cacheDir = resultMap.getOrDefault(Constants.CACHE_DIR_KEY, null);
if (!Utils.isEmptyString(cacheDir)) {
System.out.println(Constants.CACHE_DIR_KEY + "=" + cacheDir);
resultMap.remove(Constants.CACHE_DIR_KEY);
}

Pattern pattern = Pattern.compile(key == null ? ".*" : key);
resultMap.entrySet().stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019, 2021, Oracle and/or its affiliates.
// Copyright (c) 2019, 2024, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.

package com.oracle.weblogic.imagetool.util;
Expand All @@ -10,14 +10,13 @@
public final class Constants {

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

private Constants() {
//restrict access
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.ResourceBundle;
import java.util.Set;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -545,13 +546,13 @@ public static String getPasswordFromInputs(String passwordStr, Path passwordFile
* @param defaultValue if no environment variable is defined, nor system property, return this value
* @return the value defined in the env or system property
*/
public static String getEnvironmentProperty(String name, String defaultValue) {
public static String getEnvironmentProperty(String name, Supplier<String> defaultValue) {
String result = System.getenv(name);
if (isEmptyString(result)) {
result = System.getProperty(name);
}
if (isEmptyString(result)) {
return defaultValue;
return defaultValue.get();
}
return result;
}
Expand All @@ -562,7 +563,7 @@ public static String getEnvironmentProperty(String name, String defaultValue) {
* @return working directory
*/
public static String getBuildWorkingDir() throws IOException {
String workingDir = getEnvironmentProperty("WLSIMG_BLDDIR", System.getProperty("user.home"));
String workingDir = getEnvironmentProperty("WLSIMG_BLDDIR", () -> System.getProperty("user.home"));
Path path = Paths.get(workingDir);

boolean pathExists = Files.exists(path, LinkOption.NOFOLLOW_LINKS);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
// Copyright (c) 2020, 2022, Oracle and/or its affiliates.
// Copyright (c) 2020, 2024, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.

package com.oracle.weblogic.imagetool.aru;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.stream.Collectors;

import com.oracle.weblogic.imagetool.ResourceUtils;
import com.oracle.weblogic.imagetool.installer.FmwInstallerType;
import com.oracle.weblogic.imagetool.logging.LoggingFacade;
import com.oracle.weblogic.imagetool.logging.LoggingFactory;
import com.oracle.weblogic.imagetool.test.annotations.ReduceTestLogging;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
Expand All @@ -23,22 +21,17 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

@ReduceTestLogging(loggerClass = AruUtil.class)
@Tag("unit")
class AruUtilTest {
private static final LoggingFacade logger = LoggingFactory.getLogger(AruUtil.class);
private static Level oldLevel;

@BeforeAll
static void setUp() throws NoSuchFieldException, IllegalAccessException {
oldLevel = logger.getLevel();
logger.setLevel(Level.SEVERE);
// insert test class into AruUtil to intercept REST calls to ARU
MockAruUtil.insertMockAruInstance(new TestAruUtil());
}

@AfterAll
static void tearDown() throws NoSuchFieldException, IllegalAccessException {
logger.setLevel(oldLevel);
MockAruUtil.removeMockAruInstance();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import javax.xml.xpath.XPathExpressionException;

import com.oracle.weblogic.imagetool.api.model.CachedFile;
import com.oracle.weblogic.imagetool.aru.AruException;
import com.oracle.weblogic.imagetool.installer.InstallerType;
import com.oracle.weblogic.imagetool.logging.LoggingFacade;
import com.oracle.weblogic.imagetool.logging.LoggingFactory;
import com.oracle.weblogic.imagetool.test.annotations.ReduceTestLogging;
import com.oracle.weblogic.imagetool.util.BuildPlatform;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
Expand All @@ -31,6 +29,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;

@Tag("unit")
@ReduceTestLogging(loggerClass = CachedFile.class)
class CachedFileTest {

static Path cacheDir;
Expand Down Expand Up @@ -139,19 +138,12 @@ void resolveFallbackToLocalArch() throws IOException {

@Test
void copyFile(@TempDir Path contextDir) throws Exception {
LoggingFacade logger = LoggingFactory.getLogger(CachedFile.class);
Level oldLevel = logger.getLevel();
logger.setLevel(Level.OFF);
try {
CachedFile wlsInstallerFile = new CachedFile(InstallerType.WLS, ver12213);
// copy the file from the cache store to the fake build context directory
Path result = wlsInstallerFile.copyFile(cacheStore, contextDir.toString());
// check to see if the file was copied correctly by examining the contents of the resulting file
assertLinesMatch(fileContents, Files.readAllLines(result),
"copied file contents do not match source");
} finally {
logger.setLevel(oldLevel);
}
CachedFile wlsInstallerFile = new CachedFile(InstallerType.WLS, ver12213);
// copy the file from the cache store to the fake build context directory
Path result = wlsInstallerFile.copyFile(cacheStore, contextDir.toString());
// check to see if the file was copied correctly by examining the contents of the resulting file
assertLinesMatch(fileContents, Files.readAllLines(result),
"copied file contents do not match source");
}

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

package com.oracle.weblogic.imagetool.cachestore;
Expand Down Expand Up @@ -48,7 +48,7 @@ void checkValueInCache() {
assertTrue(cache().containsKey(testKey)),
"containsKey failed to find key or value that was just added");

// check (another way) that the value that was just added
// check (another way) that the value was just added
assertDoesNotThrow(() ->
assertEquals(testVal, cache().getValueFromCache(testKey), "Found unexpected value in cache"),
"Get from cache threw an exception");
Expand All @@ -73,5 +73,9 @@ void verifyCacheSize() {
assertDoesNotThrow(() ->
assertNotNull(cache().getCacheItems(), "Get cache items should never be null"),
"getCacheItems threw an exception");

assertDoesNotThrow(() ->
assertEquals(0, cache().getCacheItems().size(), "Get cache items should never be null"),
"getCacheItems threw an exception");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020, 2022, Oracle and/or its affiliates.
// Copyright (c) 2020, 2024, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.

package com.oracle.weblogic.imagetool.cachestore;
Expand All @@ -11,6 +11,8 @@

import com.oracle.weblogic.imagetool.aru.AruException;
import com.oracle.weblogic.imagetool.aru.MockAruUtil;
import com.oracle.weblogic.imagetool.cli.menu.CommonOptions;
import com.oracle.weblogic.imagetool.test.annotations.ReduceTestLogging;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
Expand All @@ -22,6 +24,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

@ReduceTestLogging(loggerClass = CommonOptions.class)
@Tag("unit")
class OPatchFileTest {
private static CacheStore cacheStore;
Expand Down
Loading

0 comments on commit e5f4c31

Please sign in to comment.