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
|
|
|
|
2017-09-06 11:47:13 +02:00
|
|
|
protected String context;
|
2015-05-28 13:19:25 +02:00
|
|
|
|
2017-09-06 11:47:13 +02:00
|
|
|
protected AccountingPersistence(String context){
|
|
|
|
this.context = context;
|
2016-09-26 13:03:17 +02:00
|
|
|
}
|
|
|
|
|
2018-11-02 12:36:39 +01:00
|
|
|
private PersistenceBackend getPersistenceBackend(){
|
2017-09-06 11:47:13 +02:00
|
|
|
return PersistenceBackendFactory.getPersistenceBackend(this.context);
|
2015-07-01 10:56:44 +02:00
|
|
|
}
|
|
|
|
|
2015-05-28 13:19:25 +02:00
|
|
|
/**
|
2017-10-27 18:17:37 +02:00
|
|
|
* Persist the 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
|
2017-10-27 18:17:37 +02:00
|
|
|
* so that the UsageRecord can be recorder later.
|
|
|
|
* @param record the UsageRecord to persist
|
2015-12-18 17:09:05 +01:00
|
|
|
* @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 {
|
2018-11-02 12:36:39 +01:00
|
|
|
getPersistenceBackend().account(record);
|
2015-12-18 17:09:05 +01:00
|
|
|
} catch (org.gcube.documentstore.exception.InvalidValueException e) {
|
|
|
|
throw new InvalidValueException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-06 11:47:13 +02:00
|
|
|
public void flush() throws Exception {
|
2018-11-02 12:36:39 +01:00
|
|
|
getPersistenceBackend().flush();
|
2017-09-06 11:47:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used {@link AccountingPersistence#flush() instead}
|
|
|
|
* @param timeout
|
|
|
|
* @param timeUnit
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
@Deprecated
|
2015-07-14 12:20:36 +02:00
|
|
|
public void flush(long timeout, TimeUnit timeUnit) throws Exception {
|
2017-09-06 11:47:13 +02:00
|
|
|
this.flush();
|
2015-06-30 17:32:30 +02:00
|
|
|
}
|
|
|
|
|
2015-10-12 18:03:39 +02:00
|
|
|
public void close() throws Exception{
|
2018-11-02 12:36:39 +01:00
|
|
|
getPersistenceBackend().close();
|
2015-10-12 18:03:39 +02:00
|
|
|
}
|
2015-05-28 13:19:25 +02:00
|
|
|
|
2017-09-06 11:47:13 +02:00
|
|
|
public boolean isConnectionActive() throws Exception {
|
2018-11-02 12:36:39 +01:00
|
|
|
return getPersistenceBackend().isConnectionActive();
|
2017-09-06 11:47:13 +02:00
|
|
|
}
|
2017-05-11 16:38:38 +02:00
|
|
|
|
2015-05-28 13:19:25 +02:00
|
|
|
}
|