diff --git a/src/main/java/org/gcube/accounting/persistence/AccountingPersistence.java b/src/main/java/org/gcube/accounting/persistence/AccountingPersistence.java index 26f0bcd..e2650ad 100644 --- a/src/main/java/org/gcube/accounting/persistence/AccountingPersistence.java +++ b/src/main/java/org/gcube/accounting/persistence/AccountingPersistence.java @@ -5,8 +5,8 @@ package org.gcube.accounting.persistence; import java.util.concurrent.TimeUnit; -import org.gcube.common.scope.api.ScopeProvider; import org.gcube.documentstore.exception.InvalidValueException; +import org.gcube.documentstore.persistence.PersistenceBackend; import org.gcube.documentstore.persistence.PersistenceBackendFactory; import org.gcube.documentstore.records.Record; @@ -16,16 +16,10 @@ import org.gcube.documentstore.records.Record; */ public class AccountingPersistence { - private static final AccountingPersistence accountingPersistence; + protected PersistenceBackend persistenceBackend; - private AccountingPersistence(){} - - static { - accountingPersistence = new AccountingPersistence(); - } - - protected static synchronized AccountingPersistence getInstance(){ - return accountingPersistence; + protected AccountingPersistence(String scope){ + persistenceBackend = PersistenceBackendFactory.getPersistenceBackend(scope); } /** @@ -38,29 +32,19 @@ public class AccountingPersistence { * @throws InvalidValueException */ public void account(final Record record) throws InvalidValueException { - //String scope = BasicUsageRecord.getScopeFromToken(); - String scope = ScopeProvider.instance.get(); try { - PersistenceBackendFactory.getPersistenceBackend(scope).account(record); + persistenceBackend.account(record); } catch (org.gcube.documentstore.exception.InvalidValueException e) { throw new InvalidValueException(e); } } - public void flushAll(long timeout, TimeUnit timeUnit) throws Exception { - PersistenceBackendFactory.flushAll(timeout, timeUnit); - } - public void flush(long timeout, TimeUnit timeUnit) throws Exception { - //String scope = BasicUsageRecord.getScopeFromToken(); - String scope = ScopeProvider.instance.get(); - PersistenceBackendFactory.flush(scope, timeout, timeUnit); + persistenceBackend.flush(timeout, timeUnit); } public void close() throws Exception{ - //String scope = BasicUsageRecord.getScopeFromToken(); - String scope = ScopeProvider.instance.get(); - PersistenceBackendFactory.getPersistenceBackend(scope).close(); + persistenceBackend.close(); } } diff --git a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceFactory.java b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceFactory.java index ed054f0..190352c 100644 --- a/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceFactory.java +++ b/src/main/java/org/gcube/accounting/persistence/AccountingPersistenceFactory.java @@ -3,6 +3,11 @@ */ package org.gcube.accounting.persistence; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +import org.gcube.common.scope.api.ScopeProvider; import org.gcube.documentstore.persistence.PersistenceBackendFactory; /** @@ -11,12 +16,29 @@ import org.gcube.documentstore.persistence.PersistenceBackendFactory; */ public class AccountingPersistenceFactory { + private AccountingPersistenceFactory(){} + + protected final static Map persistences; + + static { + persistences = new HashMap(); + } + public static void setFallbackLocation(String path){ PersistenceBackendFactory.setFallbackLocation(path); } - public static AccountingPersistence getPersistence() { - return AccountingPersistence.getInstance(); + public synchronized static AccountingPersistence getPersistence() { + String scope = ScopeProvider.instance.get(); + AccountingPersistence accountingPersistence = persistences.get(scope); + if(accountingPersistence==null){ + accountingPersistence = new AccountingPersistence(scope); + } + return accountingPersistence; + } + + public static void flushAll(long timeout, TimeUnit timeUnit){ + PersistenceBackendFactory.flushAll(timeout, timeUnit); } }