aslcore/src/org/gcube/application/framework/core/vremanagement/impl/ISInfo.java

225 lines
7.6 KiB
Java

package org.gcube.application.framework.core.vremanagement.impl;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.axis.message.addressing.Address;
import org.apache.axis.message.addressing.EndpointReferenceType;
import org.gcube.application.framework.core.security.PortalSecurityManager;
import org.gcube.application.framework.core.session.D4ScienceSession;
import org.gcube.application.framework.core.vremanagement.ISInfoI;
import org.gcube.common.core.contexts.GCUBERemotePortTypeContext;
import org.gcube.common.core.contexts.GHNContext;
import org.gcube.common.core.informationsystem.client.AtomicCondition;
import org.gcube.common.core.informationsystem.client.ISClient;
import org.gcube.common.core.informationsystem.client.RPDocument;
import org.gcube.common.core.informationsystem.client.XMLResult;
import org.gcube.common.core.informationsystem.client.queries.GCUBEGenericQuery;
import org.gcube.common.core.informationsystem.client.queries.WSResourceQuery;
import org.gcube.common.core.informationsystem.publisher.ISPublisher;
import org.gcube.common.core.informationsystem.publisher.ISPublisherException;
import org.gcube.common.core.resources.GCUBEExternalRunningInstance;
import org.gcube.common.core.resources.GCUBEResource;
import org.gcube.common.core.security.GCUBESecurityManagerImpl;
import org.gcube.common.core.state.GCUBEResourceHome;
import org.gcube.common.vremanagement.ghnmanager.stubs.AddScopeInputParams;
import org.gcube.common.vremanagement.ghnmanager.stubs.GHNManagerPortType;
import org.gcube.common.vremanagement.ghnmanager.stubs.ScopeRIParams;
import org.gcube.common.vremanagement.ghnmanager.stubs.service.GHNManagerServiceAddressingLocator;
//TODO: done!!!
public class ISInfo implements ISInfoI {
/**
* D4ScienceSession to be used
*/
protected D4ScienceSession session;
/**
* is client in order to query IS
*/
protected static ISClient client = null;
protected static ISPublisher publisher = null;
/**
* Constructs a ISInfo object
* @param session the D4Science session to be used for retrieving information needed
*/
public ISInfo(D4ScienceSession session)
{
this.session = session;
try {
publisher = GHNContext.getImplementation(ISPublisher.class);
} catch (Exception e) {
e.printStackTrace();
}
if(client == null)
{
try {
client = GHNContext.getImplementation(ISClient.class);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
client = null;
}
}
}
/* (non-Javadoc)
* @see org.gcube.application.framework.ISInfoI#queryIS(java.lang.String)
*/
public List<XMLResult> queryIS(String query) {
try {
GCUBEGenericQuery queryMan = client.getQuery(GCUBEGenericQuery.class);
queryMan.setExpression(query);
System.out.println(session.getScopeName());
return client.execute(queryMan, session.getScope());
} catch (Exception e) {
e.printStackTrace();
}
return new ArrayList<XMLResult>();
}
/* (non-Javadoc)
* @see org.gcube.application.framework.ISInfoI#getWS(java.lang.String)
*/
public List<RPDocument> getWS(String type)
{
WSResourceQuery query = null;
try {
query = client.getQuery(WSResourceQuery.class);
query.addAtomicConditions(new AtomicCondition("/gc:ServiceClass", type));
return client.execute(query, session.getScope());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new ArrayList<RPDocument>();
}
/* (non-Javadoc)
* @see org.gcube.application.framework.ISInfoI#addGHNToScope(java.lang.String)
*/
public void addGHNToScope(String url) {
//TODO: this will probably change...
GCUBESecurityManagerImpl managerSec = new PortalSecurityManager(session);
EndpointReferenceType endpoint = new EndpointReferenceType();
try {
endpoint.setAddress(new Address(url));
GHNManagerServiceAddressingLocator locator = new GHNManagerServiceAddressingLocator();
GHNManagerPortType pt = GCUBERemotePortTypeContext.getProxy(locator.getGHNManagerPortTypePort(endpoint),
session.getScope(),managerSec);
AddScopeInputParams params = new AddScopeInputParams();
params.setScope(session.getScopeName());
params.setMap(""); //eventually, set here the new Service Map
pt.addScope(params);
} catch (Exception e) {
e.printStackTrace();
}
}
public void removeGHNToScope(String url) {
//TODO: this will probably change...
GCUBESecurityManagerImpl managerSec = new PortalSecurityManager(session);
EndpointReferenceType endpoint = new EndpointReferenceType();
try {
endpoint.setAddress(new Address(url));
GHNManagerServiceAddressingLocator locator = new GHNManagerServiceAddressingLocator();
GHNManagerPortType pt = GCUBERemotePortTypeContext.getProxy(locator.getGHNManagerPortTypePort(endpoint),
session.getScope(),managerSec);
pt.removeScope(session.getScopeName());
} catch (Exception e) {
e.printStackTrace();
}
}
/* (non-Javadoc)
* @see org.gcube.application.framework.ISInfoI#addRIToScope(java.lang.String, java.lang.String, java.lang.String)
*/
public void addRIToScope(String url, String className, String name) {
//TODO: this will probably change...
GCUBESecurityManagerImpl managerSec = new PortalSecurityManager(session);
EndpointReferenceType endpoint = new EndpointReferenceType();
try {
endpoint.setAddress(new Address(url));
GHNManagerServiceAddressingLocator locator = new GHNManagerServiceAddressingLocator();
GHNManagerPortType pt = GCUBERemotePortTypeContext.getProxy(locator.getGHNManagerPortTypePort(endpoint),
session.getScope(), managerSec);
ScopeRIParams params = new ScopeRIParams();
params.setClazz(className);
params.setName(name);
params.setScope(session.getScopeName());
pt.addRIToScope(params);
} catch (Exception e) {
e.printStackTrace();
}
}
public void addExternalRIToVRE(File file)
{
GCUBEExternalRunningInstance ri = null;
try {
ri = GHNContext.getImplementation(GCUBEExternalRunningInstance.class);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String ret = "";
if(file.exists()){
try {
ri.load(new FileReader(file));
} catch (Exception e) {
String message = "Error while loading profile for the External Running Instance with id=" + ri.getID() + "\n";
System.out.println(message);
e.printStackTrace();
ret += message;
}
}else{
String message = "An error occur during the approval of the External Running Instance with id=" + ri.getID() + "\n";
System.out.println(message);
ret += message;
}
try {
// TODO Change this
publisher.registerGCUBEResource(ri, session.getScope(), new PortalSecurityManager(session));
} catch (ISPublisherException e) {
String message = "Registration error for the External Running Instance with id=" + ri.getID() + "\n";
System.out.println(message);
e.printStackTrace();
ret += message;
}
}
public void removeExternalRIToVRE(String id)
{
try {
// TODO Change this
publisher.removeGCUBEResource(id, GCUBEExternalRunningInstance.TYPE, session.getScope(), new PortalSecurityManager(session));
} catch (ISPublisherException e) {
String message = "Registration error for the External Running Instance with id=" +id + "\n";
System.out.println(message);
e.printStackTrace();
}
}
}