You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@oodt.apache.org by ma...@apache.org on 2011/06/24 16:26:41 UTC

svn commit: r1139325 - /oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/

Author: mattmann
Date: Fri Jun 24 14:26:40 2011
New Revision: 1139325

URL: http://svn.apache.org/viewvc?rev=1139325&view=rev
Log:
- progress towards OODT-70: Add ability for sequential and parallel task specifications for Workflows

Added:
    oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/SequentialWorkflowProcessor.java
      - copied, changed from r1137249, oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/IterativeWorkflowProcessorThread.java
    oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/WorkflowProcessor.java
      - copied, changed from r1137249, oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/WorkflowProcessorThread.java
Removed:
    oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/IterativeWorkflowProcessorThread.java
    oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/WorkflowProcessorThread.java
Modified:
    oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/NonBlockingIterativeWorkflowProcessorThread.java
    oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/NonBlockingThreadPoolWorkflowEngine.java
    oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/ThreadPoolWorkflowEngine.java

Modified: oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/NonBlockingIterativeWorkflowProcessorThread.java
URL: http://svn.apache.org/viewvc/oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/NonBlockingIterativeWorkflowProcessorThread.java?rev=1139325&r1=1139324&r2=1139325&view=diff
==============================================================================
--- oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/NonBlockingIterativeWorkflowProcessorThread.java (original)
+++ oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/NonBlockingIterativeWorkflowProcessorThread.java Fri Jun 24 14:26:40 2011
@@ -55,14 +55,14 @@ import java.util.logging.Logger;
  * @version $Revision$
  * 
  * <p>
- * An instance of the {@link WorkflowProcessorThread} that works with the 
+ * An instance of the {@link WorkflowProcessor} that works with the 
  * non-blocking thread pool engine.
  * </p>
  * 
  */
 
