Search in sources :

Example 1 with ConfigCode

use of org.jvnet.hk2.config.ConfigCode 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 2 with ConfigCode

use of org.jvnet.hk2.config.ConfigCode in project Payara by payara.

the class UpdateResourceRef method execute.

/**
 * Execution method for updating the configuration of a ResourceRef. Will be
 * replicated if the target is a cluster.
 *
 * @param context context for the command.
 */
@Override
public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    final Logger logger = context.getLogger();
    // Make a list of all ResourceRefs that need to change
    List<ResourceRef> resourceRefsToChange = new ArrayList<>();
    // Add the ResourceRef from a named server if the target is a server
    Server server = domain.getServerNamed(target);
    // if the target is a server
    if (server != null) {
        ResourceRef serverResourceRef = server.getResourceRef(name);
        // if the ResourceRef doesn't exist
        if (serverResourceRef == null) {
            report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
            return;
        }
        resourceRefsToChange.add(serverResourceRef);
    }
    // Add the ResourceRef from a named config if the target is a config
    Config config = domain.getConfigNamed(target);
    // if the target is a config
    if (config != null) {
        ResourceRef configResourceRef = config.getResourceRef(name);
        // if the ResourceRef doesn't exist
        if (configResourceRef == null) {
            report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
            return;
        }
        resourceRefsToChange.add(configResourceRef);
    }
    // Add the ResourceRefs from a named cluster if the target is a cluster
    Cluster cluster = domain.getClusterNamed(target);
    // if the target is a cluster
    if (cluster != null) {
        ResourceRef clusterResourceRef = cluster.getResourceRef(name);
        // if the ResourceRef doesn't exist
        if (clusterResourceRef == null) {
            report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
            return;
        }
        resourceRefsToChange.add(clusterResourceRef);
        for (Server instance : cluster.getInstances()) {
            ResourceRef instanceResourceRef = instance.getResourceRef(name);
            // if the server in the cluster contains the ResourceRef
            if (instanceResourceRef != null) {
                resourceRefsToChange.add(instanceResourceRef);
            }
        }
    }
    // Add the ResourceRefs from a named Deployment Group if the target is a Deployment Group
    DeploymentGroup dg = domain.getDeploymentGroupNamed(target);
    if (dg != null) {
        ResourceRef ref = dg.getResourceRef(name);
        if (ref == null) {
            report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
            return;
        }
        resourceRefsToChange.add(ref);
        for (Server instance : dg.getInstances()) {
            ResourceRef instanceResourceRef = instance.getResourceRef(name);
            // if the server in the dg contains the ResourceRef
            if (instanceResourceRef != null) {
                resourceRefsToChange.add(instanceResourceRef);
            }
        }
    }
    // Apply the configuration to the listed ResourceRefs
    try {
        ConfigSupport.apply(new ConfigCode() {

            @Override
            public Object run(ConfigBeanProxy... params) throws PropertyVetoException, TransactionFailure {
                for (ConfigBeanProxy proxy : params) {
                    if (proxy instanceof ResourceRef) {
                        ResourceRef resourceRefProxy = (ResourceRef) proxy;
                        if (enabled != null) {
                            resourceRefProxy.setEnabled(enabled.toString());
                        }
                    }
                }
                report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
                return true;
            }
        }, resourceRefsToChange.toArray(new ResourceRef[] {}));
    } catch (TransactionFailure ex) {
        report.failure(logger, ex.getLocalizedMessage());
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) Server(com.sun.enterprise.config.serverbeans.Server) Config(com.sun.enterprise.config.serverbeans.Config) ArrayList(java.util.ArrayList) Cluster(com.sun.enterprise.config.serverbeans.Cluster) ActionReport(org.glassfish.api.ActionReport) Logger(java.util.logging.Logger) PropertyVetoException(java.beans.PropertyVetoException) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) ConfigCode(org.jvnet.hk2.config.ConfigCode) ResourceRef(com.sun.enterprise.config.serverbeans.ResourceRef) DeploymentGroup(fish.payara.enterprise.config.serverbeans.DeploymentGroup)

Example 3 with ConfigCode

use of org.jvnet.hk2.config.ConfigCode 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);
                if (portRange != null) {
                    newNetworkListener.setPortRange(portRange);
                }
                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 4 with ConfigCode

