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;
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 String FORCED_URL;
private HTTPCall httpCall;
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()");
}
protected static void forceURL(String url) {
FORCED_URL = url;
}
/**
* {@inheritDoc}
*/
@Override
protected void prepareConnection(PersistenceBackendConfiguration configuration) throws Exception {
String url = configuration.getProperty(URL_PROPERTY_KEY);
if (url == null || url.compareTo("") == 0) {
logger.debug("Invalid URL provided from Configuration. Looking for RunningInstance.");
SimpleQuery serviceQuery = queryForService();
List<String> addresses = ICFactory.client().submit(serviceQuery);
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);
String url = null;
if(FORCED_URL!=null) {
url = FORCED_URL;
} else {
url = configuration.getProperty(URL_PROPERTY_KEY);
if (url == null || url.compareTo("") == 0) {
logger.debug("Invalid URL provided from Configuration. Looking for RunningInstance.");
SimpleQuery serviceQuery = queryForService();
List<String> addresses = ICFactory.client().submit(serviceQuery);
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);
@ -94,10 +101,9 @@ public class PersistenceAccountingService extends PersistenceBackend {
protected void accountWithFallback(Record... records) throws Exception {
try {
send(records);
} catch (Exception e) {
} catch (Throwable e) {
super.accountWithFallback(records);
}
}
@Override
@ -117,7 +123,6 @@ public class PersistenceAccountingService extends PersistenceBackend {
@Override
protected void clean() throws Exception {
// TODO Auto-generated method stub
}
}