From fe78dc48f16382d188e0b7b5e32fa9ed5414bde8 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Fri, 6 Nov 2015 16:59:35 +0000 Subject: [PATCH] Restored unpatched version for IC git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@120227 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../AccountingPersistenceBackendFactory.java | 87 +++++++------------ 1 file changed, 32 insertions(+), 55 deletions(-) diff --git a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java index 407126b..d379cd5 100644 --- a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java +++ b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceBackendFactory.java @@ -8,7 +8,6 @@ import java.util.HashMap; import java.util.Map; import java.util.ServiceLoader; import java.util.concurrent.TimeUnit; -import java.util.concurrent.locks.ReentrantLock; import org.gcube.accounting.aggregation.scheduler.AggregationScheduler; import org.gcube.common.scope.api.ScopeProvider; @@ -21,27 +20,23 @@ import org.slf4j.LoggerFactory; * */ public abstract class AccountingPersistenceBackendFactory { - + private static final Logger logger = LoggerFactory.getLogger(AccountingPersistenceBackendFactory.class); - + public final static String HOME_SYSTEM_PROPERTY = "user.home"; - + private static final String ACCOUTING_FALLBACK_FILENAME = "accountingFallback.log"; - + private static String fallbackLocation; - private static ReentrantLock lock = new ReentrantLock(); - - private static ReentrantLock persistenceLock = new ReentrantLock(); - private static Map persistencePersistenceBackends; - + static { persistencePersistenceBackends = new HashMap(); } - + private static File file(File file) throws IllegalArgumentException { - + if(!file.isDirectory()){ file = file.getParentFile(); } @@ -49,57 +44,42 @@ public abstract class AccountingPersistenceBackendFactory { //create folder structure if not exist if (!file.exists()) file.mkdirs(); - + return file; - + } - - protected static void setFallbackLocation(String path){ - lock.lock(); - try{ - if(fallbackLocation == null){ - if(path==null){ - path = System.getProperty(HOME_SYSTEM_PROPERTY); - } - file(new File(path)); - fallbackLocation = path; - + + protected synchronized static void setFallbackLocation(String path){ + if(fallbackLocation == null){ + if(path==null){ + path = System.getProperty(HOME_SYSTEM_PROPERTY); } - }finally{ - lock.unlock(); + file(new File(path)); + fallbackLocation = path; } } - - protected static AccountingPersistenceBackend getPersistenceBackend() { + + protected static synchronized AccountingPersistenceBackend getPersistenceBackend() { String scope = ScopeProvider.instance.get(); if(scope==null){ logger.error("No Scope available. FallbackPersistence will be used"); File fallbackFile = new File(fallbackLocation, ACCOUTING_FALLBACK_FILENAME); return new FallbackPersistence(fallbackFile); } - - persistenceLock.lock(); + AccountingPersistenceBackend persistence = persistencePersistenceBackends.get(scope); if(persistence==null){ - FallbackPersistence fallbackPersistence; - try{ - ScopeBean bean = new ScopeBean(scope); - /* - if(bean.is(Type.VRE)){ - bean = bean.enclosingScope(); - } - */ - String name = bean.name(); - - File fallbackFile = new File(fallbackLocation, String.format("%s.%s", name, ACCOUTING_FALLBACK_FILENAME)); - fallbackPersistence = new FallbackPersistence(fallbackFile); - // set only to avoid deadlock - persistencePersistenceBackends.put(scope, fallbackPersistence); - }finally{ - persistenceLock.unlock(); + + ScopeBean bean = new ScopeBean(scope); + /* + if(bean.is(Type.VRE)){ + bean = bean.enclosingScope(); } - - + */ + String name = bean.name(); + + File fallbackFile = new File(fallbackLocation, String.format("%s.%s", name, ACCOUTING_FALLBACK_FILENAME)); + FallbackPersistence fallbackPersistence = new FallbackPersistence(fallbackFile); try { ServiceLoader serviceLoader = ServiceLoader.load(AccountingPersistenceBackend.class); for (AccountingPersistenceBackend foundPersistence : serviceLoader) { @@ -135,11 +115,8 @@ public abstract class AccountingPersistenceBackendFactory { persistence.setAggregationScheduler(AggregationScheduler.newInstance()); persistence.setFallback(fallbackPersistence); persistencePersistenceBackends.put(scope, persistence); - } else{ - persistenceLock.unlock(); } - - + return persistence; } @@ -160,6 +137,6 @@ public abstract class AccountingPersistenceBackendFactory { } } } - - + + }