Skip to content

Commit e826df4

Browse files
authored
fix: Configuration store minor fixes (#91)
1 parent ae3125e commit e826df4

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

eppo/src/main/java/cloud/eppo/android/ConfigurationStore.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import static cloud.eppo.android.util.Utils.logTag;
44

55
import android.app.Application;
6-
import android.os.AsyncTask;
76
import android.util.Log;
7+
import androidx.annotation.Nullable;
88
import cloud.eppo.ufc.dto.FlagConfig;
99
import cloud.eppo.ufc.dto.FlagConfigResponse;
1010
import cloud.eppo.ufc.dto.adapters.EppoModule;
@@ -14,16 +14,19 @@
1414
import java.io.BufferedReader;
1515
import java.io.BufferedWriter;
1616
import java.io.IOException;
17+
import java.util.Map;
1718
import java.util.concurrent.ConcurrentHashMap;
19+
import java.util.concurrent.ExecutorService;
20+
import java.util.concurrent.Executors;
1821
import java.util.concurrent.atomic.AtomicBoolean;
1922

2023
public class ConfigurationStore {
2124
private static final String TAG = logTag(ConfigurationStore.class);
2225
private final ObjectMapper mapper = new ObjectMapper().registerModule(EppoModule.eppoModule());
2326
private final ConfigCacheFile cacheFile;
24-
27+
private final Object cacheLock = new Object();
2528
private final AtomicBoolean loadedFromFetchResponse = new AtomicBoolean(false);
26-
private ConcurrentHashMap<String, FlagConfig> flags;
29+
@Nullable private Map<String, FlagConfig> flags;
2730

2831
public ConfigurationStore(Application application, String cacheFileNameSuffix) {
2932
cacheFile = new ConfigCacheFile(application, cacheFileNameSuffix);
@@ -38,7 +41,8 @@ public void loadFromCache(CacheLoadCallback callback) {
3841
return;
3942
}
4043

41-
AsyncTask.execute(
44+
ExecutorService executor = Executors.newSingleThreadExecutor();
45+
executor.execute(
4246
() -> {
4347
Log.d(TAG, "Loading from cache");
4448
try {
@@ -66,10 +70,11 @@ public void loadFromCache(CacheLoadCallback callback) {
6670
callback.onCacheLoadFail();
6771
}
6872
});
73+
executor.shutdown();
6974
}
7075

71-
protected FlagConfigResponse readCacheFile() throws IOException {
72-
synchronized (cacheFile) {
76+
@Nullable protected FlagConfigResponse readCacheFile() throws IOException {
77+
synchronized (cacheLock) {
7378
try (BufferedReader reader = cacheFile.getReader()) {
7479
return mapper.readValue(reader, FlagConfigResponse.class);
7580
}
@@ -91,20 +96,22 @@ public void setFlagsFromJsonString(String jsonString) throws JsonProcessingExcep
9196
}
9297

9398
public void asyncWriteToCache(String jsonString) {
94-
AsyncTask.execute(
99+
ExecutorService executor = Executors.newSingleThreadExecutor();
100+
executor.execute(
95101
() -> {
96102
Log.d(TAG, "Saving configuration to cache file");
97103
try {
98-
synchronized (cacheFile) {
99-
BufferedWriter writer = cacheFile.getWriter();
100-
writer.write(jsonString);
101-
writer.close();
104+
synchronized (cacheLock) {
105+
try (BufferedWriter writer = cacheFile.getWriter()) {
106+
writer.write(jsonString);
107+
}
102108
}
103109
Log.d(TAG, "Updated cache file");
104-
} catch (Exception e) {
105-
Log.e(TAG, "Unable to cache config to file", e);
110+
} catch (IOException e) {
111+
Log.e(TAG, "Unable write to cache config to file", e);
106112
}
107113
});
114+
executor.shutdown();
108115
}
109116

110117
public FlagConfig getFlag(String flagKey) {

0 commit comments

Comments
 (0)