X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;ds=sidebyside;f=runtime%2Forg.argeo.slc.server%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fserver%2Fclient%2FSlcServerHttpClient.java;h=6087d2a1b7f470d0f4e15098349892edf64f9bed;hb=b0c2c01573db47690afdf723e49fb7fa39561e8e;hp=7102a749c014dc6d43a9d411416efe13bde6932b;hpb=3e1feafae6dee22c13272e992fc967cc42776f15;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/server/client/SlcServerHttpClient.java b/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/server/client/SlcServerHttpClient.java index 7102a749c..6087d2a1b 100644 --- a/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/server/client/SlcServerHttpClient.java +++ b/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/server/client/SlcServerHttpClient.java @@ -1,121 +1,40 @@ package org.argeo.slc.server.client; -import java.io.IOException; -import java.io.InputStream; -import java.net.HttpURLConnection; -import java.net.URL; +import java.util.List; import java.util.Map; -import javax.xml.transform.stream.StreamSource; +import org.argeo.slc.execution.ExecutionModuleDescriptor; +import org.argeo.slc.msg.ExecutionAnswer; +import org.argeo.slc.process.RealizedFlow; +import org.argeo.slc.runtime.SlcAgentDescriptor; -import org.apache.commons.io.IOUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.argeo.slc.SlcException; -import org.springframework.oxm.Unmarshaller; -import org.springframework.util.Assert; +/** Abstraction of the access to HTTP services of an SLC Server. */ +public interface SlcServerHttpClient extends HttpServicesClient { + public final static String LIST_AGENTS = "listAgents.service"; + public final static String IS_SERVER_READY = "isServerReady.service"; + public final static String NEW_SLC_EXECUTION = "newSlcExecution.service"; + public final static String GET_MODULE_DESCRIPTOR = "getExecutionDescriptor.service"; + public final static String LIST_MODULE_DESCRIPTORS = "listModulesDescriptors.service"; + public final static String LIST_RESULTS = "listResults.service"; -public class SlcServerHttpClient { - private final static Log log = LogFactory.getLog(SlcServerHttpClient.class); + /** Wait for one agent to be available. */ + public SlcAgentDescriptor waitForOneAgent(); - private Unmarshaller unmarshaller; - private String baseUrl; + /** Wait for the http server to be ready. */ + public void waitForServerToBeReady(); - private Long retryPeriod = 1000l; + /** Start an execution flow on the given agent. */ + public ExecutionAnswer startFlow(String agentId, RealizedFlow realizedFlow); - @SuppressWarnings(value = { "unchecked" }) - public T callService(String path, Map parameters) { - try { - return (T) callServiceLowLevel(path, parameters); - } catch (Exception e) { - throw new SlcException("Cannot call service " + path + " on " - + baseUrl, e); - } - } + /** Assume one agent and one version per module. */ + public ExecutionAnswer startFlowDefault(String moduleName, String flowName, + Map args); - @SuppressWarnings(value = { "unchecked" }) - public T callServiceSafe(String path, Map parameters, - long timeout) { - long begin = System.currentTimeMillis(); - try { - Object obj = null; - long duration = System.currentTimeMillis() - begin; - while (duration < timeout) { - try { - obj = callServiceLowLevel(path, parameters); - } catch (IOException e) { - if (log.isTraceEnabled()) - log.trace("Exception when calling service " + path - + " on " + baseUrl, e); - } + /** List execution modules descriptors. */ + public List listModuleDescriptors(String agentId); - if (obj != null) - break; - - // wait a bit - try { - Thread.sleep(retryPeriod); - } catch (InterruptedException e) { - // silent - } - } - - if (obj == null) - throw new SlcException( - "Service " - + path - + " on " - + baseUrl - + " did not return an answer after calling it safely for " - + duration + " ms."); - return (T) obj; - } catch (Exception e) { - throw new SlcException( - "Unexpected exception when safely calling service " + path - + " on " + baseUrl, e); - } - } - - protected Object callServiceLowLevel(String path, - Map parameters) throws IOException { - Assert.notNull(baseUrl, "base url"); - InputStream in = null; - try { - URL url = new URL(baseUrl + path); - HttpURLConnection connection = (HttpURLConnection) url - .openConnection(); - if (parameters != null) { - for (String key : parameters.keySet()) { - connection.addRequestProperty(key, parameters.get(key)); - } - } - - connection.connect(); - - in = connection.getInputStream(); - StreamSource source = new StreamSource(in); - Object obj = unmarshaller.unmarshal(source); - return obj; - } finally { - IOUtils.closeQuietly(in); - } - } - - public void setUnmarshaller(Unmarshaller unmarshaller) { - this.unmarshaller = unmarshaller; - } - - public void setBaseUrl(String baseUrl) { - this.baseUrl = baseUrl; - } - - public Long getRetryPeriod() { - return retryPeriod; - } - - /** Retry period in ms when accessing service safely. Default is 1000 ms. */ - public void setRetryPeriod(Long retryPeriod) { - this.retryPeriod = retryPeriod; - } + /** Retrieve a single execution module descriptot. */ + public ExecutionModuleDescriptor getModuleDescriptor(String agentId, + String moduleName, String version); }