Search in sources :

Example 6 with WebappClassLoader

use of org.glassfish.web.loader.WebappClassLoader in project Payara by payara.

the class WebContainer method postConstruct.

@Override
public void postConstruct() {
    ReentrantReadWriteLock mapperLock = grizzlyService.obtainMapperLock();
    mapperLock.writeLock().lock();
    try {
        createProbeProviders();
        injectionMgr = serviceLocator.getService(InjectionManager.class);
        invocationMgr = serviceLocator.getService(InvocationManager.class);
        tldProviders = serviceLocator.getAllServices(TldProvider.class);
        createStatsProviders();
        setJspFactory();
        _appsWorkRoot = instance.getApplicationCompileJspPath().getAbsolutePath();
        _modulesRoot = instance.getApplicationRepositoryPath();
        appsStubRoot = instance.getApplicationStubPath().getAbsolutePath();
        // TODO: ParserUtils should become a @Service and it should initialize itself.
        // TODO: there should be only one EntityResolver for both DigesterFactory
        // and ParserUtils
        File root = _serverContext.getInstallRoot();
        File libRoot = new File(root, "lib");
        File schemas = new File(libRoot, "schemas");
        File dtds = new File(libRoot, "dtds");
        try {
            ParserUtils.setSchemaResourcePrefix(schemas.toURI().toURL().toString());
            ParserUtils.setDtdResourcePrefix(dtds.toURI().toURL().toString());
            ParserUtils.setEntityResolver(serviceLocator.<EntityResolver>getService(EntityResolver.class, "web"));
        } catch (MalformedURLException e) {
            logger.log(SEVERE, EXCEPTION_SET_SCHEMAS_DTDS_LOCATION, e);
        }
        instanceName = _serverContext.getInstanceName();
        webContainerFeatureFactory = getWebContainerFeatureFactory();
        configureDynamicReloadingSettings();
        setDebugLevel();
        String maxDepth = null;
        org.glassfish.web.config.serverbeans.WebContainer configWC = serverConfig.getExtensionByType(org.glassfish.web.config.serverbeans.WebContainer.class);
        if (configWC != null) {
            maxDepth = configWC.getPropertyValue(DISPATCHER_MAX_DEPTH);
        }
        if (maxDepth != null) {
            int depth = -1;
            try {
                depth = Integer.parseInt(maxDepth);
            } catch (NumberFormatException e) {
            }
            if (depth > 0) {
                Request.setMaxDispatchDepth(depth);
                logger.log(FINE, MAX_DISPATCH_DEPTH_SET, maxDepth);
            }
        }
        File currentLogFile = loggingRuntime.getCurrentLogFile();
        if (currentLogFile != null) {
            logServiceFile = currentLogFile.getAbsolutePath();
        }
        Level level = Logger.getLogger("org.apache.catalina.level").getLevel();
        if (level != null) {
            logLevel = level.getName();
        }
        _embedded = serviceLocator.getService(EmbeddedWebContainer.class);
        _embedded.setWebContainer(this);
        _embedded.setLogServiceFile(logServiceFile);
        _embedded.setLogLevel(logLevel);
        _embedded.setFileLoggerHandlerFactory(fileLoggerHandlerFactory);
        _embedded.setWebContainerFeatureFactory(webContainerFeatureFactory);
        _embedded.setCatalinaHome(instance.getInstanceRoot().getAbsolutePath());
        _embedded.setCatalinaBase(instance.getInstanceRoot().getAbsolutePath());
        _embedded.setUseNaming(false);
        if (_debug > 1) {
            _embedded.setDebug(_debug);
        }
        _embedded.setLogger(new IASLogger(logger));
        engine = _embedded.createEngine();
        engine.setParentClassLoader(EmbeddedWebContainer.class.getClassLoader());
        engine.setService(_embedded);
        _embedded.addEngine(engine);
        ((StandardEngine) engine).setDomain(_serverContext.getDefaultDomainName());
        engine.setName(_serverContext.getDefaultDomainName());
        /*
             * Set the server info. By default, the server info is taken from Version#getVersion. However, customers may override it
             * via the product.name system property. Some customers prefer not to disclose the server info for security reasons, in
             * which case they would set the value of the product.name system property to the empty string. In this case, the server
             * name will not be publicly disclosed via the "Server" HTTP response header (which will be suppressed) or any container
             * generated error pages. However, it will still appear in the server logs (see IT 6900).
             */
        String serverInfo = System.getProperty("product.name");
        if (serverInfo == null) {
            ServerInfo.setServerInfo(Version.getVersion());
            ServerInfo.setPublicServerInfo(Version.getVersion());
        } else if (serverInfo.isEmpty()) {
            ServerInfo.setServerInfo(Version.getVersion());
            ServerInfo.setPublicServerInfo(serverInfo);
        } else {
            ServerInfo.setServerInfo(serverInfo);
            ServerInfo.setPublicServerInfo(serverInfo);
        }
        initInstanceSessionProperties();
        configListener = addAndGetWebConfigListener();
        ObservableBean bean = (ObservableBean) ConfigSupport.getImpl(serverConfig.getHttpService());
        bean.addListener(configListener);
        bean = (ObservableBean) ConfigSupport.getImpl(serverConfig.getNetworkConfig().getNetworkListeners());
        bean.addListener(configListener);
        if (serverConfig.getAvailabilityService() != null) {
            bean = (ObservableBean) ConfigSupport.getImpl(serverConfig.getAvailabilityService());
            bean.addListener(configListener);
        }
        transactions.addListenerForType(SystemProperty.class, configListener);
        configListener.setNetworkConfig(serverConfig.getNetworkConfig());
        // embedded mode does not have manager-propertie in domain.xml
        if (configListener.managerProperties != null) {
            ObservableBean managerBean = (ObservableBean) ConfigSupport.getImpl(configListener.managerProperties);
            managerBean.addListener(configListener);
        }
        if (serverConfig.getJavaConfig() != null) {
            ((ObservableBean) ConfigSupport.getImpl(serverConfig.getJavaConfig())).addListener(configListener);
        }
        configListener.setContainer(this);
        configListener.setLogger(logger);
        events.register(this);
        grizzlyService.addMapperUpdateListener(configListener);
        HttpService httpService = serverConfig.getHttpService();
        NetworkConfig networkConfig = serverConfig.getNetworkConfig();
        if (networkConfig != null) {
            // continue;
            securityService = serverConfig.getSecurityService();
            // Configure HTTP listeners
            NetworkListeners networkListeners = networkConfig.getNetworkListeners();
            if (networkListeners != null) {
                List<NetworkListener> listeners = networkListeners.getNetworkListener();
                for (NetworkListener listener : listeners) {
                    createHttpListener(listener, httpService);
                }
            }
            setDefaultRedirectPort(defaultRedirectPort);
            // Configure virtual servers
            createHosts(httpService, securityService);
        }
        loadSystemDefaultWebModules();
        // _lifecycle.fireLifecycleEvent(START_EVENT, null);
        _started = true;
        /*
             * Start the embedded container. Make sure to set the thread's context classloader to the classloader of this class (see
             * IT 8866 for details)
             */
        ClassLoader current = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        try {
            /*
                 * Trigger a call to sun.awt.AppContext.getAppContext(). This will pin the classloader of this class in memory and fix a
                 * memory leak affecting instances of WebappClassLoader that was caused by a JRE implementation change in 1.6.0_15
                 * onwards. See IT 11110
                 */
            ImageIO.getCacheDirectory();
            _embedded.start();
        } catch (LifecycleException le) {
            logger.log(SEVERE, UNABLE_TO_START_WEB_CONTAINER, le);
        } finally {
            // Restore original context classloader
            Thread.currentThread().setContextClassLoader(current);
        }
    } finally {
        mapperLock.writeLock().unlock();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) InvocationManager(org.glassfish.api.invocation.InvocationManager) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) StandardEngine(org.apache.catalina.core.StandardEngine) NetworkListeners(org.glassfish.grizzly.config.dom.NetworkListeners) WebappClassLoader(org.glassfish.web.loader.WebappClassLoader) TldProvider(org.glassfish.api.web.TldProvider) LifecycleException(org.apache.catalina.LifecycleException) NetworkConfig(org.glassfish.grizzly.config.dom.NetworkConfig) EntityResolver(org.xml.sax.EntityResolver) HttpService(com.sun.enterprise.config.serverbeans.HttpService) IASLogger(com.sun.enterprise.web.logger.IASLogger) Level(java.util.logging.Level) ObservableBean(org.jvnet.hk2.config.ObservableBean) File(java.io.File) InjectionManager(com.sun.enterprise.container.common.spi.util.InjectionManager) NetworkListener(org.glassfish.grizzly.config.dom.NetworkListener)

