Search in sources :

Example 71 with BeanManager

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

the class LazyRealm method instance.

private Realm instance() {
    if (delegate == null) {
        synchronized (this) {
            if (delegate == null) {
                final Object instance;
                ClassLoader cl = Thread.currentThread().getContextClassLoader();
                if (container != null && container.getLoader() != null && container.getLoader().getClassLoader() != null) {
                    cl = container.getLoader().getClassLoader();
                }
                final Class<?> clazz;
                try {
                    clazz = cl.loadClass(realmClass);
                } catch (final ClassNotFoundException e) {
                    throw new TomEERuntimeException(e);
                }
                if (!cdi) {
                    try {
                        final ObjectRecipe recipe = new ObjectRecipe(clazz);
                        recipe.allow(Option.CASE_INSENSITIVE_PROPERTIES);
                        recipe.allow(Option.IGNORE_MISSING_PROPERTIES);
                        recipe.allow(Option.FIELD_INJECTION);
                        recipe.allow(Option.PRIVATE_PROPERTIES);
                        if (properties != null) {
                            final Properties props = new PropertiesAdapter().unmarshal(properties.trim().replaceAll("\\p{Space}*(\\p{Alnum}*)=", "\n$1="));
                            recipe.setAllProperties(props);
                        }
                        instance = recipe.create();
                    } catch (final Exception e) {
                        throw new TomEERuntimeException(e);
                    }
                } else {
                    final WebBeansContext webBeansContext;
                    try {
                        webBeansContext = WebBeansContext.currentInstance();
                        if (webBeansContext == null) {
                            return null;
                        }
                    } catch (final IllegalStateException ise) {
                        // too early to have a cdi bean, skip these methods - mainly init() but @PostConstruct works then
                        return null;
                    }
                    final BeanManager bm = webBeansContext.getBeanManagerImpl();
                    final Set<Bean<?>> beans = bm.getBeans(clazz);
                    final Bean<?> bean = bm.resolve(beans);
                    if (bean == null) {
                        return null;
                    }
                    creationalContext = bm.createCreationalContext(null);
                    instance = bm.getReference(bean, clazz, creationalContext);
                }
                if (instance == null) {
                    throw new TomEERuntimeException("realm can't be retrieved from cdi");
                }
                if (instance instanceof Realm) {
                    delegate = (Realm) instance;
                    delegate.setContainer(container);
                    delegate.setCredentialHandler(credentialHandler);
                    if (Lifecycle.class.isInstance(delegate)) {
                        if (init) {
                            try {
                                final Lifecycle lifecycle = Lifecycle.class.cast(delegate);
                                lifecycle.init();
                                if (start) {
                                    lifecycle.start();
                                }
                            } catch (final LifecycleException e) {
                            // no-op
                            }
                        }
                    }
                } else {
                    delegate = new LowTypedRealm(instance);
                    delegate.setContainer(container);
                    delegate.setCredentialHandler(credentialHandler);
                }
                for (final PropertyChangeListener listener : support.getPropertyChangeListeners()) {
                    delegate.addPropertyChangeListener(listener);
                }
            }
        }
    }
    return delegate;
}
Also used : LifecycleException(org.apache.catalina.LifecycleException) PropertyChangeListener(java.beans.PropertyChangeListener) Lifecycle(org.apache.catalina.Lifecycle) Properties(java.util.Properties) TomEERuntimeException(org.apache.tomee.catalina.TomEERuntimeException) LifecycleException(org.apache.catalina.LifecycleException) IOException(java.io.IOException) TomEERuntimeException(org.apache.tomee.catalina.TomEERuntimeException) Bean(javax.enterprise.inject.spi.Bean) PropertiesAdapter(org.apache.openejb.config.sys.PropertiesAdapter) WebBeansContext(org.apache.webbeans.config.WebBeansContext) ObjectRecipe(org.apache.xbean.recipe.ObjectRecipe) BeanManager(javax.enterprise.inject.spi.BeanManager) Realm(org.apache.catalina.Realm)

Example 72 with BeanManager

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

the class NoConnectorTest method run.

@Test
public void run() {
    final Meecrowave.Builder config = new Meecrowave.Builder();
    config.setSkipHttp(true);
    try (final Meecrowave meecrowave = new Meecrowave(config.includePackages(NoConnectorTest.class.getName())).bake()) {
        final BeanManager beanManager = CDI.current().getBeanManager();
        assertEquals("yeah", SomeBean.class.cast(beanManager.getReference(beanManager.resolve(beanManager.getBeans(SomeBean.class)), SomeBean.class, beanManager.createCreationalContext(null))).get());
        IntStream.of(config.getHttpPort(), config.getHttpsPort()).forEach(port -> {
            try (final Socket socket = new Socket("localhost", port)) {
                fail("port " + port + " is opened");
            } catch (final IOException e) {
            // ok
            }
        });
        assertEquals(0, meecrowave.getTomcat().getService().findConnectors().length);
    }
}
Also used : IOException(java.io.IOException) BeanManager(javax.enterprise.inject.spi.BeanManager) Socket(java.net.Socket) Test(org.junit.Test)