use of org.jvnet.hk2.config.ConfigCode in project Payara by payara.

the class RestMonitoringLoader method registerApplication.

private void registerApplication() throws Exception {
    LOGGER.log(Level.FINE, "Registering the Rest Monitoring Application...");
    // Create the application-ref entry in the domain.xml
    ConfigCode code = new ConfigCode() {

        @Override
        public Object run(ConfigBeanProxy... proxies) throws PropertyVetoException, TransactionFailure {
            // Get the system application config
            SystemApplications systemApplications = (SystemApplications) proxies[0];
            Application application = null;
            for (Application systemApplication : systemApplications.getApplications()) {
                if (systemApplication.getName().equals(applicationName)) {
                    application = systemApplication;
                    break;
                }
            }
            if (application == null) {
                throw new IllegalStateException("The Rest Monitoring application has no system app entry!");
            }
            // Create the application-ref
            Server s = (Server) proxies[1];
            List<ApplicationRef> arefs = s.getApplicationRef();
            ApplicationRef aref = s.createChild(ApplicationRef.class);
            aref.setRef(application.getName());
            aref.setEnabled(Boolean.TRUE.toString());
            aref.setVirtualServers(getVirtualServerListAsString());
            arefs.add(aref);
            return true;
        }
    };
    Server server = domain.getServerNamed(serverEnv.getInstanceName());
    ConfigSupport.apply(code, domain.getSystemApplications(), server);
    // Update the adapter state
    LOGGER.log(Level.FINE, "Rest Monitoring Application Registered.");
}
Also used : ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) Server(com.sun.enterprise.config.serverbeans.Server) ConfigCode(org.jvnet.hk2.config.ConfigCode) SystemApplications(com.sun.enterprise.config.serverbeans.SystemApplications) Application(com.sun.enterprise.config.serverbeans.Application) ApplicationRef(com.sun.enterprise.config.serverbeans.ApplicationRef)

Example 5 with ConfigCode

use of org.jvnet.hk2.config.ConfigCode in project Payara by payara.

the class RestMonitoringLoader method reconfigureSystemApplication.

private void reconfigureSystemApplication() throws Exception {
    Application systemApplication = restMonitoringAdapter.getSystemApplicationConfig();
    LOGGER.log(Level.FINE, "Reconfiguring the Rest Monitoring Application...");
    // Reconfigure the system-application entry in the domain.xml
    ConfigCode code = new ConfigCode() {

        @Override
        public Object run(ConfigBeanProxy... proxies) throws PropertyVetoException, TransactionFailure {
            Application systemApplication = (Application) proxies[0];
            systemApplication.setContextRoot(contextRoot);
            return true;
        }
    };
    ConfigSupport.apply(code, systemApplication);
    LOGGER.log(Level.FINE, "Rest Monitoring Application Reconfigured.");
}
Also used : ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) ConfigCode(org.jvnet.hk2.config.ConfigCode) Application(com.sun.enterprise.config.serverbeans.Application)

Aggregations

ConfigBeanProxy (org.jvnet.hk2.config.ConfigBeanProxy)10 ConfigCode (org.jvnet.hk2.config.ConfigCode)10 PropertyVetoException (java.beans.PropertyVetoException)9 ActionReport (org.glassfish.api.ActionReport)5 Protocol (org.glassfish.grizzly.config.dom.Protocol)5 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)5 Config (com.sun.enterprise.config.serverbeans.Config)4 Server (com.sun.enterprise.config.serverbeans.Server)4 ArrayList (java.util.ArrayList)4 NetworkListeners (org.glassfish.grizzly.config.dom.NetworkListeners)4 Application (com.sun.enterprise.config.serverbeans.Application)3 ApplicationRef (com.sun.enterprise.config.serverbeans.ApplicationRef)3 CommandTarget (org.glassfish.config.support.CommandTarget)3 NetworkListener (org.glassfish.grizzly.config.dom.NetworkListener)3 Protocols (org.glassfish.grizzly.config.dom.Protocols)3 Target (org.glassfish.internal.api.Target)3 Cluster (com.sun.enterprise.config.serverbeans.Cluster)2 Module (com.sun.enterprise.config.serverbeans.Module)2 SystemApplications (com.sun.enterprise.config.serverbeans.SystemApplications)2 DeploymentGroup (fish.payara.enterprise.config.serverbeans.DeploymentGroup)2