Merge branch 'pullrequest' into docker
diff --git a/taverna-docker-activity/src/main/java/org/apache/taverna/activities/docker/DockerActivity.java b/taverna-docker-activity/src/main/java/org/apache/taverna/activities/docker/DockerActivity.java
index 4ac2acf..0562a0b 100644
--- a/taverna-docker-activity/src/main/java/org/apache/taverna/activities/docker/DockerActivity.java
+++ b/taverna-docker-activity/src/main/java/org/apache/taverna/activities/docker/DockerActivity.java
@@ -189,6 +189,10 @@
     }
 
 
+    public DockerContainerConfiguration getContainerConfiguration() {
+        return containerConfiguration;
+    }
+
     private String getRenderedParam(ReferenceService referenceService, InvocationContext context, T2Reference key) {
         return (String) referenceService.renderIdentifier(key, String.class, context);
 
@@ -205,11 +209,6 @@
     return out;
     }
 
-    private boolean isStarted(Container container){
-        return  container.getStatus() != null
-                && container.getStatus().startsWith("Up");
-    }
-
     private Container getContainerFromName(RemoteClient remoteClient, String containerName){
         List<Container> containerList = remoteClient.listContainers();
         for(Container container : containerList){
@@ -221,6 +220,13 @@
                 }
             }
         }
-    return null;
+        return null;
     }
+
+    private boolean isStarted(Container container){
+        return  container.getStatus() != null
+                && container.getStatus().startsWith("Up");
+    }
+
+
 }
diff --git a/taverna-docker-activity/src/main/java/org/apache/taverna/activities/docker/DockerActivityHealthChecker.java b/taverna-docker-activity/src/main/java/org/apache/taverna/activities/docker/DockerActivityHealthChecker.java
new file mode 100644
index 0000000..aab03ec
--- /dev/null
+++ b/taverna-docker-activity/src/main/java/org/apache/taverna/activities/docker/DockerActivityHealthChecker.java
@@ -0,0 +1,49 @@
+package org.apache.taverna.activities.docker;
+
+import org.apache.taverna.visit.VisitReport;
+import org.apache.taverna.workflowmodel.health.HealthCheck;
+import org.apache.taverna.workflowmodel.health.HealthChecker;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class DockerActivityHealthChecker implements HealthChecker<Object> {
+
+    @Override
+    public boolean canVisit(Object o) {
+        return o instanceof DockerActivity;
+    }
+
+    @Override
+    public VisitReport visit(Object o, List<Object> list) {
+        DockerActivity activity = (DockerActivity) o;
+        DockerContainerConfiguration contCfg = activity.getContainerConfiguration();
+        String containerName = contCfg.getName();
+        boolean createValid = ValidationUtil.validateCreateContainer(contCfg, containerName);
+
+        List<VisitReport> reports = new ArrayList<VisitReport>();
+
+        if (createValid) {
+            reports.add(new VisitReport(HealthCheck.getInstance(), activity,
+                    "Docker create container operation is healthy",
+                    HealthCheck.NO_PROBLEM, VisitReport.Status.OK));
+        } else {
+            reports.add(new VisitReport(HealthCheck.getInstance(), activity,
+                    "Docker container creation health check failed. " +
+                            "A container with name " + containerName + " already exists.",
+                    HealthCheck.INVALID_CONFIGURATION, VisitReport.Status.SEVERE));
+        }
+
+        // collection all reports together
+        VisitReport.Status worstStatus = VisitReport.getWorstStatus(reports);
+        VisitReport report = new VisitReport(HealthCheck.getInstance(), activity,
+                "Docker Activity Health Report", HealthCheck.NO_PROBLEM, worstStatus, reports);
+
+        return report;
+    }
+
+    @Override
+    public boolean isTimeConsuming() {
+        return false;
+    }
+}
diff --git a/taverna-docker-activity/src/main/java/org/apache/taverna/activities/docker/ValidationUtil.java b/taverna-docker-activity/src/main/java/org/apache/taverna/activities/docker/ValidationUtil.java
new file mode 100644
index 0000000..6246f33
--- /dev/null
+++ b/taverna-docker-activity/src/main/java/org/apache/taverna/activities/docker/ValidationUtil.java
@@ -0,0 +1,77 @@
+package org.apache.taverna.activities.docker;
+
+import com.github.dockerjava.api.model.Container;
+
+import java.util.List;
+
+public class ValidationUtil {
+
+    /**
+     * Validates create container eligibility
+     * @param configuration
+     * @param containerName
+     * @return
+     */
+    public static boolean validateCreateContainer(DockerContainerConfiguration configuration, String containerName){
+        RemoteClient remoteClient = new RemoteClient(configuration);
+        Container container =  getContainerFromName(remoteClient,containerName);
+        return container == null;
+    }
+
+    /**
+     * Validates start container eligibility
+     * @param configuration
+     * @param containerName
+     * @return
+     */
+    public static boolean validateStartContainer(DockerContainerConfiguration configuration, String containerName){
+        RemoteClient remoteClient = new RemoteClient(configuration);
+        Container container =  getContainerFromName(remoteClient,containerName);
+        return !isStarted(container);
+    }
+
+    /**
+     * Validates stop container eligibility
+     * @param configuration
+     * @param containerName
+     * @return
+     */
+    public static boolean validateStopContainer(DockerContainerConfiguration configuration, String containerName){
+        RemoteClient remoteClient = new RemoteClient(configuration);
+        Container container =  getContainerFromName(remoteClient,containerName);
+        return isStarted(container);
+    }
+
+    /**
+     * Validates create container eligibility
+     * @param configuration
+     * @param containerName
+     * @return
+     */
+    public static boolean validateDeleteContainer(DockerContainerConfiguration configuration, String containerName){
+        RemoteClient remoteClient = new RemoteClient(configuration);
+        Container container =  getContainerFromName(remoteClient,containerName);
+        return container != null;
+    }
+
+    private static Container getContainerFromName(RemoteClient remoteClient, String containerName){
+        List<Container> containerList = remoteClient.listContainers();
+        for(Container container : containerList){
+            if(container.getNames().length > 0){
+                for(String name : container.getNames()){
+                    if(name.endsWith(containerName)){
+                        return container;
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+
+    private static boolean isStarted(Container container){
+        return  container.getStatus() != null
+                && container.getStatus().startsWith("Up");
+    }
+
+}
diff --git a/taverna-docker-activity/src/main/resources/META-INF/spring/docker-activity-context-osgi.xml b/taverna-docker-activity/src/main/resources/META-INF/spring/docker-activity-context-osgi.xml
index 6875bfb..69540e7 100755
--- a/taverna-docker-activity/src/main/resources/META-INF/spring/docker-activity-context-osgi.xml
+++ b/taverna-docker-activity/src/main/resources/META-INF/spring/docker-activity-context-osgi.xml
@@ -25,5 +25,6 @@
                       http://www.springframework.org/schema/osgi/spring-osgi.xsd">
 
     <reference id="configurationManager" interface="org.apache.taverna.configuration.ConfigurationManager" />
+    <service ref="dockerActivityHealthChecker" interface="org.apache.taverna.workflowmodel.health.HealthChecker" />
 
 </beans:beans>
diff --git a/taverna-docker-activity/src/main/resources/META-INF/spring/docker-activity-context.xml b/taverna-docker-activity/src/main/resources/META-INF/spring/docker-activity-context.xml
index c3bb871..90d99eb 100755
--- a/taverna-docker-activity/src/main/resources/META-INF/spring/docker-activity-context.xml
+++ b/taverna-docker-activity/src/main/resources/META-INF/spring/docker-activity-context.xml
@@ -26,4 +26,6 @@
         <constructor-arg name="configurationManager" ref="configurationManager" />
     </bean>
 
+    <bean id="dockerActivityHealthChecker" class="org.apache.taverna.activities.docker.DockerActivityHealthChecker" />
+
 </beans>
diff --git a/taverna-docker-activity/src/test/java/org/apache/taverna/activities/docker/test/DockerActivityTest.java b/taverna-docker-activity/src/test/java/org/apache/taverna/activities/docker/test/DockerActivityTest.java
index 751c247..8d69092 100755
--- a/taverna-docker-activity/src/test/java/org/apache/taverna/activities/docker/test/DockerActivityTest.java
+++ b/taverna-docker-activity/src/test/java/org/apache/taverna/activities/docker/test/DockerActivityTest.java
@@ -52,7 +52,7 @@
     public static final String DOCKER_REGISTRY = "https://registry-1.docker.io/v2";
 
     public static void main(String[] args) throws Exception {
-    DockerActivityTest activityTest = new DockerActivityTest();
+        DockerActivityTest activityTest = new DockerActivityTest();
         activityTest.setup();
         activityTest.testAll();
     }
@@ -81,12 +81,12 @@
 
     @Test
     public void testAll() throws Exception {
-//        testCreateContainer();
+        testCreateContainer();
         testInspectImage();
-//        testListContainers();
-//        testStartContainer();
-//        testStopContainer();
-//        testDeleteContainer();
+        testListContainers();
+        testStartContainer();
+        testStopContainer();
+        testDeleteContainer();
     }
 
     /**