Search in sources :

Example 1 with JCDIService

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

the class WebModuleListener method configureJsp.

// ------------------------------------------------------- Private Methods
/**
 * Configure all JSP related aspects of the web module, including
 * any relevant TLDs as well as the jsp config settings of the
 * JspServlet (using the values from sun-web.xml's jsp-config).
 */
private void configureJsp(WebModule webModule) {
    ServletContext servletContext = webModule.getServletContext();
    servletContext.setAttribute("org.glassfish.jsp.isStandaloneWebapp", Boolean.valueOf(webModule.isStandalone()));
    // Find tld URI and set it to ServletContext attribute
    List<URI> appLibUris = webModule.getDeployAppLibs();
    Map<URI, List<String>> appLibTldMap = new HashMap<URI, List<String>>();
    if (appLibUris != null && appLibUris.size() > 0) {
        Pattern pattern = Pattern.compile("META-INF/.*\\.tld");
        for (URI uri : appLibUris) {
            List<String> entries = JarURIPattern.getJarEntries(uri, pattern);
            if (entries != null && entries.size() > 0) {
                appLibTldMap.put(uri, entries);
            }
        }
    }
    Collection<TldProvider> tldProviders = webContainer.getTldProviders();
    Map<URI, List<String>> tldMap = new HashMap<URI, List<String>>();
    for (TldProvider tldProvider : tldProviders) {
        // Skip any JSF related TLDs for non-JSF apps
        if ("jsfTld".equals(tldProvider.getName()) && !webModule.isJsfApplication()) {
            continue;
        }
        Map<URI, List<String>> tmap = tldProvider.getTldMap();
        if (tmap != null) {
            tldMap.putAll(tmap);
        }
    }
    tldMap.putAll(appLibTldMap);
    servletContext.setAttribute("com.sun.appserv.tld.map", tldMap);
    /*
         * Discover all TLDs that are known to contain listener
         * declarations, and store the resulting map as a
         * ServletContext attribute
         */
    Map<URI, List<String>> tldListenerMap = new HashMap<URI, List<String>>();
    for (TldProvider tldProvider : tldProviders) {
        // Skip any JSF related TLDs for non-JSF apps
        if ("jsfTld".equals(tldProvider.getName()) && !webModule.isJsfApplication()) {
            continue;
        }
        Map<URI, List<String>> tmap = tldProvider.getTldListenerMap();
        if (tmap != null) {
            tldListenerMap.putAll(tmap);
        }
    }
    tldListenerMap.putAll(appLibTldMap);
    servletContext.setAttribute("com.sun.appserv.tldlistener.map", tldListenerMap);
    ServiceLocator defaultServices = webContainer.getServerContext().getDefaultServices();
    // set services for jsf injection
    servletContext.setAttribute(Constants.HABITAT_ATTRIBUTE, defaultServices);
    SunWebAppImpl bean = webModule.getIasWebAppConfigBean();
    // Find the default jsp servlet
    Wrapper wrapper = (Wrapper) webModule.findChild(org.apache.catalina.core.Constants.JSP_SERVLET_NAME);
    if (wrapper == null) {
        return;
    }
    if (webModule.getTldValidation()) {
        wrapper.addInitParameter("enableTldValidation", "true");
    }
    if (bean != null && bean.getJspConfig() != null) {
        WebProperty[] props = bean.getJspConfig().getWebProperty();
        for (int i = 0; i < props.length; i++) {
            String pname = props[i].getAttributeValue("name");
            String pvalue = props[i].getAttributeValue("value");
            if (_logger.isLoggable(Level.FINE)) {
                _logger.log(Level.FINE, LogFacade.JSP_CONFIG_PROPERTY, "[" + webModule.getID() + "] is [" + pname + "] = [" + pvalue + "]");
            }
            wrapper.addInitParameter(pname, pvalue);
        }
    }
    // Override any log setting with the container wide logging level
    wrapper.addInitParameter("logVerbosityLevel", getJasperLogLevel());
    ResourceInjectorImpl resourceInjector = new ResourceInjectorImpl(webModule);
    servletContext.setAttribute("com.sun.appserv.jsp.resource.injector", resourceInjector);
    // START SJSAS 6311155
    String sysClassPath = ASClassLoaderUtil.getModuleClassPath((ServiceLocator) defaultServices, webModule.getID(), null);
    // If the configuration flag usMyFaces is set, remove jakarta.faces.jar
    // from the system class path
    Boolean useMyFaces = (Boolean) servletContext.getAttribute("com.sun.faces.useMyFaces");
    if (useMyFaces != null && useMyFaces) {
        sysClassPath = sysClassPath.replace("jakarta.faces.jar", "$disabled$.raj");
        // jsf-connector.jar manifest has a Class-Path to jakarta.faces.jar
        sysClassPath = sysClassPath.replace("jsf-connector.jar", "$disabled$.raj");
    }
    // servletContext.getAttribute(("org.apache.catalina.jsp_classpath")
    if (_logger.isLoggable(Level.FINE)) {
        _logger.log(Level.FINE, LogFacade.SYS_CLASSPATH, webModule.getID() + " is " + sysClassPath);
    }
    if (sysClassPath.equals("")) {
        // In embedded mode, services returns SingleModulesRegistry and
        // it has no modules.
        // Try "java.class.path" system property instead.
        sysClassPath = System.getProperty("java.class.path");
    }
    sysClassPath = trimSysClassPath(sysClassPath);
    wrapper.addInitParameter("com.sun.appserv.jsp.classpath", sysClassPath);
    // END SJSAS 6311155
    // Configure JSP monitoring
    servletContext.setAttribute("org.glassfish.jsp.monitor.probeEmitter", new JspProbeEmitterImpl(webModule));
    // Pass BeanManager's ELResolver as ServletContext attribute
    // (see IT 11168)
    InvocationManager invocationMgr = webContainer.getInvocationManager();
    WebComponentInvocation inv = new WebComponentInvocation(webModule);
    try {
        invocationMgr.preInvoke(inv);
        JCDIService jcdiService = defaultServices.getService(JCDIService.class);
        // JCDIService can be absent if weld integration is missing in the runtime, so check for null is needed.
        if (jcdiService != null && jcdiService.isCurrentModuleJCDIEnabled()) {
            jcdiService.setELResolver(servletContext);
        }
    } catch (NamingException e) {
    // Ignore
    } finally {
        invocationMgr.postInvoke(inv);
    }
}
Also used : SunWebAppImpl(org.glassfish.web.deployment.runtime.SunWebAppImpl) JarURIPattern(com.sun.enterprise.util.net.JarURIPattern) Pattern(java.util.regex.Pattern) JspProbeEmitterImpl(com.sun.enterprise.web.jsp.JspProbeEmitterImpl) WebProperty(org.glassfish.web.deployment.runtime.WebProperty) JCDIService(com.sun.enterprise.container.common.spi.JCDIService) HashMap(java.util.HashMap) ResourceInjectorImpl(com.sun.enterprise.web.jsp.ResourceInjectorImpl) InvocationManager(org.glassfish.api.invocation.InvocationManager) URI(java.net.URI) ServiceLocator(org.glassfish.hk2.api.ServiceLocator) ServletContext(javax.servlet.ServletContext) ArrayList(java.util.ArrayList) List(java.util.List) NamingException(javax.naming.NamingException) TldProvider(org.glassfish.api.web.TldProvider)

