<commit_before>package com.camunda.fox.platform.subsystem.impl.service.execution;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.interceptor.InvocationContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.jboss.weld.context.ejb.EjbLiteral;
import org.jboss.weld.context.ejb.EjbRequestContext;

import com.camunda.fox.platform.spi.ProcessArchiveCallback;

/**
 * <p>ProcessArchiveCallback allowing us to execute a job in the context of 
 * the owning ProcessArchive while activating the CDI RequestScope.</p>
 * 
 * <p>See: org.jboss.as.weld.ejb.EjbRequestScopeActivationInterceptor</p>
 * 
 * @author <NAME>
 *
 */
public class JobRequestContextInterceptor implements ProcessArchiveCallback<Void> {
  
  protected final CommandContext commandContext;
  protected final Command<?> executeJobCommand;

  public JobRequestContextInterceptor(CommandContext commandContext, Command<?> executeJobCommand) {
    this.commandContext = commandContext;
    this.executeJobCommand = executeJobCommand;
  }

  public Void execute() {
    try {
    
      // attempt looking up the bean manager
      BeanManager bm = InitialContext.doLookup("java:comp/BeanManager");
      proceedWithRequestContext(bm);

    } catch (NamingException e) {      
      // ignore. (not a CDI deployment)
      
      proceedWithoutRequestContext();
    }

    return null;
  }

  protected void proceedWithRequestContext(BeanManager beanManager) {
    final Bean< ? > bean = beanManager.resolve(beanManager.getBeans(EjbRequestContext.class, EjbLiteral.INSTANCE));
    final CreationalContext< ? > ctx = beanManager.createCreationalContext(bean);
    EjbRequestContext requestContext = (EjbRequestContext) beanManager.getReference(bean, EjbRequestContext.class, ctx);    
    final InvocationContext invocationContext = new JobInvocationContext();
    
    try {
      // activate the CDI request context
      requestContext.associate(invocationContext);
      requestContext.activate();

      // now actually execute the job
      executeJobCommand.execute(commandContext);
      
    } finally {
      // deactivate the CDI request context
      requestContext.deactivate();
      requestContext.dissociate(invocationContext);              <commit_msg>HEMERA-2789 make sure JobExecutorRequestContext only activates the RequestContext if it is not already active.<commit_after>package com.camunda.fox.platform.subsystem.impl.service.execution;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import javax.enterprise.context.ContextNotActiveException;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.interceptor.InvocationContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.jboss.weld.context.ejb.EjbLiteral;
import org.jboss.weld.context.ejb.EjbRequestContext;

import com.camunda.fox.platform.spi.ProcessArchiveCallback;

/**
 * <p>ProcessArchiveCallback allowing us to execute a job in the context of 
 * the owning ProcessArchive while activating the CDI RequestScope.</p>
 * 
 * <p>See: org.jboss.as.weld.ejb.EjbRequestScopeActivationInterceptor</p>
 * 
 * @author <NAME>
 *
 */
public class JobRequestContextInterceptor implements ProcessArchiveCallback<Void> {
  
  protected final CommandContext commandContext;
  protected final Command<?> executeJobCommand;

  public JobRequestContextInterceptor(CommandContext commandContext, Command<?> executeJobCommand) {
    this.commandContext = commandContext;
    this.executeJobCommand = executeJobCommand;
  }

  public Void execute() {
    try {
    
      // attempt looking up the bean manager
      BeanManager bm = InitialContext.doLookup("java:comp/BeanManager");
      proceedWithRequestContext(bm);

    } catch (NamingException e) {      
      // ignore. (not a CDI deployment)
      
      proceedWithoutRequestContext();
    }

    return null;
  }

  protected void proceedWithRequestContext(BeanManager beanManager) {
    final Bean< ? > bean = beanManager.resolve(beanManager.getBeans(EjbRequestContext.class, EjbLiteral.INSTANCE));
    final CreationalContext< ? > ctx = beanManager.createCreationalContext(bean);
    EjbRequestContext requestContext = (EjbRequestContext) beanManager.getReference(bean, EjbRequestContext.class, ctx);    
    final InvocationContext invocationContext = new JobInvocationContext();
    
    try {
      // if this call succeeds then the context is already active
      // HEMERA-2789
      beanManager.getContext(RequestScoped.class);
      executeJobCommand.execute(commandContext);
      return;
    } catch (ContextNotActiveException exception) {
    }
    
    try {
      // activate the CDI request context
      requestContext.associate(invocationContext);
      requestContext.activate();

      // now actually execute the job
      executeJobCommand.execute(commandContext);
      return;
      
    } finally {
      // deactivate the CDI request context
      requestContext.invalidate();
      requestContext.deactivate();
      requestContext.dissociate(invocationContext);              
{'fraction_non_alphanumeric': 0.08753971055418285, 'fraction_numerical': 0.0014119308153900459, 'mean_word_length': 4.151818181818181, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2660280', 'n_tokens_mistral': 1591, 'n_tokens_neox': 1530, 'n_words': 329}