Example 73 with BeanManager

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

the class MeecrowaveClientLifecycleListenerTest method autoClose.

@Test
public void autoClose() throws IOException, NoSuchFieldException, IllegalAccessException {
    try (final Meecrowave meecrowave = new Meecrowave(new Meecrowave.Builder().randomHttpPort().includePackages(MeecrowaveClientLifecycleListenerTest.class.getName())).bake()) {
        final JohnzonCdiExtension johnzonCdiExtension = CDI.current().select(JohnzonCdiExtension.class).get();
        final Field jsonbs = JohnzonCdiExtension.class.getDeclaredField("jsonbs");
        jsonbs.setAccessible(true);
        final BeanManager beanManager = CDI.current().getBeanManager();
        final JohnzonCdiExtension extensionInstance = JohnzonCdiExtension.class.cast(beanManager.getContext(ApplicationScoped.class).get(beanManager.resolve(beanManager.getBeans(JohnzonCdiExtension.class))));
        final Collection<?> o = Collection.class.cast(jsonbs.get(extensionInstance));
        {
            // ensure server is init whatever test suite we run in
            final Client client = ClientBuilder.newClient();
            get(meecrowave, client);
            client.close();
        }
        final int origin = o.size();
        final Client client = ClientBuilder.newClient();
        final JsonbJaxrsProvider<?> provider = new JsonbJaxrsProvider<>();
        client.register(provider);
        get(meecrowave, client);
        assertEquals(origin + 1, o.size());
        client.close();
        assertEquals(origin, o.size());
    }
}
Also used : Field(java.lang.reflect.Field) JsonbJaxrsProvider(org.apache.johnzon.jaxrs.jsonb.jaxrs.JsonbJaxrsProvider) ClientBuilder(javax.ws.rs.client.ClientBuilder) Client(javax.ws.rs.client.Client) BeanManager(javax.enterprise.inject.spi.BeanManager) Meecrowave(org.apache.meecrowave.Meecrowave) JohnzonCdiExtension(org.apache.johnzon.jsonb.cdi.JohnzonCdiExtension) Test(org.junit.Test)

Example 74 with BeanManager

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

the class EntityManagerBean method init.

void init(final PersistenceUnitInfo info, final BeanManager bm) {
    final PersistenceProvider provider;
    try {
        provider = PersistenceProvider.class.cast(Thread.currentThread().getContextClassLoader().loadClass(info.getPersistenceProviderClassName()).newInstance());
    } catch (final InstantiationException | IllegalAccessException | ClassNotFoundException e) {
        throw new IllegalArgumentException("Bad provider: " + info.getPersistenceProviderClassName());
    }
    final EntityManagerFactory factory = provider.createContainerEntityManagerFactory(info, new HashMap() {

        {
            put("javax.persistence.bean.manager", bm);
            if (ValidationMode.NONE != info.getValidationMode()) {
                ofNullable(findValidatorFactory(bm)).ifPresent(factory -> put("javax.persistence.validation.factory", factory));
            }
        }
    });
    instanceFactory = synchronization == SynchronizationType.SYNCHRONIZED ? factory::createEntityManager : () -> factory.createEntityManager(synchronization);
}
Also used : PassivationCapable(javax.enterprise.inject.spi.PassivationCapable) ValidationMode(javax.persistence.ValidationMode) Collections.emptySet(java.util.Collections.emptySet) Vetoed(javax.enterprise.inject.Vetoed) Optional.ofNullable(java.util.Optional.ofNullable) PersistenceUnitInfo(javax.persistence.spi.PersistenceUnitInfo) Set(java.util.Set) HashMap(java.util.HashMap) EntityManager(javax.persistence.EntityManager) PersistenceProvider(javax.persistence.spi.PersistenceProvider) Supplier(java.util.function.Supplier) Collections.singletonList(java.util.Collections.singletonList) CreationalContext(javax.enterprise.context.spi.CreationalContext) HashSet(java.util.HashSet) Type(java.lang.reflect.Type) EntityManagerFactory(javax.persistence.EntityManagerFactory) Arrays.asList(java.util.Arrays.asList) EntityManagerScoped(org.apache.meecrowave.jpa.api.EntityManagerScoped) SynchronizationType(javax.persistence.SynchronizationType) Annotation(java.lang.annotation.Annotation) Bean(javax.enterprise.inject.spi.Bean) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) BeanManager(javax.enterprise.inject.spi.BeanManager) HashMap(java.util.HashMap) PersistenceProvider(javax.persistence.spi.PersistenceProvider) EntityManagerFactory(javax.persistence.EntityManagerFactory)

Example 75 with BeanManager

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

the class Injector method inject.

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

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