Skip to content

Commit 6a62c01

Browse files
committed
remove java preferences from cachestore
1 parent d2ed805 commit 6a62c01

File tree

8 files changed

+36
-244
lines changed

8 files changed

+36
-244
lines changed

imagetool/src/main/java/com/oracle/weblogic/imagetool/api/meta/CacheStore.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,4 @@ public interface CacheStore {
6666
*/
6767
Map<String, String> getCacheItems();
6868

69-
/**
70-
* Set cache directory to new location.
71-
*
72-
* @param value a directory path
73-
* @return true if successful
74-
*/
75-
boolean setCacheDir(String value);
7669
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
ListCacheItems.class,
2525
AddInstallerEntry.class,
2626
AddPatchEntry.class,
27-
GetCacheDir.class,
28-
SetCacheDir.class,
2927
AddEntry.class,
3028
DeleteEntry.class,
3129
HelpCommand.class

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

Lines changed: 0 additions & 58 deletions
This file was deleted.

imagetool/src/main/java/com/oracle/weblogic/imagetool/impl/meta/CacheStoreFactory.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public class CacheStoreFactory implements Supplier<CacheStore> {
1818

1919
static {
2020
cashStoreMap.put(Constants.FILE_CACHE, FileCacheStore.CACHE_STORE);
21-
cashStoreMap.put(Constants.PREF_CACHE, PreferenceCacheStore.CACHE_STORE);
2221
}
2322

2423
public CacheStore getCacheStore(String backingType) {
@@ -27,13 +26,6 @@ public CacheStore getCacheStore(String backingType) {
2726

2827
@Override
2928
public CacheStore get() {
30-
String backingType = null;
31-
try {
32-
backingType = System.getenv(Constants.CACHE_STORE_TYPE);
33-
backingType = Utils.isEmptyString(backingType) ? System.getProperty(Constants.CACHE_STORE_TYPE, Constants.FILE_CACHE) : backingType;
34-
} catch (Exception e) {
35-
e.printStackTrace();
36-
}
37-
return getCacheStore(backingType);
29+
return FileCacheStore.CACHE_STORE;
3830
}
3931
}

imagetool/src/main/java/com/oracle/weblogic/imagetool/impl/meta/FileCacheStore.java

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,17 @@
66

77
import com.oracle.weblogic.imagetool.api.meta.CacheStore;
88
import com.oracle.weblogic.imagetool.util.Constants;
9+
import com.oracle.weblogic.imagetool.util.Utils;
910

1011
import java.io.BufferedReader;
1112
import java.io.File;
1213
import java.io.FileOutputStream;
1314
import java.io.FileReader;
1415
import java.io.IOException;
15-
import java.nio.file.Paths;
1616
import java.time.LocalDateTime;
1717
import java.util.Map;
1818
import java.util.Objects;
1919
import java.util.Properties;
20-
import java.util.prefs.BackingStoreException;
21-
import java.util.prefs.Preferences;
2220
import java.util.stream.Collectors;
2321
import java.util.stream.Stream;
2422

@@ -28,18 +26,11 @@ public enum FileCacheStore implements CacheStore {
2826

2927
private final Properties properties = new Properties();
3028
private String metadataPath;
31-
private final String DEFAULT_CACHE_DIR = Paths.get(System.getProperty("user.home"), "cache")
32-
.toAbsolutePath().toString();
33-
private final Preferences preferences = Preferences.userRoot().node(Constants.WEBLOGIC_IMAGETOOL);
3429

3530
FileCacheStore() {
3631
try {
37-
metadataPath = preferences.get(Constants.METADATA_PREF_KEY, null);
38-
if (metadataPath == null || metadataPath.isEmpty()) {
39-
metadataPath = String.format("%s%s%s", DEFAULT_CACHE_DIR, File.separator, Constants.DEFAULT_META_FILE);
40-
preferences.put(Constants.METADATA_PREF_KEY, metadataPath);
41-
preferences.flush();
42-
}
32+
String userCacheDir = Utils.getCacheDir();
33+
metadataPath = String.format("%s%s%s", userCacheDir, File.separator, Constants.DEFAULT_META_FILE);
4334
File metadataFile = new File(metadataPath);
4435
if (metadataFile.exists() && metadataFile.isFile()) {
4536
loadProperties(metadataFile);
@@ -48,7 +39,7 @@ public enum FileCacheStore implements CacheStore {
4839
metadataFile.createNewFile();
4940
}
5041
if (properties.getProperty(Constants.CACHE_DIR_KEY) == null) {
51-
properties.put(Constants.CACHE_DIR_KEY, DEFAULT_CACHE_DIR);
42+
properties.put(Constants.CACHE_DIR_KEY, userCacheDir);
5243
persistToDisk();
5344
}
5445
File cacheDir = new File(properties.getProperty(Constants.CACHE_DIR_KEY));
@@ -63,7 +54,7 @@ public enum FileCacheStore implements CacheStore {
6354

6455
@Override
6556
public String getCacheDir() {
66-
return properties.getProperty(Constants.CACHE_DIR_KEY, DEFAULT_CACHE_DIR);
57+
return properties.getProperty(Constants.CACHE_DIR_KEY);
6758
}
6859

6960
@Override
@@ -138,26 +129,5 @@ private void loadProperties(File propsFile) {
138129
}
139130
}
140131

141-
public boolean setCacheDir(String cacheDirPath) {
142-
if (cacheDirPath != null) {
143-
properties.put(Constants.CACHE_DIR_KEY, cacheDirPath);
144-
try {
145-
metadataPath = getCacheDir() + File.separator + Constants.DEFAULT_META_FILE;
146-
File metaDataFile = new File(metadataPath);
147-
if (metaDataFile.exists() && metaDataFile.isFile()) {
148-
loadProperties(metaDataFile);
149-
} else {
150-
metaDataFile.getParentFile().mkdirs();
151-
metaDataFile.createNewFile();
152-
}
153-
preferences.put(Constants.METADATA_PREF_KEY, metadataPath);
154-
preferences.flush();
155-
return persistToDisk();
156-
} catch (BackingStoreException | IOException e) {
157-
e.printStackTrace();
158-
}
159-
}
160-
return false;
161-
}
162132

163133
}

imagetool/src/main/java/com/oracle/weblogic/imagetool/impl/meta/PreferenceCacheStore.java

Lines changed: 0 additions & 117 deletions
This file was deleted.

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,35 @@ public static String getBuildWorkingDir() throws IOException {
570570
return workingDir;
571571
}
572572

573+
/**
574+
* returns the cache store directory
575+
* @return cache directory
576+
*/
577+
public static String getCacheDir() throws IOException {
578+
String cacheDir = System.getenv("WLSIMG_CACHEDIR");
579+
if (cacheDir == null ) {
580+
cacheDir = System.getProperty("user.home") + "/cache";
581+
}
582+
Path path = Paths.get(cacheDir);
583+
584+
boolean pathExists =
585+
Files.exists(path,
586+
new LinkOption[]{ LinkOption.NOFOLLOW_LINKS});
587+
588+
if (!pathExists) {
589+
throw new IOException("Cache Directory does not exists " + cacheDir);
590+
} else {
591+
if (!Files.isDirectory(path)) {
592+
throw new IOException("Cache Directory specified is not a directory " + cacheDir);
593+
}
594+
if (!Files.isWritable(path)) {
595+
throw new IOException("Cache Directory specified is not writable " + cacheDir);
596+
}
597+
}
598+
599+
return cacheDir;
600+
}
601+
573602
/**
574603
* Return the version number inside a opatch file
575604
* @param fileName full path to the opatch patch

imagetool/src/test/java/com/oracle/weblogic/imagetool/impl/meta/FileCacheStoreTest.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,7 @@
1919
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
2020
public class FileCacheStoreTest {
2121

22-
private CacheStore cacheStore = new CacheStoreFactory().getCacheStore(Constants.FILE_CACHE);
23-
24-
@Test
25-
public void a1setCacheDir() throws IOException {
26-
Path cacheDirPath = Files.createTempDirectory("test_imagetool");
27-
cacheDirPath.toFile().deleteOnExit();
28-
// change the cache directory from default so it doesn't modify user data
29-
cacheStore.setCacheDir(cacheDirPath.toAbsolutePath().toString());
30-
}
31-
32-
@Test
33-
public void a2getCacheDir() {
34-
assertNotNull(cacheStore.getCacheDir());
35-
File cacheDir = new File(cacheStore.getCacheDir());
36-
assertTrue(cacheDir.exists() && cacheDir.isDirectory());
37-
}
22+
private CacheStore cacheStore = new CacheStoreFactory().get();
3823

3924
@Test
4025
public void a3addToCache() {

0 commit comments

Comments
 (0)