uri-resolver-manager/src/main/java/org/gcube/portlets/user/uriresolvermanager/util/RuntimeResourceReader.java

141 lines
4.1 KiB
Java

/**
*
*/
package org.gcube.portlets.user.uriresolvermanager.util;
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.Iterator;
import java.util.List;
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.Group;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.portlets.user.uriresolvermanager.entity.ServiceProperty;
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;
/**
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* @Oct 7, 2014
*
*/
public class RuntimeResourceReader {
public static final String RESOURCE_NAME = "Gis-Resolver";
public static final Logger logger = LoggerFactory.getLogger(RuntimeResourceReader.class);
public List<ServiceProperty> serviceProperties;
private String resourceName;
/**
*
* @param scope
* @return the application URI
* @throws Exception
*/
private String readResource(String scope, String resourceName) throws Exception
{
try{
this.resourceName = resourceName;
String infraName = ScopeUtil.getInfrastructureNameFromScope(scope);
logger.info("Instancing root scope: "+infraName);
ScopeProvider.instance.set(infraName);
SimpleQuery query = queryFor(ServiceEndpoint.class);
query.addCondition("$resource/Profile/Name/string() eq '"+resourceName+"'");
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: "+resourceName);
ServiceEndpoint se = r.get(0);
if(se.profile()==null)
throw new Exception("IS profile is null for resource: "+resourceName);
Group<AccessPoint> accessPoints = se.profile().accessPoints();
if(accessPoints.size()==0) throw new Exception("Accesspoint in resource "+resourceName+" not found");
AccessPoint ap = accessPoints.iterator().next();
logger.info("returning GIS resolver URI root scope: "+ap.address());
Group<Property> properties = ap.properties();
if(properties.size()==0){
logger.warn("Properties in resource "+resourceName+" not found");
}else{
serviceProperties = new ArrayList<ServiceProperty>(properties.size());
Iterator<Property> iter = properties.iterator();
while (iter.hasNext()) {
Property prop = iter.next();
serviceProperties.add(new ServiceProperty(prop.value(), true));
}
}
return ap.address();
// parameters.setUser(ap.username()); //username
//
// String decryptedPassword = StringEncrypter.getEncrypter().decrypt(ap.password());
//
// parameters.setPassword(decryptedPassword); //password
// Group<Property> properties = ap.properties();
}catch (Exception e) {
logger.error("Sorry, an error occurred on reading the resource "+RESOURCE_NAME+ "Runtime Reosurces",e);
throw new Exception("Sorry, an error occurred on reading the resource "+RESOURCE_NAME+ "Runtime Reosurces");
}
}
public List<ServiceProperty> getServiceProperties() {
return serviceProperties;
}
public String getResourceName() {
return resourceName;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("RuntimeResourceReader [serviceProperties=");
builder.append(serviceProperties);
builder.append(", resourceName=");
builder.append(resourceName);
builder.append("]");
return builder.toString();
}
public static void main(String[] args) {
RuntimeResourceReader resolver = new RuntimeResourceReader();
try {
resolver.readResource("/gcube", "Gis-Resolver");
System.out.println(resolver);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}