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> T callService(String path, Map<String, String> 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<String, Object> args);
- @SuppressWarnings(value = { "unchecked" })
- public <T> T callServiceSafe(String path, Map<String, String> 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<ExecutionModuleDescriptor> 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<String, String> 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);
}