Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License"). You may not
   * use this file except in compliance with the License. A copy of the License is
   * located at
   * 
   * http://aws.amazon.com/apache2.0
   * 
  * or in the "license" file accompanying this file. This file 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 com.amazonaws.services.simpleworkflow.flow.test;
 
 import java.util.Map;
 import java.util.UUID;
 
 
 public class TestGenericWorkflowClient implements GenericWorkflowClient {
 
     private static class StartChildWorkflowReplyImpl implements StartChildWorkflowReply {
 
         private final Settable<Stringresult;
 
         private final String runId;
 
         private StartChildWorkflowReplyImpl(Settable<StringresultString runId) {
             this. = result;
             this. = runId;
         }
 
         @Override
         public String getRunId() {
             return ;
         }
 
         @Override
         public Promise<StringgetResult() {
             return ;
         }
     }
 
     private final class ChildWorkflowTryCatchFinally extends TryCatchFinally {
 
         private final StartChildWorkflowExecutionParameters parameters;
 
         private final WorkflowExecution childExecution;
 
         private final Settable<Stringresult;

        
Child workflow doesn't set result to ready state before completing all its tasks. So we need to set external result only in doFinally.
 
         private final Settable<StringexecuteResult = new Settable<String>();
 
         private final WorkflowDefinition childWorkflowDefinition;
 
         private final DecisionContext childContext;
 
         private boolean failed;
 
 
         private ChildWorkflowTryCatchFinally(StartChildWorkflowExecutionParameters parametersWorkflowExecution childExecution,
                 WorkflowDefinition childWorkflowDefinitionDecisionContext contextSettable<Stringresult) {
             this. = parameters;
             this. = childExecution;
            this. = childWorkflowDefinition;
            this. = context;
            this. = result;
        }
        @Override
        protected void doTry() throws Throwable {
        }
        @Override
        protected void doCatch(Throwable ethrows Throwable {
             = true;
            if (e instanceof WorkflowException) {
                WorkflowException we = (WorkflowExceptione;
                throw new ChildWorkflowFailedException(0, .getWorkflowType(), e.getMessage(),
                        we.getDetails());
            }
            else if (e instanceof CancellationException) {
                throw e;
            }
            // Unless there is problem in the framework or generic workflow implementation this shouldn't be executed
            Exception failure = new ChildWorkflowFailedException(0, .getWorkflowType(), e.getMessage(),
                    "null");
            failure.initCause(e);
            throw failure;
        }
        @Override
        protected void doFinally() throws Throwable {
            if (!) {
                if (.get() == null && .isReady()) {
                    .set(.get());
                }
            }
            else {
                .set(null);
            }
            .remove(this..getWorkflowId());
        }
        public void signalRecieved(final String signalNamefinal String details) {
            if (getState() != .) {
                throw new SignalExternalWorkflowException(0, "UNKNOWN_EXTERNAL_WORKFLOW_EXECUTION");
            }
            new Task(this) {
                @Override
                protected void doExecute() throws Throwable {
                    .signalRecieved(signalNamedetails);
                }
            };
        }
            return ;
        }
        public String getWorkflowState() throws WorkflowException {
            return .getWorkflowState();
        }
        public WorkflowExecution getWorkflowExecution() {
            return ;
        }
            return ;
        }
    }
            DecisionContextProvider decisionContextProvider) {
        this. = factoryFactory;
        this. = decisionContextProvider;
    }
        this(factoryFactorynew DecisionContextProviderImpl());
    }
    public TestGenericWorkflowClient() {
        this(nullnew DecisionContextProviderImpl());
    }
        return ;
    }
    public void setFactoryFactory(WorkflowDefinitionFactoryFactory factoryFactory) {
        this. = factoryFactory;
    }
        return ;
    }
    public void setDecisionContextProvider(DecisionContextProvider decisionContextProvider) {
        this. = decisionContextProvider;
    }
    @Override
        Settable<Stringresult = new Settable<String>();
        startChildWorkflow(parametersreplyresult);
        return reply;
    }
    private void startChildWorkflow(final StartChildWorkflowExecutionParameters parameters,
            final Settable<StartChildWorkflowReplyreplyfinal Settable<Stringresult) {
        String workflowId = parameters.getWorkflowId();
        WorkflowType workflowType = parameters.getWorkflowType();
        WorkflowExecution childExecution = new WorkflowExecution();
        final String runId = UUID.randomUUID().toString();
        //TODO: Validate parameters against registration options to find missing timeouts or other options
        try {
            DecisionContext parentDecisionContext = .getDecisionContext();
            if (workflowId == null) {
                workflowId = .getDecisionContext().getWorkflowClient().generateUniqueId();
            }
            childExecution.setWorkflowId(workflowId);
            childExecution.setRunId(runId);
            final GenericActivityClient activityClient = parentDecisionContext.getActivityClient();
            final WorkflowClock workflowClock = parentDecisionContext.getWorkflowClock();
            WorkflowDefinitionFactory factory;
            if ( == null) {
                throw new IllegalStateException("required property factoryFactory is null");
            }
            factory = .getWorkflowDefinitionFactory(workflowType);
            if (factory == null) {
                throw new StartChildWorkflowFailedException(0, childExecutionworkflowTypecause);
            }
            TestWorkflowContext workflowContext = new TestWorkflowContext();
            workflowContext.setWorkflowExecution(childExecution);
            workflowContext.setWorkflowType(parameters.getWorkflowType());
            workflowContext.setParentWorkflowExecution(parentDecisionContext.getWorkflowContext().getWorkflowExecution());
            workflowContext.setTagList(parameters.getTagList());
            workflowContext.setTaskList(parameters.getTaskList());
            DecisionContext context = new TestDecisionContext(activityClientTestGenericWorkflowClient.thisworkflowClock,
                    workflowContext);
            //this, parameters, childExecution, workflowClock, activityClient);
            final WorkflowDefinition childWorkflowDefinition = factory.getWorkflowDefinition(context);
            final ChildWorkflowTryCatchFinally tryCatch = new ChildWorkflowTryCatchFinally(parameterschildExecution,
                    childWorkflowDefinitioncontextresult);
            workflowContext.setRootTryCatch(tryCatch);
            ChildWorkflowTryCatchFinally currentRun = .get(workflowId);
            if (currentRun != null) {
                throw new StartChildWorkflowFailedException(0, childExecutionworkflowTypecause);
            }
            .put(workflowIdtryCatch);
            continueAsNewWorkflowExecution(tryCatchresult);
        }
        catch (StartChildWorkflowFailedException e) {
            throw e;
        }
        catch (Throwable e) {
            // This cause is chosen to represent internal error for sub-workflow creation
            StartChildWorkflowFailedException failure = new StartChildWorkflowFailedException(0, childExecutionworkflowType,
                    cause);
            failure.initCause(e);
            throw failure;
        }
        finally {
            reply.set(new StartChildWorkflowReplyImpl(resultrunId));
        }
    }
    private void continueAsNewWorkflowExecution(final ChildWorkflowTryCatchFinally tryCatchfinal Settable<Stringresult) {
        // It is always set to ready with null if no continuation is necessary
        final Promise<ContinueAsNewWorkflowExecutionParameterscontinueAsNew = tryCatch.getContinueAsNew();
        new Task(continueAsNew) {
            @Override
            protected void doExecute() throws Throwable {
                ContinueAsNewWorkflowExecutionParameters cp = continueAsNew.get();
                if (cp == null) {
                    return;
                }
                StartChildWorkflowExecutionParameters nextParameters = new StartChildWorkflowExecutionParameters();
                nextParameters.setInput(cp.getInput());
                WorkflowExecution previousWorkflowExecution = tryCatch.getWorkflowExecution();
                String workflowId = previousWorkflowExecution.getWorkflowId();
                nextParameters.setWorkflowId(workflowId);
                StartChildWorkflowExecutionParameters previousParameters = tryCatch.getParameters();
                nextParameters.setWorkflowType(previousParameters.getWorkflowType());
                long startToClose = cp.getExecutionStartToCloseTimeoutSeconds();
                if (startToClose == .) {
                    startToClose = previousParameters.getExecutionStartToCloseTimeoutSeconds();
                }
                nextParameters.setExecutionStartToCloseTimeoutSeconds(startToClose);
                long taskStartToClose = cp.getTaskStartToCloseTimeoutSeconds();
                if (taskStartToClose == .) {
                    taskStartToClose = previousParameters.getTaskStartToCloseTimeoutSeconds();
                }
                nextParameters.setTaskStartToCloseTimeoutSeconds(taskStartToClose);
                int taskPriority = cp.getTaskPriority();
                nextParameters.setTaskPriority(taskPriority);
                Settable<StartChildWorkflowReplyreply = new Settable<StartChildWorkflowReply>();
                startChildWorkflow(nextParametersreplyresult);
            }
        };
    }
    @Override
    public Promise<StringstartChildWorkflow(String workflowString versionString input) {
        WorkflowType workflowType = new WorkflowType().withName(workflow).withVersion(version);
        parameters.setWorkflowType(workflowType);
        parameters.setInput(input);
        Settable<Stringresult = new Settable<String>();
        startChildWorkflow(parametersreplyresult);
        return result;
    }
    @Override
    public Promise<StringstartChildWorkflow(final String workflowfinal String versionfinal Promise<Stringinput) {
        return new Functor<String>(input) {
            @Override
            protected Promise<StringdoExecute() throws Throwable {
                return startChildWorkflow(workflowversioninput.get());
            }
        };
    }
    @Override
    public Promise<VoidsignalWorkflowExecution(final SignalExternalWorkflowParameters signalParameters) {
        WorkflowExecution signaledExecution = new WorkflowExecution();
        signaledExecution.setWorkflowId(signalParameters.getWorkflowId());
        signaledExecution.setRunId(signalParameters.getRunId());
        final ChildWorkflowTryCatchFinally childTryCatch = .get(signalParameters.getWorkflowId());
        if (childTryCatch == null) {
            throw new SignalExternalWorkflowException(0, signaledExecution"UNKNOWN_EXTERNAL_WORKFLOW_EXECUTION");
        }
        String openExecutionRunId = childTryCatch.getWorkflowExecution().getRunId();
        if (signalParameters.getRunId() != null && !openExecutionRunId.equals(signalParameters.getRunId())) {
            throw new SignalExternalWorkflowException(0, signaledExecution"Unknown Execution (runId doesn't match)");
        }
        childTryCatch.signalRecieved(signalParameters.getSignalName(), signalParameters.getInput());
        return Promise.Void();
    }
    @Override
    public void requestCancelWorkflowExecution(WorkflowExecution execution) {
        String workflowId = execution.getWorkflowId();
        if (workflowId == null) {
            throw new IllegalArgumentException("null workflowId");
        }
        final ChildWorkflowTryCatchFinally childTryCatch = .get(workflowId);
        if (childTryCatch == null) {
            throw new UnknownResourceException("Unknown excution: " + execution.toString());
        }
        String openExecutionRunId = childTryCatch.getWorkflowExecution().getRunId();
        if (execution.getRunId() != null && !openExecutionRunId.equals(execution.getRunId())) {
            throw new UnknownResourceException("Unknown Execution (runId doesn't match)");
        }
        childTryCatch.cancel(new CancellationException());
    }
    public String getWorkflowState(WorkflowExecution executionthrows WorkflowException {
        String workflowId = execution.getWorkflowId();
        if (workflowId == null) {
            throw new IllegalArgumentException("null workflowId");
        }
        final ChildWorkflowTryCatchFinally childTryCatch = .get(workflowId);
        if (childTryCatch == null) {
            throw new UnknownResourceException(execution.toString());
        }
        String openExecutionRunId = childTryCatch.getWorkflowExecution().getRunId();
        if (execution.getRunId() != null && !openExecutionRunId.equals(execution.getRunId())) {
            throw new UnknownResourceException("Unknown Execution (runId doesn't match)");
        }
        return childTryCatch.getWorkflowState();
    }
    @Override
        DecisionContext decisionContext = .getDecisionContext();
        decisionContext.getWorkflowContext().setContinueAsNewOnCompletion(parameters);
    }
    @Override
    public String generateUniqueId() {
        return UUID.randomUUID().toString();
    }
New to GrepCode? Check out our FAQ X