Search in sources :

Example 31 with BeanManager

use of javax.enterprise.inject.spi.BeanManager in project meecrowave by apache.

the class JpaExtension method initBeans.

void initBeans(@Observes final AfterDeploymentValidation adv, final BeanManager bm) {
    if (entityManagerBeans.isEmpty()) {
        return;
    }
    // only not portable part is this config read, could be optional
    final ServletContext sc = ServletContext.class.cast(bm.getReference(bm.resolve(bm.getBeans(ServletContext.class)), ServletContext.class, bm.createCreationalContext(null)));
    final Meecrowave.Builder config = Meecrowave.Builder.class.cast(sc.getAttribute("meecrowave.configuration"));
    final Map<String, String> props = new HashMap<>();
    if (config != null) {
        ofNullable(config.getProperties()).ifPresent(p -> p.stringPropertyNames().stream().filter(k -> k.startsWith("jpa.property.")).forEach(k -> props.put(k.substring("jpa.property.".length()), p.getProperty(k))));
    }
    final Map<String, PersistenceUnitInfo> infoIndex = unitBuilders.stream().map(bean -> {
        final CreationalContext<?> cc = bm.createCreationalContext(null);
        try {
            final Bean<?> resolvedBean = bm.resolve(bm.getBeans(PersistenceUnitInfoBuilder.class, bean.getQualifiers().toArray(new Annotation[bean.getQualifiers().size()])));
            final PersistenceUnitInfoBuilder builder = PersistenceUnitInfoBuilder.class.cast(bm.getReference(resolvedBean, PersistenceUnitInfoBuilder.class, cc));
            if (builder.getManagedClasses().isEmpty()) {
                builder.setManagedClassNames(jpaClasses).setExcludeUnlistedClasses(true);
            }
            props.forEach(builder::addProperty);
            return builder.toInfo();
        } finally {
            cc.release();
        }
    }).collect(toMap(PersistenceUnitInfo::getPersistenceUnitName, identity()));
    entityManagerBeans.forEach((k, e) -> {
        PersistenceUnitInfo info = infoIndex.get(k.unitName);
        if (info == null) {
            info = tryCreateDefaultPersistenceUnit(k.unitName, bm, props);
        }
        if (info == null) {
            // not valid
            adv.addDeploymentProblem(new IllegalArgumentException("Didn't find any PersistenceUnitInfoBuilder for " + k));
        } else {
            e.init(info, bm);
        }
    });
}
Also used : AfterBeanDiscovery(javax.enterprise.inject.spi.AfterBeanDiscovery) MappedSuperclass(javax.persistence.MappedSuperclass) ProcessBean(javax.enterprise.inject.spi.ProcessBean) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CreationalContext(javax.enterprise.context.spi.CreationalContext) BeforeBeanDiscovery(javax.enterprise.inject.spi.BeforeBeanDiscovery) Collectors.toMap(java.util.stream.Collectors.toMap) Observes(javax.enterprise.event.Observes) AfterDeploymentValidation(javax.enterprise.inject.spi.AfterDeploymentValidation) Map(java.util.Map) SynchronizationType(javax.persistence.SynchronizationType) DataSource(javax.sql.DataSource) Jpa(org.apache.meecrowave.jpa.api.Jpa) Entity(javax.persistence.Entity) Extension(javax.enterprise.inject.spi.Extension) Optional.ofNullable(java.util.Optional.ofNullable) PersistenceUnitInfoBuilder(org.apache.meecrowave.jpa.api.PersistenceUnitInfoBuilder) PersistenceUnitInfo(javax.persistence.spi.PersistenceUnitInfo) Collection(java.util.Collection) ProcessAnnotatedType(javax.enterprise.inject.spi.ProcessAnnotatedType) WithAnnotations(javax.enterprise.inject.spi.WithAnnotations) Set(java.util.Set) List(java.util.List) Stream(java.util.stream.Stream) Unit(org.apache.meecrowave.jpa.api.Unit) Annotation(java.lang.annotation.Annotation) Function.identity(java.util.function.Function.identity) ServletContext(javax.servlet.ServletContext) Embeddable(javax.persistence.Embeddable) Meecrowave(org.apache.meecrowave.Meecrowave) Bean(javax.enterprise.inject.spi.Bean) BeanManager(javax.enterprise.inject.spi.BeanManager) PersistenceUnitInfoBuilder(org.apache.meecrowave.jpa.api.PersistenceUnitInfoBuilder) HashMap(java.util.HashMap) PersistenceUnitInfo(javax.persistence.spi.PersistenceUnitInfo) Annotation(java.lang.annotation.Annotation) ProcessBean(javax.enterprise.inject.spi.ProcessBean) Bean(javax.enterprise.inject.spi.Bean) CreationalContext(javax.enterprise.context.spi.CreationalContext) ServletContext(javax.servlet.ServletContext) Meecrowave(org.apache.meecrowave.Meecrowave)

Example 32 with BeanManager

use of javax.enterprise.inject.spi.BeanManager in project meecrowave by apache.

the class MeecrowaveRuleBase method doInject.

private static CreationalContext<Object> doInject(final Object instance) {
    final BeanManager bm = CDI.current().getBeanManager();
    final AnnotatedType<?> annotatedType = bm.createAnnotatedType(instance.getClass());
    final InjectionTarget injectionTarget = bm.createInjectionTarget(annotatedType);
    final CreationalContext<Object> creationalContext = bm.createCreationalContext(null);
    injectionTarget.inject(instance, creationalContext);
    return creationalContext;
}
Also used : InjectionTarget(javax.enterprise.inject.spi.InjectionTarget) BeanManager(javax.enterprise.inject.spi.BeanManager)

Example 33 with BeanManager

use of javax.enterprise.inject.spi.BeanManager in project cxf by apache.

the class ClassUnwrapperTest method getBeanReference.

@SuppressWarnings("unchecked")
private <T> T getBeanReference(Class<T> clazz) {
    final BeanManager beanManager = lifecycle.getBeanManager();
    final Set<Bean<?>> beans = beanManager.getBeans(clazz);
    final Bean<?> bean = beanManager.resolve(beans);
    return (T) beanManager.getReference(bean, clazz, beanManager.createCreationalContext(bean));
}
Also used : BeanManager(javax.enterprise.inject.spi.BeanManager) Bean(javax.enterprise.inject.spi.Bean)

Example 34 with BeanManager

use of javax.enterprise.inject.spi.BeanManager in project cxf by apache.

the class CXFCdiServlet method loadBus.

@Override
protected void loadBus(ServletConfig servletConfig) {
    Bus bus = null;
    final BeanManager beanManager = CDI.current().getBeanManager();
    if (beanManager != null) {
        final Set<Bean<?>> candidates = beanManager.getBeans(CdiBusBean.CXF);
        if (!candidates.isEmpty()) {
            final Bean<?> candidate = beanManager.resolve(candidates);
            bus = (Bus) beanManager.getReference(candidate, Bus.class, beanManager.createCreationalContext(candidate));
        }
    }
    if (bus != null) {
        setBus(bus);
    } else {
        busCreated = true;
        setBus(BusFactory.newInstance().createBus());
    }
}
Also used : Bus(org.apache.cxf.Bus) BeanManager(javax.enterprise.inject.spi.BeanManager) Bean(javax.enterprise.inject.spi.Bean)

Example 35 with BeanManager

use of javax.enterprise.inject.spi.BeanManager in project tomee by apache.

the class Assembler method deployMBean.