Example 2 with JCDIService

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

the class EjbDescriptor method getConstructorInterceptors.

/**
 * Return bean constructor for AroundConstruct interceptors
 */
private List<EjbInterceptor> getConstructorInterceptors(ClassLoader classLoader) {
    List<EjbInterceptor> callbackInterceptors = null;
    String shortClassName = ejbClassName;
    int i = ejbClassName.lastIndexOf('.');
    if (i > -1) {
        shortClassName = ejbClassName.substring(i + 1);
    }
    JCDIService jcdiService = (sl == null) ? null : sl.getService(JCDIService.class);
    if (jcdiService != null && jcdiService.isJCDIEnabled(getEjbBundleDescriptor())) {
        try {
            Class<?> beanClass = classLoader.loadClass(getEjbClassName());
            Constructor<?>[] ctors = beanClass.getDeclaredConstructors();
            String[] parameterClassNames = null;
            MethodDescriptor dummy = new MethodDescriptor();
            for (Constructor<?> ctor : ctors) {
                if (ctor.getAnnotation(Inject.class) != null) {
                    // @Inject constructor
                    Class<?>[] ctorParamTypes = ctor.getParameterTypes();
                    parameterClassNames = dummy.getParameterClassNamesFor(null, ctorParamTypes);
                    callbackInterceptors = getClassOrMethodInterceptors(new MethodDescriptor(shortClassName, null, parameterClassNames, MethodDescriptor.EJB_BEAN));
                    break;
                }
            }
        } catch (Throwable t) {
            _logger.log(Level.SEVERE, "enterprise.deployment.backend.methodClassLoadFailure", new Object[] { this.getEjbClassName() });
            throw new RuntimeException(t);
        }
    }
    if (callbackInterceptors == null) {
        // non-CDI or no @Inject constructor - use no-arg constructor
        callbackInterceptors = getClassOrMethodInterceptors(new MethodDescriptor(shortClassName, null, new String[0], MethodDescriptor.EJB_BEAN));
    }
    return callbackInterceptors;
}
Also used : Inject(javax.inject.Inject) JCDIService(com.sun.enterprise.container.common.spi.JCDIService) Constructor(java.lang.reflect.Constructor) MethodDescriptor(com.sun.enterprise.deployment.MethodDescriptor)

Example 3 with JCDIService

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

the class InjectionManagerImpl method createManagedObject.

/**
 * Create a managed object for the given class. The object will be injected and if invokePostConstruct is true,
 * any @PostConstruct methods on the instance's class(and super-classes) will be invoked after injection. The returned
 * object can be cast to the clazz type but is not necessarily a direct reference to the managed instance. All
 * invocations on the returned object should be on its public methods.
 *
 * It is the responsibility of the caller to destroy the returned object by calling destroyManagedObject(Object
 * managedObject).
 *
 * @param clazz
 *            Class to be instantiated
 * @param invokePostConstruct
 *            if true, invoke any @PostConstruct methods on the instance's class(and super-classes) after injection.
 * @return managed object
 * @throws InjectionException
 */
public <T> T createManagedObject(Class<T> clazz, boolean invokePostConstruct) throws InjectionException {
    T managedObject = null;
    try {
        ManagedBean managedBeanAnn = clazz.getAnnotation(ManagedBean.class);
        ManagedBeanManager managedBeanMgr = serviceLocator.getService(ManagedBeanManager.class);
        if (managedBeanAnn != null) {
            // EE style @ManagedBean
            // Create , inject, and call PostConstruct (if necessary) via managed bean manager
            managedObject = managedBeanMgr.createManagedBean(clazz, invokePostConstruct);
        } else {
            JCDIService jcdiService = serviceLocator.getService(JCDIService.class);
            if ((jcdiService != null) && jcdiService.isCurrentModuleJCDIEnabled()) {
                // Create , inject, and call PostConstruct (if necessary) via managed bean manager
                managedObject = managedBeanMgr.createManagedBean(clazz, invokePostConstruct);
            } else {
                // Not in a 299-enabled module and not annoated with @ManagedBean, so
                // just instantiate using new and perform injection
                Constructor<T> noArgCtor = clazz.getConstructor();
                managedObject = noArgCtor.newInstance();
                // Inject and call PostConstruct if necessary
                injectInstance(managedObject, invokePostConstruct);
            }
        }
    } catch (Exception e) {
        throw new InjectionException(localStrings.getLocalString("injection-manager.error-creating-managed-object", "Error creating managed object for class: {0}", clazz), e);
    }
    return managedObject;
}
Also used : InjectionException(com.sun.enterprise.container.common.spi.util.InjectionException) JCDIService(com.sun.enterprise.container.common.spi.JCDIService) ManagedBean(javax.annotation.ManagedBean) NamingException(javax.naming.NamingException) InjectionException(com.sun.enterprise.container.common.spi.util.InjectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ManagedBeanManager(com.sun.enterprise.container.common.spi.ManagedBeanManager)

Example 4 with JCDIService

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

the class ManagedBeanManagerImpl method createManagedBean.

/**
 * @param desc can be null if JCDI enabled bundle.
 * @param managedBeanClass
 * @return
 * @throws Exception
 */
@Override
public <T> T createManagedBean(ManagedBeanDescriptor desc, Class<T> managedBeanClass) throws Exception {
    JCDIService jcdiService = habitat.getService(JCDIService.class);
    BundleDescriptor bundleDescriptor = null;
    if (desc == null) {
        bundleDescriptor = getBundle();
    } else {
        bundleDescriptor = desc.getBundleDescriptor();
    }
    if (bundleDescriptor == null) {
        throw new IllegalStateException("Class " + managedBeanClass + " is not a valid EE ManagedBean class");
    }
    T callerObject = null;
    if ((jcdiService != null) && jcdiService.isJCDIEnabled(bundleDescriptor)) {
        // Have 299 create, inject, and call PostConstruct on managed bean
        JCDIService.JCDIInjectionContext<?> jcdiContext = jcdiService.createManagedObject(managedBeanClass, bundleDescriptor);
        callerObject = (T) jcdiContext.getInstance();
        // Need to keep track of context in order to destroy properly
        Map<Object, JCDIService.JCDIInjectionContext<?>> bundleNonManagedObjs = jcdiManagedBeanInstanceMap.get(bundleDescriptor);
        bundleNonManagedObjs.put(callerObject, jcdiContext);
    } else {
        if (desc == null) {
            throw new IllegalArgumentException("CDI not enabled and no managed bean found for class: " + managedBeanClass.getName());
        }
        JavaEEInterceptorBuilder interceptorBuilder = (JavaEEInterceptorBuilder) desc.getInterceptorBuilder();
        InterceptorInvoker interceptorInvoker = interceptorBuilder.createInvoker(null);
        // This is the object passed back to the caller.
        callerObject = (T) interceptorInvoker.getProxy();
        Object[] interceptorInstances = interceptorInvoker.getInterceptorInstances();
        // Inject interceptor instances
        for (int i = 0; i < interceptorInstances.length; i++) {
            inject(interceptorInstances[i], desc);
        }
        interceptorInvoker.invokeAroundConstruct();
        // This is the managed bean class instance
        Object managedBean = interceptorInvoker.getTargetInstance();
        inject(managedBean, desc);
        interceptorInvoker.invokePostConstruct();
        desc.addBeanInstanceInfo(managedBean, interceptorInvoker);
    }
    return callerObject;
}
Also used : JCDIService(com.sun.enterprise.container.common.spi.JCDIService) InterceptorInvoker(com.sun.enterprise.container.common.spi.InterceptorInvoker) BundleDescriptor(com.sun.enterprise.deployment.BundleDescriptor) WebBundleDescriptor(com.sun.enterprise.deployment.WebBundleDescriptor) EjbBundleDescriptor(com.sun.enterprise.deployment.EjbBundleDescriptor) JavaEEInterceptorBuilder(com.sun.enterprise.container.common.spi.JavaEEInterceptorBuilder)

Example 5 with JCDIService

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

the class ManagedBeanManagerImpl method destroyManagedBean.

@Override
public void destroyManagedBean(Object managedBean, boolean validate) {
    BundleDescriptor bundle = getBundle();
    JCDIService jcdiService = habitat.getService(JCDIService.class);
    if ((jcdiService != null) && jcdiService.isJCDIEnabled(bundle)) {
        Map<Object, JCDIService.JCDIInjectionContext<?>> bundleNonManagedObjs = jcdiManagedBeanInstanceMap.get(bundle);
        // in a failure scenario it's possible that bundleNonManagedObjs is null
        if (bundleNonManagedObjs == null) {
            if (validate) {
                throw new IllegalStateException("Unknown JCDI-enabled managed bean " + managedBean + " of class " + managedBean.getClass());
            }
            _logger.log(Level.FINE, "Unknown JCDI-enabled managed bean " + managedBean + " of class " + managedBean.getClass());
        } else {
            JCDIService.JCDIInjectionContext<?> context = bundleNonManagedObjs.remove(managedBean);
            if (context == null) {
                if (validate) {
                    throw new IllegalStateException("Unknown JCDI-enabled managed bean " + managedBean + " of class " + managedBean.getClass());
                }
                _logger.log(Level.FINE, "Unknown JCDI-enabled managed bean " + managedBean + " of class " + managedBean.getClass());
                return;
            }
            // Call PreDestroy and cleanup
            context.cleanup(true);
        }
    } else {
        Object managedBeanInstance = null;
        try {
            Field proxyField = managedBean.getClass().getDeclaredField("__ejb31_delegate");
            final Field finalF = proxyField;
            PrivilegedExceptionAction<Void> action = () -> {
                if (!finalF.isAccessible()) {
                    finalF.setAccessible(true);
                }
                return null;
            };
            AccessController.doPrivileged(action);
            Proxy proxy = (Proxy) proxyField.get(managedBean);
            InterceptorInvoker invoker = (InterceptorInvoker) Proxy.getInvocationHandler(proxy);
            managedBeanInstance = invoker.getTargetInstance();
        } catch (Exception e) {
            throw new IllegalArgumentException("invalid managed bean " + managedBean, e);
        }
        ManagedBeanDescriptor desc = bundle.getManagedBeanByBeanClass(managedBeanInstance.getClass().getName());
        if (desc == null) {
            throw new IllegalStateException("Could not retrieve managed bean descriptor for " + managedBean + " of class " + managedBean.getClass());
        }
        InterceptorInvoker invoker = (InterceptorInvoker) desc.getSupportingInfoForBeanInstance(managedBeanInstance);
        try {
            invoker.invokePreDestroy();
        } catch (Exception e) {
            _logger.log(Level.FINE, "Managed bean " + desc.getBeanClassName() + " PreDestroy", e);
        }
        desc.clearBeanInstanceInfo(managedBeanInstance);
    }
}
Also used : JCDIService(com.sun.enterprise.container.common.spi.JCDIService) InterceptorInvoker(com.sun.enterprise.container.common.spi.InterceptorInvoker) ManagedBeanDescriptor(com.sun.enterprise.deployment.ManagedBeanDescriptor) BundleDescriptor(com.sun.enterprise.deployment.BundleDescriptor) WebBundleDescriptor(com.sun.enterprise.deployment.WebBundleDescriptor) EjbBundleDescriptor(com.sun.enterprise.deployment.EjbBundleDescriptor) Field(java.lang.reflect.Field) Proxy(java.lang.reflect.Proxy) NamingObjectProxy(org.glassfish.api.naming.NamingObjectProxy)

Aggregations

JCDIService (com.sun.enterprise.container.common.spi.JCDIService)9 BundleDescriptor (com.sun.enterprise.deployment.BundleDescriptor)4 EjbBundleDescriptor (com.sun.enterprise.deployment.EjbBundleDescriptor)4 WebBundleDescriptor (com.sun.enterprise.deployment.WebBundleDescriptor)4 InterceptorInvoker (com.sun.enterprise.container.common.spi.InterceptorInvoker)3 JavaEEInterceptorBuilder (com.sun.enterprise.container.common.spi.JavaEEInterceptorBuilder)3 ManagedBeanManager (com.sun.enterprise.container.common.spi.ManagedBeanManager)3 ManagedBean (javax.annotation.ManagedBean)3 NamingException (javax.naming.NamingException)3 InjectionException (com.sun.enterprise.container.common.spi.util.InjectionException)2 ManagedBeanDescriptor (com.sun.enterprise.deployment.ManagedBeanDescriptor)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 JavaEEInterceptorBuilderFactory (com.sun.enterprise.container.common.spi.JavaEEInterceptorBuilderFactory)1 InterceptorInfo (com.sun.enterprise.container.common.spi.util.InterceptorInfo)1 InterceptorDescriptor (com.sun.enterprise.deployment.InterceptorDescriptor)1 MethodDescriptor (com.sun.enterprise.deployment.MethodDescriptor)1 JarURIPattern (com.sun.enterprise.util.net.JarURIPattern)1 JspProbeEmitterImpl (com.sun.enterprise.web.jsp.JspProbeEmitterImpl)1