<?xml version="1.0" encoding="UTF-8"?>
<!--
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 - JOnAS: Java(TM) Open Application Server
 - Copyright (C) 2005-2009 Bull S.A.S.
 - Contact: jonas-team@ow2.org
 -
 - 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(s): Benoit PELLETIER
 -
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 - $Id$
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 -->

<!--
 -  build.xml file for creating a new JOnAS's cluster configuration
 -->

<project name="JOnAS_BASE tools"
         default="create_jonas_cluster"
         basedir="."
         xmlns:jant="http://jonas.objectweb.org/ant">

  <!-- Define some properties -->
  <target name="init_0">

    <!-- properties -->
    <property environment="myenv" />
    <property name="jonas.root" value="${myenv.JONAS_ROOT}" />
    <property name="jonas.base" value="${myenv.JONAS_BASE}" />
    <property name="cluster.examples.root" value="${jonas.root}/examples" />

    <typedef resource="org/ow2/jonas/ant/antlib.xml"
             classpath="${jonas.root}/lib/common/ow_jonas_ant.jar"
             uri="http://jonas.objectweb.org/ant" />

  </target>

  <target name="init_1" depends="init_0">

    <!-- Define directories -->
    <property name="conf.dir" value="${user.home}/jc.config/conf" />
    <property name="catalina.ext.dir" value="${jonas.root}/lib/catalina/server/lib" />

    <!-- Load user properties -->
    <property file="${conf.dir}/jonas-newjc.properties" />
    <property file="./build-jc.properties" />
    <condition property="clusterdaemon.jdk" else="${myenv.JAVA_HOME}" value="${cluster.daemon.jdk}">
      <isset property="cluster.daemon.jdk" />
    </condition>
  </target>

  <target name="init_2">
    <condition property="output.dir" value="${cluster.output.dir}">
       <isset property="${cluster.output.dir}" />
    </condition>
    <condition property="output.dir" value="${jonas.base}/output">
        <not>
          <isset property="${cluster.output.dir}" />
        </not>
    </condition>
    <property name="jonas.base.prefix" value="${jonas.base.defaultprefix}" />
    <property name="carol.protocol" value="${carol.defaultprotocol}" />
    <property name="rajdbc.name" value="${rajdbc.defaultname}" />
    <property name="webcontainer.service.name" value="${webcontainer.service.defaultname}" />
    <property name="cluster.architecture" value="${cluster.architecture.default}" />

    <property name="cluster.architecture" value="diffWebEjb" />
    <property name="web.instnb" value="2" />
    <property name="ejb.instnb" value="2" />

  </target>
  <target name="install_cluster_examples" unless="${skip.cluster.examples}">
    <ant dir="${cluster.examples.root}/cluster-j2ee14" antfile="build.xml" target="install"/>
    <ant dir="${cluster.examples.root}/cluster-javaee5" antfile="build.xml" target="install"/>
  </target>
  <target name="define_jdbcra_values">
    <!-- init values for default jdbcRa -->
    <property name="db.name" value="${rajdbc.name}" />
    <jant:property name="rajdbc.user" value="rajdbc.${db.name}.user" />
    <jant:property name="rajdbc.password" value="rajdbc.${db.name}.password" />
    <jant:property name="rajdbc.url" value="rajdbc.${db.name}.url" />
    <jant:property name="rajdbc.drivername" value="rajdbc.${db.name}.drivername" />
    <jant:property name="rajdbc.jndiname" value="rajdbc.${db.name}.jndiname" />
    <jant:property name="rajdbc.maxpoolsize" value="rajdbc.${db.name}.maxpoolsize" defaultValue="100" />
    <jant:property name="rajdbc.jdbcdriverjarfile" value="rajdbc.${db.name}.jdbcdriverjarfile" />
    <jant:property name="rajdbc.mappername" value="rajdbc.${db.name}.mappername" />
    <jant:property name="rajdbc.url" value="rajdbc.${db.name}.url" />
    <!-- init the rajdbc.p6spy if it's not already done -->
    <property name="rajdbc.p6spy" value="off" />
  </target>

  <!-- choose number of instances for web, ejb and web/ejb levels -->
  <target name="choose_inst_nb_diff" if="diffWebEjb">
    <input message="Choose the number of web instances : [default : 2] :" addproperty="web.instnb" defaultvalue="2" />

    <input message="Choose the number of ejb instances : [default : 2] :" addproperty="ejb.instnb" defaultvalue="2" />

    <echo message="${web.instnb}" file="${web.instnb.file}" />
    <echo message="${ejb.instnb}" file="${ejb.instnb.file}" />

  </target>

  <target name="choose_inst_nb_both" if="bothWebEjb">
    <input message="Choose the number of web/ejb instances : [default : 4] :" addproperty="web.instnb" defaultvalue="4" />
    <echo message="${web.instnb}" file="${web.instnb.file}" />
    <echo message="0" file="${ejb.instnb.file}" />
  </target>

  <target name="create_db_inst" depends="init_0">
    <ant dir="${jonas.root}/" antfile="${jonas.root}/templates/newjb/build-jb.xml"
       target="create_jonas_base" inheritall="false">
      <property file="./build-db.properties" />
      </ant>
  </target>

  <target name="create_master_inst" depends="init_0">
    <ant dir="${jonas.root}/" antfile="${jonas.root}/templates/newjb/build-jb.xml"
       target="create_jonas_base" inheritall="false">
      <property file="./build-master.properties" />
      </ant>
  </target>


  <!-- New JOnAS Cluster Configuration -->
  <target name="create_jonas_cluster" description="Create a new JOnAS Cluster Configuration">

    <echo message="mode silence:${silence.mode}" />

    <condition property="condition">
      <equals arg1="true" arg2="${silence.mode}" />
    </condition>
    <antcall target="create_jonas_cluster_by_step" />
    <antcall target="create_jonas_cluster_auto" />

  </target>

  <!-- run the right echo target according the chosen architecture  -->
  <target name="echo_inst_nb_diff" if="diffWebEjb">
    <echo message="WEB INSTANCES NB      = ${web.instnb} " />
    <echo message="EJB INSTANCES NB      = ${ejb.instnb} " />
  </target>
  <target name="echo_inst_nb_both" if="bothWebEjb">
    <echo message="EJB/WEB INSTANCES NB  = ${web.instnb} " />
  </target>

  <!-- Internal creation of new cluster configuration -->
  <target name="internal_create_jonas_cluster" depends="init_1,define_jdbcra_values">

    <echo message="JONAS_ROOT            = ${jonas.root}" />
    <echo message="PREFIX FOR JONAS_BASE = ${output.dir}/${jonas.base.prefix} " />
    <echo message="ARCHITECTURE          = ${cluster.architecture} " />
    <antcall target="echo_inst_nb_diff">
      <param name="web.instnb" value="${web.instnb}" />
      <param name="ejb.instnb" value="${ejb.instnb}" />
    </antcall>

    <antcall target="echo_inst_nb_both">
      <param name="web.instnb" value="${web.instnb}" />
      <param name="ejb.instnb" value="${ejb.instnb}" />
    </antcall>
    <antcall target="install_cluster_examples"/>
    <antcall target="create_db_inst">
      <param name="jonas.root" value="${jonas.root}"/>
      <param name="jonas.base" value="${output.dir}/${db.dir}"/>
    </antcall>

    <antcall target="create_master_inst">
      <param name="jonas.root" value="${jonas.root}"/>
      <param name="jonas.base" value="${output.dir}/${master.dir}"/>
    </antcall>

    <jant:newjc jonasRoot="${jonas.root}" destDirPrefix="${output.dir}/${jonas.base.prefix}" update="off" webinstnb="${web.instnb}" ejbinstnb="${ejb.instnb}" arch="${cluster.architecture}" cdDir="${output.dir}/${cluster.daemon.dir}">
      <common>
        <dbcluster portRange="${db.portrange}" />
        <carolcluster portRange="${carol.portrange}" protocols="${carol.protocol}" jrmpOptimization="${carol.jrmpopt}" />
        <mailcluster type="Session" name="${mail.session.name}" />
        <mailcluster type="MimePartDataSource" name="${mail.mimepartds.name}" mailTo="${mail.to}" subject="${mail.subject}" />
        <jmscluster portRange="${jms.portrange}" initialTopics="${jms.topics}" initialQueues="${jms.queues}" />
        <wsdlPublishcluster>
          <file name="${wsdl.file.name}" dir="${wsdl.file.dir}" encoding="ISO-8859-1" />
        </wsdlPublishcluster>

      <!-- To have an example of a domain.xml file with URLs, see first node domain.xml file -->
      <discoverycluster
          masterNode="${discovery.masternode}" sourcePortRange="${discovery.sourceportrange}"
          greetingPortRange="${discovery.greetingportrange}" mcastAddr="${discovery.mcastaddr}"
          mcastPort="${discovery.mcastport}" jonasRoot="${jonas.root}"
          domainName="${discovery.domainName}" domainDesc="${discovery.domainDesc}"
          clusterName="${discovery.clusterName}" clusterDesc="${discovery.clusterDesc}"
          protocol="${carol.protocol}" carolPortRange="${carol.portrange}"
          clusterNodesName="${cluster.nodesName}" cdName="${cluster.daemon.name}"
          cdProtocol="${cluster.daemon.protocol}" cdPort="${cluster.daemon.port}"/>
         <!-- Copy deployables from JONAS_ROOT/deploy to each JONAS_BASE/deploy-->
         <deployablecluster
            target="copy" sourceDir="${jonas.root}/deploy"
            destDir="deploy" values="haTransactions,sampleCluster2,sampleCluster3"
            extension="ear"/>
         <deployablecluster
                    target="exclude" destDir="deploy"
                    values="joram"
                    extension="xml"/>
      </common>

      <weblevel>
        <servicescluster partialList="${jonas.web.services}" fullList="${jonas.full.services}" />
        <webcontainercluster
          name="${webcontainer.service.name}" httpPortRange="${webcontainer.httpportrange}"
          ajpPortRange="${webcontainer.ajpportrange}" stickySession="${webcontainer.stickysession}"
          modjkEnabled="${webcontainer.mod_jkenabled}" modjkRootDir="${output.dir}"
          modjkDir="${webcontainer.mod_jk.dir}" modJkLbFactorRange="${webcontainer.mod_jk.lbfactorrange}"
          directorEnabled="${webcontainer.directorenabled}" directorPortRange="${webcontainer.directorportrange}"
          directorLbFactorRange="${webcontainer.director.lbfactorrange}" directorDir="${output.dir}/${webcontainer.director.dir}"
          clusterMcastAddr="${webcontainer.cluster.mcastaddr}" clusterMcastPort="${webcontainer.cluster.mcastport}"
          clusterListenPortRange="${webcontainer.cluster.listenportrange}" clusterName="${webcontainer.cluster.name}"/>
        <deploymentcluster ondemandenabled="${webinstances.ondemandenabled}" ondemandredirectport="${webinstances.ondemand.redirectport}"
          devmodeenabled="${webinstances.devmodeenabled}" depmonitorDevModeEnabled="${webinstances.depmonitor.devmodeenabled}" />
        <libcluster destDir="${catalina.ext.dir}">
          <fileset dir="${user.home}/${drivers.dir}">
            <include name="${director.lib.jarfile}" />
          </fileset>
        </libcluster>
      </weblevel>

      <ejblevel>
        <servicescluster partialList="${jonas.ejb.services}" fullList="${jonas.full.services}" />
        <deploymentcluster ondemandenabled="${ejbinstances.ondemandenabled}" devmodeenabled="${ejbinstances.devmodeenabled}" depmonitorDevModeEnabled="${ejbinstances.depmonitor.devmodeenabled}" />
        <jdbcRacluster name="db_access_jdbc1" mapperName="${rajdbc.mappername}" user="${rajdbc.user}" password="${rajdbc.password}" url="${rajdbc.url}" driverName="${rajdbc.drivername}" maxPoolSize="${rajdbc.maxpoolsize}" jndiName="${rajdbc.jndiname}" autoload="on" p6spy="${rajdbc.p6spy}" />
        <jdbcRacluster name="db_access_jdbc_xa1" mapperName="${rajdbc.mappername}" user="${rajdbc.user}" password="${rajdbc.password}" url="${rajdbc.url}" driverName="${rajdbc.drivername}" maxPoolSize="${rajdbc.maxpoolsize}" jndiName="jdbc_xa1" autoload="on" p6spy="${rajdbc.p6spy}" />
        <jdbcRacluster name="db_access_jdbc_xa2" mapperName="${rajdbc.mappername}" user="${rajdbc.user}" password="${rajdbc.password}" url="${rajdbc.url}" driverName="${rajdbc.drivername}" maxPoolSize="${rajdbc.maxpoolsize}" jndiName="jdbc_xa2" autoload="on" p6spy="${rajdbc.p6spy}" />
        <dbmcluster dataSources="${rajdbc.name}" />
        <cmicluster mcastAddr="${cmi.mcastaddr}" mcastPort="${cmi.mcastport}" />
        <hacluster mcastAddr="${ha.mcastaddr}" mcastPort="${ha.mcastport}" />
        <libcluster>
          <fileset dir="${user.home}/${drivers.dir}">
            <include name="${rajdbc.jdbcdriverjarfile}" />
          </fileset>
        </libcluster>
      </ejblevel>
      <script domainName="${discovery.domainName}" scriptdir="${output.dir}" nodeNamePrefix="${script.node.basename}" cdDir="${output.dir}/${cluster.daemon.dir}" dbDir="${output.dir}/${db.dir}" dbName="${db.nodeName}" masterDir="${output.dir}/${master.dir}" masterName="${master.nodeName}"/>
      <clusterDaemon name="${cluster.daemon.name}" domainName="${discovery.domainName}" cdDir="${output.dir}/${cluster.daemon.dir}" protocol="${cluster.daemon.protocol}" port="${cluster.daemon.port}" jdk="${clusterdaemon.jdk}" interactionMode="${cluster.daemon.interactionMode}" autoBoot="${cluster.daemon.autoBoot}" xprm="${cluster.daemon.xprm}" clusterNodesName="${cluster.nodesName}"/>
    </jant:newjc>

    <!-- Uncomment this line to allow the master to connect to all the nodes on startup -->
    <!-- copy domain.xml file of the cluster to the master node -->
     <copy todir="${output.dir}/${master.dir}/conf" file="${output.dir}/${jonas.base.prefix}1/conf/domain.xml" />
    <!-- copy workers.properties file to the master node -->
      <copy todir="${output.dir}/${master.dir}/conf" file="${output.dir}/${webcontainer.mod_jk.dir}/workers.properties" failonerror="false" />
    <!-- clean the cluster daemon configuration -->
    <delete includeemptydirs="true">
      <fileset dir="${output.dir}/${cluster.daemon.dir}/conf" excludes="carol.properties trace.properties clusterd.xml jgroups-discovery.xml" />
    </delete>
    <touch file="${output.dir}/${cluster.daemon.dir}/conf/jonas.properties"/>

    <!-- add the execute permission to the script files -->
    <chmod file="${output.dir}/jcl4sc" perm="u+x"/>
  </target>

  <!-- New JOnAS Cluster Configuration -->
  <target name="create_jonas_cluster_by_step" depends="init_1" description="Create a new JOnAS Cluster Configuration" unless="condition">

    <input message="Choose your output directory : [default : ${jonas.base}] :" addproperty="output.dir" defaultvalue="${jonas.base}" />
    <input message="Choose your prefix for the JONAS_BASE directories : [default : jb] :" addproperty="jonas.base.prefix" defaultvalue="${jonas.base.defaultprefix}" />
    <input message="Choose your protocol : [default : ${carol.defaultprotocol}] :" validargs="jrmp,iiop,irmi," addproperty="carol.protocol" defaultvalue="${carol.defaultprotocol}" />
    <input message="Choose your database : [default : ${rajdbc.defaultname}] :" addproperty="rajdbc.name" defaultvalue="${rajdbc.defaultname}" />
    <input message="Choose your cluster architecture [bothWebEjb/diffWebEjb] : [default : ${cluster.architecture.default}] :" validargs="bothWebEjb,diffWebEjb," addproperty="cluster.architecture" defaultvalue="${cluster.architecture.default}" />

    <!-- set properties used to execute the right branch of ant script -->
    <condition property="diffWebEjb">
      <equals arg1="${cluster.architecture}" arg2="diffWebEjb" />
    </condition>
    <condition property="bothWebEjb">
      <equals arg1="${cluster.architecture}" arg2="bothWebEjb" />
    </condition>

    <!-- chosen values are stored in temporary files by the called target -->
    <property name="web.instnb.file" value="${output.dir}/web.instnb.file.tmp" />
    <property name="ejb.instnb.file" value="${output.dir}/ejb.instnb.file.tmp" />

    <mkdir dir="${output.dir}" />

    <antcall target="choose_inst_nb_diff">
      <param name="web.instnb.file" value="${web.instnb.file}" />
      <param name="ejb.instnb.file" value="${ejb.instnb.file}" />
    </antcall>
    <antcall target="choose_inst_nb_both">
      <param name="web.instnb.file" value="${web.instnb.file}" />
      <param name="ejb.instnb.file" value="${ejb.instnb.file}" />
    </antcall>

    <loadfile property="web.instnb" srcFile="${web.instnb.file}" failonerror="false" />
    <loadfile property="ejb.instnb" srcFile="${ejb.instnb.file}" failonerror="false" />

    <delete file="${web.instnb.file}" failonerror="false" />
    <delete file="${ejb.instnb.file}" failonerror="false" />
    <antcall target="internal_create_jonas_cluster">
      <param name="web.instnb" value="${web.instnb}" />
      <param name="ejb.instnb" value="${ejb.instnb}" />
    </antcall>

  </target>

  <!-- New JOnAS Cluster Configuration -->
  <target name="create_jonas_cluster_auto" depends="init_1,init_2" description="Create a new JOnAS Cluster Configuration" if="condition">

    <!-- set properties used to execute the right branch of ant script -->
    <condition property="diffWebEjb">
      <equals arg1="${cluster.architecture}" arg2="diffWebEjb" />
    </condition>
    <condition property="bothWebEjb">
      <equals arg1="${cluster.architecture}" arg2="bothWebEjb" />
    </condition>

    <!-- chosen values are stored in temporary files by the called target -->
    <property name="web.instnb.file" value="${output.dir}/web.instnb.file.tmp" />
    <property name="ejb.instnb.file" value="${output.dir}/ejb.instnb.file.tmp" />
    <delete dir="${output.dir}" deleteonexit="true"/>
    <mkdir dir="${output.dir}" />

    <loadfile property="web.instnb" srcFile="${web.instnb.file}" failonerror="false" />
    <loadfile property="ejb.instnb" srcFile="${ejb.instnb.file}" failonerror="false" />

    <delete file="${web.instnb.file}" failonerror="false" />
    <delete file="${ejb.instnb.file}" failonerror="false" />

    <antcall target="internal_create_jonas_cluster">
      <param name="web.instnb" value="${web.instnb}" />
      <param name="ejb.instnb" value="${ejb.instnb}" />
    </antcall>

  </target>

</project>
