Search in sources :

Example 6 with InjectionException

use of com.sun.enterprise.container.common.spi.util.InjectionException in project Payara by payara.

the class InjectionManagerImpl method injectInstance.

public void injectInstance(Object instance, boolean invokePostConstruct) throws InjectionException {
    ComponentInvocation invocation = invocationMgr.getCurrentInvocation();
    if (invocation == null) {
        throw new InjectionException(localStrings.getLocalString("injection-manager.null-invocation-context", "Null invocation context"));
    }
    JndiNameEnvironment componentEnvironment = compEnvManager.getJndiNameEnvironment(invocation.getComponentId());
    if (componentEnvironment == null) {
        throw new InjectionException(localStrings.getLocalString("injection-manager.no-descriptor-registered-for-invocation", "No descriptor registered for current invocation: {0}", invocation));
    }
    inject(instance.getClass(), instance, componentEnvironment, null, invokePostConstruct);
}
Also used : InjectionException(com.sun.enterprise.container.common.spi.util.InjectionException) JndiNameEnvironment(com.sun.enterprise.deployment.JndiNameEnvironment) ComponentInvocation(org.glassfish.api.invocation.ComponentInvocation)

Example 7 with InjectionException

use of com.sun.enterprise.container.common.spi.util.InjectionException in project Payara by payara.

the class J2EEInstanceListener method handleAfterEvent.

private void handleAfterEvent(InstanceEvent event, InstanceEvent.EventType eventType) {
    Wrapper wrapper = event.getWrapper();
    Context context = (Context) wrapper.getParent();
    if (!(context instanceof WebModule)) {
        return;
    }
    WebModule wm = (WebModule) context;
    Object instance;
    if (eventType == InstanceEvent.EventType.AFTER_FILTER_EVENT) {
        instance = event.getFilter();
    } else {
        instance = event.getServlet();
    }
    if (instance == null) {
        return;
    }
    // Emit monitoring probe event
    if (instance instanceof Servlet) {
        if (eventType == InstanceEvent.EventType.AFTER_INIT_EVENT) {
            wm.servletInitializedEvent(wrapper.getName());
        } else if (eventType == InstanceEvent.EventType.AFTER_DESTROY_EVENT) {
            wm.servletDestroyedEvent(wrapper.getName());
        }
    }
    // EE invocation context
    try {
        if (eventType == InstanceEvent.EventType.AFTER_DESTROY_EVENT && !DefaultServlet.class.equals(instance.getClass()) && !JspServlet.class.equals(instance.getClass())) {
            injectionMgr.destroyManagedObject(instance, false);
        }
    } catch (InjectionException ie) {
        String msg = _rb.getString(LogFacade.EXCEPTION_DURING_HANDLE_EVENT);
        msg = MessageFormat.format(msg, new Object[] { eventType, wm });
        _logger.log(Level.SEVERE, msg, ie);
    }
    ComponentInvocation inv = new WebComponentInvocation(wm, instance);
    try {
        // if postinvoke failed in this event the invocation stack would get corrupted by double-remove
        if (event.getException() == null || !(event.getException() instanceof PostInvokeHandlingException)) {
            im.postInvoke(inv);
        }
    } catch (Exception ex) {
        String msg = _rb.getString(LogFacade.EXCEPTION_DURING_HANDLE_EVENT);
        msg = MessageFormat.format(msg, new Object[] { eventType, wm });
        PostInvokeHandlingException rethrown = new PostInvokeHandlingException(msg, ex);
        if (event.getException() != null) {
            rethrown.addSuppressed(event.getException());
        }
        throw rethrown;
    } finally {
        if (eventType == InstanceEvent.EventType.AFTER_DESTROY_EVENT) {
            if (tm != null) {
                tm.componentDestroyed(instance, inv);
            }
        } else if (eventType == InstanceEvent.EventType.AFTER_FILTER_EVENT || eventType == InstanceEvent.EventType.AFTER_SERVICE_EVENT) {
            // Emit monitoring probe event
            if (eventType == InstanceEvent.EventType.AFTER_SERVICE_EVENT) {
                ServletResponse response = event.getResponse();
                int status = -1;
                if (response != null && response instanceof HttpServletResponse) {
                    status = ((HttpServletResponse) response).getStatus();
                }
                wm.afterServiceEvent(wrapper.getName(), status);
            }
            // BEGIN IASRI# 4646060
            if (im.getCurrentInvocation() == null) {
                // END IASRI# 4646060
                try {
                    // clear security context
                    Realm ra = context.getRealm();
                    if (ra != null && (ra instanceof RealmInitializer)) {
                        // cleanup not only securitycontext but also PolicyContext
                        ((RealmInitializer) ra).logout();
                    }
                } catch (Exception ex) {
                    String msg = _rb.getString(LogFacade.EXCEPTION_DURING_HANDLE_EVENT);
                    msg = MessageFormat.format(msg, new Object[] { eventType, wm });
                    _logger.log(Level.SEVERE, msg, ex);
                }
                if (tm != null) {
                    try {
                        if (tm.getTransaction() != null) {
                            tm.rollback();
                        }
                        tm.cleanTxnTimeout();
                    } catch (Exception ex) {
                    }
                }
            }
            if (tm != null) {
                tm.componentDestroyed(instance, inv);
            }
        }
    }
}
Also used : AppServSecurityContext(com.sun.enterprise.security.integration.AppServSecurityContext) ServerContext(org.glassfish.internal.api.ServerContext) ServletRequestWrapper(javax.servlet.ServletRequestWrapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) ComponentInvocation(org.glassfish.api.invocation.ComponentInvocation) WebComponentInvocation(com.sun.enterprise.web.WebComponentInvocation) RealmInitializer(com.sun.enterprise.security.integration.RealmInitializer) HttpServletResponse(javax.servlet.http.HttpServletResponse) WebModule(com.sun.enterprise.web.WebModule) String(java.lang.String) InjectionException(com.sun.enterprise.container.common.spi.util.InjectionException) InjectionException(com.sun.enterprise.container.common.spi.util.InjectionException) WebComponentInvocation(com.sun.enterprise.web.WebComponentInvocation) JspServlet(org.apache.jasper.servlet.JspServlet) Servlet(javax.servlet.Servlet) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) DefaultServlet(org.apache.catalina.servlets.DefaultServlet)

Example 8 with InjectionException

use of com.sun.enterprise.container.common.spi.util.InjectionException in project Payara by payara.

the class GlassFishInjectionProvider method getComponentEnvironment.

// --------------------------------------------------------- Private Methods
/**
 * <p>
 * This is based off of code in <code>InjectionManagerImpl</code>.
 * </p>
 *
 * @return <code>JndiNameEnvironment</code>
 * @throws InjectionException
 *             if we're unable to obtain the <code>JndiNameEnvironment</code>
 */
private JndiNameEnvironment getComponentEnvironment() throws InjectionException {
    ComponentInvocation invocation = invocationManager.getCurrentInvocation();
    if (invocation == null) {
        throw new InjectionException("null invocation context");
    }
    if (invocation.getInvocationType() != SERVLET_INVOCATION) {
        throw new InjectionException("Wrong invocation type");
    }
    JndiNameEnvironment componentEnvironment = (JndiNameEnvironment) invocation.getJndiEnvironment();
    if (componentEnvironment == null) {
        throw new InjectionException("No descriptor registered for " + " current invocation : " + invocation);
    }
    return componentEnvironment;
}
Also used : InjectionException(com.sun.enterprise.container.common.spi.util.InjectionException) JndiNameEnvironment(com.sun.enterprise.deployment.JndiNameEnvironment) ComponentInvocation(org.glassfish.api.invocation.ComponentInvocation)

Example 9 with InjectionException

use of com.sun.enterprise.container.common.spi.util.InjectionException in project Payara by payara.

the class GlassFishInjectionProvider method getPostConstructMethod.

/**
 * <p>
 * This is based off of code in <code>InjectionManagerImpl</code>.
 * </p>
 *
 * @param injectionInfo
 *            InjectionInfo
 * @param resourceClass
 *            target class
 * @return a Method marked with the @PostConstruct annotation
 * @throws InjectionException
 *             if an error occurs
 */
private Method getPostConstructMethod(InjectionInfo injectionInfo, Class<? extends Object> resourceClass) throws InjectionException {
    Method postConstructMethod = injectionInfo.getPostConstructMethod();
    if (postConstructMethod == null) {
        String postConstructMethodName = injectionInfo.getPostConstructMethodName();
        // This does not include super-classses.
        for (Method declaredMethod : resourceClass.getDeclaredMethods()) {
            // methods with no arguments.
            if (declaredMethod.getName().equals(postConstructMethodName) && declaredMethod.getParameterTypes().length == 0) {
                postConstructMethod = declaredMethod;
                injectionInfo.setPostConstructMethod(postConstructMethod);
                break;
            }
        }
    }
    if (postConstructMethod == null) {
        throw new InjectionException("InjectionManager exception. PostConstruct method " + injectionInfo.getPostConstructMethodName() + " could not be found in class " + injectionInfo.getClassName());
    }
    return postConstructMethod;
}
Also used : InjectionException(com.sun.enterprise.container.common.spi.util.InjectionException) Method(java.lang.reflect.Method)

Example 10 with InjectionException

use of com.sun.enterprise.container.common.spi.util.InjectionException in project Payara by payara.

the class InjectionServicesImpl method aroundInject.

@Override
public <T> void aroundInject(InjectionContext<T> injectionContext) {
    try {
        ServiceLocator serviceLocator = Globals.getDefaultHabitat();
        ComponentEnvManager compEnvManager = serviceLocator.getService(ComponentEnvManager.class);
        EjbContainerServices containerServices = serviceLocator.getService(EjbContainerServices.class);
        JndiNameEnvironment componentEnv = compEnvManager.getCurrentJndiNameEnvironment();
        if (componentEnv == null) {
            InvocationManager invMgr = serviceLocator.getService(InvocationManager.class);
            if (invMgr.getCurrentInvocation() != null) {
                componentEnv = (JndiNameEnvironment) invMgr.<ComponentInvocation>getCurrentInvocation().getJNDIEnvironment();
            }
        }
        ManagedBeanDescriptor mbDesc = null;
        JndiNameEnvironment injectionEnv = (JndiNameEnvironment) bundleContext;
        AnnotatedType annotatedType = injectionContext.getAnnotatedType();
        Class targetClass = annotatedType.getJavaClass();
        String targetClassName = targetClass.getName();
        Object target = injectionContext.getTarget();
        if (isInterceptor(targetClass) && (componentEnv != null && !componentEnv.equals(injectionEnv))) {
            // Resources injected into interceptors must come from the environment in which the interceptor is
            // intercepting, not the environment in which the interceptor resides (for everything else!)
            // Must use the injectionEnv to get the injection info to determine where in jndi to look for the objects to inject.
            // must use the current jndi component env to lookup the objects to inject
            injectionManager.inject(targetClass, target, injectionEnv, null, false);
        } else {
            if (componentEnv == null) {
                // throw new IllegalStateException("No valid EE environment for injection of " + targetClassName);
                System.err.println("No valid EE environment for injection of " + targetClassName);
                injectionContext.proceed();
                return;
            }
            if (componentEnv instanceof EjbDescriptor) {
                EjbDescriptor ejbDesc = (EjbDescriptor) componentEnv;
                if (containerServices.isEjbManagedObject(ejbDesc, targetClass)) {
                    injectionEnv = componentEnv;
                } else {
                    if (bundleContext instanceof EjbBundleDescriptor) {
                        // Check if it's a @ManagedBean class within an ejb-jar.  In that case,
                        // special handling is needed to locate the EE env dependencies
                        mbDesc = bundleContext.getManagedBeanByBeanClass(targetClassName);
                    }
                }
            }
            if (mbDesc != null) {
                injectionManager.injectInstance(target, mbDesc.getGlobalJndiName(), false);
            } else {
                if (injectionEnv instanceof EjbBundleDescriptor) {
                    // set the environment of the ejb bundle.
                    if (target == null) {
                        injectionManager.injectClass(targetClass, compEnvManager.getComponentEnvId(injectionEnv), false);
                    } else {
                        injectionManager.injectInstance(target, compEnvManager.getComponentEnvId(injectionEnv), false);
                    }
                } else {
                    if (target == null) {
                        injectionManager.injectClass(targetClass, injectionEnv, false);
                    } else {
                        injectionManager.injectInstance(target, injectionEnv, false);
                    }
                }
            }
        }
        injectionContext.proceed();
    } catch (InjectionException ie) {
        throw new IllegalStateException(ie.getMessage(), ie);
    }
}
Also used : InvocationManager(org.glassfish.api.invocation.InvocationManager) ComponentEnvManager(com.sun.enterprise.container.common.spi.util.ComponentEnvManager) EjbContainerServices(org.glassfish.ejb.api.EjbContainerServices) ServiceLocator(org.glassfish.hk2.api.ServiceLocator) InjectionException(com.sun.enterprise.container.common.spi.util.InjectionException)

Aggregations

InjectionException (com.sun.enterprise.container.common.spi.util.InjectionException)16 InjectionManager (com.sun.enterprise.container.common.spi.util.InjectionManager)4 Method (java.lang.reflect.Method)4 ComponentInvocation (org.glassfish.api.invocation.ComponentInvocation)4 JndiNameEnvironment (com.sun.enterprise.deployment.JndiNameEnvironment)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 NamingException (javax.naming.NamingException)3 Handler (javax.xml.ws.handler.Handler)3 JCDIService (com.sun.enterprise.container.common.spi.JCDIService)2 ManagedBeanManager (com.sun.enterprise.container.common.spi.ManagedBeanManager)2 ComponentEnvManager (com.sun.enterprise.container.common.spi.util.ComponentEnvManager)2 InjectionTarget (com.sun.enterprise.deployment.InjectionTarget)2 ServletAdapter (com.sun.xml.ws.transport.http.servlet.ServletAdapter)2 ServletAdapterList (com.sun.xml.ws.transport.http.servlet.ServletAdapterList)2 ManagedBean (javax.annotation.ManagedBean)2 ServiceLocator (org.glassfish.hk2.api.ServiceLocator)2 InjectionCapable (com.sun.enterprise.deployment.InjectionCapable)1 ResourceReferenceDescriptor (com.sun.enterprise.deployment.ResourceReferenceDescriptor)1 WebBundleDescriptor (com.sun.enterprise.deployment.WebBundleDescriptor)1 AppServSecurityContext (com.sun.enterprise.security.integration.AppServSecurityContext)1