ckan-util-library/src/test/java/org/gcube/datacatalogue/ckanutillibrary/server/RuntimeResourceReader.java

165 lines
4.2 KiB
Java

package org.gcube.datacatalogue.ckanutillibrary.server;
/**
*
*/
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
import java.util.ArrayList;
import java.util.List;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class RuntimeResourceReader.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Mar 10, 2017
*/
public class RuntimeResourceReader {
public static final Logger logger = LoggerFactory.getLogger(RuntimeResourceReader.class);
private List<String> scopes = new ArrayList<String>();
/**
* Instantiates a new runtime resource reader.
*
* @param scope the scope
* @param platformName the platform name
* @param endPoint the end point
* @throws Exception the exception
*/
public RuntimeResourceReader(String scope, String platformName, String runtimeName, String endPoint) throws Exception {
read(scope, platformName, runtimeName, endPoint);
}
/**
* Read.
*
* @param scope the scope
* @param runtimeName the runtime name
* @param endPoint the end point
* @return the server parameters
* @throws Exception the exception
*/
private void read(String scope, String platformName, String runtimeName, String endPoint) throws Exception{
String originalScope = null;
try{
originalScope = ScopeProvider.instance.get();
ScopeProvider.instance.set(scope);
SimpleQuery query = queryFor(ServiceEndpoint.class);
query.addCondition("$resource/Profile/Category/text() eq 'Application'");
//query.addCondition("$resource/Profile/Category/Name/text() eq '"+runtimeName+"'");
if(platformName!=null && !platformName.isEmpty())
query.addCondition("$resource/Profile/Platform/Name/text() eq '"+platformName+"'");
if(endPoint!=null && !endPoint.isEmpty())
query.addCondition("$resource/Profile/AccessPoint/Interface/Endpoint/text() eq '"+endPoint+"'");
logger.info("RuntimeReader, using scope: "+scope + ", query is: "+query.toString());
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> r = client.submit(query);
if (r == null || r.isEmpty()) throw new Exception("Cannot retrieve the runtime resource: "+runtimeName);
for (ServiceEndpoint serviceEndpoint : r) {
System.out.println(serviceEndpoint.toString());
}
ServiceEndpoint se = r.get(0);
if(se.profile()==null)
throw new Exception("IS profile is null for resource: "+platformName);
scopes.addAll(se.scopes().asCollection());
}catch (Exception e) {
System.out.println(e);
logger.error("Sorry, an error occurred on reading parameters in Runtime Resources",e);
}finally{
if(originalScope!=null && !originalScope.isEmpty()){
ScopeProvider.instance.set(originalScope);
logger.info("scope provider setted to orginal scope: "+originalScope);
}else{
ScopeProvider.instance.reset();
logger.info("scope provider reset");
}
}
return;
}
/**
* Gets the scopes.
*
* @return the scopes
*/
public List<String> getScopes() {
return scopes;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("RuntimeResourceReader [scopes=");
builder.append(scopes);
builder.append("]");
return builder.toString();
}
// /**
// * The main method.
// *
// * @param args the arguments
// */
// public static void main(String[] args) {
//
// String scope = "/d4science.research-infrastructures.eu";
// String platformName = null;
// String runtimeName = "CKanDataCatalogue";
// String endPoint = null;
// RuntimeResourceReader reader;
// try {
// reader = new RuntimeResourceReader(scope, platformName, runtimeName, endPoint);
// System.out.println(reader.toString());
//
// System.out.println("Scopes are: "+reader.getScopes().size());
// }
// catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
// }
}