Added possibility to force URL

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/document-store-lib-accounting-service@160518 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-12-14 17:09:49 +00:00
parent 5a889f1f63
commit dabcadaf5d
1 changed files with 25 additions and 20 deletions

View File

@ -1,6 +1,3 @@
/**
*
*/
package org.gcube.documentstore.persistence; package org.gcube.documentstore.persistence;
import java.util.Arrays; import java.util.Arrays;
@ -34,6 +31,8 @@ public class PersistenceAccountingService extends PersistenceBackend {
private static final String USER_AGENT = "document-store-lib-accounting-service"; private static final String USER_AGENT = "document-store-lib-accounting-service";
private static String FORCED_URL;
private HTTPCall httpCall; private HTTPCall httpCall;
private static String classFormat = "$resource/Profile/ServiceClass/text() eq '%1s'"; private static String classFormat = "$resource/Profile/ServiceClass/text() eq '%1s'";
@ -48,26 +47,34 @@ public class PersistenceAccountingService extends PersistenceBackend {
.addCondition(String.format(containsFormat, SERVICE_ENTRY_NAME)).setResult("$entry/text()"); .addCondition(String.format(containsFormat, SERVICE_ENTRY_NAME)).setResult("$entry/text()");
} }
protected static void forceURL(String url) {
FORCED_URL = url;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
protected void prepareConnection(PersistenceBackendConfiguration configuration) throws Exception { protected void prepareConnection(PersistenceBackendConfiguration configuration) throws Exception {
String url = null;
String url = configuration.getProperty(URL_PROPERTY_KEY); if(FORCED_URL!=null) {
if (url == null || url.compareTo("") == 0) { url = FORCED_URL;
logger.debug("Invalid URL provided from Configuration. Looking for RunningInstance."); } else {
SimpleQuery serviceQuery = queryForService(); url = configuration.getProperty(URL_PROPERTY_KEY);
List<String> addresses = ICFactory.client().submit(serviceQuery); if (url == null || url.compareTo("") == 0) {
if (addresses == null || addresses.isEmpty()) { logger.debug("Invalid URL provided from Configuration. Looking for RunningInstance.");
String error = String.format("No Running Instance %s:%s found in the current context", SERVICE_CLASS, SimpleQuery serviceQuery = queryForService();
SERVICE_NAME); List<String> addresses = ICFactory.client().submit(serviceQuery);
throw new Exception(error); if (addresses == null || addresses.isEmpty()) {
String error = String.format("No Running Instance %s:%s found in the current context", SERVICE_CLASS,
SERVICE_NAME);
throw new Exception(error);
}
Random random = new Random();
int index = random.nextInt(addresses.size());
url = addresses.get(index);
} }
Random random = new Random();
int index = random.nextInt(addresses.size());
url = addresses.get(index);
} }
logger.debug("Accounting Service URL to be contacted is {}", url); logger.debug("Accounting Service URL to be contacted is {}", url);
@ -94,10 +101,9 @@ public class PersistenceAccountingService extends PersistenceBackend {
protected void accountWithFallback(Record... records) throws Exception { protected void accountWithFallback(Record... records) throws Exception {
try { try {
send(records); send(records);
} catch (Exception e) { } catch (Throwable e) {
super.accountWithFallback(records); super.accountWithFallback(records);
} }
} }
@Override @Override
@ -117,7 +123,6 @@ public class PersistenceAccountingService extends PersistenceBackend {
@Override @Override
protected void clean() throws Exception { protected void clean() throws Exception {
// TODO Auto-generated method stub // TODO Auto-generated method stub
} }
} }