All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.glassfish.web.embed.impl.WebContainerImpl Maven / Gradle / Ivy

There is a newer version: 7.2024.1.Alpha1
Show newest version
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 1997-2012 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
 * or packager/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */
// Portions Copyright [2019-2022] Payara Foundation and/or affiliates

package org.glassfish.web.embed.impl;

import com.sun.enterprise.config.serverbeans.HttpService;
import com.sun.enterprise.web.ContextFacade;
import com.sun.enterprise.web.EmbeddedWebContainer;
import com.sun.enterprise.web.VirtualServerFacade;
import com.sun.enterprise.web.WebConnector;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import org.apache.catalina.Container;
import org.apache.catalina.Engine;
import org.apache.catalina.core.StandardHost;
import org.glassfish.api.admin.ServerEnvironment;
import org.glassfish.api.container.Sniffer;
import org.glassfish.embeddable.GlassFishException;
import org.glassfish.embeddable.web.*;
import org.glassfish.embeddable.web.config.SslConfig;
import org.glassfish.embeddable.web.config.SslType;
import org.glassfish.embeddable.web.config.WebContainerConfig;
import org.glassfish.grizzly.config.dom.*;
import org.glassfish.hk2.api.ActiveDescriptor;
import org.glassfish.hk2.api.ServiceHandle;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.utilities.BuilderHelper;
import org.glassfish.internal.api.ServerContext;
import org.glassfish.internal.embedded.Port;
import org.glassfish.internal.embedded.Ports;
import org.jvnet.hk2.annotations.ContractsProvided;
import org.jvnet.hk2.annotations.Service;
import org.jvnet.hk2.config.*;
import org.jvnet.hk2.config.types.Property;

import java.beans.PropertyVetoException;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 * Class representing an embedded web container, which supports the
 * programmatic creation of different types of web protocol listeners
 * and virtual servers, and the registration of static and dynamic
 * web resources into the URI namespace.
 *
 * @author Amy Roh
 */
@Service
@ContractsProvided({WebContainerImpl.class, WebContainer.class})
public class WebContainerImpl implements WebContainer {


    private ServiceHandle container;

    private ServiceHandle embeddedInhabitant;

    @Inject
    private ServiceLocator habitat;

    @Inject @Named(ServerEnvironment.DEFAULT_INSTANCE_NAME)
    private HttpService httpService;

    private static Logger log =
            Logger.getLogger(WebContainerImpl.class.getName());

    @Inject @Named(ServerEnvironment.DEFAULT_INSTANCE_NAME)
    private NetworkConfig networkConfig;

    @Inject
    private ServerContext serverContext;


    // ----------------------------------------------------- Instance Variables


    private WebContainerConfig config;

    private EmbeddedWebContainer embedded;

    private Engine engine = null;

    private boolean initialized = false;

    private String listenerName = "embedded-listener";

    private List listeners = new ArrayList();

    private String securityEnabled = "false";

    private com.sun.enterprise.web.WebContainer webContainer;


    // --------------------------------------------------------- Private Methods


    private void init() {

        if (initialized) {
            return;
        }

        if (config == null) {
            // use default settings
            config = new WebContainerConfig();
        }

        container = habitat.getServiceHandle(org.glassfish.api.container.Container.class,
                "com.sun.enterprise.web.WebContainer");
        if (container == null) {
            log.severe("Cannot find webcontainer implementation");
            return;
        }

        ActiveDescriptor activeDescriptor = habitat.getBestDescriptor(
                BuilderHelper.createContractFilter("com.sun.enterprise.web.EmbeddedWebContainer"));
        if (activeDescriptor == null) {
            log.severe("Cannot find embedded implementation");
            return;
        }
        embeddedInhabitant = habitat.getServiceHandle(activeDescriptor);

        try {

            webContainer = (com.sun.enterprise.web.WebContainer) container.getService();
            embedded = (EmbeddedWebContainer) embeddedInhabitant.getService();

            if ((webContainer == null) || (embedded == null)) {
                log.severe("Cannot find webcontainer implementation");
                return;
            }

            engine = webContainer.getEngine();
            if (engine == null) {
                log.severe("Cannot find engine implementation");
                return;
            }

            initialized = true;

        } catch (Exception e) {
            log.severe("Init exception " + e.getMessage());
        }

    }

