Search in sources :

Example 1 with NetworkListeners

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

the class CreateNetworkListener method execute.

/**
 * Executes the command with the command parameters passed as Properties where the keys are the paramter names and
 * the values the parameter values
 *
 * @param context information
 */
public void execute(AdminCommandContext context) {
    Target targetUtil = services.getService(Target.class);
    Config newConfig = targetUtil.getConfig(target);
    if (newConfig != null) {
        config = newConfig;
    }
    final ActionReport report = context.getActionReport();
    NetworkConfig networkConfig = config.getNetworkConfig();
    NetworkListeners nls = networkConfig.getNetworkListeners();
    // ensure we don't have one of this name already
    for (NetworkListener networkListener : nls.getNetworkListener()) {
        if (networkListener.getName().equals(listenerName)) {
            report.setMessage(MessageFormat.format(rb.getString(LogFacade.CREATE_NETWORK_LISTENER_FAIL_DUPLICATE), listenerName));
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
    }
    if (!verifyUniquePort(networkConfig)) {
        report.setMessage(MessageFormat.format(rb.getString(LogFacade.PORT_IN_USE), port, address));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    Protocol prot = networkConfig.findProtocol(protocol);
    if (prot == null) {
        report.setMessage(MessageFormat.format(rb.getString(LogFacade.CREATE_HTTP_FAIL_PROTOCOL_NOT_FOUND), protocol));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    if (prot.getHttp() == null && prot.getPortUnification() == null) {
        report.setMessage(MessageFormat.format(rb.getString(LogFacade.CREATE_NETWORK_LISTENER_FAIL_BAD_PROTOCOL), protocol));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    try {
        ConfigSupport.apply(new ConfigCode() {

            public Object run(ConfigBeanProxy... params) throws TransactionFailure, PropertyVetoException {
                NetworkListeners listeners = (NetworkListeners) params[0];
                NetworkListener newNetworkListener = listeners.createChild(NetworkListener.class);
                newNetworkListener.setProtocol(protocol);
                newNetworkListener.setTransport(transport);
                newNetworkListener.setEnabled(enabled.toString());
                newNetworkListener.setJkEnabled(jkEnabled.toString());
                newNetworkListener.setPort(port);
                newNetworkListener.setThreadPool(threadPool);
                newNetworkListener.setName(listenerName);
                newNetworkListener.setAddress(address);
                listeners.getNetworkListener().add(newNetworkListener);
                ((VirtualServer) params[1]).addNetworkListener(listenerName);
                return newNetworkListener;
            }
        }, nls, findVirtualServer(prot));
    } catch (TransactionFailure e) {
        e.printStackTrace();
        report.setMessage(MessageFormat.format(rb.getString(LogFacade.CREATE_NETWORK_LISTENER_FAIL), listenerName) + (e.getMessage() == null ? "No reason given" : e.getMessage()));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        report.setFailureCause(e);
        return;
    }
    report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) PropertyVetoException(java.beans.PropertyVetoException) Target(org.glassfish.internal.api.Target) CommandTarget(org.glassfish.config.support.CommandTarget) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) ConfigCode(org.jvnet.hk2.config.ConfigCode) Config(com.sun.enterprise.config.serverbeans.Config) NetworkConfig(org.glassfish.grizzly.config.dom.NetworkConfig) NetworkConfig(org.glassfish.grizzly.config.dom.NetworkConfig) NetworkListeners(org.glassfish.grizzly.config.dom.NetworkListeners) ActionReport(org.glassfish.api.ActionReport) Protocol(org.glassfish.grizzly.config.dom.Protocol) NetworkListener(org.glassfish.grizzly.config.dom.NetworkListener)

Example 2 with NetworkListeners

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

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

the class ParentConfigListenerTest method addHttpListenerTest.

@Test
public void addHttpListenerTest() throws TransactionFailure {
    NetworkListenersContainer container = habitat.getService(NetworkListenersContainer.class);
    ConfigSupport.apply(new SingleConfigCode<NetworkListeners>() {

        public Object run(NetworkListeners param) throws TransactionFailure {
            NetworkListener newListener = param.createChild(NetworkListener.class);
            newListener.setName("Funky-Listener");
            newListener.setPort("8078");
            param.getNetworkListener().add(newListener);
            return null;
        }
    }, container.httpService);
    getHabitat().<Transactions>getService(Transactions.class).waitForDrain();
    assertTrue(container.received);
    ObservableBean bean = (ObservableBean) ConfigSupport.getImpl(container.httpService);
    // let's check that my newly added listener is available in the habitat.
    List<ServiceHandle<NetworkListener>> networkListeners = habitat.getAllServiceHandles(NetworkListener.class);
    boolean found = false;
    for (ServiceHandle<NetworkListener> nlSH : networkListeners) {
        NetworkListener nl = (NetworkListener) nlSH.getService();
        if (nl.getName().equals("Funky-Listener")) {
            found = true;
        }
    }
    Assert.assertTrue("Newly added listener not found", found);
    // direct access.
    NetworkListener nl = habitat.getService(NetworkListener.class, "Funky-Listener");
    Assert.assertTrue("Direct access to newly added listener failed", nl != null);
    bean.removeListener(container);
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) Transactions(org.jvnet.hk2.config.Transactions) ServiceHandle(org.glassfish.hk2.api.ServiceHandle) NetworkListeners(org.glassfish.grizzly.config.dom.NetworkListeners) ObservableBean(org.jvnet.hk2.config.ObservableBean) NetworkListener(org.glassfish.grizzly.config.dom.NetworkListener) Test(org.junit.Test)

Example 4 with NetworkListeners

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

the class ParentTest method parents.

@Test
public void parents() {
    NetworkListeners service = getHabitat().getService(NetworkListeners.class);
    assertNotNull(service);
    NetworkListener listener = service.getNetworkListener().get(0);
    assertNotNull(listener);
    ConfigBeanProxy parent = service.getParent();
    assertNotNull(parent);
    NetworkListeners myService = listener.getParent(NetworkListeners.class);
    assertNotNull(myService);
    assertNotNull(myService.getNetworkListener().get(0).getName());
}
Also used : ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) NetworkListeners(org.glassfish.grizzly.config.dom.NetworkListeners) NetworkListener(org.glassfish.grizzly.config.dom.NetworkListener) Test(org.junit.Test)

Example 5 with NetworkListeners

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

the class TransactionCallBackTest method doTest.

public void doTest() throws TransactionFailure {
    ConfigBean serviceBean = (ConfigBean) ConfigBean.unwrap(habitat.<NetworkListeners>getService(NetworkListeners.class));
    Map<String, String> configChanges = new HashMap<String, String>();
    configChanges.put("name", "funky-listener");
    ConfigSupport.createAndSet(serviceBean, NetworkListener.class, configChanges, new TransactionCallBack<WriteableView>() {

        @SuppressWarnings({ "unchecked" })
        public void performOn(WriteableView param) throws TransactionFailure {
            // if you know the type...
            NetworkListener listener = param.getProxy(NetworkListener.class);
            listener.setName("Aleksey");
            // if you don't know the type
            Method m;
            try {
                m = param.getProxyType().getMethod("setAddress", String.class);
                m.invoke(param.getProxy(param.getProxyType()), "localhost");
            } catch (NoSuchMethodException e) {
                throw new TransactionFailure("Cannot find getProperty method", e);
            } catch (IllegalAccessException e) {
                throw new TransactionFailure("Cannot call getProperty method", e);
            } catch (InvocationTargetException e) {
                throw new TransactionFailure("Cannot call getProperty method", e);
            }
        }
    });
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) HashMap(java.util.HashMap) ConfigBean(org.jvnet.hk2.config.ConfigBean) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) WriteableView(org.jvnet.hk2.config.WriteableView) NetworkListeners(org.glassfish.grizzly.config.dom.NetworkListeners) NetworkListener(org.glassfish.grizzly.config.dom.NetworkListener)

Aggregations

NetworkListeners (org.glassfish.grizzly.config.dom.NetworkListeners)16 NetworkListener (org.glassfish.grizzly.config.dom.NetworkListener)12 NetworkConfig (org.glassfish.grizzly.config.dom.NetworkConfig)5 Protocol (org.glassfish.grizzly.config.dom.Protocol)5 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)5 Config (com.sun.enterprise.config.serverbeans.Config)4 PropertyVetoException (java.beans.PropertyVetoException)4 Protocols (org.glassfish.grizzly.config.dom.Protocols)3 Test (org.junit.Test)3 ConfigBeanProxy (org.jvnet.hk2.config.ConfigBeanProxy)3 ObservableBean (org.jvnet.hk2.config.ObservableBean)3 ActionReport (org.glassfish.api.ActionReport)2 CommandTarget (org.glassfish.config.support.CommandTarget)2 VirtualServer (org.glassfish.embeddable.web.VirtualServer)2 ThreadPool (org.glassfish.grizzly.config.dom.ThreadPool)2 Transport (org.glassfish.grizzly.config.dom.Transport)2 Target (org.glassfish.internal.api.Target)2 org.jvnet.hk2.config (org.jvnet.hk2.config)2 ConfigBean (org.jvnet.hk2.config.ConfigBean)2 ConfigCode (org.jvnet.hk2.config.ConfigCode)2