Rationalizing the way of creating AccountingPersistence

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@124351 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2016-02-19 11:41:41 +00:00
parent c4fd8c253c
commit 262975dbf7
2 changed files with 31 additions and 25 deletions

View File

@ -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();
}
}

View File

@ -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<String, AccountingPersistence> persistences;
static {
persistences = new HashMap<String, AccountingPersistence>();
}
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);
}
}