From fc746d78e62a2a057667bc9f14a2c78787b8d5fc Mon Sep 17 00:00:00 2001 From: Kalle Stenflo Date: Thu, 5 Nov 2015 22:08:22 +0100 Subject: [PATCH] Stricter configuration of CacheProvide. --- .../jsonpath/spi/cache/CacheProvider.java | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/json-path/src/main/java/com/jayway/jsonpath/spi/cache/CacheProvider.java b/json-path/src/main/java/com/jayway/jsonpath/spi/cache/CacheProvider.java index 1a256f3d..8f3e0b23 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/spi/cache/CacheProvider.java +++ b/json-path/src/main/java/com/jayway/jsonpath/spi/cache/CacheProvider.java @@ -2,20 +2,35 @@ package com.jayway.jsonpath.spi.cache; import com.jayway.jsonpath.JsonPathException; +import static com.jayway.jsonpath.internal.Utils.notNull; + public class CacheProvider { - private static Cache cache = new LRUCache(200); + private static Cache cache; public static void setCache(Cache cache){ - if (cache != null){ - CacheProvider.cache = cache; + notNull(cache, "Cache may not be null"); + 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() { - try { - return cache; - } catch (Exception e) { - throw new JsonPathException("Failed to get cache", e); + if(CacheProvider.cache == null){ + synchronized (CacheProvider.class){ + if(CacheProvider.cache == null){ + CacheProvider.cache = getDefaultCache(); + } + } } + return CacheProvider.cache; + } + + + private static Cache getDefaultCache(){ + return new LRUCache(200); } } \ No newline at end of file