From 7c662a0c931d541620233317c9896e1671f20ea2 Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Mon, 1 Aug 2016 16:02:51 +0000 Subject: [PATCH] factory class added git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/ckan-util-library@130932 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../ckanutillibrary/CkanUtilsFactory.java | 65 +++++++++++++++++++ .../ckanutillibrary/TestCKanLib.java | 19 ++++++ 2 files changed, 84 insertions(+) create mode 100644 src/main/java/org/gcube/datacatalogue/ckanutillibrary/CkanUtilsFactory.java diff --git a/src/main/java/org/gcube/datacatalogue/ckanutillibrary/CkanUtilsFactory.java b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/CkanUtilsFactory.java new file mode 100644 index 0000000..2a947f7 --- /dev/null +++ b/src/main/java/org/gcube/datacatalogue/ckanutillibrary/CkanUtilsFactory.java @@ -0,0 +1,65 @@ +package org.gcube.datacatalogue.ckanutillibrary; + +import java.util.concurrent.ConcurrentHashMap; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Please invoke this method to retrieve an object of this kind per scope. + * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) + */ +public class CkanUtilsFactory { + + private static final Logger logger = LoggerFactory.getLogger(CkanUtilsFactory.class); + private static final long MAX_LIFETIME = 1000 * 60 * 5; // 5 MINUTES + private static CkanUtilsFactory instance = new CkanUtilsFactory(); + private static ConcurrentHashMap cache; + + private class CacheBean{ + CKanUtilsImpl utils; + long ttl; + + public CacheBean(long ttl, CKanUtilsImpl utils){ + this.ttl = ttl; + this.utils = utils; + } + } + + private CkanUtilsFactory(){ + + logger.debug("Ckan factory object build"); + cache = new ConcurrentHashMap(); + + } + + /** + * Get the factory instance + * @return + */ + public static CkanUtilsFactory getFactory(){ + logger.debug("Factory requested"); + return instance; + } + + public CKanUtilsImpl getUtilsPerScope(String scope) throws Exception{ + if(scope == null || scope.isEmpty()) + throw new IllegalArgumentException("Invalid scope given!"); + + if(cache.containsKey(scope) && !expired(cache.get(scope))){ + return cache.get(scope).utils; + } + else{ + logger.debug("Creating utils for scope " + scope); + CKanUtilsImpl utils = new CKanUtilsImpl(scope); + cache.put(scope, new CacheBean(System.currentTimeMillis(), utils)); + return utils; + } + } + + private boolean expired(CacheBean cacheBean) { + boolean expired = (cacheBean.ttl + MAX_LIFETIME <= System.currentTimeMillis()); + logger.debug("expired is " + expired); + return expired; + } +} diff --git a/src/test/java/org/gcube/datacatalogue/ckanutillibrary/TestCKanLib.java b/src/test/java/org/gcube/datacatalogue/ckanutillibrary/TestCKanLib.java index 7803d9e..0e34d6d 100644 --- a/src/test/java/org/gcube/datacatalogue/ckanutillibrary/TestCKanLib.java +++ b/src/test/java/org/gcube/datacatalogue/ckanutillibrary/TestCKanLib.java @@ -18,6 +18,25 @@ public class TestCKanLib { CKanUtilsImpl instance; + //@Test + public void factoryTest() throws Exception{ + + CkanUtilsFactory factory = CkanUtilsFactory.getFactory(); + + while(true){ + factory.getUtilsPerScope("/gcube"); + Thread.sleep(60* 1000 * 3); + factory.getUtilsPerScope("/gcube"); + break; + } + + for (int i = 0; i < 5; i++) { + Thread.sleep(1000); + factory.getUtilsPerScope("/gcube"); + } + + } + //@Test public void before() throws Exception{