Search in sources :

Example 11 with InjectionException

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

the class NonModuleInjectionServices method aroundInject.

@Override
public <T> void aroundInject(InjectionContext<T> injectionContext) {
    try {
        ServiceLocator serviceLocator = Globals.getDefaultHabitat();
        ComponentEnvManager compEnvManager = serviceLocator.getService(ComponentEnvManager.class);
        JndiNameEnvironment componentEnv = compEnvManager.getCurrentJndiNameEnvironment();
        Object target = injectionContext.getTarget();
        String targetClass = target.getClass().getName();
        if (componentEnv == null) {
            // throw new IllegalStateException("No valid EE environment for injection of " + targetClass);
            System.err.println("No valid EE environment for injection of " + targetClass);
            injectionContext.proceed();
            return;
        }
        injectionManager.injectInstance(target, componentEnv, false);
        injectionContext.proceed();
    } catch (InjectionException ie) {
        throw new IllegalStateException(ie.getMessage(), ie);
    }
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) InjectionException(com.sun.enterprise.container.common.spi.util.InjectionException) ComponentEnvManager(com.sun.enterprise.container.common.spi.util.ComponentEnvManager)

Example 12 with InjectionException

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

the class InjectionManagerImpl method _inject.

/**
 * Internal injection operation. componentId is only specified if componentId-specific lookup operation should be used.
 */
