diff --git a/src/main/java/org/gcube/application/framework/core/util/RuntimeResource.java b/src/main/java/org/gcube/application/framework/core/util/RuntimeResource.java new file mode 100644 index 0000000..2f568aa --- /dev/null +++ b/src/main/java/org/gcube/application/framework/core/util/RuntimeResource.java @@ -0,0 +1,161 @@ +package org.gcube.application.framework.core.util; + + +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.Query; +import org.gcube.resources.discovery.client.queries.api.SimpleQuery; + +import static org.gcube.resources.discovery.icclient.ICFactory.*; + +import java.rmi.RemoteException; +import java.util.ArrayList; +import java.util.List; + +import org.gcube.application.framework.core.cache.CachesManager; +import org.gcube.application.framework.core.genericresources.model.ISGenericResource; +import org.gcube.application.framework.core.session.ASLSession; +import org.gcube.application.framework.core.session.SessionManager; +import org.gcube.common.resources.gcore.ServiceEndpoint; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.informationsystem.publisher.RegistryPublisher; +import org.gcube.informationsystem.publisher.RegistryPublisherFactory; +import org.gcube.informationsystem.publisher.ScopedPublisher; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RuntimeResource { + + /** The logger. */ + private static final Logger logger = LoggerFactory.getLogger(RuntimeResource.class); + + + protected static ScopedPublisher scopedPublisher = null; + protected static RegistryPublisher publisher = null; + protected static DiscoveryClient client = null; + + /** + * The D4Science session to be used + */ + ASLSession session; + + + public RuntimeResource(String extrenalSessionID, String username) + { + session = SessionManager.getInstance().getASLSession(extrenalSessionID, username); + try { + ScopeProvider.instance.set(session.getScope()); + scopedPublisher = RegistryPublisherFactory.scopedPublisher(); + publisher = RegistryPublisherFactory.create(); + } catch (Exception e) { + logger.error("Exception:", e); + } + if(client == null) + { + try { + client = clientFor(ServiceEndpoint.class); + } catch (Exception e) { + // TODO Auto-generated catch block + logger.error("Exception:", e); + client = null; + } + } + } + + + public RuntimeResource(ASLSession session) { + super(); + this.session = session; + try { + ScopeProvider.instance.set(session.getScope()); + scopedPublisher = RegistryPublisherFactory.scopedPublisher(); + publisher = RegistryPublisherFactory.create(); + } catch (Exception e) { + logger.error("Exception:", e); + } + if(client == null) + { + try { + client = clientFor(ServiceEndpoint.class); + } catch (Exception e) { + logger.error("Exception:", e); + client = null; + } + } + } + + /** + * adds the RuntimeResource to the IS + * @param runtimeResource + * @return the id of the newly created runtime resource. + * @throws RemoteException + */ + public String createRuntimeResource(ServiceEndpoint runtimeResource) throws RemoteException { + try { + List scopes=new ArrayList(); + scopes.add(session.getScope()); + ServiceEndpoint se = scopedPublisher.create(runtimeResource, scopes); + logger.info("Created Generic Resource with id: "+se.id()+" on scope: "+scopes.toString()); + return se.id(); + } catch (Exception e) { + logger.error("Exception:", e); + throw new RemoteException(); + } + } + + /** + * it replaces the runtime resource with this one + * @param runtimeResource + * @return + */ + public String updateRuntimeResource(ServiceEndpoint runtimeResource) throws RemoteException { + try { + ServiceEndpoint se = scopedPublisher.update(runtimeResource); + logger.info("Updated Generic Resource with id: "+runtimeResource.id()+"\tNew id : "+se.id()); + return se.id(); + } catch (Exception e) { + logger.error("Exception:", e); + throw new RemoteException(); + } + } + + + /** + * it replaces the runtime resource with this one + * @param runtimeResource + * @return + */ + public String deleteRuntimeResource(ServiceEndpoint runtimeResource) throws RemoteException { + try { + List scopes=new ArrayList(); + scopes.add(session.getScope()); + ServiceEndpoint se = scopedPublisher.remove(runtimeResource,scopes); + logger.info("Deleted Generic Resource with id: "+runtimeResource.id()); + return se.id(); + } catch (Exception e) { + logger.error("Exception:", e); + throw new RemoteException(); + } + } + + public List getRuntimeResourceByName(String name){ + SimpleQuery query = queryFor(ServiceEndpoint.class); + DiscoveryClient client = clientFor(ServiceEndpoint.class); + query.addCondition("$resource/Profile/Name eq '"+name+"'"); + List resources = client.submit(query); + return resources; + } + + public List getRuntimeResourceById(String id){ + SimpleQuery query = queryFor(ServiceEndpoint.class); + DiscoveryClient client = clientFor(ServiceEndpoint.class); + query.addCondition("$resource/ID eq '"+id+"'"); + List resources = client.submit(query); + return resources; + } + + + + + +}