@@ -34,8 +34,8 @@ public final class ServerTemplateImpl implements ServerTemplate {
3434
3535 private final KeysAndValues defaultConfig ;
3636 private FirebaseRemoteConfigClient client ;
37- private ServerTemplateData cache ;
38- private final AtomicReference <String > cachedTemplate ; // Added field for cached template
37+ private AtomicReference < ServerTemplateData > cache ;
38+ private final AtomicReference <String > cachedTemplate ;
3939 private static final Logger logger = LoggerFactory .getLogger (ServerTemplate .class );
4040
4141 public static class Builder implements ServerTemplate .Builder {
@@ -69,8 +69,11 @@ private ServerTemplateImpl(Builder builder) {
6969 this .defaultConfig = builder .defaultConfig ;
7070 this .cachedTemplate = new AtomicReference <>(builder .cachedTemplate );
7171 this .client = builder .client ;
72+ this .cache = new AtomicReference <>(null );
73+
74+ String initialTemplate = this .cachedTemplate .get ();
7275 try {
73- this .cache = ServerTemplateData .fromJSON (this . cachedTemplate . get ( ));
76+ this .cache . set ( ServerTemplateData .fromJSON (initialTemplate ));
7477 } catch (FirebaseRemoteConfigException e ) {
7578 e .printStackTrace ();
7679 }
@@ -79,7 +82,8 @@ private ServerTemplateImpl(Builder builder) {
7982 @ Override
8083 public ServerConfig evaluate (@ Nullable KeysAndValues context )
8184 throws FirebaseRemoteConfigException {
82- if (this .cache == null ) {
85+ ServerTemplateData cachedData = this .cache .get ();
86+ if (cachedData == null ) {
8387 throw new FirebaseRemoteConfigException (ErrorCode .FAILED_PRECONDITION ,
8488 "No Remote Config Server template in cache. Call load() before calling evaluate()." );
8589 }
@@ -94,8 +98,8 @@ public ServerConfig evaluate(@Nullable KeysAndValues context)
9498
9599 ConditionEvaluator conditionEvaluator = new ConditionEvaluator ();
96100 ImmutableMap <String , Boolean > evaluatedCondition = ImmutableMap .copyOf (
97- conditionEvaluator .evaluateConditions (cache .getServerConditions (), context ));
98- ImmutableMap <String , Parameter > parameters = ImmutableMap .copyOf (cache .getParameters ());
101+ conditionEvaluator .evaluateConditions (cachedData .getServerConditions (), context ));
102+ ImmutableMap <String , Parameter > parameters = ImmutableMap .copyOf (cachedData .getParameters ());
99103 mergeDerivedConfigValues (evaluatedCondition , parameters , configValues );
100104
101105 return new ServerConfig (configValues );
@@ -110,7 +114,7 @@ public ServerConfig evaluate() throws FirebaseRemoteConfigException {
110114 public ApiFuture <Void > load () throws FirebaseRemoteConfigException {
111115 String serverTemplate = client .getServerTemplate ();
112116 this .cachedTemplate .set (serverTemplate );
113- this .cache = ServerTemplateData .fromJSON (serverTemplate );
117+ this .cache . set ( ServerTemplateData .fromJSON (serverTemplate ) );
114118 return ApiFutures .immediateFuture (null );
115119 }
116120
@@ -125,12 +129,15 @@ public String getCachedTemplate() {
125129
126130 @ Override
127131 public String toJson () {
128- return this .cache .toJSON ();
132+ ServerTemplateData currentCache = this .cache .get ();
133+ if (currentCache == null ) {
134+ return "{}" ;
135+ }
136+ return currentCache .toJSON ();
129137 }
130138
131139 private void mergeDerivedConfigValues (ImmutableMap <String , Boolean > evaluatedCondition ,
132140 ImmutableMap <String , Parameter > parameters , Map <String , Value > configValues ) {
133- // Overlays config Value objects derived by evaluating the template.
134141 for (String parameterName : parameters .keySet ()) {
135142 Parameter parameter = parameters .get (parameterName );
136143 if (parameter == null ) {
0 commit comments