javax.enterprise.inject.InjectionException Java Examples

The following examples show how to use javax.enterprise.inject.InjectionException. 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: CdiResourceProvider.java    From tomee with Apache License 2.0 5 votes vote down vote up
public CdiResourceProvider(final ClassLoader loader, final Class<?> clazz, final Collection<Injection> injectionCollection, final Context initialContext, final WebBeansContext owbCtx) {
    injections = injectionCollection;
    webbeansContext = owbCtx;
    classLoader = loader;
    context = (Context) Proxy.newProxyInstance(classLoader, new Class<?>[]{Context.class}, new InitialContextWrapper(initialContext));

    postConstructMethod = ResourceUtils.findPostConstructMethod(clazz);
    preDestroyMethod = ResourceUtils.findPreDestroyMethod(clazz);

    bm = webbeansContext == null ? null : webbeansContext.getBeanManagerImpl();
    this.clazz = clazz;
    if (bm != null && bm.isInUse()) {
        try {
            final Set<Bean<?>> beans = bm.getBeans(clazz);
            bean = bm.resolve(beans);
        } catch (final InjectionException ie) {
            final String msg = "Resource class " + clazz.getName() + " can not be instantiated";
            throw new WebApplicationException(Response.serverError().entity(msg).build());
        }

        if (bean != null && bm.isNormalScope(bean.getScope())) {
            // singleton is faster
            normalScopeCreator = new ProvidedInstanceBeanCreator(bm.getReference(bean, bean.getBeanClass(), bm.createCreationalContext(bean)));
        } else {
            normalScopeCreator = null;
            validateConstructorExists();
        }
    } else {
        bean = null;
        normalScopeCreator = null;
        validateConstructorExists();
    }

    findContexts(clazz);
}
 
Example #2
Source File: CdiResourceProvider.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public Object create() {
    try {
        toClean = bm.createCreationalContext(bean);
        return bm.getReference(bean, bean.getBeanClass(), toClean);
    } catch (final InjectionException ie) {
        final String msg = "Failed to instantiate: " + bean;
        Logger.getInstance(LogCategory.OPENEJB_CDI, this.getClass()).error(msg, ie);
        throw new WebApplicationException(Response.serverError().entity(msg).build());
    }
}
 
Example #3
Source File: ExceptionControlExtension.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies all injection points for every handler are valid.
 *
 * @param afterDeploymentValidation Lifecycle event
 * @param bm  BeanManager instance
 */
@SuppressWarnings("UnusedDeclaration")
public void verifyInjectionPoints(@Observes final AfterDeploymentValidation afterDeploymentValidation,
                                  final BeanManager bm)
{
    if (!isActivated)
    {
        return;
    }

    for (Map.Entry<Type, Collection<HandlerMethod<? extends Throwable>>> entry : allHandlers.entrySet())
    {
        for (HandlerMethod<? extends Throwable> handler : entry.getValue())
        {
            for (InjectionPoint ip : ((HandlerMethodImpl<? extends Throwable>) handler).getInjectionPoints(bm))
            {
                try
                {
                    bm.validate(ip);
                }
                catch (InjectionException e)
                {
                    afterDeploymentValidation.addDeploymentProblem(e);
                }
            }
        }
    }
}
 
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;
}