/**
 * 
 */
package net.sf.taverna.t2.activities.externaltool;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import de.uni_luebeck.inb.knowarc.usecases.invocation.InvocationException;
import de.uni_luebeck.inb.knowarc.usecases.invocation.UseCaseInvocation;
import de.uni_luebeck.inb.knowarc.usecases.invocation.ssh.SshUseCaseInvocation;

import net.sf.taverna.t2.workflowmodel.RunDeletionListener;

/**
 * @author alanrw
 *
 */
public class ExternalToolRunDeletionListener implements RunDeletionListener {
	
	private static Logger logger = Logger.getLogger(ExternalToolRunDeletionListener.class);

	private static Map<String, Set<UseCaseInvocation>> runToInvocationsMap = Collections.synchronizedMap(new HashMap<String, Set<UseCaseInvocation>>());

	/* (non-Javadoc)
	 * @see net.sf.taverna.t2.workflowmodel.RunDeletionListener#deleteRun(java.lang.String)
	 */
	@Override
	public void deleteRun(String runToDelete) {
		Set<UseCaseInvocation> invocations = runToInvocationsMap.get(runToDelete);
		runToInvocationsMap.remove(runToDelete);
		for (UseCaseInvocation uci : invocations) {
			try {
				uci.cleanup();
			}
			catch (InvocationException e) {
				logger.error(e);
			}
		}
	}
	
	public static void rememberInvocation(String runId, UseCaseInvocation invocation) {
		Set<UseCaseInvocation> invocations = runToInvocationsMap.get(runId);
		if (invocations == null) {
			invocations = Collections.synchronizedSet(new HashSet<UseCaseInvocation>());
			runToInvocationsMap.put(runId, invocations);
		}
		invocations.add(invocation);
	}

}

@@ -1102,16 +1102,46 @@
 elete);%0A
+%09%09if (invocations != null) %7B%0A%09
 %09%09runToI
@@ -1175,16 +1175,17 @@
 elete);%0A
+%09
 %09%09for (U
@@ -1222,16 +1222,17 @@
 ions) %7B%0A
+%09
 %09%09%09try %7B
@@ -1236,16 +1236,17 @@
 y %7B%0A%09%09%09%09
+%09
 uci.clea
@@ -1259,13 +1259,11 @@
 %0A%09%09%09
-%7D%0A%09%09%09
+%09%7D 
 catc
@@ -1294,16 +1294,17 @@
 ) %7B%0A%09%09%09%09
+%09
 logger.e
@@ -1312,16 +1312,22 @@
 ror(e);%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D

Tweak to the deletion of run-associated data