    private void bind(Port port, WebListener webListener, String vsId) {

        String protocol = Port.HTTP_PROTOCOL;
        final int portNumber = port.getPortNumber();
        final String defaultVS = vsId;
        final WebListener listener = webListener;

        if (webListener == null) {
            listenerName = getListenerName();
            webListener = new HttpListener();
            webListener.setId(listenerName);
            webListener.setPort(portNumber);
        } else {
            listenerName = webListener.getId();
            protocol = webListener.getProtocol();
        }
        listeners.add(webListener);

        if (protocol.equals(Port.HTTP_PROTOCOL)) {
            securityEnabled = "false";
        } else if (protocol.equals(Port.HTTPS_PROTOCOL)) {
            securityEnabled = "true";
        }

        try {

            ConfigSupport.apply(new SingleConfigCode() {
                public Object run(Protocols param) throws TransactionFailure {
                    final Protocol protocol = param.createChild(Protocol.class);
                    protocol.setName(listenerName);
                    protocol.setSecurityEnabled(securityEnabled);
                    param.getProtocol().add(protocol);
                    final Http http = protocol.createChild(Http.class);
                    http.setDefaultVirtualServer(defaultVS);
                    http.setFileCache(http.createChild(FileCache.class));
                    protocol.setHttp(http);
                    return protocol;
                }
            }, networkConfig.getProtocols());

            ConfigSupport.apply(new ConfigCode() {
                public Object run(ConfigBeanProxy... params) throws TransactionFailure {
                    NetworkListeners nls = (NetworkListeners) params[0];
                    Transports transports = (Transports) params[1];
                    final NetworkListener listener = nls.createChild(NetworkListener.class);
                    listener.setName(listenerName);
                    listener.setPort(Integer.toString(portNumber));
                    listener.setProtocol(listenerName);
                    listener.setThreadPool("http-thread-pool");
                    if (listener.findThreadPool() == null) {
                        final ThreadPool pool = nls.createChild(ThreadPool.class);
                        pool.setName(listenerName);
                        listener.setThreadPool(listenerName);
                    }
                    listener.setTransport("tcp");
                    if (listener.findTransport() == null) {
                        final Transport transport = transports.createChild(Transport.class);
                        transport.setName(listenerName);
                        listener.setTransport(listenerName);
                    }
                    nls.getNetworkListener().add(listener);
                    return listener;
                }
            }, networkConfig.getNetworkListeners(), networkConfig.getTransports());

            if (webListener.getProtocol().equals("https")) {
                NetworkListener networkListener = networkConfig.getNetworkListener(listenerName);
                Protocol httpProtocol = networkListener.findHttpProtocol();
                ConfigSupport.apply(new SingleConfigCode() {
                    public Object run(Protocol param) throws TransactionFailure {
                        Ssl newSsl = param.createChild(Ssl.class);
                        populateSslElement(newSsl, listener);
                        System.out.println("SSL "+newSsl.getKeyStore()+" "+newSsl.getKeyStorePassword()+" "+newSsl.getTrustStore()+" "+newSsl.getTrustStorePassword());
                        param.setSsl(newSsl);
                        return newSsl;
                    }
                }, httpProtocol);
            }

            com.sun.enterprise.config.serverbeans.VirtualServer vs =
                    httpService.getVirtualServerByName(config.getVirtualServerId());
            ConfigSupport.apply(new SingleConfigCode() {
                public Object run(com.sun.enterprise.config.serverbeans.VirtualServer avs)
                        throws PropertyVetoException {
                    avs.addNetworkListener(listenerName);
                    return avs;
                }
            }, vs);

        } catch (Exception e) {
            if (listeners.contains(webListener)) {
                listeners.remove(webListener);
            }
            e.printStackTrace();
        }

    }

