Search in sources :

Example 6 with Bean

use of javax.enterprise.inject.spi.Bean in project Activiti by Activiti.

the class BusinessProcessContext method get.

@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;
    }
}
Also used : Bean(javax.enterprise.inject.spi.Bean) BusinessProcess(org.activiti.cdi.BusinessProcess)

Example 7 with Bean

use of javax.enterprise.inject.spi.Bean in project aries by apache.

the class Phase_Publish method processRequirementDefinedServices.

private void processRequirementDefinedServices(BeanManagerImpl beanManagerImpl) {
    Collection<ServiceModel> serviceModels = _bc.getServiceModels();
    for (ServiceModel serviceModel : serviceModels) {
        try {
            Class<?> beanClass = _bc.loadClass(serviceModel.getBeanClass());
            Set<Bean<?>> beans = beanManagerImpl.getBeans(beanClass, AnyLiteral.INSTANCE);
            if (beans.isEmpty()) {
                _log.error("CDIe - MANIFEST service processing cannot find bean for class {}", serviceModel.getBeanClass());
                continue;
            }
            Bean<?> bean = beanManagerImpl.resolve(beans);
            CreationalContext<?> creationalContext = beanManagerImpl.createCreationalContext(bean);
            List<String> provides = serviceModel.getProvides();
            List<Class<?>> interfaces = new ArrayList<>();
            for (String provide : provides) {
                try {
                    interfaces.add(_bc.loadClass(provide));
                } catch (Exception e) {
                    _log.error("CDIe - Failure loading provided interface for service {}", provide);
                }
            }
            ServiceDeclaration serviceDeclaration = new ServiceDeclaration(ServiceLiteral.from(interfaces.toArray(new Class<?>[interfaces.size()]), serviceModel.getProperties()), bean, creationalContext);
            processServiceDeclaration(serviceDeclaration);
        } catch (ClassNotFoundException cnfe) {
            _log.error("CDIe - MANIFEST service processing cannot load class {}", serviceModel.getBeanClass(), cnfe);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ReferenceBean(org.apache.aries.cdi.container.internal.bean.ReferenceBean) ConfigurationBean(org.apache.aries.cdi.container.internal.bean.ConfigurationBean) Bean(javax.enterprise.inject.spi.Bean) ServiceModel(org.apache.aries.cdi.container.internal.model.ServiceModel)

Example 8 with Bean

use of javax.enterprise.inject.spi.Bean in project aries by apache.

the class OSGiBeanDescriptorTests method testReferences.

@SuppressWarnings("serial")
public void testReferences() throws Exception {
    Bundle tb1Bundle = installBundle("tb1.jar");
    Bundle tb2Bundle = installBundle("tb2.jar");
    CdiContainer cdiContainer = waitForCdiContainer(tb1Bundle.getBundleId());
    try {
        BeanManager beanManager = cdiContainer.getBeanManager();
        Set<Bean<?>> beans = beanManager.getBeans("beanimpl");
        Bean<?> bean = beanManager.resolve(beans);
        CreationalContext<?> ctx = beanManager.createCreationalContext(bean);
        BeanService<?> beanService = (BeanService<?>) beanManager.getReference(bean, new TypeLiteral<BeanService<?>>() {
        }.getType(), ctx);
        assertNotNull(beanService);
        assertEquals("POJO-IMPLBEAN-IMPL", beanService.doSomething());
    } finally {
        tb2Bundle.uninstall();
        tb1Bundle.uninstall();
    }
}
Also used : Bundle(org.osgi.framework.Bundle) BeanManager(javax.enterprise.inject.spi.BeanManager) BeanService(org.apache.aries.cdi.test.interfaces.BeanService) CdiContainer(org.osgi.service.cdi.CdiContainer) Bean(javax.enterprise.inject.spi.Bean)

Example 9 with Bean

use of javax.enterprise.inject.spi.Bean in project aries by apache.

the class ConfigurationTests method testNamedConfiguration.

@SuppressWarnings({ "unchecked", "serial" })
public void testNamedConfiguration() throws Exception {
    Bundle tb3Bundle = installBundle("tb3.jar");
    Configuration configurationA = null, configurationB = null;
    try {
        configurationA = configurationAdmin.getConfiguration("configA", "?");
        Dictionary<String, Object> properties = new Hashtable<>();
        properties.put("ports", new int[] { 12, 4567 });
        configurationA.update(properties);
        configurationB = configurationAdmin.getConfiguration("configB", "?");
        properties = new Hashtable<>();
        properties.put("color", "green");
        properties.put("ports", new int[] { 80 });
        configurationB.update(properties);
        Filter filter = bundleContext.createFilter("(&(objectClass=" + CdiContainer.class.getName() + ")(service.bundleid=" + tb3Bundle.getBundleId() + ")(" + CdiConstants.CDI_CONTAINER_STATE + "=CREATED))");
        ServiceTracker<CdiContainer, CdiContainer> st = new ServiceTracker<>(bundleContext, filter, null);
        st.open();
        CdiContainer container = st.waitForService(timeout);
        assertNotNull(container);
        int t = st.getTrackingCount();
        BeanManager beanManager = container.getBeanManager();
        Set<Bean<?>> beans = beanManager.getBeans("configB");
        assertNotNull(beans);
        Bean<? extends Object> bean = beanManager.resolve(beans);
        CreationalContext<?> ctx = beanManager.createCreationalContext(bean);
        Map<String, Object> config = (Map<String, Object>) beanManager.getReference(bean, new TypeLiteral<Map<String, Object>>() {
        }.getType(), ctx);
        assertNotNull(config);
        assertEquals("green", config.get("color"));
        assertArrayEquals(new int[] { 80 }, (int[]) config.get("ports"));
        configurationA.delete();
        while (t == st.getTrackingCount()) {
            Thread.sleep(10);
        }
        assertTrue(st.isEmpty());
        st.close();
        filter = bundleContext.createFilter("(&(objectClass=" + CdiContainer.class.getName() + ")(service.bundleid=" + tb3Bundle.getBundleId() + ")(" + CdiConstants.CDI_CONTAINER_STATE + "=" + CdiEvent.Type.WAITING_FOR_CONFIGURATIONS + "))");
        st = new ServiceTracker<>(bundleContext, filter, null);
        st.open();
        assertFalse(st.isEmpty());
    } finally {
        if (configurationB != null) {
            try {
                configurationB.delete();
            } catch (Exception e) {
            // ignore
            }
        }
        tb3Bundle.uninstall();
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) ServiceTracker(org.osgi.util.tracker.ServiceTracker) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) Bean(javax.enterprise.inject.spi.Bean) Filter(org.osgi.framework.Filter) BeanManager(javax.enterprise.inject.spi.BeanManager) Map(java.util.Map) CdiContainer(org.osgi.service.cdi.CdiContainer)

Example 10 with Bean

use of javax.enterprise.inject.spi.Bean in project aries by apache.

the class EventsTests method testEventsGetSent.

public void testEventsGetSent() throws Exception {
    BeanManager beanManager = cdiContainer.getBeanManager();
    assertNotNull(beanManager);
    @SuppressWarnings("serial") Set<Bean<?>> beans = beanManager.getBeans(Object.class, new AnnotationLiteral<CdiEventObserverQualifier>() {
    });
    Bean<?> bean = beanManager.resolve(beans);
    CreationalContext<?> ctx = beanManager.createCreationalContext(bean);
    Object bcb = beanManager.getReference(bean, Object.class, ctx);
    assertNotNull(bcb);
    @SuppressWarnings("unchecked") BeanService<List<CdiEvent>> bti = (BeanService<List<CdiEvent>>) bcb;
    List<CdiEvent> list = bti.get();
    assertNotNull(list);
    assertEquals(3, list.size());
}
Also used : Bean(javax.enterprise.inject.spi.Bean) List(java.util.List) CdiEvent(org.osgi.service.cdi.CdiEvent) BeanManager(javax.enterprise.inject.spi.BeanManager) CdiEventObserverQualifier(org.apache.aries.cdi.test.interfaces.CdiEventObserverQualifier) BeanService(org.apache.aries.cdi.test.interfaces.BeanService)

Aggregations

Bean (javax.enterprise.inject.spi.Bean)259 Test (org.junit.Test)119 CreationalContext (javax.enterprise.context.spi.CreationalContext)110 BeanManager (javax.enterprise.inject.spi.BeanManager)76 URL (java.net.URL)68 Path (org.uberfire.backend.vfs.Path)67 KieModuleService (org.kie.workbench.common.services.shared.project.KieModuleService)66 Package (org.guvnor.common.services.project.model.Package)43 HashSet (java.util.HashSet)31 Annotation (java.lang.annotation.Annotation)18 ArrayList (java.util.ArrayList)14 Module (org.guvnor.common.services.project.model.Module)13 AnnotationLiteral (javax.enterprise.util.AnnotationLiteral)11 Type (java.lang.reflect.Type)9 Set (java.util.Set)9 AfterDeploymentValidation (javax.enterprise.inject.spi.AfterDeploymentValidation)9 BeanManagerImpl (org.apache.webbeans.container.BeanManagerImpl)9 Any (javax.enterprise.inject.Any)8 AbstractBuiltInBean (org.jboss.weld.bean.builtin.AbstractBuiltInBean)8 Weld (org.jboss.weld.environment.se.Weld)8