Example 7 with WebappClassLoader

use of org.glassfish.web.loader.WebappClassLoader in project Payara by payara.

the class VirtualServer method findWebModuleInJ2eeApp.

/**
 * Finds and returns information about a web module embedded within a J2EE application, which is identified by a string
 * of the form <code>a:b</code> or <code>a#b</code>, where <code>a</code> is the name of the J2EE application and
 * <code>b</code> is the name of the embedded web module.
 *
 * @param appsBean
 * @param id
 * @return null if <code>id</code> does not identify a web module embedded within a J2EE application.
 */
protected WebModuleConfig findWebModuleInJ2eeApp(Applications appsBean, String id, ApplicationRegistry appRegistry) {
    WebModuleConfig webModuleConfig = null;
    // Check for ':' separator
    int separatorIndex = id.indexOf(Constants.NAME_SEPARATOR);
    if (separatorIndex == -1) {
        // Check for '#' separator
        separatorIndex = id.indexOf('#');
    }
    if (separatorIndex != -1) {
        String appID = id.substring(0, separatorIndex);
        String moduleID = id.substring(separatorIndex + 1);
        com.sun.enterprise.config.serverbeans.Application appBean = appsBean.getModule(com.sun.enterprise.config.serverbeans.Application.class, appID);
        if ((appBean != null) && Boolean.valueOf(appBean.getEnabled())) {
            String location = appBean.getLocation();
            String moduleDir = DeploymentUtils.getRelativeEmbeddedModulePath(location, moduleID);
            ApplicationInfo appInfo = appRegistry.get(appID);
            Application app = appInfo != null ? appInfo.getMetaData(Application.class) : null;
            if (appInfo == null) {
                // XXX ApplicaionInfo is NULL after restart
                _logger.log(Level.SEVERE, LogFacade.VS_DEFAULT_WEB_MODULE_DISABLED, new Object[] { id, getID() });
                return webModuleConfig;
            }
            WebBundleDescriptorImpl webBundleDescriptorImpl = app.getModuleByTypeAndUri(WebBundleDescriptorImpl.class, moduleID);
            String webUri = webBundleDescriptorImpl.getModuleDescriptor().getArchiveUri();
            String contextRoot = webBundleDescriptorImpl.getModuleDescriptor().getContextRoot();
            if (moduleID.equals(webUri)) {
                webBundleDescriptorImpl.setName(moduleID);
                webBundleDescriptorImpl.setContextRoot(contextRoot);
                webModuleConfig = new WebModuleConfig();
                webModuleConfig.setDescriptor(webBundleDescriptorImpl);
                webModuleConfig.setLocation(new File(new StringBuilder(location).append(File.separator).append(moduleDir).toString()));
                webModuleConfig.setParentLoader(EmbeddedWebContainer.class.getClassLoader());
                webModuleConfig.setAppClassLoader(privileged(() -> new WebappClassLoader(EmbeddedWebContainer.class.getClassLoader(), app)));
            }
        } else {
            _logger.log(SEVERE, VS_DEFAULT_WEB_MODULE_DISABLED, new Object[] { id, getID() });
        }
    }
    return webModuleConfig;
}
Also used : ApplicationInfo(org.glassfish.internal.data.ApplicationInfo) WebappClassLoader(org.glassfish.web.loader.WebappClassLoader) WebBundleDescriptorImpl(org.glassfish.web.deployment.descriptor.WebBundleDescriptorImpl) Application(com.sun.enterprise.deployment.Application) File(java.io.File)

