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

org.ow2.jonas.ant.jonasbase.WebContainer Maven / Gradle / Ivy

There is a newer version: 5.3.0
Show newest version
/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2004-2008 Bull S.A.S.
 * Contact: [email protected]
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id: WebContainer.java 15428 2008-10-07 11:20:29Z sauthieg $
 * --------------------------------------------------------------------------
 */

package org.ow2.jonas.ant.jonasbase;

import org.ow2.jonas.ant.JOnASBaseTask;

import org.apache.tools.ant.Task;

import org.ow2.jonas.ant.jonasbase.web.Jetty;
import org.ow2.jonas.ant.jonasbase.web.Tomcat;

/**
 * Allow to configure the WebContainer service.
 * @author Florent Benoit
 */
public class WebContainer extends Tasks {

    /**
     * Info for the logger.
     */
    private static final String INFO = "[WebContainer] ";

    /**
     * Name of the implementation class for Jetty.
     */
    private static final String JETTY_SERVICE = "org.ow2.jonas.web.jetty6.Jetty6Service";

    /**
     * Name of the implementation class for Tomcat.
     */
    private static final String TOMCAT_SERVICE = "org.ow2.jonas.web.tomcat6.Tomcat6Service";

    /**
     * Name of the implementation class for the default service (tomcat).
     */
    private static final String DEFAULT_SERVICE = TOMCAT_SERVICE;

    /**
     * Default port number.
     */
    private static final String DEFAULT_PORT = "9000";

    /**
     * is serviceName set ?
     */
    private boolean serviceNameSet = false;

    /**
     * Default constructor.
     */
    public WebContainer() {
        super();
    }

    /**
     * Set the port number for the WebContainer.
     * Optionnal, if missing, we look at the inner element <jetty>/<http> or <tomcat>/<http>.
     * It overrides both <jetty>/<http> or <tomcat>/<http> values if set.
     * @param portNumber the port for the HTTP web Container
     */
    public void setPort(final String portNumber) {

        // For tomcat
        JReplace propertyReplace = new JReplace();
        propertyReplace.setConfigurationFile(JOnASBaseTask.TOMCAT_CONF_FILE);
        propertyReplace.setToken(DEFAULT_PORT);
        propertyReplace.setValue(portNumber);
        propertyReplace.setLogInfo(INFO + "Setting Tomcat port number to : " + portNumber);
        addTask(propertyReplace);

        // For Jetty
        propertyReplace = new JReplace();
        propertyReplace.setConfigurationFile(JOnASBaseTask.JETTY_CONF_FILE);
        propertyReplace.setToken(DEFAULT_PORT);
        propertyReplace.setValue(portNumber);
        propertyReplace.setLogInfo(INFO + "Setting Jetty port number to : " + portNumber);
        addTask(propertyReplace);
    }


    /**
     * Set the name of the web container : jetty or tomcat.
     * Optionnal, if missing, we look at the inner element <jetty> or <tomcat>.
     * This setting is priority 1 if both <jetty> or <tomcat> are set.
     * @param containerName jetty or tomcat.
     */
    public void setName(final String containerName) {

        if ("jetty".equalsIgnoreCase(containerName)) {
            addTask(createServiceNameReplace(JETTY_SERVICE));
            serviceNameSet = true;
        } else if ("tomcat".equalsIgnoreCase(containerName)) {
            addTask(createServiceNameReplace(TOMCAT_SERVICE));
            serviceNameSet = true;
        }
    }

    /**
     * Create a JReplace Task for changing sdervice classname in jonas.properties.
     * @param serviceName service classname to use.
     * @return Returns a JReplace Task.
     */
    private Task createServiceNameReplace(final String serviceName) {
        // Replace the service in jonas.properties file
        JReplace propertyReplace = new JReplace();
        propertyReplace.setConfigurationFile(JOnASBaseTask.JONAS_CONF_FILE);
        propertyReplace.setToken(DEFAULT_SERVICE);
        propertyReplace.setValue(serviceName);
        propertyReplace.setLogInfo(INFO + "Setting service to : " + serviceName);
        return propertyReplace;
    }

    /**
     * Configure the Jetty WebContainer.
     * @param jetty Jetty Configuration.
     */
    public void addConfiguredJetty(final Jetty jetty) {
        if (!serviceNameSet) {
            addTask(createServiceNameReplace(JETTY_SERVICE));
            serviceNameSet = true;
        }
        addTasks(jetty);
    }

    /**
     * Configure the Tomcat WebContainer.
     * @param tomcat Tomcat Configuration.
     */
    public void addConfiguredTomcat(final Tomcat tomcat) {
        if (!serviceNameSet) {
            addTask(createServiceNameReplace(TOMCAT_SERVICE));
            serviceNameSet = true;
        }
        addTasks(tomcat);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy