Search in sources :

Example 21 with NetworkListener

use of org.glassfish.grizzly.config.dom.NetworkListener in project Payara by payara.

the class EmbeddedTest method setup.

@BeforeClass
public static void setup() {
    Server.Builder builder = new Server.Builder("build");
    server = builder.build();
    NetworkConfig nc = server.getHabitat().getService(NetworkConfig.class, ServerEnvironment.DEFAULT_INSTANCE_NAME);
    List<NetworkListener> listeners = nc.getNetworkListeners().getNetworkListener();
    System.out.println("Network listener size before creation " + listeners.size());
    for (NetworkListener nl : listeners) {
        System.out.println("Network listener " + nl.getPort());
    }
    try {
        http = server.createPort(8080);
        ContainerBuilder b = server.createConfig(ContainerBuilder.Type.web);
        server.addContainer(b);
        EmbeddedWebContainer embedded = (EmbeddedWebContainer) b.create(server);
        embedded.bind(http, "http");
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    listeners = nc.getNetworkListeners().getNetworkListener();
    System.out.println("Network listener size after creation " + listeners.size());
    Assert.assertTrue(listeners.size() == 1);
    for (NetworkListener nl : listeners) {
        System.out.println("Network listener " + nl.getPort());
    }
    Collection<NetworkListener> cnl = server.getHabitat().getAllServices(NetworkListener.class);
    System.out.println("Network listener size after creation " + cnl.size());
    for (NetworkListener nl : cnl) {
        System.out.println("Network listener " + nl.getPort());
    }
    server.addContainer(ContainerBuilder.Type.all);
}
Also used : ContainerBuilder(org.glassfish.internal.embedded.ContainerBuilder) Server(org.glassfish.internal.embedded.Server) EmbeddedWebContainer(org.glassfish.api.embedded.web.EmbeddedWebContainer) ContainerBuilder(org.glassfish.internal.embedded.ContainerBuilder) Builder(org.glassfish.internal.embedded.ScatteredArchive.Builder) NetworkConfig(org.glassfish.grizzly.config.dom.NetworkConfig) IOException(java.io.IOException) NetworkListener(org.glassfish.grizzly.config.dom.NetworkListener) BeforeClass(org.junit.BeforeClass)

Example 22 with NetworkListener

use of org.glassfish.grizzly.config.dom.NetworkListener in project Payara by payara.

the class ExistingConfigurationTest method setupServer.

@Test
public void setupServer() throws Exception {
    Server server = null;
    System.out.println("setup started with gf installation " + System.getProperty("basedir"));
    File f = new File(System.getProperty("basedir"));
    f = new File(f, "target");
    f = new File(f, "dependency");
    f = new File(f, "glassfish4");
    f = new File(f, "glassfish");
    if (f.exists()) {
        System.out.println("Using gf at " + f.getAbsolutePath());
    } else {
        System.out.println("GlassFish not found at " + f.getAbsolutePath());
        Assert.assertTrue(f.exists());
    }
    try {
        EmbeddedFileSystem.Builder efsb = new EmbeddedFileSystem.Builder();
        efsb.installRoot(f, false);
        // find the domain root.
        f = new File(f, "domains");
        f = new File(f, "domain1");
        f = new File(f, "config");
        f = new File(f, "domain.xml");
        Assert.assertTrue(f.exists());
        efsb.configurationFile(f);
        Server.Builder builder = new Server.Builder("inplanted");
        builder.embeddedFileSystem(efsb.build());
        server = builder.build();
        ServiceLocator habitat = server.getHabitat();
        Collection<VirtualServer> vss = habitat.getAllServices(VirtualServer.class);
        Assert.assertTrue(vss.size() > 0);
        for (VirtualServer vs : vss) {
            System.out.println("Virtual Server " + vs.getId());
        }
        Collection<NetworkListener> nls = habitat.getAllServices(NetworkListener.class);
        for (NetworkListener nl : nls) {
            System.out.println("Network listener " + nl.getPort());
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        if (server != null) {
            server.stop();
        }
    }
}
Also used : ServiceLocator(org.glassfish.hk2.api.ServiceLocator) EmbeddedFileSystem(org.glassfish.internal.embedded.EmbeddedFileSystem) Server(org.glassfish.internal.embedded.Server) VirtualServer(com.sun.enterprise.config.serverbeans.VirtualServer) File(java.io.File) VirtualServer(com.sun.enterprise.config.serverbeans.VirtualServer) NetworkListener(org.glassfish.grizzly.config.dom.NetworkListener) Test(org.junit.Test)

Example 23 with NetworkListener

use of org.glassfish.grizzly.config.dom.NetworkListener in project Payara by payara.

the class WsUtil method getWebServerInfoForDAS.

public WebServerInfo getWebServerInfoForDAS() {
    WebServerInfo wsi = new WebServerInfo();
    if (this.networkListeners == null) {
        List<Integer> adminPorts = new ArrayList<Integer>();
        for (org.glassfish.api.container.Adapter subAdapter : WebServiceContractImpl.getInstance().getAdapters()) {
            if (subAdapter instanceof AdminAdapter) {
                AdminAdapter aa = (AdminAdapter) subAdapter;
                adminPorts.add(aa.getListenPort());
            } else if (subAdapter instanceof AdminConsoleAdapter) {
                AdminConsoleAdapter aca = (AdminConsoleAdapter) subAdapter;
                adminPorts.add(aca.getListenPort());
            }
        }
        for (NetworkListener nl : config.getNetworkConfig().getNetworkListeners().getNetworkListener()) {
            if (!adminPorts.contains(Integer.valueOf(nl.getPort()))) {
                // get rid of admin ports
                if (networkListeners == null)
                    networkListeners = new ArrayList<NetworkListener>();
                networkListeners.add(nl);
            }
        }
    }
    // Fix for issue 13107490
    if ((networkListeners != null) && (!networkListeners.isEmpty())) {
        for (NetworkListener listener : networkListeners) {
            String host = listener.getAddress();
            if (listener.getAddress().equals("0.0.0.0"))
                try {
                    host = InetAddress.getLocalHost().getHostName();
                } catch (UnknownHostException e) {
                    // fallback
                    host = "localhost";
                }
            if (listener.findHttpProtocol().getSecurityEnabled().equals("false"))
                wsi.setHttpVS(new VirtualServerInfo("http", host, Integer.parseInt(listener.getPort())));
            else if (listener.findHttpProtocol().getSecurityEnabled().equals("true"))
                wsi.setHttpsVS(new VirtualServerInfo("https", host, Integer.parseInt(listener.getPort())));
        }
    } else {
        wsi.setHttpVS(new VirtualServerInfo("http", "localhost", 0));
        wsi.setHttpsVS(new VirtualServerInfo("https", "localhost", 0));
    }
    return wsi;
}
Also used : VirtualServerInfo(org.glassfish.web.deployment.util.VirtualServerInfo) WebServerInfo(org.glassfish.web.deployment.util.WebServerInfo) AdminConsoleAdapter(com.sun.enterprise.v3.admin.adapter.AdminConsoleAdapter) AdminAdapter(com.sun.enterprise.v3.admin.AdminAdapter) NetworkListener(org.glassfish.grizzly.config.dom.NetworkListener)

Example 24 with NetworkListener

use of org.glassfish.grizzly.config.dom.NetworkListener 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 25 with NetworkListener

use of org.glassfish.grizzly.config.dom.NetworkListener 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)

Aggregations

NetworkListener (org.glassfish.grizzly.config.dom.NetworkListener)74 NetworkConfig (org.glassfish.grizzly.config.dom.NetworkConfig)21 Protocol (org.glassfish.grizzly.config.dom.Protocol)18 Config (com.sun.enterprise.config.serverbeans.Config)17 NetworkListeners (org.glassfish.grizzly.config.dom.NetworkListeners)14 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)12 Test (org.junit.Test)11 ActionReport (org.glassfish.api.ActionReport)10 CommandTarget (org.glassfish.config.support.CommandTarget)9 Target (org.glassfish.internal.api.Target)9 VirtualServer (com.sun.enterprise.config.serverbeans.VirtualServer)8 HttpService (com.sun.enterprise.config.serverbeans.HttpService)7 ArrayList (java.util.ArrayList)7 Protocols (org.glassfish.grizzly.config.dom.Protocols)7 ObservableBean (org.jvnet.hk2.config.ObservableBean)7 Transactions (org.jvnet.hk2.config.Transactions)7 IOException (java.io.IOException)6 Http (org.glassfish.grizzly.config.dom.Http)6 PropertyVetoException (java.beans.PropertyVetoException)5 ThreadPool (org.glassfish.grizzly.config.dom.ThreadPool)5