-public class NonBlockingIterativeWorkflowProcessorThread implements
-        WorkflowProcessorThread, WorkflowStatus, CoreMetKeys {
+public class NonBlockingIterativeWorkflowProcessorThread extends 
+        WorkflowProcessor implements WorkflowStatus, CoreMetKeys {
 
     /* the default queue name if we're using resmgr job submission */
     private static final String DEFAULT_QUEUE_NAME = "high";
@@ -96,7 +96,7 @@ public class NonBlockingIterativeWorkflo
 
     /* our log stream */
     private static Logger LOG = Logger
-            .getLogger(IterativeWorkflowProcessorThread.class.getName());
+            .getLogger(SequentialWorkflowProcessor.class.getName());
 
     private Map CONDITION_CACHE = new HashMap();
 
@@ -107,20 +107,10 @@ public class NonBlockingIterativeWorkflo
     private String currentJobId = null;
 
     public NonBlockingIterativeWorkflowProcessorThread(WorkflowInstance wInst,
-            WorkflowInstanceRepository instRep, URL wParentUrl) {
-        workflowInst = wInst;
-        taskId = workflowInst.getCurrentTaskId();
-        this.instanceRepository = instRep;
-
-        /* start out the gates running */
-        running = true;
-
-        pollingWaitTime = Long
-                .getLong(
-                        "org.apache.oodt.cas.workflow.engine.resourcemgr.pollingWaitTime",
-                        10).longValue();
-
-        wmgrParentUrl = wParentUrl;
+            WorkflowInstanceRepository instRep, URL wParentUrl, long pollingWaitTime) {
+      super(wInst, instRep, wParentUrl, pollingWaitTime);
+      /* start out the gates running */
+      running = true;
     }
 
     /*
@@ -491,105 +481,7 @@ public class NonBlockingIterativeWorkflo
         return true;
     }*/
 
-    private String getTaskNameById(String taskId) {
-        for (Iterator i = workflowInst.getWorkflow().getTasks().iterator(); i
-                .hasNext();) {
-            WorkflowTask task = (WorkflowTask) i.next();
-            if (task.getTaskId().equals(taskId)) {
-                return task.getTaskName();
-            }
-        }
-
-        return null;
-    }
-
-    private boolean satisfied(List conditionList, String taskId) {
-        for (Iterator i = conditionList.iterator(); i.hasNext();) {
-            WorkflowCondition c = (WorkflowCondition) i.next();
-            WorkflowConditionInstance cInst = null;
-
-            // see if we've already cached this condition instance
-            if (CONDITION_CACHE.get(taskId) != null) {
-                HashMap conditionMap = (HashMap) CONDITION_CACHE.get(taskId);
-
-                /*
-                 * okay we have some conditions cached for this task, see if we
-                 * have the one we need
-                 */
-                if (conditionMap.get(c.getConditionId()) != null) {
-                    cInst = (WorkflowConditionInstance) conditionMap.get(c
-                            .getConditionId());
-                }
-                /* if not, then go ahead and create it and cache it */
-                else {
-                    cInst = GenericWorkflowObjectFactory
-                            .getConditionObjectFromClassName(c
-                                    .getConditionInstanceClassName());
-                    conditionMap.put(c.getConditionId(), cInst);
-                }
-            }
-            /* no conditions cached yet, so set everything up */
-            else {
-                HashMap conditionMap = new HashMap();
-                cInst = GenericWorkflowObjectFactory
-                        .getConditionObjectFromClassName(c
-                                .getConditionInstanceClassName());
-                conditionMap.put(c.getConditionId(), cInst);
-                CONDITION_CACHE.put(taskId, conditionMap);
-            }
 
-            // actually perform the evaluation
-            if (!cInst.evaluate(workflowInst.getSharedContext(), c.getTaskConfig())) {
-                return false;
-            }
-        }
-
-        return true;
-    }
-
-    private String getHostname() {
-        try {
-            // Get hostname by textual representation of IP address
-            InetAddress addr = InetAddress.getLocalHost();
-            // Get the host name
-            String hostname = addr.getHostName();
-            return hostname;
-        } catch (UnknownHostException e) {
-        }
-        return null;
-    }
-
-    private void persistWorkflowInstance() {
-        try {
-            instanceRepository.updateWorkflowInstance(workflowInst);
-        } catch (InstanceRepositoryException e) {
-            LOG.log(Level.WARNING, "Exception persisting workflow instance: ["
-                    + workflowInst.getId() + "]: Message: " + e.getMessage());
-        }
-    }
-
-    private void executeTaskLocally(WorkflowTaskInstance instance,
-            Metadata met, WorkflowTaskConfiguration cfg, String taskName) {
-        try {
-            LOG.log(Level.INFO, "Executing task: [" + taskName + "] locally");
-            instance.run(met, cfg);
-        } catch (Exception e) {
-            e.printStackTrace();
-            LOG.log(Level.WARNING, "Exception executing task: [" + taskName
-                    + "] locally: Message: " + e.getMessage());
-        }
-    }
-
-    private boolean safeCheckJobComplete(String jobId) {
-        try {
-            return rClient.isJobComplete(jobId);
-        } catch (Exception e) {
-            LOG.log(Level.WARNING,
-                    "Exception checking completion status for job: [" + jobId
-                            + "]: Messsage: " + e.getMessage());
-            return false;
-        }
-    }
     
     private WorkflowTask getTaskById(Workflow w, String Id){
     	List tasks = w.getTasks();

Modified: oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/NonBlockingThreadPoolWorkflowEngine.java
URL: http://svn.apache.org/viewvc/oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/NonBlockingThreadPoolWorkflowEngine.java?rev=1139325&r1=1139324&r2=1139325&view=diff
==============================================================================
--- oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/NonBlockingThreadPoolWorkflowEngine.java (original)
+++ oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/NonBlockingThreadPoolWorkflowEngine.java Fri Jun 24 14:26:40 2011
@@ -30,7 +30,7 @@ import org.apache.oodt.cas.workflow.stru
 import org.apache.oodt.cas.workflow.structs.exceptions.EngineException;
 import org.apache.oodt.cas.workflow.structs.exceptions.InstanceRepositoryException;
 import org.apache.oodt.cas.workflow.util.GenericWorkflowObjectFactory;
-import org.apache.oodt.cas.workflow.engine.IterativeWorkflowProcessorThread;
+import org.apache.oodt.cas.workflow.engine.SequentialWorkflowProcessor;
 import org.apache.oodt.cas.workflow.instrepo.WorkflowInstanceRepository;
 import org.apache.oodt.cas.workflow.engine.NonBlockingShepardThread;
 import org.apache.oodt.commons.util.DateConvert;
@@ -151,8 +151,12 @@ public class NonBlockingThreadPoolWorkfl
     }
     
     public void submitWorkflowInstancetoPool(WorkflowInstance wInst){
-    	IterativeWorkflowProcessorThread worker = new IterativeWorkflowProcessorThread(
-                wInst, instRep, this.wmgrUrl);
+      long pollingWaitTime = Long
+      .getLong(
+              "org.apache.oodt.cas.workflow.engine.resourcemgr.pollingWaitTime",
+              10).longValue();
+    	SequentialWorkflowProcessor worker = new SequentialWorkflowProcessor(
+                wInst, instRep, this.wmgrUrl, pollingWaitTime);
         worker.setRClient(rClient);
         workerMap.put(wInst.getId(), worker);
 
@@ -231,7 +235,7 @@ public class NonBlockingThreadPoolWorkfl
      */
     public synchronized void pauseWorkflowInstance(String workflowInstId) {
         // okay, try and look up that worker thread in our hash map
-        IterativeWorkflowProcessorThread worker = (IterativeWorkflowProcessorThread) workerMap
+        SequentialWorkflowProcessor worker = (SequentialWorkflowProcessor) workerMap
                 .get(workflowInstId);
         if (worker == null) {
             LOG
@@ -255,7 +259,7 @@ public class NonBlockingThreadPoolWorkfl
      */
     public synchronized void resumeWorkflowInstance(String workflowInstId) {
         // okay, try and look up that worker thread in our hash map
-        IterativeWorkflowProcessorThread worker = (IterativeWorkflowProcessorThread) workerMap
+        SequentialWorkflowProcessor worker = (SequentialWorkflowProcessor) workerMap
                 .get(workflowInstId);
         if (worker == null) {
             LOG.log(Level.WARNING,
@@ -370,7 +374,7 @@ public class NonBlockingThreadPoolWorkfl
      */
     public synchronized void stopWorkflow(String workflowInstId) {
         // okay, try and look up that worker thread in our hash map
-        IterativeWorkflowProcessorThread worker = (IterativeWorkflowProcessorThread) workerMap
+        SequentialWorkflowProcessor worker = (SequentialWorkflowProcessor) workerMap
                 .get(workflowInstId);
         if (worker == null) {
             LOG.log(Level.WARNING,

Copied: oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/SequentialWorkflowProcessor.java (from r1137249, oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/IterativeWorkflowProcessorThread.java)
URL: http://svn.apache.org/viewvc/oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/SequentialWorkflowProcessor.java?p2=oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/SequentialWorkflowProcessor.java&p1=oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/IterativeWorkflowProcessorThread.java&r1=1137249&r2=1139325&rev=1139325&view=diff
==============================================================================
--- oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/IterativeWorkflowProcessorThread.java (original)
+++ oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/SequentialWorkflowProcessor.java Fri Jun 24 14:26:40 2011
@@ -15,594 +15,241 @@
  * limitations under the License.
  */
 
-
 package org.apache.oodt.cas.workflow.engine;
 
 //OODT imports
 import org.apache.oodt.cas.resource.structs.Job;
 import org.apache.oodt.cas.resource.structs.exceptions.JobExecutionException;
-import org.apache.oodt.cas.resource.system.XmlRpcResourceManagerClient;
 import org.apache.oodt.cas.workflow.instrepo.WorkflowInstanceRepository;
 import org.apache.oodt.cas.workflow.metadata.CoreMetKeys;
 import org.apache.oodt.cas.workflow.structs.TaskJobInput;
 import org.apache.oodt.cas.workflow.structs.WorkflowInstance;
 import org.apache.oodt.cas.workflow.structs.WorkflowStatus;
 import org.apache.oodt.cas.workflow.structs.WorkflowTask;
-import org.apache.oodt.cas.workflow.structs.WorkflowTaskConfiguration;
 import org.apache.oodt.cas.workflow.structs.WorkflowTaskInstance;
-import org.apache.oodt.cas.workflow.structs.WorkflowCondition;
-import org.apache.oodt.cas.workflow.structs.WorkflowConditionInstance;
 import org.apache.oodt.cas.workflow.structs.exceptions.InstanceRepositoryException;
 import org.apache.oodt.cas.workflow.util.GenericWorkflowObjectFactory;
-import org.apache.oodt.cas.metadata.Metadata;
 import org.apache.oodt.commons.util.DateConvert;
 
 //JDK imports
-import java.net.InetAddress;
 import java.net.URL;
-import java.net.UnknownHostException;
 import java.util.Date;
 import java.util.Iterator;
-import java.util.Map;
-import java.util.HashMap;
-import java.util.List;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
 /**
- * @author mattmann
- * @version $Revision$
  * 
- * <p>
- * An instance of the {@link WorkflowProcessorThread} that processes through an
- * iterative {@link WorkflowInstance}. This class keeps an
+ * An instance of the {@link WorkflowProcessor} that processes through an
+ * iterative {@link this.workflowInstance}. This class keeps an
  * <code>Iterator</code> that allows it to move from one end of a sequential
  * {@link Workflow} processing pipeline to another. This class should only be
  * used to process science pipeline style {@link Workflow}s, i.e., those which
  * resemble an iterative processing pipelines, with no forks, or concurrent task
  * executions.
- * </p>
+ * 
+ * @author mattmann
  * 
  */
 
-public class IterativeWorkflowProcessorThread implements
-        WorkflowProcessorThread, WorkflowStatus, CoreMetKeys {
-
-    /* the default queue name if we're using resmgr job submission */
-    private static final String DEFAULT_QUEUE_NAME = "high";
-
-    /* an iterator representing the current task that we are on in the workflow */
-    private Iterator taskIterator = null;
-
-    /* the workflow instance that this processor thread is processing */
-    private WorkflowInstance workflowInst = null;
-
-    /* should our workflow processor thread start running? */
-    private boolean running = false;
-
-    /*
-     * the amount of seconds to wait inbetween checking for task pre-condition
-     * satisfaction
-     */
-    private long waitForConditionSatisfy = -1;
-
-    /* our instance repository used to persist workflow instance info */
-    private WorkflowInstanceRepository instanceRepository = null;
-
-    /*
-     * our client to a resource manager: if null, local task execution will be
-     * performed
-     */
-    private XmlRpcResourceManagerClient rClient = null;
-
-    /* polling wait for res mgr */
-    private long pollingWaitTime = 10L;
-
-    /*
-     * should our workflow processor thread pause, and not move onto the next
-     * task?
-     */
-    private boolean pause = false;
-
-    /* our log stream */
-    private static Logger LOG = Logger
-            .getLogger(IterativeWorkflowProcessorThread.class.getName());
-
-    private Map CONDITION_CACHE = new HashMap();
-
-    /* the parent workflow manager url that executed this processor thread */
-    private URL wmgrParentUrl = null;
-
-    /* the currently executing jobId if we're using the resource manager */
-    private String currentJobId = null;
-
-    public IterativeWorkflowProcessorThread(WorkflowInstance wInst,
-            WorkflowInstanceRepository instRep, URL wParentUrl) {
-        workflowInst = wInst;
-        taskIterator = workflowInst.getWorkflow().getTasks().iterator();
-        this.instanceRepository = instRep;
-
-        /* start out the gates running */
-        running = true;
-
-        /*
-         * get the amount of wait time inbetween checking for task pre-condition
-         * satisfaction
-         */
-        waitForConditionSatisfy = Long.getLong(
-                "org.apache.oodt.cas.workflow.engine.preConditionWaitTime",
-                10).longValue();
-
-        pollingWaitTime = Long
-                .getLong(
-                        "org.apache.oodt.cas.workflow.engine.resourcemgr.pollingWaitTime",
-                        10).longValue();
-
-        wmgrParentUrl = wParentUrl;
-    }
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see java.lang.Runnable#run()
-     */
-    public void run() {
-        /*
-         * okay, we got into the run method, mark the start date time for the
-         * workflow instance here
-         */
-        String startDateTimeIsoStr = DateConvert.isoFormat(new Date());
-        workflowInst.setStartDateTimeIsoStr(startDateTimeIsoStr);
-        // persist it
-        persistWorkflowInstance();
-
-        while (running && taskIterator.hasNext()) {
-            if (pause) {
-                LOG.log(Level.FINE,
-                        "IterativeWorkflowProcessorThread: Skipping execution: Paused: CurrentTask: "
-                                + getTaskNameById(workflowInst
-                                        .getCurrentTaskId()));
-                continue;
-            }
-
-            WorkflowTask task = (WorkflowTask) taskIterator.next();
-            workflowInst.setCurrentTaskId(task.getTaskId());
-
-            // now persist it
-            persistWorkflowInstance();
-
-            // check to see if req met fields are present
-            // if they aren't, set the status to METERROR, and then fail
-            if (!checkTaskRequiredMetadata(task, this.workflowInst
-                    .getSharedContext())) {
-                this.workflowInst.setStatus(METADATA_MISSING);
-                persistWorkflowInstance();
-                // now break out of this run loop
-                return;
-            }
-
-            // this is where the pre-conditions come in
-            // only execute the below code when it's passed all of its
-            // pre-conditions
-            if (task.getConditions() != null) {
-                while (!satisfied(task.getConditions(), task.getTaskId()) && !isStopped()) {
-
-                    // if we're not paused, go ahead and pause us now
-                    if (!isPaused()) {
-                        pause();
-                    }
-
-                    LOG.log(Level.FINEST, "Pre-conditions for task: "
-                            + task.getTaskName() + " unsatisfied: waiting: "
-                            + waitForConditionSatisfy
-                            + " seconds before checking again.");
-                    try {
-                        Thread.currentThread().sleep(
-                                waitForConditionSatisfy * 1000);
-                    } catch (InterruptedException ignore) {
-                    }
-
-                    // check to see if we've been resumed, if so, break
-                    // the loop and start
-                    if (!isPaused()) {
-                        break;
-                    }
-                }
-                
-                // check to see if we've been killed
-                if (isStopped()){
-                    break;
-                }
-                
-
-                // un pause us (if needed)
-                if (isPaused()) {
-                    resume();
-                }
-            }
-
-            // task execution
-            LOG.log(Level.FINEST,
-                    "IterativeWorkflowProcessorThread: Executing task: "
-                            + task.getTaskName());
-
-            WorkflowTaskInstance taskInstance = GenericWorkflowObjectFactory
-                    .getTaskObjectFromClassName(task.getTaskInstanceClassName());
-            // add the TaskId and the JobId and ProcessingNode
-            // TODO: unfake the JobId
-            workflowInst.getSharedContext().replaceMetadata(TASK_ID,
-                    task.getTaskId());
-            workflowInst.getSharedContext().replaceMetadata(WORKFLOW_INST_ID,
-                    workflowInst.getId());
-            workflowInst.getSharedContext().replaceMetadata(JOB_ID,
-                    workflowInst.getId());
-            workflowInst.getSharedContext().replaceMetadata(PROCESSING_NODE,
-                    getHostname());
-            workflowInst.getSharedContext().replaceMetadata(
-                    WORKFLOW_MANAGER_URL, this.wmgrParentUrl.toString());
-
-            if (rClient != null) {
-                // build up the Job
-                // and the Job Input
-                Job taskJob = new Job();
-                taskJob.setName(task.getTaskId());
-                taskJob
-                        .setJobInstanceClassName("org.apache.oodt.cas.workflow.structs.TaskJob");
-                taskJob
-                        .setJobInputClassName("org.apache.oodt.cas.workflow.structs.TaskJobInput");
-                taskJob.setLoadValue(new Integer(2));
-                taskJob.setQueueName(task.getTaskConfig().getProperty(
-                        QUEUE_NAME) != null ? task.getTaskConfig()
-                        .getProperty(QUEUE_NAME) : DEFAULT_QUEUE_NAME);
-
-                TaskJobInput in = new TaskJobInput();
-                in.setDynMetadata(workflowInst.getSharedContext());
-                in.setTaskConfig(task.getTaskConfig());
-                in.setWorkflowTaskInstanceClassName(task
-                        .getTaskInstanceClassName());
-
-                workflowInst.setStatus(RESMGR_SUBMIT);
-                persistWorkflowInstance();
-
-                try {
-                    // this is * NOT * a blocking operation so when it returns
-                    // the job may not actually have finished executing
-                    // so we go into a waiting/sleep behavior using the passed
-                    // back job id to wait until the job has actually finished
-                    // executing
-
-                    this.currentJobId = rClient.submitJob(taskJob, in);
-
-                    while (!safeCheckJobComplete(this.currentJobId) && !isStopped()) {
-                        // sleep for 5 seconds then come back
-                        // and check again
-                        try {
-                            Thread.currentThread()
-                                    .sleep(pollingWaitTime * 1000);
-                        } catch (InterruptedException ignore) {
-                        }
-                    }
-
-                    // okay job is done: TODO: fix this hack
-                    // the task update time was set remotely
-                    // by remote task, so let's read it now
-                    // from the instRepo (which will have the updated
-                    // time)
-
-                    if (isStopped()) {
-                        // this means that this workflow was killed, so
-                        // gracefully exit
-                        break;
-                    }
-
-                    WorkflowInstance updatedInst = null;
-                    try {
-                        updatedInst = instanceRepository
-                                .getWorkflowInstanceById(workflowInst.getId());
-                        workflowInst = updatedInst;
-                    } catch (InstanceRepositoryException e) {
-                        e.printStackTrace();
-                        LOG.log(Level.WARNING, "Unable to get "
-                                + "updated workflow " + "instance record "
-                                + "when executing remote job: Message: "
-                                + e.getMessage());
-                    }
-
-                } catch (JobExecutionException e) {
-                    LOG.log(Level.WARNING,
-                            "Job execution exception using resource manager to execute job: Message: "
-                                    + e.getMessage());
-                }
-            } else {
-                // we started, so mark it
-                workflowInst.setStatus(STARTED);
-                // go ahead and persist the workflow instance, after we
-                // save the current task start date time
-                String currentTaskIsoStartDateTimeStr = DateConvert
-                        .isoFormat(new Date());
-                workflowInst
-                        .setCurrentTaskStartDateTimeIsoStr(currentTaskIsoStartDateTimeStr);
-                workflowInst.setCurrentTaskEndDateTimeIsoStr(null); /*
-                                                                     * clear
-                                                                     * this out
-                                                                     * until
-                                                                     * it's
-                                                                     * ready
-                                                                     */
-                persistWorkflowInstance();
-                executeTaskLocally(taskInstance, workflowInst
-                        .getSharedContext(), task.getTaskConfig(), task
-                        .getTaskName());
-                String currentTaskIsoEndDateTimeStr = DateConvert
-                        .isoFormat(new Date());
-                workflowInst
-                        .setCurrentTaskEndDateTimeIsoStr(currentTaskIsoEndDateTimeStr);
-                persistWorkflowInstance();
-            }
-
-            LOG.log(Level.FINEST,
-                    "IterativeWorkflowProcessorThread: Completed task: "
-                            + task.getTaskName());
-
-        }
-
-        LOG.log(Level.FINEST,
-                "IterativeWorkflowProcessorThread: Completed workflow: "
-                        + workflowInst.getWorkflow().getName());
-        if (!isStopped()) {
-            stop();
-        }
-
-    }
-
-    public WorkflowInstance getWorkflowInstance() {
-        return workflowInst;
-    }
-
-    public synchronized void stop() {
-        running = false;
-        // if the resource manager is active
-        // then kill the current job there
-        if (this.rClient != null && this.currentJobId != null) {
-            if (!this.rClient.killJob(this.currentJobId)) {
-                LOG.log(Level.WARNING, "Attempt to kill "
-                        + "current resmgr job: [" + this.currentJobId
-                        + "]: failed");
-            }
-        }
-
-        workflowInst.setStatus(FINISHED);
-        String isoEndDateTimeStr = DateConvert.isoFormat(new Date());
-        workflowInst.setEndDateTimeIsoStr(isoEndDateTimeStr);
-        persistWorkflowInstance();
-    }
-
-    public synchronized void resume() {
-        pause = false;
-        workflowInst.setStatus(STARTED);
-        persistWorkflowInstance();
-    }
-
-    public synchronized void pause() {
-        pause = true;
-        workflowInst.setStatus(PAUSED);
-        persistWorkflowInstance();
-    }
-
-    /**
-     * @return True if the WorkflowInstance managed by this processor is paused.
-     */
-    public boolean isPaused() {
-        return pause == true;
-    }
-
-    public boolean isStopped() {
-        return !running;
-    }
-
-    /**
-     * @return Returns the fCurrentTaskId.
-     */
-    public String getCurrentTaskId() {
-        return workflowInst.getCurrentTaskId();
-    }
-
-    /**
-     * @param workflowInst
-     *            The fWorkflowInst to set.
-     */
-    public void setWorkflowInst(WorkflowInstance workflowInst) {
-        workflowInst = workflowInst;
-    }
-
-    /**
-     * @return Returns the waitForConditionSatisfy.
-     */
-    public long getWaitforConditionSatisfy() {
-        return waitForConditionSatisfy;
-    }
-
-    /**
-     * @param waitforConditionSatisfy
-     *            The waitForConditionSatisfy to set.
-     */
-    public void setWaitforConditionSatisfy(long waitforConditionSatisfy) {
-        waitForConditionSatisfy = waitforConditionSatisfy;
-    }
-
-    /**
-     * @return the instRep
-     */
-    public WorkflowInstanceRepository getInstanceRepository() {
-        return instanceRepository;
-    }
-
-    /**
-     * @param instRep
-     *            the instRep to set
-     */
-    public void setInstanceRepository(WorkflowInstanceRepository instRep) {
-        this.instanceRepository = instRep;
-    }
-
-    /**
-     * @return the rClient
-     */
-    public XmlRpcResourceManagerClient getRClient() {
-        return rClient;
-    }
-
-    /**
-     * @param client
-     *            the rClient to set
-     */
-    public void setRClient(XmlRpcResourceManagerClient client) {
-        rClient = client;
-        if (rClient != null) {
-            LOG.log(Level.INFO, "Resource Manager Job Submission enabled to: ["
-                    + rClient.getResMgrUrl() + "]");
-        }
-    }
-
-    /**
-     * @return the wmgrParentUrl
-     */
-    public URL getWmgrParentUrl() {
-        return wmgrParentUrl;
-    }
-
-    /**
-     * @param wmgrParentUrl
-     *            the wmgrParentUrl to set
-     */
-    public void setWmgrParentUrl(URL wmgrParentUrl) {
-        this.wmgrParentUrl = wmgrParentUrl;
-    }
-
-    private boolean checkTaskRequiredMetadata(WorkflowTask task,
-            Metadata dynMetadata) {
-        if (task.getRequiredMetFields() == null
-                || (task.getRequiredMetFields() != null && task
-                        .getRequiredMetFields().size() == 0)) {
-            LOG.log(Level.INFO, "Task: [" + task.getTaskName()
-                    + "] has no required metadata fields");
-            return true; /* no required metadata, so we're fine */
-        }
-
-        for (Iterator i = task.getRequiredMetFields().iterator(); i.hasNext();) {
-            String reqField = (String) i.next();
-            if (!dynMetadata.containsKey(reqField)) {
-                LOG.log(Level.SEVERE, "Checking metadata key: [" + reqField
-                        + "] for task: [" + task.getTaskName()
-                        + "]: failed: aborting workflow");
-                return false;
-            }
-        }
+public class SequentialWorkflowProcessor extends WorkflowProcessor implements
+    WorkflowStatus, CoreMetKeys, Runnable {
 
-        LOG.log(Level.INFO, "All required metadata fields present for task: ["
-                + task.getTaskName() + "]");
+  private Iterator<WorkflowTask> taskIterator;
 
-        return true;
-    }
+  /* our log stream */
+  private static Logger LOG = Logger
+      .getLogger(SequentialWorkflowProcessor.class.getName());
+
+  public SequentialWorkflowProcessor(WorkflowInstance wInst,
+      WorkflowInstanceRepository instRep, URL wParentUrl, long conditionWait) {
+    super(wInst, instRep, wParentUrl, conditionWait);
+    taskIterator = this.workflowInstance.getWorkflow().getTasks().iterator();
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see java.lang.Runnable#run()
+   */
+  public void run() {
+
+    String startDateTimeIsoStr = DateConvert.isoFormat(new Date());
+    this.workflowInstance.setStartDateTimeIsoStr(startDateTimeIsoStr);
+    this.persistWorkflowInstance();
+
+    while (running && taskIterator.hasNext()) {
+      if (isPaused()) {
+        LOG.log(Level.FINE,
+            "SequentialProcessor: Skipping execution: Paused: CurrentTask: "
+                + getTaskNameById(this.workflowInstance.getCurrentTaskId()));
+        continue;
+      }
+
+      WorkflowTask task = (WorkflowTask) taskIterator.next();
+      this.workflowInstance.setCurrentTaskId(task.getTaskId());
+
+      this.persistWorkflowInstance();
+      if (!checkTaskRequiredMetadata(task,
+          this.workflowInstance.getSharedContext())) {
+        this.workflowInstance.setStatus(METADATA_MISSING);
+        this.persistWorkflowInstance();
+        return;
+      }
+
+      if (task.getConditions() != null) {
+        while (!satisfied(task.getConditions(), task.getTaskId())
+            && isRunning()) {
+
+          if (!isPaused()) {
+            pause();
+          }
+
+          LOG.log(Level.FINEST,
+              "Pre-conditions for task: " + task.getTaskName()
+                  + " unsatisfied: waiting: " + waitForConditionSatisfy
+                  + " seconds before checking again.");
+          try {
+            Thread.currentThread().sleep(waitForConditionSatisfy * 1000);
+          } catch (InterruptedException ignore) {
+          }
+
+          if (!isPaused()) {
+            break;
+          }
+        }
+
+        if (!isRunning()) {
+          break;
+        }
+
+        if (isPaused()) {
+          resume();
+        }
+      }
+      LOG.log(
+          Level.FINEST,
+          "IterativeWorkflowProcessorThread: Executing task: "
+              + task.getTaskName());
+
+      WorkflowTaskInstance taskInstance = GenericWorkflowObjectFactory
+          .getTaskObjectFromClassName(task.getTaskInstanceClassName());
+      this.workflowInstance.getSharedContext().replaceMetadata(TASK_ID,
+          task.getTaskId());
+      this.workflowInstance.getSharedContext().replaceMetadata(
+          WORKFLOW_INST_ID, this.workflowInstance.getId());
+      this.workflowInstance.getSharedContext().replaceMetadata(JOB_ID,
+          this.workflowInstance.getId());
+      this.workflowInstance.getSharedContext().replaceMetadata(PROCESSING_NODE,
+          getHostname());
+      this.workflowInstance.getSharedContext().replaceMetadata(
+          WORKFLOW_MANAGER_URL, this.wmgrParentUrl.toString());
+
+      if (rClient != null) {
+        Job taskJob = new Job();
+        taskJob.setName(task.getTaskId());
+        taskJob
+            .setJobInstanceClassName("org.apache.oodt.cas.workflow.structs.TaskJob");
+        taskJob
+            .setJobInputClassName("org.apache.oodt.cas.workflow.structs.TaskJobInput");
+        taskJob.setLoadValue(new Integer(2));
+        taskJob
+            .setQueueName(task.getTaskConfig().getProperty(QUEUE_NAME) != null ? task
+                .getTaskConfig().getProperty(QUEUE_NAME) : DEFAULT_QUEUE_NAME);
+
+        TaskJobInput in = new TaskJobInput();
+        in.setDynMetadata(this.workflowInstance.getSharedContext());
+        in.setTaskConfig(task.getTaskConfig());
+        in.setWorkflowTaskInstanceClassName(task.getTaskInstanceClassName());
 
-    private String getTaskNameById(String taskId) {
-        for (Iterator i = workflowInst.getWorkflow().getTasks().iterator(); i
-                .hasNext();) {
-            WorkflowTask task = (WorkflowTask) i.next();
-            if (task.getTaskId().equals(taskId)) {
-                return task.getTaskName();
-            }
-        }
+        this.workflowInstance.setStatus(RESMGR_SUBMIT);
+        this.persistWorkflowInstance();
 
-        return null;
-    }
-
-    private boolean satisfied(List conditionList, String taskId) {
-        for (Iterator i = conditionList.iterator(); i.hasNext();) {
-            WorkflowCondition c = (WorkflowCondition) i.next();
-            WorkflowConditionInstance cInst = null;
-
-            // see if we've already cached this condition instance
-            if (CONDITION_CACHE.get(taskId) != null) {
-                HashMap conditionMap = (HashMap) CONDITION_CACHE.get(taskId);
-
-                /*
-                 * okay we have some conditions cached for this task, see if we
-                 * have the one we need
-                 */
-                if (conditionMap.get(c.getConditionId()) != null) {
-                    cInst = (WorkflowConditionInstance) conditionMap.get(c
-                            .getConditionId());
-                }
-                /* if not, then go ahead and create it and cache it */
-                else {
-                    cInst = GenericWorkflowObjectFactory
-                            .getConditionObjectFromClassName(c
-                                    .getConditionInstanceClassName());
-                    conditionMap.put(c.getConditionId(), cInst);
-                }
-            }
-            /* no conditions cached yet, so set everything up */
-            else {
-                HashMap conditionMap = new HashMap();
-                cInst = GenericWorkflowObjectFactory
-                        .getConditionObjectFromClassName(c
-                                .getConditionInstanceClassName());
-                conditionMap.put(c.getConditionId(), cInst);
-                CONDITION_CACHE.put(taskId, conditionMap);
-            }
-
-            // actually perform the evaluation
-            if (!cInst.evaluate(workflowInst.getSharedContext(), c.getTaskConfig())) {
-                return false;
-            }
-        }
-
-        return true;
-    }
-
-    private String getHostname() {
         try {
-            // Get hostname by textual representation of IP address
-            InetAddress addr = InetAddress.getLocalHost();
-            // Get the host name
-            String hostname = addr.getHostName();
-            return hostname;
-        } catch (UnknownHostException e) {
-        }
-        return null;
-    }
-
-    private void persistWorkflowInstance() {
-        try {
-            instanceRepository.updateWorkflowInstance(workflowInst);
-        } catch (InstanceRepositoryException e) {
-            LOG.log(Level.WARNING, "Exception persisting workflow instance: ["
-                    + workflowInst.getId() + "]: Message: " + e.getMessage());
-        }
-    }
-
-    private void executeTaskLocally(WorkflowTaskInstance instance,
-            Metadata met, WorkflowTaskConfiguration cfg, String taskName) {
-        try {
-            LOG.log(Level.INFO, "Executing task: [" + taskName + "] locally");
-            instance.run(met, cfg);
-        } catch (Exception e) {
+          this.currentJobId = rClient.submitJob(taskJob, in);
+          while (!safeCheckJobComplete(this.currentJobId) && isRunning()) {
+            try {
+              Thread.currentThread().sleep(pollingWaitTime * 1000);
+            } catch (InterruptedException ignore) {
+            }
+          }
+
+          if (!isRunning()) {
+            break;
+          }
+
+          WorkflowInstance updatedInst = null;
+          try {
+            updatedInst = instanceRepository
+                .getWorkflowInstanceById(this.workflowInstance.getId());
+            this.workflowInstance = updatedInst;
+          } catch (InstanceRepositoryException e) {
             e.printStackTrace();
-            LOG.log(Level.WARNING, "Exception executing task: [" + taskName
-                    + "] locally: Message: " + e.getMessage());
-        }
-    }
-
-    private boolean safeCheckJobComplete(String jobId) {
-        try {
-            return rClient.isJobComplete(jobId);
-        } catch (Exception e) {
-            LOG.log(Level.WARNING,
-                    "Exception checking completion status for job: [" + jobId
-                            + "]: Messsage: " + e.getMessage());
-            return false;
-        }
-    }
+            LOG.log(Level.WARNING, "Unable to get " + "updated workflow "
+                + "instance record " + "when executing remote job: Message: "
+                + e.getMessage());
+          }
+
+        } catch (JobExecutionException e) {
+          LOG.log(Level.WARNING,
+              "Job execution exception using resource manager to execute job: Message: "
+                  + e.getMessage());
+        }
+      } else {
+        this.workflowInstance.setStatus(STARTED);
+        String currentTaskIsoStartDateTimeStr = DateConvert
+            .isoFormat(new Date());
+        this.workflowInstance
+            .setCurrentTaskStartDateTimeIsoStr(currentTaskIsoStartDateTimeStr);
+        this.workflowInstance.setCurrentTaskEndDateTimeIsoStr(null); 
+        this.persistWorkflowInstance();
+        this.executeTaskLocally(taskInstance,
+            this.workflowInstance.getSharedContext(), task.getTaskConfig(),
+            task.getTaskName());
+        String currentTaskIsoEndDateTimeStr = DateConvert.isoFormat(new Date());
+        this.workflowInstance
+            .setCurrentTaskEndDateTimeIsoStr(currentTaskIsoEndDateTimeStr);
+        this.persistWorkflowInstance();
+      }
+
+      LOG.log(Level.FINEST, "SequentialWorkflowProcessor: Completed task: "
+          + task.getTaskName());
+
+    }
+
+    LOG.log(Level.FINEST, "SequentialWorkflowProcessor: Completed workflow: "
+        + this.workflowInstance.getWorkflow().getName());
+    if (isRunning()) {
+      stop();
+    }
+
+  }
+
+  public synchronized void stop() {
+    running = false;
+    if (this.rClient != null && this.currentJobId != null) {
+      if (!this.rClient.killJob(this.currentJobId)) {
+        LOG.log(Level.WARNING, "Attempt to kill " + "current resmgr job: ["
+            + this.currentJobId + "]: failed");
+      }
+    }
+
+    this.workflowInstance.setStatus(FINISHED);
+    String isoEndDateTimeStr = DateConvert.isoFormat(new Date());
+    this.workflowInstance.setEndDateTimeIsoStr(isoEndDateTimeStr);
+    this.persistWorkflowInstance();
+  }
+
+  public synchronized void resume() {
+    this.paused = false;
+    this.workflowInstance.setStatus(STARTED);
+    this.persistWorkflowInstance();
+  }
+
+  public synchronized void pause() {
+    this.paused = true;
+    this.workflowInstance.setStatus(PAUSED);
+    this.persistWorkflowInstance();
+  }
 
 }

Modified: oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/ThreadPoolWorkflowEngine.java
URL: http://svn.apache.org/viewvc/oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/ThreadPoolWorkflowEngine.java?rev=1139325&r1=1139324&r2=1139325&view=diff
==============================================================================
--- oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/ThreadPoolWorkflowEngine.java (original)
+++ oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/ThreadPoolWorkflowEngine.java Fri Jun 24 14:26:40 2011
@@ -15,7 +15,6 @@
  * limitations under the License.
  */
 
-
 package org.apache.oodt.cas.workflow.engine;
 
 //OODT imports
@@ -27,7 +26,7 @@ import org.apache.oodt.cas.workflow.stru
 import org.apache.oodt.cas.workflow.structs.WorkflowTask;
 import org.apache.oodt.cas.workflow.structs.exceptions.EngineException;
 import org.apache.oodt.cas.workflow.structs.exceptions.InstanceRepositoryException;
-import org.apache.oodt.cas.workflow.engine.IterativeWorkflowProcessorThread;
+import org.apache.oodt.cas.workflow.engine.SequentialWorkflowProcessor;
 import org.apache.oodt.cas.workflow.instrepo.WorkflowInstanceRepository;
 import org.apache.oodt.commons.util.DateConvert;
 
@@ -46,420 +45,436 @@ import EDU.oswego.cs.dl.util.concurrent.
 import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;
 
 /**
- * @author mattmann
- * @version $Revsion$
  * 
- * <p>
  * The ThreadPooling portion of the WorkflowEngine. This class is meant to be an
  * extension point for WorkflowEngines that want to implement ThreadPooling.
  * This WorkflowEngine provides everything needed to manage a ThreadPool using
  * Doug Lea's wonderful java.util.concurrent package that made it into JDK5.
- * </p>
+ * 
+ * @author mattmann
+ * 
  */
 public class ThreadPoolWorkflowEngine implements WorkflowEngine, WorkflowStatus {
 
-    /* our thread pool */
-    private PooledExecutor pool = null;
+  /* our thread pool */
+  private PooledExecutor pool = null;
 
-    /* our worker thread hash mapping worker threads to workflow instance ids */
-    private HashMap workerMap = null;
+  /* our worker thread hash mapping worker threads to workflow instance ids */
+  private HashMap workerMap = null;
 
-    /* our log stream */
-    private static final Logger LOG = Logger
-            .getLogger(ThreadPoolWorkflowEngine.class.getName());
-
-    /* our instance repository */
-    private WorkflowInstanceRepository instRep = null;
-
-    /* our resource manager client */
-    private XmlRpcResourceManagerClient rClient = null;
-
-    /* the URL pointer to the parent Workflow Manager */
-    private URL wmgrUrl = null;
-
-    /**
-     * Default Constructor.
-     * 
-     * @param instRep
-     *            The WorkflowInstanceRepository to be used by this engine.
-     * @param queueSize
-     *            The size of the queue that the workflow engine should use
-     *            (irrelevant if unlimitedQueue is set to true)
-     * @param maxPoolSize
-     *            The minimum thread pool size.
-     * @param minPoolSize
-     *            The maximum thread pool size.
-     * @param threadKeepAliveTime
-     *            The amount of minutes that each thread in the pool should be
-     *            kept alive.
-     * @param unlimitedQueue
-     *            Whether or not to use a queue whose bounds are dictated by the
-     *            physical memory of the underlying hardware.
-     * @param resUrl
-     *            A URL pointer to a resource manager. If this is set Tasks will
-     *            be wrapped as Resource Manager {@link Job}s and sent through
-     *            the Resource Manager. If this parameter is not set, local
-     *            execution (the default) will be used
-     */
-    public ThreadPoolWorkflowEngine(WorkflowInstanceRepository instRep,
-            int queueSize, int maxPoolSize, int minPoolSize,
-            long threadKeepAliveTime, boolean unlimitedQueue, URL resUrl) {
-
-        this.instRep = instRep;
-        Channel c = null;
-        if (unlimitedQueue) {
-            c = new LinkedQueue();
-        } else {
-            c = new BoundedBuffer(queueSize);
-        }
-
-        pool = new PooledExecutor(c, maxPoolSize);
-        pool.setMinimumPoolSize(minPoolSize);
-        pool.setKeepAliveTime(1000 * 60 * threadKeepAliveTime);
-
-        workerMap = new HashMap();
-
-        if (resUrl != null)
-            rClient = new XmlRpcResourceManagerClient(resUrl);
-    }
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.apache.oodt.cas.workflow.engine.WorkflowEngine#pauseWorkflowInstance(java.lang.String)
-     */
-    public synchronized void pauseWorkflowInstance(String workflowInstId) {
-        // okay, try and look up that worker thread in our hash map
-        IterativeWorkflowProcessorThread worker = (IterativeWorkflowProcessorThread) workerMap
-                .get(workflowInstId);
-        if (worker == null) {
-            LOG
-                    .log(
-                            Level.WARNING,
-                            "WorkflowEngine: Attempt to pause workflow instance id: "
-                                    + workflowInstId
-                                    + ", however, this engine is not tracking its execution");
-            return;
-        }
-
-        // otherwise, all good
-        worker.pause();
-
-    }
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.apache.oodt.cas.workflow.engine.WorkflowEngine#resumeWorkflowInstance(java.lang.String)
-     */
-    public synchronized void resumeWorkflowInstance(String workflowInstId) {
-        // okay, try and look up that worker thread in our hash map
-        IterativeWorkflowProcessorThread worker = (IterativeWorkflowProcessorThread) workerMap
-                .get(workflowInstId);
-        if (worker == null) {
-            LOG.log(Level.WARNING,
-                    "WorkflowEngine: Attempt to resume workflow instance id: "
-                            + workflowInstId + ", however, this engine is "
-                            + "not tracking its execution");
-            return;
-        }
-
-        // also check to make sure that the worker is currently paused
-        // only can resume WorkflowInstances that are paused, right?
-        if (!worker.isPaused()) {
-            LOG.log(Level.WARNING,
-                    "WorkflowEngine: Attempt to resume a workflow that "
-                            + "isn't paused currently: instance id: "
-                            + workflowInstId);
-            return;
-        }
-
-        // okay, all good
-        worker.resume();
-
-    }
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.apache.oodt.cas.workflow.engine.WorkflowEngine#startWorkflow(org.apache.oodt.cas.workflow.structs.Workflow,
-     *      org.apache.oodt.cas.metadata.Metadata)
-     */
-    public synchronized WorkflowInstance startWorkflow(Workflow workflow,
-            Metadata metadata) throws EngineException {
-        // to start the workflow, we create a default workflow instance
-        // populate it
-        // persist it
-        // add it to the worker map
-        // start it
-
-        WorkflowInstance wInst = new WorkflowInstance();
-        wInst.setWorkflow(workflow);
-        wInst.setCurrentTaskId(((WorkflowTask) workflow.getTasks().get(0))
-                .getTaskId());
-        wInst.setSharedContext(metadata);
-        wInst.setStatus(CREATED);
-        persistWorkflowInstance(wInst);
-
-        IterativeWorkflowProcessorThread worker = new IterativeWorkflowProcessorThread(
-                wInst, instRep, this.wmgrUrl);
-        worker.setRClient(rClient);
-        workerMap.put(wInst.getId(), worker);
-
-        wInst.setStatus(QUEUED);
-        persistWorkflowInstance(wInst);
-
-        try {
-            pool.execute(worker);
-        } catch (InterruptedException e) {
-            throw new EngineException(e);
-        }
-
-        return wInst;
-
-    }
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.apache.oodt.cas.workflow.engine.WorkflowEngine#getInstanceRepository()
-     */
-    public WorkflowInstanceRepository getInstanceRepository() {
-        return this.instRep;
-    }
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.apache.oodt.cas.workflow.engine.WorkflowEngine#updateMetadata(java.lang.String,
-     *      org.apache.oodt.cas.metadata.Metadata)
-     */
-    public synchronized boolean updateMetadata(String workflowInstId,
-            Metadata met) {
-        // okay, try and look up that worker thread in our hash map
-        IterativeWorkflowProcessorThread worker = (IterativeWorkflowProcessorThread) workerMap
-                .get(workflowInstId);
-        if (worker == null) {
-            LOG.log(Level.WARNING,
-                    "WorkflowEngine: Attempt to update metadata context "
-                            + "for workflow instance id: " + workflowInstId
-                            + ", however, this engine is "
-                            + "not tracking its execution");
-            return false;
-        }
-
-        worker.getWorkflowInstance().setSharedContext(met);
-        try {
-            persistWorkflowInstance(worker.getWorkflowInstance());
-        } catch (Exception e) {
-            LOG.log(Level.WARNING, "Exception persisting workflow instance: ["
-                    + worker.getWorkflowInstance().getId() + "]: Message: "
-                    + e.getMessage());
-            return false;
-        }
-
-        return true;
-    }
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.apache.oodt.cas.workflow.engine.WorkflowEngine#setWorkflowManagerUrl(java.net.URL)
-     */
-    public void setWorkflowManagerUrl(URL url) {
-        this.wmgrUrl = url;
-    }
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.apache.oodt.cas.workflow.engine.WorkflowEngine#stopWorkflow(java.lang.String)
-     */
-    public synchronized void stopWorkflow(String workflowInstId) {
-        // okay, try and look up that worker thread in our hash map
-        IterativeWorkflowProcessorThread worker = (IterativeWorkflowProcessorThread) workerMap
-                .get(workflowInstId);
-        if (worker == null) {
-            LOG.log(Level.WARNING,
-                    "WorkflowEngine: Attempt to stop workflow instance id: "
-                            + workflowInstId + ", however, this engine is "
-                            + "not tracking its execution");
-            return;
-        }
-
-        worker.stop();
-    }
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.apache.oodt.cas.workflow.engine.WorkflowEngine#getCurrentTaskWallClockMinutes(java.lang.String)
-     */
-    public double getCurrentTaskWallClockMinutes(String workflowInstId) {
-        // get the workflow instance that we're talking about
-        WorkflowInstance inst = safeGetWorkflowInstanceById(workflowInstId);
-        return getCurrentTaskWallClockMinutes(inst);
-    }
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.apache.oodt.cas.workflow.engine.WorkflowEngine#getWorkflowInstanceMetadata(java.lang.String)
-     */
-    public Metadata getWorkflowInstanceMetadata(String workflowInstId) {
-        // okay, try and look up that worker thread in our hash map
-        IterativeWorkflowProcessorThread worker = (IterativeWorkflowProcessorThread) workerMap
-                .get(workflowInstId);
-        if (worker == null) {
-            // try and get the metadata
-            // from the workflow instance repository (as it was persisted)
-            try {
-                WorkflowInstance inst = instRep
-                        .getWorkflowInstanceById(workflowInstId);
-                return inst.getSharedContext();
-            } catch (InstanceRepositoryException e) {
-                LOG.log(Level.FINEST,
-                        "WorkflowEngine: Attempt to get metadata "
-                                + "for workflow instance id: " + workflowInstId
-                                + ", however, this engine is "
-                                + "not tracking its execution and the id: ["
-                                + workflowInstId + "] "
-                                + "was never persisted to "
-                                + "the instance repository");
-                e.printStackTrace();
-                return new Metadata();
-            }
-        }
-
-        return worker.getWorkflowInstance().getSharedContext();
-    }
-
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.apache.oodt.cas.workflow.engine.WorkflowEngine#getWallClockMinutes(java.lang.String)
-     */
-    public double getWallClockMinutes(String workflowInstId) {
-        // get the workflow instance that we're talking about
-        WorkflowInstance inst = safeGetWorkflowInstanceById(workflowInstId);
-        return getWallClockMinutes(inst);
-    }
-
-    protected static double getWallClockMinutes(WorkflowInstance inst) {
-        if (inst == null) {
-            return 0.0;
-        }
-
-        Date currentDateOrStopTime = (inst.getEndDateTimeIsoStr() != null
-                && !inst.getEndDateTimeIsoStr().equals("") && !inst
-                .getEndDateTimeIsoStr().equals("null")) ? safeDateConvert(inst
-                .getEndDateTimeIsoStr()) : new Date();
-
-        Date workflowStartDateTime = null;
-
-        if (inst.getStartDateTimeIsoStr() == null
-                || (inst.getStartDateTimeIsoStr() != null && (inst
-                        .getStartDateTimeIsoStr().equals("") || inst
-                        .getStartDateTimeIsoStr().equals("null")))) {
-            return 0.0;
-        }
-
-        try {
-            workflowStartDateTime = DateConvert.isoParse(inst
-                    .getStartDateTimeIsoStr());
-        } catch (ParseException e) {
-            return 0.0;
-        }
-
-        long diffMs = currentDateOrStopTime.getTime()
-                - workflowStartDateTime.getTime();
-        double diffSecs = (diffMs * 1.0 / 1000.0);
-        double diffMins = diffSecs / 60.0;
-        return diffMins;
-
-    }
-
-    protected static double getCurrentTaskWallClockMinutes(WorkflowInstance inst) {
-        if (inst == null) {
-            return 0.0;
-        }
-
-        Date currentDateOrStopTime = (inst.getCurrentTaskEndDateTimeIsoStr() != null
-                && !inst.getCurrentTaskEndDateTimeIsoStr().equals("") && !inst
-                .getCurrentTaskEndDateTimeIsoStr().equals("null")) ? safeDateConvert(inst
-                .getCurrentTaskEndDateTimeIsoStr())
-                : new Date();
-
-        Date workflowTaskStartDateTime = null;
-
-        if (inst.getCurrentTaskStartDateTimeIsoStr() == null
-                || (inst.getCurrentTaskStartDateTimeIsoStr() != null && (inst
-                        .getCurrentTaskStartDateTimeIsoStr().equals("") || inst
-                        .getCurrentTaskStartDateTimeIsoStr().equals("null")))) {
-            return 0.0;
-        }
-
-        try {
-            workflowTaskStartDateTime = DateConvert.isoParse(inst
-                    .getCurrentTaskStartDateTimeIsoStr());
-        } catch (ParseException e) {
-            return 0.0;
-        }
-
-        // should never be in this state, so return 0
-        if (workflowTaskStartDateTime.after(currentDateOrStopTime)) {
-            LOG.log(Level.WARNING, "Start date time: ["
-                    + DateConvert.isoFormat(workflowTaskStartDateTime)
-                    + " of workflow inst [" + inst.getId() + "] is AFTER "
-                    + "End date time: ["
-                    + DateConvert.isoFormat(currentDateOrStopTime)
-                    + "] of workflow inst.");
-            return 0.0;
-        }
-
-        long diffMs = currentDateOrStopTime.getTime()
-                - workflowTaskStartDateTime.getTime();
-        double diffSecs = (diffMs * 1.0 / 1000.0);
-        double diffMins = diffSecs / 60.0;
-        return diffMins;
-    }
-
-    private synchronized void persistWorkflowInstance(WorkflowInstance wInst)
-            throws EngineException {
-
-        try {
-            if (wInst.getId() == null
-                    || (wInst.getId() != null && wInst.getId().equals(""))) {
-                // we have to persist it by adding it
-                // rather than updating it
-                instRep.addWorkflowInstance(wInst);
-
-            } else {
-                // persist by update
-                instRep.updateWorkflowInstance(wInst);
-            }
-        } catch (InstanceRepositoryException e) {
-            e.printStackTrace();
-            throw new EngineException(e.getMessage());
-        }
-
-    }
-
-    private WorkflowInstance safeGetWorkflowInstanceById(String workflowInstId) {
-        try {
-            return instRep.getWorkflowInstanceById(workflowInstId);
-        } catch (Exception e) {
-            return null;
-        }
-    }
-
-    private static Date safeDateConvert(String isoTimeStr) {
-        try {
-            return DateConvert.isoParse(isoTimeStr);
-        } catch (Exception ignore) {
-            ignore.printStackTrace();
-            return null;
-        }
+  /* our log stream */
+  private static final Logger LOG = Logger
+      .getLogger(ThreadPoolWorkflowEngine.class.getName());
+
+  /* our instance repository */
+  private WorkflowInstanceRepository instRep = null;
+
+  /* our resource manager client */
+  private XmlRpcResourceManagerClient rClient = null;
+
+  /* the URL pointer to the parent Workflow Manager */
+  private URL wmgrUrl = null;
+
+  /* how long to wait before checking whether a condition is satisfied. */
+  private long conditionWait;
+
+  /**
+   * Default Constructor.
+   * 
+   * @param instRep
+   *          The WorkflowInstanceRepository to be used by this engine.
+   * @param queueSize
+   *          The size of the queue that the workflow engine should use
+   *          (irrelevant if unlimitedQueue is set to true)
+   * @param maxPoolSize
+   *          The minimum thread pool size.
+   * @param minPoolSize
+   *          The maximum thread pool size.
+   * @param threadKeepAliveTime
+   *          The amount of minutes that each thread in the pool should be kept
+   *          alive.
+   * @param unlimitedQueue
+   *          Whether or not to use a queue whose bounds are dictated by the
+   *          physical memory of the underlying hardware.
+   * @param resUrl
+   *          A URL pointer to a resource manager. If this is set Tasks will be
+   *          wrapped as Resource Manager {@link Job}s and sent through the
+   *          Resource Manager. If this parameter is not set, local execution
+   *          (the default) will be used
+   */
+  public ThreadPoolWorkflowEngine(WorkflowInstanceRepository instRep,
+      int queueSize, int maxPoolSize, int minPoolSize,
+      long threadKeepAliveTime, boolean unlimitedQueue, URL resUrl) {
+
+    this.instRep = instRep;
+    Channel c = null;
+    if (unlimitedQueue) {
+      c = new LinkedQueue();
+    } else {
+      c = new BoundedBuffer(queueSize);
+    }
+
+    pool = new PooledExecutor(c, maxPoolSize);
+    pool.setMinimumPoolSize(minPoolSize);
+    pool.setKeepAliveTime(1000 * 60 * threadKeepAliveTime);
+
+    workerMap = new HashMap();
+
+    if (resUrl != null)
+      rClient = new XmlRpcResourceManagerClient(resUrl);
+
+    this.conditionWait = Long.getLong(
+        "org.apache.oodt.cas.workflow.engine.preConditionWaitTime", 10)
+        .longValue();
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see
+   * org.apache.oodt.cas.workflow.engine.WorkflowEngine#pauseWorkflowInstance
+   * (java.lang.String)
+   */
+  public synchronized void pauseWorkflowInstance(String workflowInstId) {
+    // okay, try and look up that worker thread in our hash map
+    SequentialWorkflowProcessor worker = (SequentialWorkflowProcessor) workerMap
+        .get(workflowInstId);
+    if (worker == null) {
+      LOG.log(Level.WARNING,
+          "WorkflowEngine: Attempt to pause workflow instance id: "
+              + workflowInstId
+              + ", however, this engine is not tracking its execution");
+      return;
+    }
+
+    // otherwise, all good
+    worker.pause();
+
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see
+   * org.apache.oodt.cas.workflow.engine.WorkflowEngine#resumeWorkflowInstance
+   * (java.lang.String)
+   */
+  public synchronized void resumeWorkflowInstance(String workflowInstId) {
+    // okay, try and look up that worker thread in our hash map
+    SequentialWorkflowProcessor worker = (SequentialWorkflowProcessor) workerMap
+        .get(workflowInstId);
+    if (worker == null) {
+      LOG.log(Level.WARNING,
+          "WorkflowEngine: Attempt to resume workflow instance id: "
+              + workflowInstId + ", however, this engine is "
+              + "not tracking its execution");
+      return;
+    }
+
+    // also check to make sure that the worker is currently paused
+    // only can resume WorkflowInstances that are paused, right?
+    if (!worker.isPaused()) {
+      LOG.log(Level.WARNING,
+          "WorkflowEngine: Attempt to resume a workflow that "
+              + "isn't paused currently: instance id: " + workflowInstId);
+      return;
+    }
+
+    // okay, all good
+    worker.resume();
+
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see
+   * org.apache.oodt.cas.workflow.engine.WorkflowEngine#startWorkflow(org.apache
+   * .oodt.cas.workflow.structs.Workflow, org.apache.oodt.cas.metadata.Metadata)
+   */
+  public synchronized WorkflowInstance startWorkflow(Workflow workflow,
+      Metadata metadata) throws EngineException {
+    // to start the workflow, we create a default workflow instance
+    // populate it
+    // persist it
+    // add it to the worker map
+    // start it
+
+    WorkflowInstance wInst = new WorkflowInstance();
+    wInst.setWorkflow(workflow);
+    wInst.setCurrentTaskId(((WorkflowTask) workflow.getTasks().get(0))
+        .getTaskId());
+    wInst.setSharedContext(metadata);
+    wInst.setStatus(CREATED);
+    persistWorkflowInstance(wInst);
+
+    SequentialWorkflowProcessor worker = new SequentialWorkflowProcessor(wInst,
+        instRep, this.wmgrUrl, this.conditionWait);
+    worker.setRClient(rClient);
+    workerMap.put(wInst.getId(), worker);
+
+    wInst.setStatus(QUEUED);
+    persistWorkflowInstance(wInst);
+
+    try {
+      pool.execute(worker);
+    } catch (InterruptedException e) {
+      throw new EngineException(e);
+    }
+
+    return wInst;
+
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see
+   * org.apache.oodt.cas.workflow.engine.WorkflowEngine#getInstanceRepository()
+   */
+  public WorkflowInstanceRepository getInstanceRepository() {
+    return this.instRep;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see
+   * org.apache.oodt.cas.workflow.engine.WorkflowEngine#updateMetadata(java.
+   * lang.String, org.apache.oodt.cas.metadata.Metadata)
+   */
+  public synchronized boolean updateMetadata(String workflowInstId, Metadata met) {
+    // okay, try and look up that worker thread in our hash map
+    SequentialWorkflowProcessor worker = (SequentialWorkflowProcessor) workerMap
+        .get(workflowInstId);
+    if (worker == null) {
+      LOG.log(Level.WARNING,
+          "WorkflowEngine: Attempt to update metadata context "
+              + "for workflow instance id: " + workflowInstId
+              + ", however, this engine is " + "not tracking its execution");
+      return false;
+    }
+
+    worker.getWorkflowInstance().setSharedContext(met);
+    try {
+      persistWorkflowInstance(worker.getWorkflowInstance());
+    } catch (Exception e) {
+      LOG.log(
+          Level.WARNING,
+          "Exception persisting workflow instance: ["
+              + worker.getWorkflowInstance().getId() + "]: Message: "
+              + e.getMessage());
+      return false;
+    }
+
+    return true;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see
+   * org.apache.oodt.cas.workflow.engine.WorkflowEngine#setWorkflowManagerUrl
+   * (java.net.URL)
+   */
+  public void setWorkflowManagerUrl(URL url) {
+    this.wmgrUrl = url;
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see
+   * org.apache.oodt.cas.workflow.engine.WorkflowEngine#stopWorkflow(java.lang
+   * .String)
+   */
+  public synchronized void stopWorkflow(String workflowInstId) {
+    // okay, try and look up that worker thread in our hash map
+    SequentialWorkflowProcessor worker = (SequentialWorkflowProcessor) workerMap
+        .get(workflowInstId);
+    if (worker == null) {
+      LOG.log(Level.WARNING,
+          "WorkflowEngine: Attempt to stop workflow instance id: "
+              + workflowInstId + ", however, this engine is "
+              + "not tracking its execution");
+      return;
+    }
+
+    worker.stop();
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see org.apache.oodt.cas.workflow.engine.WorkflowEngine#
+   * getCurrentTaskWallClockMinutes(java.lang.String)
+   */
+  public double getCurrentTaskWallClockMinutes(String workflowInstId) {
+    // get the workflow instance that we're talking about
+    WorkflowInstance inst = safeGetWorkflowInstanceById(workflowInstId);
+    return getCurrentTaskWallClockMinutes(inst);
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see
+   * org.apache.oodt.cas.workflow.engine.WorkflowEngine#getWorkflowInstanceMetadata
+   * (java.lang.String)
+   */
+  public Metadata getWorkflowInstanceMetadata(String workflowInstId) {
+    // okay, try and look up that worker thread in our hash map
+    SequentialWorkflowProcessor worker = (SequentialWorkflowProcessor) workerMap
+        .get(workflowInstId);
+    if (worker == null) {
+      // try and get the metadata
+      // from the workflow instance repository (as it was persisted)
+      try {
+        WorkflowInstance inst = instRep.getWorkflowInstanceById(workflowInstId);
+        return inst.getSharedContext();
+      } catch (InstanceRepositoryException e) {
+        LOG.log(Level.FINEST, "WorkflowEngine: Attempt to get metadata "
+            + "for workflow instance id: " + workflowInstId
+            + ", however, this engine is "
+            + "not tracking its execution and the id: [" + workflowInstId
+            + "] " + "was never persisted to " + "the instance repository");
+        e.printStackTrace();
+        return new Metadata();
+      }
+    }
+
+    return worker.getWorkflowInstance().getSharedContext();
+  }
+
+  /*
+   * (non-Javadoc)
+   * 
+   * @see
+   * org.apache.oodt.cas.workflow.engine.WorkflowEngine#getWallClockMinutes(
+   * java.lang.String)
+   */
+  public double getWallClockMinutes(String workflowInstId) {
+    // get the workflow instance that we're talking about
+    WorkflowInstance inst = safeGetWorkflowInstanceById(workflowInstId);
+    return getWallClockMinutes(inst);
+  }
+
+  protected static double getWallClockMinutes(WorkflowInstance inst) {
+    if (inst == null) {
+      return 0.0;
+    }
+
+    Date currentDateOrStopTime = (inst.getEndDateTimeIsoStr() != null
+        && !inst.getEndDateTimeIsoStr().equals("") && !inst
+        .getEndDateTimeIsoStr().equals("null")) ? safeDateConvert(inst
+        .getEndDateTimeIsoStr()) : new Date();
+
+    Date workflowStartDateTime = null;
+
+    if (inst.getStartDateTimeIsoStr() == null
+        || (inst.getStartDateTimeIsoStr() != null && (inst
+            .getStartDateTimeIsoStr().equals("") || inst
+            .getStartDateTimeIsoStr().equals("null")))) {
+      return 0.0;
+    }
+
+    try {
+      workflowStartDateTime = DateConvert.isoParse(inst
+          .getStartDateTimeIsoStr());
+    } catch (ParseException e) {
+      return 0.0;
+    }
+
+    long diffMs = currentDateOrStopTime.getTime()
+        - workflowStartDateTime.getTime();
+    double diffSecs = (diffMs * 1.0 / 1000.0);
+    double diffMins = diffSecs / 60.0;
+    return diffMins;
+
+  }
+
+  protected static double getCurrentTaskWallClockMinutes(WorkflowInstance inst) {
+    if (inst == null) {
+      return 0.0;
+    }
+
+    Date currentDateOrStopTime = (inst.getCurrentTaskEndDateTimeIsoStr() != null
+        && !inst.getCurrentTaskEndDateTimeIsoStr().equals("") && !inst
+        .getCurrentTaskEndDateTimeIsoStr().equals("null")) ? safeDateConvert(inst
+        .getCurrentTaskEndDateTimeIsoStr()) : new Date();
+
+    Date workflowTaskStartDateTime = null;
+
+    if (inst.getCurrentTaskStartDateTimeIsoStr() == null
+        || (inst.getCurrentTaskStartDateTimeIsoStr() != null && (inst
+            .getCurrentTaskStartDateTimeIsoStr().equals("") || inst
+            .getCurrentTaskStartDateTimeIsoStr().equals("null")))) {
+      return 0.0;
+    }
+
+    try {
+      workflowTaskStartDateTime = DateConvert.isoParse(inst
+          .getCurrentTaskStartDateTimeIsoStr());
+    } catch (ParseException e) {
+      return 0.0;
+    }
+
+    // should never be in this state, so return 0
+    if (workflowTaskStartDateTime.after(currentDateOrStopTime)) {
+      LOG.log(
+          Level.WARNING,
+          "Start date time: ["
+              + DateConvert.isoFormat(workflowTaskStartDateTime)
+              + " of workflow inst [" + inst.getId() + "] is AFTER "
+              + "End date time: ["
+              + DateConvert.isoFormat(currentDateOrStopTime)
+              + "] of workflow inst.");
+      return 0.0;
+    }
+
+    long diffMs = currentDateOrStopTime.getTime()
+        - workflowTaskStartDateTime.getTime();
+    double diffSecs = (diffMs * 1.0 / 1000.0);
+    double diffMins = diffSecs / 60.0;
+    return diffMins;
+  }
+
+  private synchronized void persistWorkflowInstance(WorkflowInstance wInst)
+      throws EngineException {
+
+    try {
+      if (wInst.getId() == null
+          || (wInst.getId() != null && wInst.getId().equals(""))) {
+        // we have to persist it by adding it
+        // rather than updating it
+        instRep.addWorkflowInstance(wInst);
+
+      } else {
+        // persist by update
+        instRep.updateWorkflowInstance(wInst);
+      }
+    } catch (InstanceRepositoryException e) {
+      e.printStackTrace();
+      throw new EngineException(e.getMessage());
+    }
+
+  }
+
+  protected WorkflowInstance safeGetWorkflowInstanceById(String workflowInstId) {
+    try {
+      return this.instRep.getWorkflowInstanceById(workflowInstId);
+    } catch (Exception e) {
+      return null;
+    }
+  }
+
+  private static Date safeDateConvert(String isoTimeStr) {
+    try {
+      return DateConvert.isoParse(isoTimeStr);
+    } catch (Exception ignore) {
+      ignore.printStackTrace();
+      return null;
     }
+  }
 
 }

Copied: oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/WorkflowProcessor.java (from r1137249, oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/WorkflowProcessorThread.java)
URL: http://svn.apache.org/viewvc/oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/WorkflowProcessor.java?p2=oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/WorkflowProcessor.java&p1=oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/WorkflowProcessorThread.java&r1=1137249&r2=1139325&rev=1139325&view=diff
==============================================================================
--- oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/WorkflowProcessorThread.java (original)
+++ oodt/trunk/workflow/src/main/java/org/apache/oodt/cas/workflow/engine/WorkflowProcessor.java Fri Jun 24 14:26:40 2011
@@ -15,58 +15,303 @@
  * limitations under the License.
  */
 
-
 package org.apache.oodt.cas.workflow.engine;
 
+//JDK imports
+import java.net.InetAddress;
+import java.net.URL;
+import java.net.UnknownHostException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
 //OODT imports
+import org.apache.oodt.cas.metadata.Metadata;
+import org.apache.oodt.cas.resource.system.XmlRpcResourceManagerClient;
+import org.apache.oodt.cas.workflow.instrepo.WorkflowInstanceRepository;
+import org.apache.oodt.cas.workflow.structs.WorkflowCondition;
+import org.apache.oodt.cas.workflow.structs.WorkflowConditionInstance;
 import org.apache.oodt.cas.workflow.structs.WorkflowInstance;
+import org.apache.oodt.cas.workflow.structs.WorkflowTask;
+import org.apache.oodt.cas.workflow.structs.WorkflowTaskConfiguration;
+import org.apache.oodt.cas.workflow.structs.WorkflowTaskInstance;
+import org.apache.oodt.cas.workflow.structs.exceptions.InstanceRepositoryException;
+import org.apache.oodt.cas.workflow.util.GenericWorkflowObjectFactory;
 
 /**
- * @author mattmann
- * @version $Revision$
- * 
- * <p>
- * A Threaded interface for processing a {@link WorkflowInstance}. The job of
- * this class is to actually take the WorkflowInstance and execute its jobs. The
- * class should maintain the state of the instance, such as the currentTaskId,
- * and so forth.
- * </p>
+ * An abstract base class representing the methodology for processing a
+ * {@link WorkflowInstance}. The job of this class is to actually take the
+ * WorkflowInstance and execute its jobs. The class should maintain the state of
+ * the instance, such as the currentTaskId, and so forth.
  * 
  */
-public interface WorkflowProcessorThread extends Runnable {
+public abstract class WorkflowProcessor {
+
+  private static final Logger LOG = Logger.getLogger(WorkflowProcessor.class
+      .getName());
+
+  protected WorkflowInstance workflowInstance;
+
+  protected long waitForConditionSatisfy = -1;
+
+  protected WorkflowInstanceRepository instanceRepository = null;
+
+  protected XmlRpcResourceManagerClient rClient = null;
+
+  protected long pollingWaitTime = 10L;
+
+  protected boolean running = false;
+
+  protected int timesPaused;
+
+  protected static final String DEFAULT_QUEUE_NAME = "high";
+
+  protected Map<String, HashMap<String, WorkflowConditionInstance>> CONDITION_CACHE = new HashMap<String, HashMap<String, WorkflowConditionInstance>>();
+
+  protected URL wmgrParentUrl = null;
+
+  protected String currentJobId = null;
+
+  protected boolean paused = false;
+
+  public WorkflowProcessor(WorkflowInstance workflowInstance,
+      WorkflowInstanceRepository instRep, URL wParentUrl, long conditionWait) {
+    this.workflowInstance = workflowInstance;
+    this.instanceRepository = instRep;
+    this.running = true;
+    this.waitForConditionSatisfy = conditionWait;
+    this.pollingWaitTime = conditionWait;
+    this.wmgrParentUrl = wParentUrl;
+
+  }
+
+  /**
+   * @return the workflowInstance
+   */
+  public WorkflowInstance getWorkflowInstance() {
+    return workflowInstance;
+  }
+
+  /**
+   * @param workflowInstance
+   *          the workflowInstance to set
+   */
+  public void setWorkflowInstance(WorkflowInstance workflowInstance) {
+    this.workflowInstance = workflowInstance;
+  }
+
+  /**
+   * <p>
+   * Stops once and for all the thread from processing the workflow. This method
+   * should not maintain the state of the workflow, it should gracefully shut
+   * down the WorkflowProcessorThread and any of its subsequent resources.
+   * </p>
+   * 
+   */
+  public abstract void stop();
+
+  /**
+   * <p>
+   * Resumes execution of a {@link #pause}d {@link WorkflowInstace} by this
+   * WorkflowProcessorThread.
+   * </p>
+   * 
+   */
+  public abstract void resume();
+
+  /**
+   * <p>
+   * Pauses exectuion of a {@link WorkflowInstace} being handled by this
+   * WorkflowProcessorThread.
+   * </p>
+   * 
+   */
+  public abstract void pause();
+
+  /**
+   * Returns the identifier of the current {@link WorkflowTask} being processed
+   * by this WorkflowProcessor.
+   * 
+   * @return the identifier of the current {@link WorkflowTask} being processed
+   *         by this WorkflowProcessor.
+   */
+  public String getCurrentTaskId() {
+    return this.workflowInstance.getCurrentTaskId();
+  }
+
+  /**
+   * @return the running
+   */
+  public boolean isRunning() {
+    return running;
+  }
+
+  /**
+   * @param running
+   *          the running to set
+   */
+  public void setRunning(boolean running) {
+    this.running = running;
+  }
+
+  /**
+   * @return the paused
+   */
+  public boolean isPaused() {
+    return paused;
+  }
+
+  /**
+   * @param paused
+   *          the paused to set
+   */
+  public void setPaused(boolean paused) {
+    this.paused = paused;
+  }
+
+  /**
+   * @return the rClient
+   */
+  public XmlRpcResourceManagerClient getrClient() {
+    return rClient;
+  }
+
+  /**
+   * @param client
+   *          the rClient to set
+   */
+  public void setRClient(XmlRpcResourceManagerClient client) {
+    rClient = client;
+    if (rClient != null) {
+      LOG.log(Level.INFO, "Resource Manager Job Submission enabled to: ["
+          + rClient.getResMgrUrl() + "]");
+    }
+  }
+
+  protected void persistWorkflowInstance() {
+    try {
+      instanceRepository.updateWorkflowInstance(this.workflowInstance);
+    } catch (InstanceRepositoryException e) {
+      LOG.log(Level.WARNING, "Exception persisting workflow instance: ["
+          + this.workflowInstance.getId() + "]: Message: " + e.getMessage());
+    }
+  }
+
+  protected void executeTaskLocally(WorkflowTaskInstance instance,
+      Metadata met, WorkflowTaskConfiguration cfg, String taskName) {
+    try {
+      LOG.log(Level.INFO, "Executing task: [" + taskName + "] locally");
+      instance.run(met, cfg);
+    } catch (Exception e) {
+      e.printStackTrace();
+      LOG.log(Level.WARNING, "Exception executing task: [" + taskName
+          + "] locally: Message: " + e.getMessage());
+    }
+  }
+
+  protected boolean safeCheckJobComplete(String jobId) {
+    try {
+      return rClient.isJobComplete(jobId);
+    } catch (Exception e) {
+      LOG.log(Level.WARNING, "Exception checking completion status for job: ["
+          + jobId + "]: Messsage: " + e.getMessage());
+      return false;
+    }
+  }
+
+  protected boolean checkTaskRequiredMetadata(WorkflowTask task,
+      Metadata dynMetadata) {
+    if (task.getRequiredMetFields() == null
+        || (task.getRequiredMetFields() != null && task.getRequiredMetFields()
+            .size() == 0)) {
+      LOG.log(Level.INFO, "Task: [" + task.getTaskName()
+          + "] has no required metadata fields");
+      return true; /* no required metadata, so we're fine */
+    }
+
+    for (String reqField : (List<String>) (List<?>) task.getRequiredMetFields()) {
+      if (!dynMetadata.containsKey(reqField)) {
+        LOG.log(Level.SEVERE, "Checking metadata key: [" + reqField
+            + "] for task: [" + task.getTaskName()
+            + "]: failed: aborting workflow");
+        return false;
+      }
+    }
+
+    LOG.log(Level.INFO, "All required metadata fields present for task: ["
+        + task.getTaskName() + "]");
+
+    return true;
+  }
+
+  protected boolean satisfied(List<WorkflowCondition> conditionList,
+      String taskId) {
+    for (WorkflowCondition c : conditionList) {
+      WorkflowConditionInstance cInst = null;
+
+      // see if we've already cached this condition instance
+      if (CONDITION_CACHE.get(taskId) != null) {
+        HashMap<String, WorkflowConditionInstance> conditionMap = CONDITION_CACHE
+            .get(taskId);
+
+        /*
+         * okay we have some conditions cached for this task, see if we have the
+         * one we need
+         */
+        if (conditionMap.get(c.getConditionId()) != null) {
+          cInst = (WorkflowConditionInstance) conditionMap.get(c
+              .getConditionId());
+        }
+        /* if not, then go ahead and create it and cache it */
+        else {
+          cInst = GenericWorkflowObjectFactory
+              .getConditionObjectFromClassName(c
+                  .getConditionInstanceClassName());
+          conditionMap.put(c.getConditionId(), cInst);
+        }
+      }
+      /* no conditions cached yet, so set everything up */
+      else {
+        HashMap<String, WorkflowConditionInstance> conditionMap = new HashMap<String, WorkflowConditionInstance>();
+        cInst = GenericWorkflowObjectFactory.getConditionObjectFromClassName(c
+            .getConditionInstanceClassName());
+        conditionMap.put(c.getConditionId(), cInst);
+        CONDITION_CACHE.put(taskId, conditionMap);
+      }
+
+      // actually perform the evaluation
+      if (!cInst.evaluate(this.workflowInstance.getSharedContext(),
+          c.getCondConfig())) {
+        return false;
+      }
+    }
+
+    return true;
+  }
+
+  protected String getTaskNameById(String taskId) {
+    for (WorkflowTask task : (List<WorkflowTask>) (List<?>) this.workflowInstance
+        .getWorkflow().getTasks()) {
+      if (task.getTaskId().equals(taskId)) {
+        return task.getTaskName();
+      }
+    }
+
+    return null;
+  }
 
-    /**
-     * @return The {@link WorkflowInstance} that this Thread is processing.
-     */
-    public WorkflowInstance getWorkflowInstance();
-
-    /**
-     * <p>
-     * Stops once and for all the thread from processing the workflow. This
-     * method should not maintain the state of the workflow, it should
-     * gracefully shut down the WorkflowProcessorThread and any of its
-     * subsequent resources.
-     * </p>
-     * 
-     */
-    public void stop();
-
-    /**
-     * <p>
-     * Resumes execution of a {@link #pause}d {@link WorkflowInstace} by this
-     * WorkflowProcessorThread.
-     * </p>
-     * 
-     */
-    public void resume();
-
-    /**
-     * <p>
-     * Pauses exectuion of a {@link WorkflowInstace} being handled by this
-     * WorkflowProcessorThread.
-     * </p>
-     * 
-     */
-    public void pause();
+  protected String getHostname() {
+    try {
+      // Get hostname by textual representation of IP address
+      InetAddress addr = InetAddress.getLocalHost();
+      // Get the host name
+      String hostname = addr.getHostName();
+      return hostname;
+    } catch (UnknownHostException e) {
+    }
+    return null;
+  }
 
 }