Example 8 with WebappClassLoader

use of org.glassfish.web.loader.WebappClassLoader in project Payara by payara.

the class VirtualServer method getDefaultWebModule.

protected WebModuleConfig getDefaultWebModule(Domain domain, WebArchivist webArchivist, ApplicationRegistry appRegistry) {
    WebModuleConfig webModuleConfig = null;
    String defaultWebModuleId = getDefaultWebModuleID();
    if (defaultWebModuleId != null) {
        // Check if the default-web-module is part of an ee-application
        webModuleConfig = findWebModuleInJ2eeApp(domain.getApplications(), defaultWebModuleId, appRegistry);
        if (webModuleConfig == null) {
            String contextRoot = null;
            String location = null;
            ConfigBeansUtilities configBeansUtilities = getConfigBeansUtilities();
            if (configBeansUtilities != null) {
                contextRoot = configBeansUtilities.getContextRoot(defaultWebModuleId);
                location = configBeansUtilities.getLocation(defaultWebModuleId);
            }
            if (contextRoot != null && location != null) {
                WebBundleDescriptorImpl webBundleDescriptorImpl = webArchivist.getDefaultWebXmlBundleDescriptor();
                webBundleDescriptorImpl.setName(DEFAULT_WEB_MODULE_NAME);
                webBundleDescriptorImpl.setContextRoot(contextRoot);
                webModuleConfig = new WebModuleConfig();
                webModuleConfig.setLocation(new File(location));
                webModuleConfig.setDescriptor(webBundleDescriptorImpl);
                webModuleConfig.setParentLoader(EmbeddedWebContainer.class.getClassLoader());
                webModuleConfig.setAppClassLoader(privileged(() -> new WebappClassLoader(EmbeddedWebContainer.class.getClassLoader(), webBundleDescriptorImpl.getApplication())));
            }
        }
        if (webModuleConfig == null) {
            _logger.log(SEVERE, VS_DEFAULT_WEB_MODULE_NOT_FOUND, new Object[] { defaultWebModuleId, getID() });
        }
    }
    return webModuleConfig;
}
Also used : WebappClassLoader(org.glassfish.web.loader.WebappClassLoader) WebBundleDescriptorImpl(org.glassfish.web.deployment.descriptor.WebBundleDescriptorImpl) ConfigBeansUtilities(com.sun.enterprise.config.serverbeans.ConfigBeansUtilities) File(java.io.File)

