<project name="Nova Orchestra" default="usage" basedir=".">
  <description>Nova Orchestra tasks.</description>

  <import file="${basedir}/common.xml"/>

  <!--
    TEST TARGETS
  -->


  <condition property="jaxp.classpath" value="-Xbootclasspath/a:lib/org.apache.servicemix.bundles.jaxp-ri-1.4.2_3.jar" else="">
    <equals arg1="${java.vendor}" arg2="IBM Corporation"/>
  </condition>

  <target name="orchestra.classpath">
    <path id="orchestra.classpath">
      <pathelement location="conf/" />
      <fileset dir="lib" includes="*.jar" excludes="org.apache.servicemix.bundles.jaxp-ri-*.jar jaxp-api-*.jar"/>
      <fileset dir="${catalina.base}/lib" includes="*.jar" />
    </path>
  </target>

  <!--
    TOMCAT TASKS
  -->

  <target name="start" description="Install Orchestra engine if not yet done and start Tomcat">
    <ant antfile="install.xml" target="install-no-fail" />
    <condition property="server.running">
      <socket server="${orchestra.servlet.host}" port="${orchestra.servlet.port}" />
    </condition>
    <fail if="server.running" message="Cannot start Orchestra server, one is already running on port : ${orchestra.servlet.port}" />

    <delete dir="${catalina.base}/temp" failonerror="false" />
    <mkdir dir="${catalina.base}/temp" />
    <echo file="stylesheet.xsl"><![CDATA[
      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output omit-xml-declaration="yes" method="text"/>
        <xsl:template match="/">
          <xsl:for-each select="Server/Service/Connector">
            <xsl:value-of select="@port" /><xsl:text> </xsl:text>
          </xsl:for-each>
        </xsl:template>
      </xsl:stylesheet>
      ]]>
    </echo>
    <xslt in="${catalina.base}/conf/server.xml" out="tomcat.ports" style="stylesheet.xsl" />
    <loadfile property="tomcat.ports" srcFile="tomcat.ports"/>
    <delete file="tomcat.ports" />
    <delete file="stylesheet.xsl" />
    <fail message="Orchestra servlet port ${orchestra.servlet.port} is not contained in tomcat configuration (ports found: ${tomcat.ports}). Please fix configuration.">
      <condition>
        <not>
          <contains string="${tomcat.ports}" substring="${orchestra.servlet.port} " />
        </not>
      </condition>
    </fail>
    <echo>Starting orchestra</echo>
    <java classname="org.apache.catalina.startup.Bootstrap" fork="true">
      <classpath>
        <fileset dir="${catalina.home}/bin" includes="*.jar" />
      </classpath>
      <jvmarg value="-Dcatalina.home=${catalina.home}" />
      <jvmarg value="-Dcatalina.base=${catalina.base}" />
      <jvmarg value="-Djava.io.tmpdir=${catalina.base}/temp" />
      <jvmarg line="${java.options} ${jaxp.classpath}" />
    </java>
  </target>

  <target name="stop" description="Stop Tomcat">
    <condition property="server.down">
      <not>
        <socket server="${orchestra.servlet.host}" port="${orchestra.servlet.port}" />
      </not>
    </condition>
    <fail if="server.down" message="Cannot stop Orchestra server, no server running on port : ${orchestra.servlet.port}" />
    <java classname="org.apache.catalina.startup.Bootstrap" fork="true">
      <classpath>
        <fileset dir="${catalina.base}/bin" includes="*.jar" />
      </classpath>
        <jvmarg value="-Dcatalina.home=${catalina.home}" />
        <jvmarg value="-Dcatalina.base=${catalina.base}" />
      	<jvmarg line="${java.options} ${jaxp.classpath}" />
        <arg line="stop"/>
    </java>
  </target>

  <target name="install" description="install Orchestra engine">
    <ant antfile="install.xml" target="install" />
  </target>

</project>
