// ----------------------------------------------------------------------
// This code is developed as part of the Java CoG Kit project
// The terms of the license can be found at http://www.cogkit.org/license
// This message may not be removed or altered.
// ----------------------------------------------------------------------

/*
 * Created on Jul 23, 2003
 */
package org.globus.cog.karajan.workflow.nodes;

import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.globus.cog.karajan.arguments.Arg;
import org.globus.cog.karajan.stack.VariableStack;
import org.globus.cog.karajan.util.TypeUtil;
import org.globus.cog.karajan.workflow.ExecutionException;
import org.globus.cog.karajan.workflow.events.FailureNotificationEvent;
import org.globus.cog.karajan.workflow.events.NotificationEvent;
import org.globus.cog.karajan.workflow.events.NotificationEventType;

public class RestartOnErrorNode extends AbstractRegexpFailureHandler {
	public static final Logger logger = Logger.getLogger(RestartOnErrorNode.class);

	public static final Arg A_MATCH = new Arg.Positional("match", 0);
	public static final Arg A_TIMES = new Arg.Positional("times", 1);

	private static final String MATCH = "##match";

	static {
		setArguments(RestartOnErrorNode.class, new Arg[] { A_MATCH, A_TIMES });
	}

	protected void partialArgumentsEvaluated(VariableStack stack) throws ExecutionException {
		if (A_MATCH.isPresent(stack)) {
			stack.setVar(MATCH, A_MATCH.getValue(stack));
		}
		else {
			stack.setVar(MATCH, ".*");
		}
		stack.setVar("#restartTimes", new Integer(TypeUtil.toInt(A_TIMES.getValue(stack))));
		super.partialArgumentsEvaluated(stack);
		startRest(stack);
	}

	protected void notificationEvent(NotificationEvent e) throws ExecutionException {
		if (NotificationEventType.EXECUTION_FAILED.equals(e.getType())) {
			VariableStack stack = e.getStack();

			if (!matches(stack, (FailureNotificationEvent) e)) {
				super.notificationEvent(e);
				return;
			}

			int itimes = stack.currentFrame().preDecrementAtomic("#restartTimes");
			if (itimes >= 0) {
				logger.debug("Restarting. " + itimes + " times left.");
				logger.debug("Stack size: " + stack.frameCount());
				this.startRest(stack);
				return;
			}
			else {
				logger.debug("Failed too many times.");
			}
		}
		super.notificationEvent(e);
	}

	protected boolean matches(VariableStack stack, FailureNotificationEvent e) {
		if (!stack.currentFrame().isDefined(MATCH)) {
			return false;
		}
		else {
			Object match = stack.currentFrame().getVar(MATCH);
			if (match instanceof List) {
				Iterator i = ((List) match).iterator();
				while (i.hasNext()) {
					if (matches(TypeUtil.toString(i.next()), e)) {
						return true;
					}
				}
				return false;
			}
			else {
				return matches(TypeUtil.toString(match), e);
			}
		}
	}
}

@@ -1992,17 +1992,16 @@
 n;%0A%09%09%09%7D%0A
-%0A
 %09%09%09int i
@@ -2088,19 +2088,79 @@
 %3E= 0) %7B%0A
-%09%09%09
+                if (logger.isDebugEnabled()) %7B%0A                
 %09logger.
@@ -2208,19 +2208,32 @@
 eft.%22);%0A
-%09%09%09
+                
 %09logger.
@@ -2276,16 +2276,34 @@
 unt());%0A
+                %7D%0A
 %09%09%09%09this
@@ -2348,19 +2348,79 @@
 %09else %7B%0A
-%09%09%09
+                if (logger.isDebugEnabled()) %7B%0A                
 %09logger.
@@ -2452,16 +2452,34 @@
 mes.%22);%0A
+                %7D%0A
 %09%09%09%7D%0A%09%09%7D

debug check