<project name="Nova Orchestra Common" default="usage" basedir=".">
  <description>Nova Orchestra common tasks.</description>
  <!-- set global properties for this build -->
  <property name="orchestra.dir" value="${basedir}"/>
  <property file="conf/orchestra.properties" />
  <property file="conf/install.properties" />
  <property environment="env" />

  <condition property="java.options" 
    value="-Djava.util.logging.config.file='${basedir}/conf/logging.properties' ${env.JAVA_OPTS}" 
    else="-Djava.util.logging.config.file='${basedir}/conf/logging.properties'">
      <isset property="env.JAVA_OPTS" />
  </condition>
  
  <target name="usage">
    <exec executable="ant">
      <arg value="-p" />
    </exec>
  </target>

  <!--
    TEST TARGETS
  -->
  <target name="test.classpath">
    <fail message="test.classpath needs to be redefined"/>
  </target>

  <target name="core.test.classpath">
    <fail message="core.test.classpath needs to be redefined"/>
  </target>
  
  <target name="test" depends="core.test.classpath" description="runs the test suite and put the results in testresults">
    <mkdir dir="testresults/xml" />
    <junit printsummary="yes" fork="yes" forkmode="once" showoutput="true" >
      <jvmarg line="${java.options}" />
      <classpath>
        <path refid="core.test.classpath" />
      </classpath>
      <formatter type="xml" />
      <!-- TEST CLASS -->
      <test todir="testresults/xml" name="org.ow2.orchestra.test.BpelTests" />
    </junit>
    <junitreport todir="testresults/xml">
      <fileset dir="testresults/xml" includes="TEST-*.xml" />
      <report todir="testresults" />
    </junitreport>
  </target>

  <target name="test-remote" depends="test.classpath" description="runs the remote test suite and put the results in testresults-remote">
    <mkdir dir="testresults-remote/xml" />
    <junit printsummary="yes" fork="yes" forkmode="once" showoutput="true">
      <jvmarg line="${java.options}" />
      <classpath>
        <path refid="test.classpath" />
      </classpath>
      <formatter type="xml" />
      <!-- TEST CLASS -->
      <test todir="testresults-remote/xml" name="org.ow2.orchestra.test.remote.RemoteTests" />
    </junit>
    <junitreport todir="testresults-remote/xml">
      <fileset dir="testresults-remote/xml" includes="TEST-*.xml" />
      <report todir="testresults-remote" />
    </junitreport>
  </target>

  <target name="test-all-stress" depends="core.test.classpath" description="runs all test suites and put the results in testresults-all">
    <mkdir dir="testresults-all-stress/xml" />
    <junit printsummary="yes" fork="yes" forkmode="once" showoutput="false">
      <jvmarg line="${java.options}" />
      <classpath>
        <path refid="core.test.classpath" />
      </classpath>
      <formatter type="xml" />
      <!-- TEST CLASS -->
      <test todir="testresults-all-stress/xml" name="org.ow2.orchestra.test.BpelTests" />
      <test todir="testresults-all-stress/xml" name="org.ow2.orchestra.test.remote.RemoteTests" />
      <test todir="testresults-all-stress/xml" name="org.ow2.orchestra.test.StressTest" />
      <test todir="testresults-all-stress/xml" name="org.ow2.orchestra.test.remote.perf.RemoteStressTest" />
    </junit>
    <junitreport todir="testresults-all-stress/xml">
      <fileset dir="testresults-all-stress/xml" includes="TEST-*.xml" />
      <report todir="testresults-all-stress" />
    </junitreport>
  </target>

  <target name="test-all" depends="core.test.classpath" description="runs all test suites and put the results in testresults-all">
    <mkdir dir="testresults-all/xml" />
    <junit printsummary="yes" fork="yes" forkmode="once" showoutput="true" >
      <jvmarg line="${java.options}" />
      <classpath>
        <path refid="core.test.classpath" />
      </classpath>
      <formatter type="xml" />
      <!-- TEST CLASS -->
      <test todir="testresults-all/xml" name="org.ow2.orchestra.test.BpelTests" />
      <test todir="testresults-all/xml" name="org.ow2.orchestra.test.remote.RemoteTests" />
    </junit>
    <junitreport todir="testresults-all/xml">
      <fileset dir="testresults-all/xml" includes="TEST-*.xml" />
      <report todir="testresults-all" />
    </junitreport>
  </target>

  <target name="stress" depends="test.classpath">
    <java classname="org.ow2.orchestra.test.remote.perf.RemoteStressTest" fork="true">
      <jvmarg line="${java.options}" />
      <classpath>
        <path refid="test.classpath" />
      </classpath>
      <arg value="algo=${algo}" />
      <arg value="threadNb=${threadNb}" />
      <arg value="printFinished=${printFinished}" />
      <arg value="printLaunched=${printLaunched}" />
      <arg value="timeBetweenVerifications=${timeBetweenVerifications}" />
      <arg value="timeBetweenPrints=${timeBetweenPrints}" />
      <arg value="testsToRunClasses=${testsToRunClasses}" />
      <arg value="thinkTime=${thinkTime}" />
      <arg value="loadTime=${loadTime}" />
      <arg value="warmupTime=${warmupTime}" />
    </java>
  </target>

  <!--
    TOMCAT TASKS
  -->

  <target name="start" description="Start orchestra">
    <fail message="Must be redefined" />
  </target>
  
  <target name="install" description="install Orchestra engine">
  </target>
  
  <target name="debug" description="Install Orchestra engine if not yet done and debug Tomcat">
    <antcall target="start">
      <param name="java.options" value="${java.options} -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000"/>
    </antcall>
  </target>
	
  <target name="stop" description="Stop Orchestra">
    <fail message="Must be redefined" />
  </target>

  <target name="init-db"  depends="test.classpath" description="Generate database schema from the environment configuration">
    <input
      message="Please enter hibernate configuration file:"
      addproperty="hibernate.location"
      defaultvalue="conf/hibernate.properties"
    />
    <input
      message="Please enter hibernate configuration name in the environment: (core for runtime and monitoring tables, history for monitoring only)"
      addproperty="hibernate.config"
      defaultvalue="core"
    />
    <property file="${hibernate.location}" />
    <sql
        driver="${hibernate.connection.driver_class}"
        url="${hibernate.connection.url}"
        userid="${hibernate.connection.username}"
        password="${hibernate.connection.password}"
        src="resources/db-schemas/${hibernate.config}/${hibernate.dialect}-drop.sql"
        classpathref="test.classpath"
        onerror="continue"
    />
    <sql
        driver="${hibernate.connection.driver_class}"
        url="${hibernate.connection.url}"
        userid="${hibernate.connection.username}"
        password="${hibernate.connection.password}"
        src="resources/db-schemas/${hibernate.config}/${hibernate.dialect}-create.sql"
        classpathref="test.classpath"
    />
  </target>
  
  <!--
      Client targets
    -->
  <target name="status" description="Get the status of orchestra and the list of deployed processes" depends="test.classpath">
    <java classname="org.ow2.orchestra.jmxclient.JMXClient" classpathref="test.classpath" fork="true" error="error.txt" failonerror="true">
      <jvmarg line="${java.options}" />
      <arg value="--status" />
    </java>
  </target>

  <!-- DEPLOY TARGETS -->
  <target name="deployCheckParams" depends="deployFromBar,deployFromDescriptor">
    <condition property="deployFail">
      <not>
        <isset property="bpel" />
      </not>
    </condition>
    <condition property="deployWithoutExt">
      <and>
        <not>
          <isset property="deployFail" />
        </not>
        <not>
          <isset property="wsdls" />
        </not>
      </and>
    </condition>
    <condition property="deployWithExt">
      <and>
        <not>
          <isset property="deployFail" />
        </not>
        <isset property="wsdls" />
      </and>
    </condition>
  </target>

  <target name="deployUsage" depends="deployCheckParams" if="deployFail" unless="bar">
    <echo message="Deploy a BPEL process: " />
    <echo message="ant deploy -Dbpel=&lt;bpel file&gt; [-Dwsdls=&lt;file,file,...&gt;]" />
    <echo message="Deploy a BPEL process from a bar: " />
    <echo message="ant deploy -Dbar=&lt;bar file&gt;" />
  </target>

  <target name="deployFromBar" if="bar" depends="test.classpath">
    <java  classname="org.ow2.orchestra.jmxclient.JMXClient" classpathref="test.classpath" fork="true" error="error.txt"  failonerror="true">
      <jvmarg line="${java.options}" />
      <arg value="--deploy" />
      <arg value="--bar" />
      <arg value="${bar}" />
    </java>
  </target>

  <target name="deployFromDescriptor" if="descriptor">
    <property file="${descriptor}" />
    <dirname file="${descriptor}" property="descr.base" />
  </target>

  <target name="deployWithoutWsdl" depends="deployCheckParams, test.classpath" if="deployWithoutExt">
    <condition property="deploy.client.base" value="${descr.base}" else="${basedir}">
      <isset property="descr.base" />
    </condition>
    <java classname="org.ow2.orchestra.jmxclient.JMXClient" classpathref="test.classpath" fork="true" dir="${deploy.client.base}" error="error.txt" failonerror="true">
      <jvmarg line="${java.options}" />
      <arg value="--deploy" />
      <arg value="--bpel" />
      <arg value="${bpel}" />
    </java>
  </target>

  <target name="deployWithWsdl" depends="deployCheckParams, test.classpath" if="deployWithExt">
    <condition property="deploy.client.base" value="${descr.base}" else="${basedir}">
      <isset property="descr.base" />
    </condition>
    <java classname="org.ow2.orchestra.jmxclient.JMXClient" classpathref="test.classpath" fork="true" dir="${deploy.client.base}" error="error.txt" failonerror="true">
      <jvmarg line="${java.options}" />
      <arg value="--deploy" />
      <arg value="--bpel" />
      <arg value="${bpel}" />
      <arg value="--wsdls" />
      <arg value="${wsdls}" />
    </java>
  </target>

  <target name="deploy" depends="deployUsage,deployWithWsdl,deployWithoutWsdl" description="Deploy a BPEL process to Orchestra" />

  <!-- UNDEPLOY TARGETS -->
  <target name="undeployUsage" unless="process">
    <echo message="Undeploy a BPEL process:" />
    <echo message="ant undeploy -Dprocess=&lt;process QName&gt;" />
  </target>

  <target name="undeployAction" if="process" depends="test.classpath">
    <java classname="org.ow2.orchestra.jmxclient.JMXClient" classpathref="test.classpath" fork="true" error="error.txt" failonerror="true">
      <jvmarg line="${java.options}" />
      <arg value="--undeploy" />
      <arg value="--process" />
      <arg value="${process}" />
    </java>
  </target>

  <target name="undeploy" depends="undeployUsage,undeployAction" description="Undeploy a BPEL process to Orchestra" />

  <!-- CALL TARGETS -->

  <target name="callCheckParams">
    <condition property="callOk">
      <and>
        <isset property="endpoint" />
        <isset property="action" />
        <isset property="message" />
      </and>
    </condition>
  </target>

  <target name="callUsage" unless="callOk">
    <echo message="Make a call to a web service:" />
    <echo message="ant call -Dendpoint=&lt;service url&gt; -Daction=&lt;SOAP action&gt; -Dmessage=&lt;message&gt;" />
  </target>

  <target name="callAction" if="callOk" depends="test.classpath">
    <java classname="org.ow2.orchestra.jmxclient.JMXClient" classpathref="test.classpath" fork="true" error="error.txt" failonerror="true">
      <jvmarg line="${java.options}" />
      <arg value="--call" />
      <arg value="--endpoint" />
      <arg value="${endpoint}" />
      <arg value="--soapAction" />
      <arg value="${action}" />
      <arg value="--soapMessage" />
      <arg value="${message}" />
    </java>
  </target>

  <target name="call" depends="callCheckParams,callUsage,callAction" description="Make a call to a web service" />

</project>
