/*
 * Copyright 2003-2004 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.apache.jmeter.control;

import java.io.Serializable;

import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.testelement.property.StringProperty;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;

/*******************************************************************************
 * 
 * @author Cyrus Montakab created 2003/06/30
 * @version $Date$ $Revision$ This is a Conditional
 *          Controller; it will execute the set of statements
 *          (samplers/controllers, etc) while the 'condition' is true. In a
 *          programming world - this is equivalant of : if (condition) {
 *          statements .... } In JMeter you may have : Thread-Group (set to loop
 *          a number of times or indefinitely, ... Samplers ... (e.g. Counter )
 *          ... Other Controllers .... ... IfController ( condition set to
 *          something like - ${counter}<10) ... statements to perform if
 *          condition is true ... ... Other Controllers /Samplers }
 * 
 ******************************************************************************/

public class IfController extends GenericController implements Serializable {

	private static Logger logger = LoggingManager.getLoggerForClass();

	private final static String CONDITION = "IfController.condition";

	/**
	 * constructor
	 */
	public IfController() {
		super();
	}

	/**
	 * constructor
	 */
	public IfController(String condition) {
		super();
		this.setCondition(condition);
	}

	/**
	 * Condition Accessor - this is gonna be like ${count}<10
	 */
	public void setCondition(String condition) {
		setProperty(new StringProperty(CONDITION, condition));
	}

	/**
	 * Condition Accessor - this is gonna be like ${count}<10
	 */
	public String getCondition() {
		return getPropertyAsString(CONDITION);
	}

	/**
	 * evaluate the condition clause log error if bad condition
	 */
	static boolean evaluateCondition(String cond) {
		logger.debug("    getCondition() : [" + cond + "]");

		String resultStr = "";
		boolean result = false;

		// now evaluate the condition using JavaScript
		Context cx = Context.enter();
		try {
			Scriptable scope = cx.initStandardObjects(null);
			Object cxResultObject = cx.evaluateString(scope, cond
			/** * conditionString ** */
			, "<cmd>", 1, null);
			resultStr = Context.toString(cxResultObject);

			if (resultStr.equals("false")) {
				result = false;
			} else if (resultStr.equals("true")) {
				result = true;
			} else {
				throw new Exception(" BAD CONDITION :: " + cond);
			}

			logger.debug("    >> evaluate Condition -  [ " + cond + "] results is  [" + result + "]");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		} finally {
			Context.exit();
		}

		return result;
	}

	/**
	 * This is overriding the parent method. IsDone indicates whether the
	 * termination condition is reached. I.e. if the condition evaluates to
	 * False - then isDone() returns TRUE
	 */
	public boolean isDone() {
		// boolean result = true;
		// try {
		// result = !evaluateCondition();
		// } catch (Exception e) {
		// logger.error(e.getMessage(), e);
		// }
		// setDone(true);
		// return result;
		// setDone(false);
		return false;
	}

	/**
	 * @see org.apache.jmeter.control.Controller#next() 'JMeterThread' iterates
	 *      thru the Controller by calling this method. IF a valid 'Sampler' is
	 *      returned, then it executes the sampler (calls sampler.sampler(xxx)
	 *      method) . So here we make sure that the samplers belonging to this
	 *      Controller do not get called - if isDone is true - if its the first
	 *      time this is run. The first time is special cause it is called prior
	 *      the iteration even starts !
	 */
	public Sampler next() {
		boolean result = evaluateCondition(getCondition());
		if (result)
			return super.next();
		else
			try {
				return nextIsNull();
			} catch (NextIsNullException e1) {
				return null;
			}
	}
}
@@ -1109,16 +1109,20 @@
 vision$ 
+%0A * 
 This is 
@@ -1134,28 +1134,16 @@
 ditional
-%0A *         
  Control
@@ -1187,25 +1187,16 @@
 ments%0A *
-         
  (sample
@@ -1251,24 +1251,16 @@
 ue. 
+%0A * 
 In a
-%0A *         
  pro
@@ -1300,16 +1300,20 @@
 nt of : 
+%0A * 
 if (cond
@@ -1349,17 +1349,34 @@
 ts .... 
-%7D
+%0A *          %7D %0A *
  In JMet
@@ -1418,28 +1418,16 @@
  to loop
-%0A *         
  a numbe
@@ -1454,16 +1454,23 @@
 nitely, 
+%0A *    
 ... Samp
@@ -1496,22 +1496,16 @@
 ter )%0A *
-      
     ... 
@@ -1523,24 +1523,31 @@
 ollers .... 
+%0A *    
 ... IfContro
@@ -1569,28 +1569,16 @@
 n set to
-%0A *         
  somethi
@@ -1602,16 +1602,26 @@
 er%7D%3C10) 
+%0A *       
 ... stat
@@ -1640,28 +1640,16 @@
 rform if
-%0A *         
  conditi
@@ -1659,19 +1659,36 @@
 is true 
-...
+%0A *       ... %0A *   
  ... Oth
@@ -1879,16 +1879,16 @@
 able %7B%0A%0A
-
 %09private
@@ -1895,16 +1895,22 @@
  static 
+final 
 Logger l

Tidy a bit