Browse Source

Stricter configuration of CacheProvide.

pull/183/merge
Kalle Stenflo 9 years ago
parent
commit
fc746d78e6
  1. 29
      json-path/src/main/java/com/jayway/jsonpath/spi/cache/CacheProvider.java

29
json-path/src/main/java/com/jayway/jsonpath/spi/cache/CacheProvider.java vendored

@ -2,20 +2,35 @@ package com.jayway.jsonpath.spi.cache;
import com.jayway.jsonpath.JsonPathException; import com.jayway.jsonpath.JsonPathException;
import static com.jayway.jsonpath.internal.Utils.notNull;
public class CacheProvider { public class CacheProvider {
private static Cache cache = new LRUCache(200); private static Cache cache;
public static void setCache(Cache cache){ public static void setCache(Cache cache){
if (cache != null){ notNull(cache, "Cache may not be null");
CacheProvider.cache = cache; synchronized (CacheProvider.class){
if(CacheProvider.cache != null){
throw new JsonPathException("Cache provider must be configured before cache is accessed.");
} else {
CacheProvider.cache = cache;
}
} }
} }
public static Cache getCache() { public static Cache getCache() {
try { if(CacheProvider.cache == null){
return cache; synchronized (CacheProvider.class){
} catch (Exception e) { if(CacheProvider.cache == null){
throw new JsonPathException("Failed to get cache", e); CacheProvider.cache = getDefaultCache();
}
}
} }
return CacheProvider.cache;
}
private static Cache getDefaultCache(){
return new LRUCache(200);
} }
} }
Loading…
Cancel
Save