Added code and exception to solve ties at root scope level when querying for service endpoints

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/ckan-util-library@130418 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-07-14 16:00:38 +00:00
parent d82c7e0279
commit bf7e442304
3 changed files with 137 additions and 38 deletions

View File

@ -15,9 +15,11 @@ import javax.xml.parsers.DocumentBuilderFactory;
import org.gcube.common.encryption.StringEncrypter;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
import org.gcube.common.resources.gcore.utils.XPathHelper;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datacatalogue.ckanutillibrary.exceptions.ApplicationProfileNotFoundException;
import org.gcube.datacatalogue.ckanutillibrary.exceptions.NoApplicationProfileMasterException;
import org.gcube.datacatalogue.ckanutillibrary.exceptions.NoCKanRuntimeResourceException;
import org.gcube.datacatalogue.ckanutillibrary.exceptions.ServiceEndPointException;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
@ -50,6 +52,9 @@ public class CKanRunningCluster {
// api key property
private final static String API_KEY_PROPERTY = "API_KEY";
// property to retrieve the master service endpoint into the /root scope
private final static String IS_MASTER_ROOT_KEY_PROPERTY = "IS_ROOT_MASTER"; // true, false.. missing means false as well
// retrieved data
private List<String> datacatalogueUrls = new ArrayList<String>();
private List<String> hostsDB = new ArrayList<String>();
@ -83,37 +88,79 @@ public class CKanRunningCluster {
throw new NoCKanRuntimeResourceException();
}
else {
if(resources.size() > 1)
logger.warn("Too many Runtime Resource having name " + RUNTIME_DB_RESOURCE_NAME +" in this scope");
try{
logger.debug(resources.toString());
for (ServiceEndpoint res : resources) {
if(resources.size() > 1){
boolean oneWasMaster = false;
Iterator<AccessPoint> accessPointIterator = res.profile().accessPoints().iterator();
logger.warn("Too many Runtime Resource having name " + RUNTIME_DB_RESOURCE_NAME +" in this scope.. Looking for the one that has the property " + IS_MASTER_ROOT_KEY_PROPERTY);
while (accessPointIterator.hasNext()) {
ServiceEndpoint.AccessPoint accessPoint = (ServiceEndpoint.AccessPoint) accessPointIterator
.next();
for (ServiceEndpoint res : resources) {
// add this host
hostsDB.add(accessPoint.address().split(":")[0]);
Iterator<AccessPoint> accessPointIterator = res.profile().accessPoints().iterator();
// save the port
int port = Integer.parseInt(accessPoint.address().split(":")[1]);
portsDB.add(port);
while (accessPointIterator.hasNext()) {
ServiceEndpoint.AccessPoint accessPoint = (ServiceEndpoint.AccessPoint) accessPointIterator
.next();
// save the name of the cluster (this should be unique)
nameDB = accessPoint.name();
// get the is master property
Property entry = accessPoint.propertyMap().get(IS_MASTER_ROOT_KEY_PROPERTY);
String isMaster = entry != null ? entry.value() : null;
// save user and password
passwordDB = StringEncrypter.getEncrypter().decrypt(accessPoint.password());
userDB = accessPoint.username();
if(isMaster == null || !isMaster.equals("true"))
continue;
break;
// set this variable
oneWasMaster = true;
// add this host
hostsDB.add(accessPoint.address().split(":")[0]);
// save the port
int port = Integer.parseInt(accessPoint.address().split(":")[1]);
portsDB.add(port);
// save the name of the cluster (this should be unique)
nameDB = accessPoint.name();
// save user and password
passwordDB = StringEncrypter.getEncrypter().decrypt(accessPoint.password());
userDB = accessPoint.username();
// now break
break;
}
}
// if none of them was master, throw an exception
if(!oneWasMaster)
throw new NoApplicationProfileMasterException();
}else{
logger.debug(resources.toString());
for (ServiceEndpoint res : resources) {
Iterator<AccessPoint> accessPointIterator = res.profile().accessPoints().iterator();
while (accessPointIterator.hasNext()) {
ServiceEndpoint.AccessPoint accessPoint = (ServiceEndpoint.AccessPoint) accessPointIterator
.next();
// add this host
hostsDB.add(accessPoint.address().split(":")[0]);
// save the port
int port = Integer.parseInt(accessPoint.address().split(":")[1]);
portsDB.add(port);
// save the name of the cluster (this should be unique)
nameDB = accessPoint.name();
// save user and password
passwordDB = StringEncrypter.getEncrypter().decrypt(accessPoint.password());
userDB = accessPoint.username();
}
}
}
}catch(Exception e ){
@ -131,29 +178,63 @@ public class CKanRunningCluster {
throw new NoCKanRuntimeResourceException();
}
else {
if(resources.size() > 1)
logger.warn("Too many Runtime Resource having name " + RUNTIME_CATALOGUE_RESOURCE_NAME +" in this scope");
logger.debug(resources.toString());
try{
if(resources.size() > 1){
boolean oneWasMaster = false;
logger.debug(resources.toString());
for (ServiceEndpoint res : resources) {
logger.warn("Too many Runtime Resource having name " + RUNTIME_CATALOGUE_RESOURCE_NAME +" in this scope.. Looking for the one that has the property " + IS_MASTER_ROOT_KEY_PROPERTY);
Iterator<AccessPoint> accessPointIterator = res.profile().accessPoints().iterator();
for (ServiceEndpoint res : resources) {
while (accessPointIterator.hasNext()) {
ServiceEndpoint.AccessPoint accessPoint = (ServiceEndpoint.AccessPoint) accessPointIterator
.next();
Iterator<AccessPoint> accessPointIterator = res.profile().accessPoints().iterator();
// add this host
datacatalogueUrls.add(accessPoint.address());
while (accessPointIterator.hasNext()) {
ServiceEndpoint.AccessPoint accessPoint = (ServiceEndpoint.AccessPoint) accessPointIterator
.next();
// retrieve sys admin token
sysAdminToken = accessPoint.propertyMap().get(API_KEY_PROPERTY).value();
sysAdminToken = StringEncrypter.getEncrypter().decrypt(sysAdminToken);
// get the is master property
Property entry = accessPoint.propertyMap().get(IS_MASTER_ROOT_KEY_PROPERTY);
String isMaster = entry != null ? entry.value() : null;
break;
if(isMaster == null || !isMaster.equals("true"))
continue;
// set this variable
oneWasMaster = true;
// add this host
datacatalogueUrls.add(accessPoint.address());
// retrieve sys admin token
sysAdminToken = accessPoint.propertyMap().get(API_KEY_PROPERTY).value();
sysAdminToken = StringEncrypter.getEncrypter().decrypt(sysAdminToken);
// break now
break;
}
}
// if none of them was master, throw an exception
if(!oneWasMaster)
throw new NoApplicationProfileMasterException();
}else{
for (ServiceEndpoint res : resources) {
Iterator<AccessPoint> accessPointIterator = res.profile().accessPoints().iterator();
while (accessPointIterator.hasNext()) {
ServiceEndpoint.AccessPoint accessPoint = (ServiceEndpoint.AccessPoint) accessPointIterator
.next();
// add this host
datacatalogueUrls.add(accessPoint.address());
// retrieve sys admin token
sysAdminToken = accessPoint.propertyMap().get(API_KEY_PROPERTY).value();
sysAdminToken = StringEncrypter.getEncrypter().decrypt(sysAdminToken);
}
}
}
}catch(Exception e ){

View File

@ -0,0 +1,20 @@
package org.gcube.datacatalogue.ckanutillibrary.exceptions;
/**
* Thrown when there are more than one application profile, but none of them was set as master
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/
public class NoApplicationProfileMasterException extends Exception {
private static final long serialVersionUID = 5874713540422734005L;
private static final String DEFAULT_MESSAGE = "There is more than one application profile into this scope"
+ " but none of them is set as master!";
public NoApplicationProfileMasterException(){
super(DEFAULT_MESSAGE);
}
public NoApplicationProfileMasterException(String message) {
super(message);
}
}

View File

@ -19,6 +19,4 @@ public class NoCKanRuntimeResourceException extends Exception {
public NoCKanRuntimeResourceException(String message) {
super(message);
}
}