Java Code Examples for javax.enterprise.inject.spi.Bean#create()

The following examples show how to use javax.enterprise.inject.spi.Bean#create() . 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: BusinessProcessContext.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T get(Contextual<T> contextual, CreationalContext<T> arg1) {

  Bean<T> bean = (Bean<T>) 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;
  }

}
 
Example 2
Source File: BusinessProcessContext.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T get(Contextual<T> contextual, CreationalContext<T> arg1) {

    Bean<T> bean = (Bean<T>) 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;
    }

}
 
Example 3
Source File: BusinessScopeContext.java    From drools-workshop with Apache License 2.0 5 votes vote down vote up
public <T> T get(Contextual<T> contextual, CreationalContext<T> creationalContext) {
    Bean bean = (Bean) contextual;
    // you can store the bean somewhere
    if (somewhere.containsKey(bean.getName())) {
        return (T) somewhere.get(bean.getName());
    } else {
        T t = (T) bean.create(creationalContext);
        somewhere.put(bean.getName(), t);
        return t;
    }
}
 
Example 4
Source File: BeanLocator.java    From AngularBeans with GNU Lesser General Public License v3.0 5 votes vote down vote up
public Object lookup(String beanName, String sessionID) {

		NGSessionScopeContext.setCurrentContext(sessionID);

		Set<Bean<?>> beans = beanManager.getBeans(beanName);

		Class beanClass = CommonUtils.beanNamesHolder.get(beanName);
		if (beans.isEmpty()) {
			beans = beanManager.getBeans(beanClass, new AnnotationLiteral<Any>() { //
			});
		}

		Bean bean = beanManager.resolve(beans);

		Class scopeAnnotationClass = bean.getScope();
		Context context;

		if (scopeAnnotationClass.equals(RequestScoped.class)) {
			context = beanManager.getContext(scopeAnnotationClass);
			if (context == null)
				return bean.create(beanManager.createCreationalContext(bean));

		} else {

			if (scopeAnnotationClass.equals(NGSessionScopeContext.class)) {
				context = NGSessionScopeContext.getINSTANCE();
			} else {
				context = beanManager.getContext(scopeAnnotationClass);
			}

		}
		CreationalContext creationalContext = beanManager.createCreationalContext(bean);
		Object reference = context.get(bean, creationalContext);

		// if(reference==null && scopeAnnotationClass.equals(RequestScoped.class)){
		// reference= bean.create(beanManager.createCreationalContext(bean));
		// }

		return reference;
	}
 
Example 5
Source File: JoynrIntegrationBean.java    From joynr with Apache License 2.0 5 votes vote down vote up
/**
 * A util method to find factories which provide some customized settings needed for
 * registration of joynr providers, i.e. implementations of the interface
 * {@link ProviderRegistrationSettingsFactory}.
 *
 * @return set of factories implementing the interface
 */
@SuppressWarnings({ "rawtypes", "unchecked", "serial" })
private Set<ProviderRegistrationSettingsFactory> getProviderRegistrationSettingsFactories() {
    Set<Bean<?>> providerSettingsFactoryBeans = beanManager.getBeans(ProviderRegistrationSettingsFactory.class,
                                                                     new AnnotationLiteral<Any>() {
                                                                     });
    Set<ProviderRegistrationSettingsFactory> providerSettingsFactories = new HashSet<>();
    for (Bean providerSettingsFactoryBean : providerSettingsFactoryBeans) {
        ProviderRegistrationSettingsFactory factory = (ProviderRegistrationSettingsFactory) providerSettingsFactoryBean.create(beanManager.createCreationalContext(providerSettingsFactoryBean));
        providerSettingsFactories.add(factory);
    }
    return providerSettingsFactories;
}
 
Example 6
Source File: BusinessProcessContext.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T get(Contextual<T> contextual, CreationalContext<T> arg1) {

  Bean<T> bean = (Bean<T>) contextual;
  String variableName = bean.getName();

  BusinessProcess businessProcess = getBusinessProcess();
  Object variable = businessProcess.getVariable(variableName);
  if (variable != null) {

    if (logger.isLoggable(Level.FINE)) {
      if(businessProcess.isAssociated()) {        
        logger.fine("Getting instance of bean '" + variableName + "' from Execution[" + businessProcess.getExecutionId() + "].");
      } else {
        logger.fine("Getting instance of bean '" + variableName + "' from transient bean store");
      }
    }

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

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

}
 
Example 7
Source File: DependentScopedTest.java    From tomee with Apache License 2.0 4 votes vote down vote up
@Test
public void test() throws Exception {
    created.clear();
    destroyed.clear();

    final Bean<ColorWheelLocal> colorWheelBean = getBean(ColorWheelLocal.class);

    final CreationalContext<ColorWheelLocal> creationalContext = beanManager.createCreationalContext(colorWheelBean);

    final ColorWheelLocal colorWheel = colorWheelBean.create(creationalContext);

    assertEquals(6, created.size());

    assertEquals(6, colorWheel.getColors().size());

    colorWheelBean.destroy(colorWheel, creationalContext);

    assertEquals(6, destroyed.size());

}