All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.activiti.cdi.impl.context.BusinessProcessContext Maven / Gradle / Ivy

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

import java.lang.annotation.Annotation;

import javax.enterprise.context.spi.Context;
import javax.enterprise.context.spi.Contextual;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;

import org.activiti.cdi.BusinessProcess;
import org.activiti.cdi.annotation.BusinessProcessScoped;
import org.activiti.cdi.impl.util.ProgrammaticBeanLookup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Implementation of the BusinessProcessContext-scope.
 * 
 * @author Daniel Meyer
 */
@SuppressWarnings("unchecked")
public class BusinessProcessContext implements Context {

  final static Logger logger = LoggerFactory.getLogger(BusinessProcessContext.class);
  
  private final BeanManager beanManager;  
  
  public BusinessProcessContext(BeanManager beanManager) {
    this.beanManager = beanManager;
  }

  protected BusinessProcess getBusinessProcess() {
    return ProgrammaticBeanLookup.lookup(BusinessProcess.class, beanManager);
  }

  @Override
  public Class< ? extends Annotation> getScope() {
    return BusinessProcessScoped.class;
  }

  @Override
  public  T get(Contextual contextual) {
    Bean bean = (Bean) contextual;
    String variableName = bean.getName();

    BusinessProcess businessProcess = getBusinessProcess();
    Object variable = businessProcess.getVariable(variableName);
    if (variable != null) {
      if(logger.isDebugEnabled()) {
        if(businessProcess.isAssociated()) {        
          logger.debug("Getting instance of bean '{}' from Execution[{}]", variableName, businessProcess.getExecutionId());
        } else {
          logger.debug("Getting instance of bean '{}' from transient bean store", variableName);
        }
      }

      return (T) variable;
    } else {
      return null;
    }

  }

  @Override
  public  T get(Contextual contextual, CreationalContext arg1) {

    Bean bean = (Bean) contextual;
    String variableName = bean.getName();

    BusinessProcess businessProcess = getBusinessProcess();
    Object variable = businessProcess.getVariable(variableName);
    if (variable != null) {
      if(logger.isDebugEnabled()) {
        if(businessProcess.isAssociated()) {        
          logger.debug("Getting instance of bean '{}' from Execution[{}]", variableName, businessProcess.getExecutionId());
        } else {
          logger.debug("Getting instance of bean '{}' from transient bean store", variableName);
        }
      }

      return (T) variable;
    } else {
      
      if(logger.isDebugEnabled()) {
        if(businessProcess.isAssociated()) {        
          logger.debug("Creating instance of bean '{}' in business process context representing Execution[{}]", variableName, businessProcess.getExecutionId());
        } else {
          logger.debug("Creating instance of bean '{}' in transient bean store", variableName);
        }
      }

      T beanInstance = bean.create(arg1);
      businessProcess.setVariable(variableName, beanInstance);
      return beanInstance;
    }

  }

  @Override
  public boolean isActive() {
    // we assume the business process is always 'active'. If no task/execution is 
    // associated, temporary instances of @BusinessProcesScoped beans are cached in the 
    // conversation / request 
    return true;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy