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

org.objectweb.jonas.ant.cluster.Script Maven / Gradle / Ivy

The newest version!
/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2006 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
 *
 * Initial developer: Benoit Pelletier
 * --------------------------------------------------------------------------
 * $Id: Script.java 9720 2006-10-11 09:46:58Z pelletib $
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.ant.cluster;

import java.io.File;


//import org.apache.tools.ant.taskdefs.Chmod;
import org.objectweb.common.Env;
import org.objectweb.jonas.ant.jonasbase.JEcho;
import org.objectweb.jonas.ant.jonasbase.JMkdir;
import org.objectweb.jonas.ant.jonasbase.JTouch;

/**
 * Allow to launch the cluster nodes
 * @author Benoit Pelletier
 */
public class Script extends ClusterTasks {

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

    /**
     * Name of the unix script file
     */
    private static final String UNIX_SCRIPT_FILE = "jcl4sc2";

    /**
     * Name of the unix script file
     */
    private static final String WIN_SCRIPT_FILE = "jcl4sc2.bat";

    /**
     * Script file Unix
     */
    private File unixScriptFile = null;

    /**
     * Script file
     */
    private File winScriptFile = null;


    /**
     * domain name
     */
    private String domainName = null;

    /**
     * node name prefix
     */
    private String nodeNamePrefix = null;

    /**
     * script dir
     */
    private String scriptDir = null;

    /**
     * nodes nb
     */
    private int instNb = 0;

    /**
     * cluster daemon directory
     */
    private String cdDir = null;

    /**
     * db directory
     */
    private String dbDir = null;

    /**
     * db instance name
     */
    private String dbName = null;

    /**
     * master directory
     */
    private String masterDir = null;

    /**
     * master instance name
     */
    private String masterName = null;


    /**
     * Default constructor
     */
    public Script() {
        super();
    }

    /**
     * Creation of the script files
     * @param destDir destination directory
     */
    public void createFiles(String destDir) {
        JMkdir mkdir = new JMkdir();
        mkdir.setDestDir(new File(destDir));
        addTask(mkdir);

        // depending the os, the unix or the windows script is created
        if (Env.isOsWindows()) {
            JTouch touchWin = new JTouch();
            winScriptFile = new File(destDir + "/" + WIN_SCRIPT_FILE);
            touchWin.setDestDir(winScriptFile);
            addTask(touchWin);

        } else {
            JTouch touchUnix = new JTouch();
            unixScriptFile = new File(destDir + "/" + UNIX_SCRIPT_FILE);
            touchUnix.setDestDir(unixScriptFile);
            addTask(touchUnix);

        }
   }

    /**
     * creation of the unix file
     */
    private void flushUnixScriptFile() {
        JEcho echo = new JEcho();
        echo.setDestDir(unixScriptFile);

        String contentFile = "\n"
            +
            "# -----------------------------------------------------------" + "\n"
            +
            "# start/stop/kill the cluster nodes" + "\n"
            +
            "# Note : " + "\n"
            +
            "#   - JONAS_ROOT has to be set" + "\n"
            +
            "#   - XWINDOWS can be set to 'yes' for starting the process in xterm" + "\n"
            +
            "# -----------------------------------------------------------" + "\n"
            +
            "\n"
            +
            "JCL_NUMBER_OF_NODES="
            +
            this.instNb + "\n"
            +
            "\n"
            +
            "JCL_CLUSTER_DAEMON_DIR="
            +
            this.cdDir + "\n"
            +
            "JCL_BASE_PREFIX="
            +
            this.getDestDirPrefix() + "\n"
            +
            "\n"
            +
            "JCL_NODE_NAME_PREFIX="
            +
            this.nodeNamePrefix + "\n"
            +
            "JCL_DOMAIN_NAME="
            +
            this.domainName + "\n"
            +
            "\n"
            +
            "JCL_EAR_FILE=$JONAS_ROOT/examples/sampleCluster2/output/apps/sampleCluster2.ear" + "\n"
            +
            "JCL_HA_EAR_FILE=$JONAS_ROOT/examples/sampleCluster2/output/apps/haTransactions.ear" + "\n"
            +
            "\n"
            +
            "JCL_DB_DIR="
            +
            this.dbDir + "\n"
            +
            "JCL_DB_NAME="
            +
            this.dbName + "\n"
            +
            "\n"
            +
            "JCL_MASTER_DIR="
            +
            this.masterDir + "\n"
            +
            "JCL_MASTER_NAME="
            +
            this.masterName + "\n"
            +
            "\n"
            +
            "export JCL_MASTER_NAME JCL_MASTER_DIR JCL_DB_NAME JCL_DB_DIR JCL_HA_EAR_FILE JCL_EAR_FILE JCL_DOMAIN_NAME JCL_NODE_NAME_PREFIX"
            +
            "\n"
            +
            "export JCL_NUMBER_OF_NODES JCL_CLUSTER_DAEMON_DIR JCL_BASE_PREFIX"
            +
            "\n"
            +
            "$JONAS_ROOT/examples/sampleCluster2/scripts/jcl.sh $*" + "\n";

        echo.setMessage(contentFile);
        echo.setLogInfo(INFO + "Flushing Configuration in '" + unixScriptFile + "'");
        addTask(echo);
    }


    /**
     * creation of the windows file
     */
    private void flushWinScriptFile() {
        JEcho echo = new JEcho();
        echo.setDestDir(winScriptFile);

        String contentFile = "\n"
            +
            "@echo off" + "\n"
            +
            ":: -----------------------" + "\n"
            +
            ":: start/stop the cluster nodes" + "\n"
            +
            ":: Note : JONAS_ROOT has to be set" + "\n"
            +
            ":: -----------------------" + "\n"
            +
            "set JCL_NUMBER_OF_NODES="
            +
            this.instNb + "\n"
            +
            "set JCL_BASE_PREFIX="
            +
            this.getDestDirPrefix().replace('/', '\\') + "\n"
            +
            "set JCL_DOMAIN_NAME="
            +
            this.domainName + "\n"
            +
            "set JCL_EAR_FILE=%JONAS_ROOT%\\examples\\sampleCluster2\\output\\apps\\sampleCluster2.ear" + "\n"
            +
            "set JCL_HA_EAR_FILE=%JONAS_ROOT%\\examples\\sampleCluster2\\output\\apps\\haTransactions.ear" + "\n"
            +
            "set JCL_NODE_NAME_PREFIX="
            +
            this.nodeNamePrefix + "\n"
            +
            "set JCL_CLUSTER_DAEMON_DIR="
            +
            this.cdDir + "\n"
            +
            "set JCL_DB_DIR="
            +
            this.dbDir + "\n"
            +
            "set JCL_DB_NAME="
            +
            this.dbName + "\n"
            +
            "set JCL_MASTER_DIR="
            +
            this.masterDir + "\n"
            +
            "set JCL_MASTER_NAME="
            +
            this.masterName + "\n"
            +
            "%JONAS_ROOT%\\examples\\sampleCluster2\\scripts\\jcl.bat %*" + "\n";


        echo.setMessage(contentFile);
        echo.setLogInfo(INFO + "Flushing Configuration in '" + winScriptFile + "'");
        addTask(echo);
    }

    /**
     * Generation of the config files
     */
    public void flushFiles() {
        flushUnixScriptFile();
        flushWinScriptFile();
    }

    /**
     * Set domainName
     * @param domainName domain name
     */
    public void setDomainName(String domainName) {
        this.domainName = domainName;
    }

    /**
     * Set nodeNamePrefix
     * @param nodeNamePrefix node name prefix
     */
    public void setNodeNamePrefix(String nodeNamePrefix) {
        this.nodeNamePrefix = nodeNamePrefix;
    }


    /**
     * Set script directory
     * @param scriptDir Script directory
     */
    public void setScriptDir(String scriptDir) {
        this.scriptDir = scriptDir;
    }

    /**
     * Set nodes number
     * @param instNb nodes nb
     */
    public void setInstNb(int instNb) {
        this.instNb = instNb;
    }

    /**
     * cluster daemon directory
     * @param cdDir directory
     */
    public void setCdDir(String cdDir) {
        this.cdDir = cdDir;
        if (File.separatorChar == '/') {
            this.cdDir = this.cdDir.replace('\\', File.separatorChar);
        } else {
            this.cdDir = this.cdDir.replace('/', File.separatorChar);
        }
     }

    /**
     * db instance directory
     * @param dbDir directory
     */
    public void setDbDir(String dbDir) {
        this.dbDir = dbDir;
        if (File.separatorChar == '/') {
            this.dbDir = this.dbDir.replace('\\', File.separatorChar);
        } else {
            this.dbDir = this.dbDir.replace('/', File.separatorChar);
        }
     }

    /**
     * db instance name
     * @param dbName name
     */
    public void setDbName(String dbName) {
        this.dbName = dbName;
     }

    /**
     * master instance directory
     * @param dbDir directory
     */
    public void setMasterDir(String masterDir) {
        this.masterDir = masterDir;
        if (File.separatorChar == '/') {
            this.masterDir = this.masterDir.replace('\\', File.separatorChar);
        } else {
            this.masterDir = this.masterDir.replace('/', File.separatorChar);
        }
     }

    /**
     * master instance name
     * @param dbName name
     */
    public void setMasterName(String masterName) {
        this.masterName = masterName;
     }


    /**
     * Generates the script tasks
     */
    public void generatesTasks() {

        // creation of the Script tasks
        createFiles(scriptDir);
        flushFiles();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy