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-12-18 17:09:05 +01:00
|
|
|
import org.gcube.documentstore.exception.InvalidValueException;
|
2016-02-19 12:41:41 +01:00
|
|
|
import org.gcube.documentstore.persistence.PersistenceBackend;
|
2015-12-18 17:09:05 +01:00
|
|
|
import org.gcube.documentstore.persistence.PersistenceBackendFactory;
|
|
|
|
import org.gcube.documentstore.records.Record;
|
2015-05-28 13:19:25 +02:00
|
|
|
|
|
|
|
/**
|
2016-10-12 14:15:28 +02:00
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
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
|
|
|
|
2016-09-26 13:03:17 +02:00
|
|
|
protected String scope;
|
2015-05-28 13:19:25 +02:00
|
|
|
|
2016-02-19 12:41:41 +01:00
|
|
|
protected AccountingPersistence(String scope){
|
2016-09-26 13:03:17 +02:00
|
|
|
this.scope = scope;
|
|
|
|
}
|
|
|
|
|
|
|
|
private PersistenceBackend getAccountingPersistence(){
|
|
|
|
return PersistenceBackendFactory.getPersistenceBackend(this.scope);
|
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.
|
2015-12-18 17:09:05 +01:00
|
|
|
* @param record the {@link #UsageRecord} to persist
|
|
|
|
* @throws InvalidValueException
|
2015-05-28 13:19:25 +02:00
|
|
|
*/
|
2015-12-18 17:09:05 +01:00
|
|
|
public void account(final Record record) throws InvalidValueException {
|
|
|
|
try {
|
2016-09-26 13:03:17 +02:00
|
|
|
getAccountingPersistence().account(record);
|
2015-12-18 17:09:05 +01:00
|
|
|
} catch (org.gcube.documentstore.exception.InvalidValueException e) {
|
|
|
|
throw new InvalidValueException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-14 12:20:36 +02:00
|
|
|
public void flush(long timeout, TimeUnit timeUnit) throws Exception {
|
2016-09-26 13:03:17 +02:00
|
|
|
getAccountingPersistence().flush(timeout, timeUnit);
|
2015-06-30 17:32:30 +02:00
|
|
|
}
|
|
|
|
|
2015-10-12 18:03:39 +02:00
|
|
|
public void close() throws Exception{
|
2016-09-26 13:03:17 +02:00
|
|
|
getAccountingPersistence().close();
|
2015-10-12 18:03:39 +02:00
|
|
|
}
|
2015-05-28 13:19:25 +02:00
|
|
|
|
2017-05-11 16:38:38 +02:00
|
|
|
|
|
|
|
|
2015-05-28 13:19:25 +02:00
|
|
|
}
|