Example 9 with WebappClassLoader

use of org.glassfish.web.loader.WebappClassLoader in project Payara by payara.

the class VirtualServer method createSystemDefaultWebModuleIfNecessary.

/**
 * If a default web module has not yet been configured and added to this virtual server's list of web modules then
 * return the configuration information needed in order to create a default web module for this virtual server.
 *
 * <p>
 * This method should be invoked only after all the standalone modules and the modules within j2ee-application elements
 * have been added to this virtual server's list of modules (only then will one know whether the user has already
 * configured a default web module or not).
 *
 * @param webArchivist
 * @return
 */
public WebModuleConfig createSystemDefaultWebModuleIfNecessary(WebArchivist webArchivist) {
    WebModuleConfig webModuleConfig = null;
    // Add a default context only if one hasn't already been loaded
    // and then too only if docroot is not null
    // 
    String docroot = getAppBase();
    if (getDefaultWebModuleID() == null && findChild("") == null && docroot != null) {
        WebBundleDescriptorImpl webBundleDescriptor = webArchivist.getDefaultWebXmlBundleDescriptor();
        webBundleDescriptor.setModuleID(DEFAULT_WEB_MODULE_NAME);
        webBundleDescriptor.setContextRoot("");
        SecurityService securityService = Globals.get(SecurityService.class);
        webBundleDescriptor.getLoginConfiguration().setRealmName(securityService.getDefaultRealm());
        webModuleConfig = new WebModuleConfig();
        webModuleConfig.setLocation(new File(docroot));
        webModuleConfig.setDescriptor(webBundleDescriptor);
        webModuleConfig.setParentLoader(serverContext.getCommonClassLoader());
        WebappClassLoader loader = privileged(() -> new WebappClassLoader(serverContext.getCommonClassLoader(), getApplication(webBundleDescriptor)));
        loader.start();
        webModuleConfig.setAppClassLoader(loader);
    }
    return webModuleConfig;
}
Also used : WebappClassLoader(org.glassfish.web.loader.WebappClassLoader) WebBundleDescriptorImpl(org.glassfish.web.deployment.descriptor.WebBundleDescriptorImpl) SecurityService(com.sun.enterprise.config.serverbeans.SecurityService) File(java.io.File)

Aggregations

WebappClassLoader (org.glassfish.web.loader.WebappClassLoader)9 File (java.io.File)5 Application (com.sun.enterprise.deployment.Application)3 WebBundleDescriptorImpl (org.glassfish.web.deployment.descriptor.WebBundleDescriptorImpl)3 MalformedURLException (java.net.MalformedURLException)2 PrivilegedActionException (java.security.PrivilegedActionException)2 ConfigBeansUtilities (com.sun.enterprise.config.serverbeans.ConfigBeansUtilities)1 HttpService (com.sun.enterprise.config.serverbeans.HttpService)1 SecurityService (com.sun.enterprise.config.serverbeans.SecurityService)1 InjectionManager (com.sun.enterprise.container.common.spi.util.InjectionManager)1 SetPermissionsAction (com.sun.enterprise.security.permissionsxml.SetPermissionsAction)1 Result (com.sun.enterprise.util.Result)1 IASLogger (com.sun.enterprise.web.logger.IASLogger)1 IOException (java.io.IOException)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 PrivilegedAction (java.security.PrivilegedAction)1 ArrayList (java.util.ArrayList)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1 JarFile (java.util.jar.JarFile)1