    private void populateSslElement(Ssl newSsl, WebListener webListener) {

        if (webListener instanceof HttpsListener) {

            HttpsListener listener = (HttpsListener) webListener;
            SslConfig sslConfig = listener.getSslConfig();
            if (sslConfig == null) {
                return;
            }

            if (sslConfig.getKeyStore() != null) {
                newSsl.setKeyStore(sslConfig.getKeyStore());
            }
            if (sslConfig.getKeyPassword() != null) {
                newSsl.setKeyStorePassword(new String(sslConfig.getKeyPassword()));
            }
            if (sslConfig.getTrustStore() != null) {
                newSsl.setTrustStore(sslConfig.getTrustStore());
            }
            if (sslConfig.getTrustPassword() != null) {
                newSsl.setTrustStorePassword(new String(sslConfig.getTrustPassword()));
            }

            if (sslConfig.getAlgorithms() != null) {
                for (SslType sslType : sslConfig.getAlgorithms()) {
                    if (sslType.equals(SslType.TLS)) {
                        newSsl.setSsl3TlsCiphers("true");
                    }
                }
            }

            if (sslConfig.getHandshakeTimeout() > 0) {
                newSsl.setSSLInactivityTimeout(sslConfig.getHandshakeTimeout());
            }
            if (sslConfig.getCertNickname() != null) {
                newSsl.setCertNickname(sslConfig.getCertNickname());
            }

        } else {
            log.severe("HttpsListener required for https protocol");
        }

    }

    private void addWebListener(WebListener webListener, String vsId)
            throws ConfigException, GlassFishException {

        if (!initialized) {
            init();
        }

        if (getWebListener(webListener.getId()) != null) {
            throw new ConfigException("Connector with name '" +
                    webListener.getId() + "' already exsits");
        }

        listenerName = webListener.getId();

        try {
            Ports ports = habitat.getService(Ports.class);
            Port port = ports.createPort(webListener.getPort());
            bind(port, webListener, vsId);
        } catch (java.io.IOException ex) {
            throw new ConfigException(ex);
        }

        webListener.setWebContainer(this);

        if (log.isLoggable(Level.INFO)) {
            log.info("Added connector " + webListener.getId() +
                    " port " + webListener.getPort() + " to virtual server " + vsId);
        }

    }

    private String getListenerName() {

        int i = 1;

        // use listenerName set via addWebListener
        if (!existsListener(listenerName)) {
            return listenerName;
        }

        // use default listener name
        String name = "embedded-listener";
        while (existsListener(name)) {
            name = "embedded-listener-" + i++;
        }

        return name;

    }

    private boolean existsListener(String lName) {

        for (NetworkListener nl :
                networkConfig.getNetworkListeners().getNetworkListener()) {
            if (nl.getName().equals(lName)) {
                return true;
            }
        }

        return false;

    }

    private void removeListener(String name) {

        try {

            NetworkListeners networkListeners = networkConfig.getNetworkListeners();
            final NetworkListener listenerToBeRemoved =
                    networkConfig.getNetworkListener(name);

            final Protocols protocols = networkConfig.getProtocols();
            final Protocol protocol = networkConfig.findProtocol(name);

            if (listenerToBeRemoved == null) {
                log.severe("Network Listener " + name + " doesn't exist");
            } else {
                final com.sun.enterprise.config.serverbeans.VirtualServer virtualServer =
                        httpService.getVirtualServerByName(
                        listenerToBeRemoved.findHttpProtocol().getHttp().getDefaultVirtualServer());
                ConfigSupport.apply(new ConfigCode() {
                    public Object run(ConfigBeanProxy... params) throws PropertyVetoException {
                        final NetworkListeners listeners = (NetworkListeners) params[0];
                        final com.sun.enterprise.config.serverbeans.VirtualServer server =
                                (com.sun.enterprise.config.serverbeans.VirtualServer) params[1];
                        listeners.getNetworkListener().remove(listenerToBeRemoved);
                        server.removeNetworkListener(listenerToBeRemoved.getName());
                        return listenerToBeRemoved;
                    }
                }, networkListeners, virtualServer);

                ConfigSupport.apply(new ConfigCode() {
                    public Object run(ConfigBeanProxy... params) throws PropertyVetoException {
                        final Protocols protocols = (Protocols) params[0];
                        final Protocol protocol = (Protocol) params[1];
                        protocols.getProtocol().remove(protocol);
                        return protocol;
                    }
                }, protocols, protocol);

            }

        } catch (TransactionFailure e) {
            log.severe("Remove listener " + name + " failed " +e.getMessage());
        }

    }


