accounting-lib/src/main/java/org/gcube/accounting/persistence/AccountingPersistence.java

64 lines
1.9 KiB
Java

/**
*
*/
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.PersistenceBackendFactory;
import org.gcube.documentstore.records.Record;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class AccountingPersistence {
private static final AccountingPersistence accountingPersistence;
private AccountingPersistence(){}
static {
accountingPersistence = new AccountingPersistence();
}
protected static synchronized AccountingPersistence getInstance(){
return accountingPersistence;
}
/**
* Persist the {@link #UsageRecord}.
* The Record is validated first, then accounted, in a separated thread.
* So that the program can continue the execution.
* If the persistence fails the class write that the record in a local file
* so that the {@link #UsageRecord} can be recorder later.
* @param record the {@link #UsageRecord} to persist
* @throws InvalidValueException
*/
public void account(final Record record) throws InvalidValueException {
String scope = ScopeProvider.instance.get();
try {
PersistenceBackendFactory.getPersistenceBackend(scope).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 = ScopeProvider.instance.get();
PersistenceBackendFactory.flush(scope, timeout, timeUnit);
}
public void close() throws Exception{
String scope = ScopeProvider.instance.get();
PersistenceBackendFactory.getPersistenceBackend(scope).close();
}
}