@SuppressWarnings("unchecked")
private void deployMBean(final WebBeansContext wc, final ClassLoader cl, final String mbeanClass, final Properties appMbeans, final String id) {
    if (LocalMBeanServer.isJMXActive()) {
        final Class<?> clazz;
        try {
            clazz = cl.loadClass(mbeanClass);
        } catch (final ClassNotFoundException e) {
            throw new OpenEJBRuntimeException(e);
        }
        // cdi can be off so init with null bean in this case
        final Bean<?> bean;
        final BeanManager bm;
        if (wc == null) {
            bm = null;
            bean = null;
        } else {
            bm = wc.getBeanManagerImpl();
            final Set<Bean<?>> beans = bm.getBeans(clazz);
            bean = bm.resolve(beans);
        }
        // create the MBean instance with cdi if possible or manually otherwise
        final Object instance;
        final CreationalContext creationalContext;
        if (bean == null) {
            try {
                instance = clazz.newInstance();
            } catch (final InstantiationException e) {
                logger.error("the mbean " + mbeanClass + " can't be registered because it can't be instantiated", e);
                return;
            } catch (final IllegalAccessException e) {
                logger.error("the mbean " + mbeanClass + " can't be registered because it can't be accessed", e);
                return;
            }
            creationalContext = null;
        } else {
            creationalContext = bm.createCreationalContext(bean);
            instance = bm.getReference(bean, clazz, creationalContext);
        }
        final MBeanServer server = LocalMBeanServer.get();
        try {
            final MBean annotation = clazz.getAnnotation(MBean.class);
            final ObjectName leaf = annotation == null || annotation.objectName().isEmpty() ? new ObjectNameBuilder("openejb.user.mbeans").set("application", id).set("group", clazz.getPackage().getName()).set("name", clazz.getSimpleName()).build() : new ObjectName(annotation.objectName());
            server.registerMBean(new DynamicMBeanWrapper(wc, instance), leaf);
            appMbeans.put(mbeanClass, leaf.getCanonicalName());
            if (creationalContext != null && (bean.getScope() == null || Dependent.class.equals(bean.getScope()))) {
                creationalContextForAppMbeans.put(leaf, creationalContext);
            }
            logger.info("Deployed MBean(" + leaf.getCanonicalName() + ")");
        } catch (final Exception e) {
            logger.error("the mbean " + mbeanClass + " can't be registered", e);
        }
    }
}
Also used : MBean(org.apache.openejb.api.jmx.MBean) Dependent(javax.enterprise.context.Dependent) InvalidObjectException(java.io.InvalidObjectException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ObjectStreamException(java.io.ObjectStreamException) ResourceAdapterInternalException(javax.resource.spi.ResourceAdapterInternalException) URISyntaxException(java.net.URISyntaxException) UndeployException(org.apache.openejb.UndeployException) DefinitionException(javax.enterprise.inject.spi.DefinitionException) ConstructionException(org.apache.xbean.recipe.ConstructionException) MBeanRegistrationException(javax.management.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ValidationException(javax.validation.ValidationException) MalformedObjectNameException(javax.management.MalformedObjectNameException) DuplicateDeploymentIdException(org.apache.openejb.DuplicateDeploymentIdException) TimeoutException(java.util.concurrent.TimeoutException) NamingException(javax.naming.NamingException) OpenEJBException(org.apache.openejb.OpenEJBException) DeploymentException(javax.enterprise.inject.spi.DeploymentException) NoSuchApplicationException(org.apache.openejb.NoSuchApplicationException) MalformedURLException(java.net.MalformedURLException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) ResourceBean(org.apache.webbeans.component.ResourceBean) MBean(org.apache.openejb.api.jmx.MBean) Bean(javax.enterprise.inject.spi.Bean) ObjectName(javax.management.ObjectName) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) CreationalContext(javax.enterprise.context.spi.CreationalContext) ObjectNameBuilder(org.apache.openejb.monitoring.ObjectNameBuilder) DynamicMBeanWrapper(org.apache.openejb.monitoring.DynamicMBeanWrapper) BeanManager(javax.enterprise.inject.spi.BeanManager) LocalMBeanServer(org.apache.openejb.monitoring.LocalMBeanServer) MBeanServer(javax.management.MBeanServer)

Aggregations

BeanManager (javax.enterprise.inject.spi.BeanManager)83 Bean (javax.enterprise.inject.spi.Bean)34 Test (org.junit.Test)16 NamingException (javax.naming.NamingException)7 Annotation (java.lang.annotation.Annotation)6 ArrayList (java.util.ArrayList)6 InjectionTarget (javax.enterprise.inject.spi.InjectionTarget)6 CdiContainer (org.apache.deltaspike.cdise.api.CdiContainer)6 CreationalContext (javax.enterprise.context.spi.CreationalContext)5 InitialContext (javax.naming.InitialContext)5 ValidatorFactory (javax.validation.ValidatorFactory)5 CarRepair (org.apache.deltaspike.cdise.tck.beans.CarRepair)5 HashMap (java.util.HashMap)4 Set (java.util.Set)4 TransactionManager (javax.transaction.TransactionManager)4 ContextControl (org.apache.deltaspike.cdise.api.ContextControl)4 Type (java.lang.reflect.Type)3 Map (java.util.Map)3 TransactionSynchronizationRegistry (javax.transaction.TransactionSynchronizationRegistry)3 Validator (javax.validation.Validator)3