Java Code Examples for org.apache.webbeans.container.BeanManagerImpl#getBeans()

The following examples show how to use org.apache.webbeans.container.BeanManagerImpl#getBeans() . 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: OpenEJBEnricher.java    From tomee with Apache License 2.0 5 votes vote down vote up
private static void doInject(final Object testInstance, final BeanContext context, final BeanManagerImpl bm) throws Exception {
    final Set<Bean<?>> beans = bm.getBeans(testInstance.getClass());
    final Bean<?> bean = bm.resolve(beans);
    final CreationalContext<?> cc = bm.createCreationalContext(bean);
    if (context != null) {
        context.set(CreationalContext.class, cc);
    }
    OWBInjector.inject(bm, testInstance, cc);
}
 
Example 2
Source File: WebappBeanManager.java    From tomee with Apache License 2.0 5 votes vote down vote up
private Set<Bean<?>> mergeBeans() {
    final Set<Bean<?>> allBeans = new CopyOnWriteArraySet<>(); // override parent one with a "webapp" bean list
    final BeanManagerImpl parentBm = getParentBm();
    if (parentBm != null) {
        for (final Bean<?> bean : parentBm.getBeans()) {
            if (filter.accept(bean)) {
                allBeans.add(bean);
            }
        }
    }
    allBeans.addAll(super.getBeans());
    return allBeans;
}
 
Example 3
Source File: CxfRsHttpListener.java    From tomee with Apache License 2.0 4 votes vote down vote up
private Comparator<?> findProviderComparator(final ServiceConfiguration serviceConfiguration, final WebBeansContext ctx) {
    final String comparatorKey = CXF_JAXRS_PREFIX + "provider-comparator";
    final String comparatorClass = serviceConfiguration.getProperties()
            .getProperty(comparatorKey, SystemInstance.get().getProperty(comparatorKey));

    Comparator<Object> comparator = null;
    if (comparatorClass == null) {
        return null; // try to rely on CXF behavior otherwise just reactivate DefaultProviderComparator.INSTANCE if it is an issue
    } else {
        final BeanManagerImpl bm = ctx == null ? null : ctx.getBeanManagerImpl();
        if (bm != null && bm.isInUse()) {
            try {
                final Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(comparatorClass);
                final Set<Bean<?>> beans = bm.getBeans(clazz);
                if (beans != null && !beans.isEmpty()) {
                    final Bean<?> bean = bm.resolve(beans);
                    final CreationalContextImpl<?> creationalContext = bm.createCreationalContext(bean);
                    comparator = Comparator.class.cast(bm.getReference(bean, clazz, creationalContext));
                    toRelease.add(creationalContext);
                }
            } catch (final Throwable th) {
                LOGGER.debug("Can't use CDI to load comparator " + comparatorClass);
            }
        }

        if (comparator == null) {
            comparator = Comparator.class.cast(ServiceInfos.resolve(serviceConfiguration.getAvailableServices(), comparatorClass));
        }
        if (comparator == null) {
            try {
                comparator = Comparator.class.cast(Thread.currentThread().getContextClassLoader().loadClass(comparatorClass).newInstance());
            } catch (final Exception e) {
                throw new IllegalArgumentException(e);
            }
        }

        for (final Type itf : comparator.getClass().getGenericInterfaces()) {
            if (!ParameterizedType.class.isInstance(itf)) {
                continue;
            }

            final ParameterizedType pt = ParameterizedType.class.cast(itf);
            if (Comparator.class == pt.getRawType() && pt.getActualTypeArguments().length > 0) {
                final Type t = pt.getActualTypeArguments()[0];
                if (Class.class.isInstance(t) && ProviderInfo.class == t) {
                    return comparator;
                }
                if (ParameterizedType.class.isInstance(t) && ProviderInfo.class == ParameterizedType.class.cast(t).getRawType()) {
                    return comparator;
                }
            }
        }

        return new ProviderComparatorWrapper(comparator);
    }
}
 
Example 4
Source File: HandlerResolverImpl.java    From tomee with Apache License 2.0 4 votes vote down vote up
private List<Handler> buildHandlers(final PortInfo portInfo, final HandlerChainData handlerChain) {
    if (!matchServiceName(portInfo, handlerChain.getServiceNamePattern()) ||
        !matchPortName(portInfo, handlerChain.getPortNamePattern()) ||
        !matchBinding(portInfo, handlerChain.getProtocolBindings())) {
        return Collections.emptyList();
    }

    final List<Handler> handlers = new ArrayList<>(handlerChain.getHandlers().size());
    for (final HandlerData handler : handlerChain.getHandlers()) {
        final WebBeansContext webBeansContext = AppFinder.findAppContextOrWeb(
                Thread.currentThread().getContextClassLoader(), AppFinder.WebBeansContextTransformer.INSTANCE);
        if (webBeansContext != null) { // cdi
            final BeanManagerImpl bm = webBeansContext.getBeanManagerImpl();
            if (bm.isInUse()) {
                try {
                    final Set<Bean<?>> beans = bm.getBeans(handler.getHandlerClass());
                    final Bean<?> bean = bm.resolve(beans);
                    if (bean != null) { // proxy so faster to do it
                        final boolean normalScoped = bm.isNormalScope(bean.getScope());
                        final CreationalContextImpl<?> creationalContext = bm.createCreationalContext(bean);
                        final Handler instance = Handler.class.cast(bm.getReference(bean, bean.getBeanClass(), creationalContext));

                        // hack for destroyHandlers()
                        handlers.add(instance);
                        handlerInstances.add(new InjectionProcessor<Handler>(instance, Collections.<Injection>emptySet(), null) {
                            @Override
                            public void preDestroy() {
                                if (!normalScoped) {
                                    creationalContext.release();
                                }
                            }
                        });
                        continue;
                    }
                } catch (final InjectionException ie) {
                    LOGGER.info(ie.getMessage(), ie);
                }
            }
        }

        try { // old way
            final Class<? extends Handler> handlerClass = handler.getHandlerClass().asSubclass(Handler.class);
            final InjectionProcessor<Handler> processor = new InjectionProcessor<>(handlerClass,
                injections,
                handler.getPostConstruct(),
                handler.getPreDestroy(),
                unwrap(context));
            processor.createInstance();
            processor.postConstruct();
            final Handler handlerInstance = processor.getInstance();

            handlers.add(handlerInstance);
            handlerInstances.add(processor);
        } catch (final Exception e) {
            throw new WebServiceException("Failed to instantiate handler", e);
        }
    }
    return handlers;
}