Java Code Examples for org.activiti.engine.impl.context.Context#setCommandContext()

The following examples show how to use org.activiti.engine.impl.context.Context#setCommandContext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommandContextInterceptor.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
public <T> T execute(CommandConfig config, Command<T> command) {
  CommandContext context = Context.getCommandContext();

  boolean contextReused = false;
  // We need to check the exception, because the transaction can be in a
  // rollback state, and some other command is being fired to compensate (eg. decrementing job retries)
  if (!config.isContextReusePossible() || context == null || context.getException() != null) {
    context = commandContextFactory.createCommandContext(command);
  } else {
    log.debug("Valid context found. Reusing it for the current command '{}'", command.getClass().getCanonicalName());
    contextReused = true;
    context.setReused(true);
  }

  try {
    
    // Push on stack
    Context.setCommandContext(context);
    Context.setProcessEngineConfiguration(processEngineConfiguration);
    if (processEngineConfiguration.getActiviti5CompatibilityHandler() != null) {
      Context.setActiviti5CompatibilityHandler(processEngineConfiguration.getActiviti5CompatibilityHandler());
    }

    return next.execute(config, command);

  } catch (Exception e) {

    context.exception(e);
    
  } finally {
    try {
      if (!contextReused) {
        context.close();
      }
    } finally {
      
      // Pop from stack
      Context.removeCommandContext();
      Context.removeProcessEngineConfiguration();
      Context.removeBpmnOverrideContext();
      Context.removeActiviti5CompatibilityHandler();
    }
  }

  return null;
}
 
Example 2
Source File: ParsedDeploymentTest.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
@Override
public void setUp() {
  Context.setCommandContext(processEngineConfiguration.getCommandContextFactory().createCommandContext(null));
}
 
Example 3
Source File: ActivitiWorkflowEngine.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
* {@inheritDoc}
*/
public WorkflowPath startWorkflow(String workflowDefinitionId, Map<QName, Serializable> parameters)
{
    try
    {
        String currentUserName = AuthenticationUtil.getFullyAuthenticatedUser();
        Authentication.setAuthenticatedUserId(currentUserName);
        
        String processDefId = createLocalId(workflowDefinitionId);
        
        // Set start task properties. This should be done before instance is started, since it's id will be used
        Map<String, Object> variables = propertyConverter.getStartVariables(processDefId, parameters);
        variables.put(WorkflowConstants.PROP_CANCELLED, Boolean.FALSE);
        
        // Add company home
        Object companyHome = nodeConverter.convertNode(getCompanyHome());
        variables.put(WorkflowConstants.PROP_COMPANY_HOME, companyHome);
         
        // Add the initiator
        NodeRef initiator = getPersonNodeRef(currentUserName);
        if (initiator != null)
        {
            variables.put(WorkflowConstants.PROP_INITIATOR, nodeConverter.convertNode(initiator));
            // Also add the initiator home reference, if one exists
            NodeRef initiatorHome = (NodeRef) nodeService.getProperty(initiator, ContentModel.PROP_HOMEFOLDER);
            if (initiatorHome != null)
            {
                variables.put(WorkflowConstants.PROP_INITIATOR_HOME, nodeConverter.convertNode(initiatorHome));
            }
        }
        
        // Start the process-instance
        CommandContext context = Context.getCommandContext();
        boolean isContextSuspended = false;
        if (context != null && context.getException() == null)
        {
            // MNT-11926: push null context to stack to avoid context reusage when new instance is not flushed
            Context.setCommandContext(null);
            isContextSuspended = true;
        }
        try
        {
            ProcessInstance instance = runtimeService.startProcessInstanceById(processDefId, variables);
            if (instance.isEnded())
            {
                return typeConverter.buildCompletedPath(instance.getId(), instance.getId());
            }
            else
            {
                WorkflowPath path = typeConverter.convert((Execution) instance);
                endStartTaskAutomatically(path, instance);
                return path;
            }
        }
        finally
        {
            if (isContextSuspended)
            {
                // pop null context out of stack
                Context.removeCommandContext();
            }
        }
    }
    catch (ActivitiException ae)
    {
        String msg = messageService.getMessage(ERR_START_WORKFLOW, workflowDefinitionId);
        if(logger.isDebugEnabled())
        {
        	logger.debug(msg, ae);
        }
        throw new WorkflowException(msg, ae);
    }
}
 
Example 4
Source File: CommandContextInterceptor.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
@Override
public <T> T execute(CommandConfig config, Command<T> command) {
    CommandContext context = Context.getCommandContext();

    boolean contextReused = false;
    // We need to check the exception, because the transaction can be in a rollback state,
    // and some other command is being fired to compensate (eg. decrementing job retries)
    if (!config.isContextReusePossible() || context == null || context.getException() != null) {
        context = commandContextFactory.createCommandContext(command);
    } else {
        LOGGER.debug("Valid context found. Reusing it for the current command '{}'", command.getClass().getCanonicalName());
        contextReused = true;
    }

    try {
        // Push on stack
        Context.setCommandContext(context);
        Context.setProcessEngineConfiguration(processEngineConfiguration);
        Flowable5CompatibilityContext.setFallbackFlowable5CompatibilityHandler(processEngineConfiguration.getFlowable5CompatibilityHandler());

        return next.execute(config, command);

    } catch (Exception e) {

        context.exception(e);

    } finally {
        try {
            if (!contextReused) {
                context.close();
            }
        } finally {
            // Pop from stack
            Context.removeCommandContext();
            Context.removeProcessEngineConfiguration();
            Context.removeBpmnOverrideContext();
            // don't remove the fallback because it's needed in Activiti 6 code like the Camel module
            // org.activiti.engine.impl.context.Context.removeFallbackcompatibilityHandler();
        }
    }

    return null;
}