    // --------------------------------------------------------- Public Methods


    public void setConfiguration(WebContainerConfig config) {

        if (!initialized) {
            init();
        }

        this.config = config;

        final WebContainerConfig webConfig = config;

        try {

            VirtualServer vs = getVirtualServer(config.getVirtualServerId());
            if (vs != null) {
                ((StandardHost)vs).setDefaultWebXmlLocation(config.getDefaultWebXml().getPath());
            }

            com.sun.enterprise.config.serverbeans.VirtualServer vsBean =
                httpService.getVirtualServerByName(config.getVirtualServerId());

            if (vsBean != null) {

                ConfigSupport.apply(new SingleConfigCode() {
                    public Object run(com.sun.enterprise.config.serverbeans.VirtualServer avs)
                            throws PropertyVetoException, TransactionFailure {
                        avs.setId(webConfig.getVirtualServerId());
                        if (webConfig.getDocRootDir() != null) {
                            avs.setDocroot(webConfig.getDocRootDir().getAbsolutePath());
                        }
                        avs.setHosts(webConfig.getHostNames());
                        avs.setNetworkListeners(webConfig.getListenerName());
                        Property property = avs.createChild(Property.class);
                        property.setName("default-web-xml");
                        property.setValue(webConfig.getDefaultWebXml().getPath());
                        avs.getProperty().add(property);
                        return avs;
                    }
                }, vsBean);

            } else {

                vs = createVirtualServer(config.getVirtualServerId(), config.getDocRootDir());
                addVirtualServer(vs);

            }

            EmbeddedWebArchivist archivist = habitat.getService(EmbeddedWebArchivist.class);
            archivist.setDefaultWebXml(config.getDefaultWebXml());

            embedded.setDirectoryListing(config.getListings());

            WebListener listener = getWebListener(config.getListenerName());
            if (listener == null) {
                listener = getWebListener(config.getPort());
                if (listener == null) {
                    boolean found = false;
                    for (Map.Entry entry : webContainer.getConnectorMap().entrySet()) {
                        if (((WebConnector)entry.getValue()).getPort() == config.getPort()) {
                            found = true;
                            log.info("Port "+config.getPort()+" is already configured");
                        }
                    }
                    if (!found) {
                        listener = createWebListener(config.getListenerName(), HttpListener.class);
                        listener.setPort(config.getPort());
                        addWebListener(listener, config.getVirtualServerId());
                    }
                }
            } else {
                if (listener.getPort() != config.getPort()) {
                    listener.setPort(config.getPort());
                }
            }

        }  catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    /**
     * Returns the list of sniffers associated with this embedded container
     * @return list of sniffers
     */
    public List getSniffers() {

        List sniffers = new ArrayList();
        sniffers.add(habitat.getService(Sniffer.class, "web"));
        sniffers.add(habitat.getService(Sniffer.class, "weld"));
        Sniffer security = habitat.getService(Sniffer.class, "Security");
        if (security!=null) {
            sniffers.add(security);
        }
        return sniffers;

    }

    /**
     * Creates a Context and configures it with the given
     * docroot and classloader.
     *
     * 

The classloader of the class on which this method is called * will be used. * *

In order to access the new Context or any of its * resources, the Context must be registered with a * VirtualServer that has been started. * * @param docRoot the docroot of the Context * * @return the new Context * * @see VirtualServer#addContext */ public Context createContext(File docRoot) { return createContext(docRoot, null); } /** * Creates a Context and configures it with the given * docroot and classloader. * *

The given classloader will be set as the thread's context * classloader whenever the new Context or any of its * resources are asked to process a request. * If a null classloader is passed, the classloader of the * class on which this method is called will be used. * *

In order to access the new Context or any of its * resources, the Context must be registered with a * VirtualServer that has been started. * * @param docRoot the docroot of the Context * @param classLoader the classloader of the Context * * @return the new Context * * @see VirtualServer#addContext */ public Context createContext(File docRoot, ClassLoader classLoader) { if (docRoot == null) { log.severe("Cannot create context with NULL docroot"); return null; } if (!initialized) { init(); } if (log.isLoggable(Level.INFO)) { log.info("Creating context '" + docRoot.getName() + "' with docBase '" + docRoot.getAbsolutePath() + "'"); } return new ContextFacade(docRoot, null, classLoader); } /** * Creates a Context, configures it with the given * docroot and classloader, and registers it with all * VirtualServer. * *

The given classloader will be set as the thread's context * classloader whenever the new Context or any of its * resources are asked to process a request. * If a null classloader is passed, the classloader of the * class on which this method is called will be used. * * @param docRoot the docroot of the Context * @param contextRoot the contextroot at which to register * @param classLoader the classloader of the Context * * @return the new Context */ public Context createContext(File docRoot, String contextRoot, ClassLoader classLoader) { Context context = createContext(docRoot, classLoader); try { addContext(context, contextRoot); } catch (Exception ex) { log.severe("Couldn't add context " + context + " using " + contextRoot); ex.printStackTrace(); } return context; } /** * Registers the given Context with all VirtualServer * at the given context root. * *

If VirtualServer has already been started, the * given context will be started as well. * * @param context the Context to register * @param contextRoot the context root at which to register * * @throws org.glassfish.embeddable.web.ConfigException if a Context already exists * at the given context root on VirtualServer * @throws GlassFishException if the given context fails * to be started */ public void addContext(Context context, String contextRoot) throws ConfigException, GlassFishException { if (contextRoot==null) { throw new ConfigException("Context root cannot be NULL"); } for (VirtualServer vs : getVirtualServers()) { if (vs.getContext(contextRoot)!=null) { throw new ConfigException("Context with contextRoot "+ contextRoot+" is already registered"); } if (!org.glassfish.api.web.Constants.ADMIN_VS.equals(vs.getID())) { vs.addContext(context, contextRoot); if (log.isLoggable(Level.INFO)) { log.info("Added context with path " + contextRoot + " from virtual server " + vs.getID()); } } } } /** * Stops the given Context and removes it from all * VirtualServer. * * @param context the Context to be stopped and removed * * @throws GlassFishException if an error occurs during the stopping * or removal of the given context */ public void removeContext(Context context) throws ConfigException, GlassFishException { String contextRoot = context.getPath(); for (VirtualServer vs : getVirtualServers()) { if (!org.glassfish.api.web.Constants.ADMIN_VS.equals(vs.getID())) { if (vs.getContext(contextRoot)!=null) { vs.removeContext(context); if (log.isLoggable(Level.INFO)) { log.info("Removed context with path " + contextRoot + " from virtual server " + vs.getID()); } } else { throw new GlassFishException("Context with context path " + context.getPath() + " does not exist on virtual server " + vs.getID()); } } } } /** * Creates a WebListener from the given class type and * assigns the given id to it. * * @param id the id of the new WebListener * @param c the class from which to instantiate the * WebListener * * @return the new WebListener instance * * @throws IllegalAccessException if the given Class or * its nullary constructor is not accessible. * @throws InstantiationException if the given Class * represents an abstract class, an interface, an array class, * a primitive type, or void; or if the class has no nullary * constructor; or if the instantiation fails for some other reason. * @throws ExceptionInInitializerError if the initialization * fails * @throws SecurityException if a security manager, s, is * present and any of the following conditions is met: * *

    *
  • invocation of {@link SecurityManager#checkMemberAccess * s.checkMemberAccess(this, Member.PUBLIC)} denies * creation of new instances of the given Class *
  • the caller's class loader is not the same as or an * ancestor of the class loader for the current class and * invocation of {@link SecurityManager#checkPackageAccess * s.checkPackageAccess()} denies access to the package * of this class *
*/ public T createWebListener(String id, Class c) throws InstantiationException, IllegalAccessException { T webListener = null; if (log.isLoggable(Level.INFO)) { log.info("Creating connector " + id); } try { webListener = c.newInstance(); webListener.setId(id); } catch (Exception e) { log.severe("Couldn't create connector " + e.getMessage()); } return webListener; } /** * Adds the given WebListener to this * WebContainer. * *

If this WebContainer has already been started, * the given webListener will be started as well. * * @param webListener the WebListener to add * * @throws ConfigException if a WebListener with the * same id has already been registered with this * WebContainer * @throws GlassFishException if the given webListener fails * to be started */ public void addWebListener(WebListener webListener) throws ConfigException, GlassFishException { if (!initialized) { init(); } addWebListener(webListener, config.getVirtualServerId()); } /** * Finds the WebListener with the given id. * * @param id the id of the WebListener to find * * @return the WebListener with the given id, or * null if no WebListener with that id has been * registered with this WebContainer */ public WebListener getWebListener(String id) { if (!initialized) { init(); } for (WebListener listener : listeners) { if (listener.getId().equals(id)) { return listener; } } return null; } private WebListener getWebListener(int port) { for (WebListener listener : listeners) { if (listener.getPort() == port) { return listener; } } return null; } /** * Gets the collection of WebListener instances registered * with this WebContainer. * * @return the (possibly empty) collection of WebListener * instances registered with this WebContainer */ public Collection getWebListeners() { return listeners; } /** * Stops the given webListener and removes it from this * WebContainer. * * @param webListener the WebListener to be stopped * and removed * * @throws GlassFishException if an error occurs during the stopping * or removal of the given webListener */ public void removeWebListener(WebListener webListener) throws GlassFishException { if (listeners.contains(webListener)) { listeners.remove(webListener); } else { throw new GlassFishException(new ConfigException( "Connector with name '" + webListener.getId()+"' does not exsits")); } removeListener(webListener.getId()); if (log.isLoggable(Level.INFO)) { log.info("Removed connector " + webListener.getId()); } } /** * Creates a VirtualServer with the given id and docroot, and * maps it to the given WebListener instances. * * @param id the id of the VirtualServer * @param docRoot the docroot of the VirtualServer * @param webListeners the list of WebListener instances from * which the VirtualServer will receive requests * * @return the new VirtualServer instance */ public VirtualServer createVirtualServer(String id, File docRoot, WebListener... webListeners) { return new VirtualServerFacade(id, docRoot, webListeners); } /** * Creates a VirtualServer with the given id and docroot, and * maps it to all WebListener instances. * * @param id the id of the VirtualServer * @param docRoot the docroot of the VirtualServer * * @return the new VirtualServer instance */ public VirtualServer createVirtualServer(String id, File docRoot) { return new VirtualServerFacade(id, docRoot, (WebListener[]) null); } /** * Adds the given VirtualServer to this * WebContainer. * *

If this WebContainer has already been started, * the given virtualServer will be started as well. * * @param virtualServer the VirtualServer to add * * @throws ConfigException if a VirtualServer with the * same id has already been registered with this * WebContainer * @throws GlassFishException if the given virtualServer fails * to be started */ public void addVirtualServer(VirtualServer virtualServer) throws ConfigException, GlassFishException { if (!initialized) { init(); } if (log.isLoggable(Level.INFO)) { log.info("Adding virtual server " + virtualServer.getID()); } com.sun.enterprise.web.VirtualServer vs = (com.sun.enterprise.web.VirtualServer) engine.findChild(virtualServer.getID()); if (vs != null) { throw new ConfigException("VirtualServer with id "+ virtualServer.getID()+" is already registered"); } Collection webListeners = virtualServer.getWebListeners(); List names = new ArrayList(); if ((webListeners != null) && (!webListeners.isEmpty())) { for (WebListener listener : webListeners) { names.add(listener.getId()); } } else { for (NetworkListener networkListener : networkConfig.getNetworkListeners().getNetworkListener()) { names.add(networkListener.getName()); } webListeners = listeners; } StringBuilder networkListeners = new StringBuilder(""); if (names.size()>0) { networkListeners.append(names.get(0)); } for (int i=1; i() { public Object run(HttpService param) throws PropertyVetoException, TransactionFailure { com.sun.enterprise.config.serverbeans.VirtualServer newVirtualServer = param.createChild(com.sun.enterprise.config.serverbeans.VirtualServer.class); newVirtualServer.setId(id); newVirtualServer.setNetworkListeners(nl); if (hosts != null) { newVirtualServer.setHosts(hosts); } Property property = newVirtualServer.createChild(Property.class); property.setName("docroot"); property.setValue(root); newVirtualServer.getProperty().add(property); param.getVirtualServer().add(newVirtualServer); return newVirtualServer; } }, httpService); } catch (Exception ex) { throw new GlassFishException(ex); } if ((webListeners != null) && (!webListeners.isEmpty())) { for (WebListener listener : webListeners) { if (getWebListener(listener.getId())==null) { addWebListener(listener, virtualServer.getID()); } } } vs = (com.sun.enterprise.web.VirtualServer) engine.findChild(id); if (vs != null) { if (log.isLoggable(Level.INFO)) { log.info("Added virtual server " + id + " docroot " + docRoot + " networklisteners " + nl); } if (virtualServer instanceof VirtualServerFacade) { ((VirtualServerFacade)virtualServer).setVirtualServer(vs); } vs.setNetworkListenerNames(names.toArray(new String[names.size()])); } else { log.severe("Could not add virtual server "+id); throw new GlassFishException( new Exception("Cannot add virtual server " + id)); } } /** * Finds the VirtualServer with the given id. * * @param id the id of the VirtualServer to find * * @return the VirtualServer with the given id, or * null if no VirtualServer with that id has been * registered with this WebContainer */ public VirtualServer getVirtualServer(String id) { if (!initialized) { init(); } return (VirtualServer)engine.findChild(id); } /** * Gets the collection of VirtualServer instances registered * with this WebContainer. * * @return the (possibly empty) collection of VirtualServer * instances registered with this WebContainer */ public Collection getVirtualServers(){ if (!initialized) { init(); } List virtualServers = new ArrayList(); for (Container child : engine.findChildren()) { if (child instanceof VirtualServer) { virtualServers.add((VirtualServer)child); } } return virtualServers; } /** * Stops the given virtualServer and removes it from this * WebContainer. * * @param virtualServer the VirtualServer to be stopped * and removed * * @throws GlassFishException if an error occurs during the stopping * or removal of the given virtualServer */ public void removeVirtualServer(VirtualServer virtualServer) throws GlassFishException { if (!initialized) { init(); } if (virtualServer instanceof Container) { engine.removeChild((Container)virtualServer); } else if (virtualServer instanceof VirtualServerFacade) { engine.removeChild(((VirtualServerFacade)virtualServer).getVirtualServer()); } } /** * Sets log level * * @param level */ public void setLogLevel(Level level) { log.setLevel(level); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy