2015-05-28 13:19:25 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package org.gcube.accounting.persistence;
|
|
|
|
|
2015-07-14 12:20:36 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2015-07-14 12:11:24 +02:00
|
|
|
|
2015-06-24 15:24:57 +02:00
|
|
|
import org.gcube.accounting.datamodel.SingleUsageRecord;
|
2015-05-28 13:19:25 +02:00
|
|
|
import org.gcube.accounting.exception.InvalidValueException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
2015-10-12 18:03:39 +02:00
|
|
|
*
|
2015-05-28 13:19:25 +02:00
|
|
|
*/
|
2015-10-12 18:03:39 +02:00
|
|
|
public class AccountingPersistence {
|
2015-05-28 13:19:25 +02:00
|
|
|
|
2015-10-12 18:03:39 +02:00
|
|
|
private static final AccountingPersistence accountingPersistence;
|
2015-05-28 13:19:25 +02:00
|
|
|
|
2015-10-12 18:03:39 +02:00
|
|
|
private AccountingPersistence(){}
|
2015-07-14 12:11:24 +02:00
|
|
|
|
2015-10-12 18:03:39 +02:00
|
|
|
static {
|
|
|
|
accountingPersistence = new AccountingPersistence();
|
2015-07-14 12:07:06 +02:00
|
|
|
}
|
2015-06-15 16:48:59 +02:00
|
|
|
|
2015-10-12 18:03:39 +02:00
|
|
|
protected static synchronized AccountingPersistence getInstance(){
|
|
|
|
return accountingPersistence;
|
2015-07-01 10:56:44 +02:00
|
|
|
}
|
|
|
|
|
2015-05-28 13:19:25 +02:00
|
|
|
/**
|
|
|
|
* Persist the {@link #UsageRecord}.
|
2015-06-25 16:41:13 +02:00
|
|
|
* The Record is validated first, then accounted, in a separated thread.
|
2015-06-25 14:30:50 +02:00
|
|
|
* So that the program can continue the execution.
|
2015-05-28 13:19:25 +02:00
|
|
|
* If the persistence fails the class write that the record in a local file
|
|
|
|
* so that the {@link #UsageRecord} can be recorder later.
|
|
|
|
* @param usageRecord the {@link #UsageRecord} to persist
|
2015-06-25 14:30:50 +02:00
|
|
|
* @throws InvalidValueException if the Record Validation Fails
|
2015-05-28 13:19:25 +02:00
|
|
|
*/
|
2015-06-25 14:30:50 +02:00
|
|
|
public void account(final SingleUsageRecord usageRecord) throws InvalidValueException{
|
2015-10-12 18:03:39 +02:00
|
|
|
AccountingPersistenceBackendFactory.getPersistenceBackend().account(usageRecord);
|
2015-05-28 13:19:25 +02:00
|
|
|
}
|
|
|
|
|
2015-07-14 12:20:36 +02:00
|
|
|
public void flush(long timeout, TimeUnit timeUnit) throws Exception {
|
2015-11-12 14:26:31 +01:00
|
|
|
AccountingPersistenceBackendFactory.flushAll(timeout, timeUnit);
|
2015-06-30 17:32:30 +02:00
|
|
|
}
|
|
|
|
|
2015-10-12 18:03:39 +02:00
|
|
|
public void close() throws Exception{
|
|
|
|
AccountingPersistenceBackendFactory.getPersistenceBackend().close();
|
|
|
|
}
|
2015-05-28 13:19:25 +02:00
|
|
|
|
|
|
|
}
|