private void _inject(final Class clazz, final Object instance, String componentId, List<InjectionCapable> injectableResources) throws InjectionException {
    for (InjectionCapable next : injectableResources) {
        try {
            String lookupName = next.getComponentEnvName();
            if (!lookupName.startsWith("java:")) {
                lookupName = "java:comp/env/" + lookupName;
            }
            Object injectedValue = null;
            // do a loop here
            for (InjectionTarget target : next.getInjectionTargets()) {
                // we can just jump to the next target
                if (!clazz.getName().equals(target.getClassName()))
                    continue;
                // otherwise we might get called for the wrong module in the EAR file
                if (injectedValue == null) {
                    injectedValue = (componentId != null) ? glassfishNamingManager.lookup(componentId, lookupName) : glassfishNamingManager.getInitialContext().lookup(lookupName);
                }
                if (target.isFieldInjectable()) {
                    final Field f = getField(target, clazz);
                    if (Modifier.isStatic(f.getModifiers()) && (instance != null)) {
                        throw new InjectionException(localStrings.getLocalString("injection-manager.illegal-use-of-static-field", "Illegal use of static field on class that only supports instance-based injection: {0}", f));
                    }
                    if ((instance == null) && !Modifier.isStatic(f.getModifiers())) {
                        throw new InjectionException(localStrings.getLocalString("injection-manager.appclient-injected-field-must-be-static", "Injected field: {0} on Application Client class: {1} must be declared static", f, clazz));
                    }
                    if (_logger.isLoggable(Level.FINE)) {
                        _logger.fine(localStrings.getLocalString("injection-manager.injecting-dependency-field", "Injecting dependency with logical name: {0} into field: {1} on class: {2}", next.getComponentEnvName(), f, clazz));
                    }
                    final Object value = injectedValue;
                    // allow for private/protected field access.
                    if (System.getSecurityManager() != null) {
                        java.security.AccessController.doPrivileged(new java.security.PrivilegedExceptionAction() {

                            public java.lang.Object run() throws Exception {
                                f.set(instance, value);
                                return null;
                            }
                        });
                    } else {
                        f.set(instance, value);
                    }
                } else if (target.isMethodInjectable()) {
                    final Method m = getMethod(next, target, clazz);
                    if (Modifier.isStatic(m.getModifiers()) && (instance != null)) {
                        throw new InjectionException(localStrings.getLocalString("injection-manager.illegal-use-of-static-method", "Illegal use of static method on class that only supports instance-based injection: {0}", m));
                    }
                    if ((instance == null) && !Modifier.isStatic(m.getModifiers())) {
                        throw new InjectionException(localStrings.getLocalString("injection-manager.appclient-injected-method-must-be-static", "Injected method: {0} on Application Client class: {1} must be declared static", m, clazz));
                    }
                    if (_logger.isLoggable(Level.FINE)) {
                        _logger.fine(localStrings.getLocalString("injection-manager.injecting-dependency-method", "Injecting dependency with logical name: {0} into method: {1} on class: {2}", next.getComponentEnvName(), m, clazz));
                    }
                    final Object value = injectedValue;
                    if (System.getSecurityManager() != null) {
                        // Wrap actual value insertion in doPrivileged to
                        // allow for private/protected field access.
                        java.security.AccessController.doPrivileged(new java.security.PrivilegedExceptionAction() {

                            public java.lang.Object run() throws Exception {
                                m.invoke(instance, new Object[] { value });
                                return null;
                            }
                        });
                    } else {
                        m.invoke(instance, new Object[] { value });
                    }
                }
            }
        } catch (Throwable t) {
            Throwable cause = (t instanceof InvocationTargetException) ? ((InvocationTargetException) t).getCause() : t;
            String msg = localStrings.getLocalString("injection-manager.exception-to-inject", "Exception attempting to inject {0} into {1}: {2}", next, clazz, cause.getMessage());
            _logger.log(Level.FINE, msg, t);
            throw new InjectionException(msg, cause);
        }
    }
}
Also used : Method(java.lang.reflect.Method) NamingException(javax.naming.NamingException) InjectionException(com.sun.enterprise.container.common.spi.util.InjectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InjectionException(com.sun.enterprise.container.common.spi.util.InjectionException) Field(java.lang.reflect.Field) InjectionCapable(com.sun.enterprise.deployment.InjectionCapable) InjectionTarget(com.sun.enterprise.deployment.InjectionTarget)

Example 13 with InjectionException

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

the class InjectionManagerImpl method getPostConstructMethod.

private Method getPostConstructMethod(InjectionInfo injInfo, Class resourceClass) throws InjectionException {
    Method m = injInfo.getPostConstructMethod();
    if (m == null) {
        String postConstructMethodName = injInfo.getPostConstructMethodName();
        // This does not include super-classes.
        for (Method next : resourceClass.getDeclaredMethods()) {
            // methods with no arguments.
            if (next.getName().equals(postConstructMethodName) && (next.getParameterTypes().length == 0)) {
                m = next;
                injInfo.setPostConstructMethod(m);
                break;
            }
        }
    }
    if (m == null) {
        throw new InjectionException(localStrings.getLocalString("injection-manager.postconstruct-not-found", "InjectionManager exception. PostConstruct method: {0} not found in class: {1}", injInfo.getPostConstructMethodName(), injInfo.getClassName()));
    }
    return m;
}
Also used : InjectionException(com.sun.enterprise.container.common.spi.util.InjectionException) Method(java.lang.reflect.Method)

Example 14 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, String componentId, 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(componentId);
    if (componentEnvironment == null) {
        throw new InjectionException(localStrings.getLocalString("injection-manager.no-descriptor-registered-for-component", "No descriptor registered for componentId: {0}", componentId));
    }
    inject(instance.getClass(), instance, componentEnvironment, componentId, 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 15 with InjectionException

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

the class InjectionManagerImpl method getPreDestroyMethod.

private Method getPreDestroyMethod(InjectionInfo injInfo, Class resourceClass) throws InjectionException {
    Method m = injInfo.getPreDestroyMethod();
    if (m == null) {
        String preDestroyMethodName = injInfo.getPreDestroyMethodName();
        // This does not include super-classses.
        for (Method next : resourceClass.getDeclaredMethods()) {
            // methods with no arguments.
            if (next.getName().equals(preDestroyMethodName) && (next.getParameterTypes().length == 0)) {
                m = next;
                injInfo.setPreDestroyMethod(m);
                break;
            }
        }
    }
    if (m == null) {
        throw new InjectionException(localStrings.getLocalString("injection-manager.predestroy-not-found", "InjectionManager exception. PreDestroy method: {0} not found in class: {1}", injInfo.getPreDestroyMethodName(), injInfo.getClassName()));
    }
    return m;
}
Also used : InjectionException(com.sun.enterprise.container.common.spi.util.InjectionException) Method(java.lang.reflect.Method)

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