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:
parent
c4fd8c253c
commit
262975dbf7
|
@ -5,8 +5,8 @@ package org.gcube.accounting.persistence;
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import org.gcube.common.scope.api.ScopeProvider;
|
|
||||||
import org.gcube.documentstore.exception.InvalidValueException;
|
import org.gcube.documentstore.exception.InvalidValueException;
|
||||||
|
import org.gcube.documentstore.persistence.PersistenceBackend;
|
||||||
import org.gcube.documentstore.persistence.PersistenceBackendFactory;
|
import org.gcube.documentstore.persistence.PersistenceBackendFactory;
|
||||||
import org.gcube.documentstore.records.Record;
|
import org.gcube.documentstore.records.Record;
|
||||||
|
|
||||||
|
@ -16,16 +16,10 @@ import org.gcube.documentstore.records.Record;
|
||||||
*/
|
*/
|
||||||
public class AccountingPersistence {
|
public class AccountingPersistence {
|
||||||
|
|
||||||
private static final AccountingPersistence accountingPersistence;
|
protected PersistenceBackend persistenceBackend;
|
||||||
|
|
||||||
private AccountingPersistence(){}
|
protected AccountingPersistence(String scope){
|
||||||
|
persistenceBackend = PersistenceBackendFactory.getPersistenceBackend(scope);
|
||||||
static {
|
|
||||||
accountingPersistence = new AccountingPersistence();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static synchronized AccountingPersistence getInstance(){
|
|
||||||
return accountingPersistence;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -38,29 +32,19 @@ public class AccountingPersistence {
|
||||||
* @throws InvalidValueException
|
* @throws InvalidValueException
|
||||||
*/
|
*/
|
||||||
public void account(final Record record) throws InvalidValueException {
|
public void account(final Record record) throws InvalidValueException {
|
||||||
//String scope = BasicUsageRecord.getScopeFromToken();
|
|
||||||
String scope = ScopeProvider.instance.get();
|
|
||||||
try {
|
try {
|
||||||
PersistenceBackendFactory.getPersistenceBackend(scope).account(record);
|
persistenceBackend.account(record);
|
||||||
} catch (org.gcube.documentstore.exception.InvalidValueException e) {
|
} catch (org.gcube.documentstore.exception.InvalidValueException e) {
|
||||||
throw new 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 {
|
public void flush(long timeout, TimeUnit timeUnit) throws Exception {
|
||||||
//String scope = BasicUsageRecord.getScopeFromToken();
|
persistenceBackend.flush(timeout, timeUnit);
|
||||||
String scope = ScopeProvider.instance.get();
|
|
||||||
PersistenceBackendFactory.flush(scope, timeout, timeUnit);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void close() throws Exception{
|
public void close() throws Exception{
|
||||||
//String scope = BasicUsageRecord.getScopeFromToken();
|
persistenceBackend.close();
|
||||||
String scope = ScopeProvider.instance.get();
|
|
||||||
PersistenceBackendFactory.getPersistenceBackend(scope).close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,11 @@
|
||||||
*/
|
*/
|
||||||
package org.gcube.accounting.persistence;
|
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;
|
import org.gcube.documentstore.persistence.PersistenceBackendFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -11,12 +16,29 @@ import org.gcube.documentstore.persistence.PersistenceBackendFactory;
|
||||||
*/
|
*/
|
||||||
public class AccountingPersistenceFactory {
|
public class AccountingPersistenceFactory {
|
||||||
|
|
||||||
|
private AccountingPersistenceFactory(){}
|
||||||
|
|
||||||
|
protected final static Map<String, AccountingPersistence> persistences;
|
||||||
|
|
||||||
|
static {
|
||||||
|
persistences = new HashMap<String, AccountingPersistence>();
|
||||||
|
}
|
||||||
|
|
||||||
public static void setFallbackLocation(String path){
|
public static void setFallbackLocation(String path){
|
||||||
PersistenceBackendFactory.setFallbackLocation(path);
|
PersistenceBackendFactory.setFallbackLocation(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static AccountingPersistence getPersistence() {
|
public synchronized static AccountingPersistence getPersistence() {
|
||||||
return AccountingPersistence.getInstance();
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue