Search in sources :

Example 1 with WebListener

use of org.glassfish.embeddable.web.WebListener in project Payara by payara.

the class WebContainerImpl method addVirtualServer.

/**
 * Adds the given <tt>VirtualServer</tt> to this
 * <tt>WebContainer</tt>.
 *
 * <p>If this <tt>WebContainer</tt> has already been started,
 * the given <tt>virtualServer</tt> will be started as well.
 *
 * @param virtualServer the <tt>VirtualServer</tt> to add
 *
 * @throws ConfigException if a <tt>VirtualServer</tt> with the
 * same id has already been registered with this
 * <tt>WebContainer</tt>
 * @throws GlassFishException if the given <tt>virtualServer</tt> 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<WebListener> webListeners = virtualServer.getWebListeners();
    List<String> names = new ArrayList<String>();
    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;
    }
    StringBuffer networkListeners = new StringBuffer("");
    if (names.size() > 0) {
        networkListeners.append(names.get(0));
    }
    for (int i = 1; i < names.size(); i++) {
        networkListeners.append(",");
        networkListeners.append(names.get(i));
    }
    String docRoot = null;
    if (virtualServer.getDocRoot() != null) {
        docRoot = virtualServer.getDocRoot().getAbsolutePath();
    }
    String hostName = null;
    if (virtualServer.getConfig() != null) {
        hostName = virtualServer.getConfig().getHostNames();
    }
    final String root = docRoot;
    final String nl = networkListeners.toString();
    final String id = virtualServer.getID();
    final String hosts = hostName;
    try {
        ConfigSupport.apply(new SingleConfigCode<HttpService>() {

            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));
    }
}
Also used : GlassFishException(org.glassfish.embeddable.GlassFishException) ConfigException(org.glassfish.embeddable.web.ConfigException) VirtualServerFacade(com.sun.enterprise.web.VirtualServerFacade) Property(org.jvnet.hk2.config.types.Property) VirtualServer(org.glassfish.embeddable.web.VirtualServer) PropertyVetoException(java.beans.PropertyVetoException) ConfigException(org.glassfish.embeddable.web.ConfigException) GlassFishException(org.glassfish.embeddable.GlassFishException) PropertyVetoException(java.beans.PropertyVetoException) WebListener(org.glassfish.embeddable.web.WebListener) HttpService(com.sun.enterprise.config.serverbeans.HttpService) org.jvnet.hk2.config(org.jvnet.hk2.config) NetworkListener(org.glassfish.grizzly.config.dom.NetworkListener)

Example 2 with WebListener

use of org.glassfish.embeddable.web.WebListener in project Payara by payara.

the class WebContainerImpl method setConfiguration.

// --------------------------------------------------------- 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<com.sun.enterprise.config.serverbeans.VirtualServer>() {

                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.<EmbeddedWebArchivist>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();
    }
}
Also used : WebContainerConfig(org.glassfish.embeddable.web.config.WebContainerConfig) VirtualServer(org.glassfish.embeddable.web.VirtualServer) PropertyVetoException(java.beans.PropertyVetoException) ConfigException(org.glassfish.embeddable.web.ConfigException) GlassFishException(org.glassfish.embeddable.GlassFishException) PropertyVetoException(java.beans.PropertyVetoException) WebListener(org.glassfish.embeddable.web.WebListener) StandardHost(org.apache.catalina.core.StandardHost) HttpListener(org.glassfish.embeddable.web.HttpListener) org.jvnet.hk2.config(org.jvnet.hk2.config) Property(org.jvnet.hk2.config.types.Property)

Example 3 with WebListener

use of org.glassfish.embeddable.web.WebListener in project Payara by payara.

the class WebContainerImpl method bind.

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<Protocols>() {

            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<Protocol>() {

                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<com.sun.enterprise.config.serverbeans.VirtualServer>() {

            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();
    }
}
Also used : Protocols(org.glassfish.grizzly.config.dom.Protocols) ThreadPool(org.glassfish.grizzly.config.dom.ThreadPool) Http(org.glassfish.grizzly.config.dom.Http) NetworkListeners(org.glassfish.grizzly.config.dom.NetworkListeners) HttpListener(org.glassfish.embeddable.web.HttpListener) Protocol(org.glassfish.grizzly.config.dom.Protocol) Transports(org.glassfish.grizzly.config.dom.Transports) Ssl(org.glassfish.grizzly.config.dom.Ssl) VirtualServer(org.glassfish.embeddable.web.VirtualServer) PropertyVetoException(java.beans.PropertyVetoException) ConfigException(org.glassfish.embeddable.web.ConfigException) GlassFishException(org.glassfish.embeddable.GlassFishException) PropertyVetoException(java.beans.PropertyVetoException) WebListener(org.glassfish.embeddable.web.WebListener) Transport(org.glassfish.grizzly.config.dom.Transport) org.jvnet.hk2.config(org.jvnet.hk2.config) NetworkListener(org.glassfish.grizzly.config.dom.NetworkListener)

Example 4 with WebListener

use of org.glassfish.embeddable.web.WebListener in project Payara by payara.

the class EmbeddedTest method setup.

@BeforeClass
public static void setup() throws GlassFishException {
    glassfish = GlassFishRuntime.bootstrap().newGlassFish();
    glassfish.start();
    WebContainer webcontainer = glassfish.getService(WebContainer.class);
    Collection<WebListener> listeners = webcontainer.getWebListeners();
    System.out.println("Network listener size before creation " + listeners.size());
    for (WebListener listener : listeners) {
        System.out.println("Network listener " + listener.getPort());
    }
    try {
        HttpListener listener = new HttpListener();
        listener.setPort(8080);
        listener.setId("embedded-listener-1");
        webcontainer.addWebListener(listener);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    listeners = webcontainer.getWebListeners();
    System.out.println("Network listener size after creation " + listeners.size());
    Assert.assertTrue(listeners.size() == 1);
    for (WebListener listener : listeners) {
        System.out.println("Network listener " + listener.getPort());
    }
}
Also used : WebListener(org.glassfish.embeddable.web.WebListener) WebContainer(org.glassfish.embeddable.web.WebContainer) HttpListener(org.glassfish.embeddable.web.HttpListener) GlassFishException(org.glassfish.embeddable.GlassFishException) NamingException(javax.naming.NamingException) BeforeClass(org.junit.BeforeClass)

Aggregations

GlassFishException (org.glassfish.embeddable.GlassFishException)4 WebListener (org.glassfish.embeddable.web.WebListener)4 PropertyVetoException (java.beans.PropertyVetoException)3 ConfigException (org.glassfish.embeddable.web.ConfigException)3 HttpListener (org.glassfish.embeddable.web.HttpListener)3 VirtualServer (org.glassfish.embeddable.web.VirtualServer)3 org.jvnet.hk2.config (org.jvnet.hk2.config)3 NetworkListener (org.glassfish.grizzly.config.dom.NetworkListener)2 Property (org.jvnet.hk2.config.types.Property)2 HttpService (com.sun.enterprise.config.serverbeans.HttpService)1 VirtualServerFacade (com.sun.enterprise.web.VirtualServerFacade)1 NamingException (javax.naming.NamingException)1 StandardHost (org.apache.catalina.core.StandardHost)1 WebContainer (org.glassfish.embeddable.web.WebContainer)1 WebContainerConfig (org.glassfish.embeddable.web.config.WebContainerConfig)1 Http (org.glassfish.grizzly.config.dom.Http)1 NetworkListeners (org.glassfish.grizzly.config.dom.NetworkListeners)1 Protocol (org.glassfish.grizzly.config.dom.Protocol)1 Protocols (org.glassfish.grizzly.config.dom.Protocols)1 Ssl (org.glassfish.grizzly.config.dom.Ssl)1