3
3
import static cloud .eppo .android .util .Utils .logTag ;
4
4
5
5
import android .app .Application ;
6
- import android .os .AsyncTask ;
7
6
import android .util .Log ;
7
+ import androidx .annotation .Nullable ;
8
8
import cloud .eppo .ufc .dto .FlagConfig ;
9
9
import cloud .eppo .ufc .dto .FlagConfigResponse ;
10
10
import cloud .eppo .ufc .dto .adapters .EppoModule ;
14
14
import java .io .BufferedReader ;
15
15
import java .io .BufferedWriter ;
16
16
import java .io .IOException ;
17
+ import java .util .Map ;
17
18
import java .util .concurrent .ConcurrentHashMap ;
19
+ import java .util .concurrent .ExecutorService ;
20
+ import java .util .concurrent .Executors ;
18
21
import java .util .concurrent .atomic .AtomicBoolean ;
19
22
20
23
public class ConfigurationStore {
21
24
private static final String TAG = logTag (ConfigurationStore .class );
22
25
private final ObjectMapper mapper = new ObjectMapper ().registerModule (EppoModule .eppoModule ());
23
26
private final ConfigCacheFile cacheFile ;
24
-
27
+ private final Object cacheLock = new Object ();
25
28
private final AtomicBoolean loadedFromFetchResponse = new AtomicBoolean (false );
26
- private ConcurrentHashMap <String , FlagConfig > flags ;
29
+ @ Nullable private Map <String , FlagConfig > flags ;
27
30
28
31
public ConfigurationStore (Application application , String cacheFileNameSuffix ) {
29
32
cacheFile = new ConfigCacheFile (application , cacheFileNameSuffix );
@@ -38,7 +41,8 @@ public void loadFromCache(CacheLoadCallback callback) {
38
41
return ;
39
42
}
40
43
41
- AsyncTask .execute (
44
+ ExecutorService executor = Executors .newSingleThreadExecutor ();
45
+ executor .execute (
42
46
() -> {
43
47
Log .d (TAG , "Loading from cache" );
44
48
try {
@@ -66,10 +70,11 @@ public void loadFromCache(CacheLoadCallback callback) {
66
70
callback .onCacheLoadFail ();
67
71
}
68
72
});
73
+ executor .shutdown ();
69
74
}
70
75
71
- protected FlagConfigResponse readCacheFile () throws IOException {
72
- synchronized (cacheFile ) {
76
+ @ Nullable protected FlagConfigResponse readCacheFile () throws IOException {
77
+ synchronized (cacheLock ) {
73
78
try (BufferedReader reader = cacheFile .getReader ()) {
74
79
return mapper .readValue (reader , FlagConfigResponse .class );
75
80
}
@@ -91,20 +96,22 @@ public void setFlagsFromJsonString(String jsonString) throws JsonProcessingExcep
91
96
}
92
97
93
98
public void asyncWriteToCache (String jsonString ) {
94
- AsyncTask .execute (
99
+ ExecutorService executor = Executors .newSingleThreadExecutor ();
100
+ executor .execute (
95
101
() -> {
96
102
Log .d (TAG , "Saving configuration to cache file" );
97
103
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
+ }
102
108
}
103
109
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 );
106
112
}
107
113
});
114
+ executor .shutdown ();
108
115
}
109
116
110
117
public FlagConfig getFlag (String flagKey ) {
0 commit comments