<project name="Nova Orchestra" default="usage" basedir=".">
  <description>Nova Orchestra tasks.</description>
  
  <import file="${basedir}/common.xml"/>
  
  <!--
    TEST TARGETS
  -->
  
  
  <condition property="add.jaxp.path">
    <equals arg1="${java.vendor}" arg2="IBM Corporation"/>
  </condition>
  <target name="jaxp.classpath" depends="jaxp.classpath.2" if="add.jaxp.path">
    <path id="jaxp.classpath">
      <fileset dir="lib" includes="org.apache.servicemix.bundles.jaxp-ri-*.jar jaxp-api-*.jar" /> 
    </path>
  </target>
  <target name="jaxp.classpath.2" unless="add.jaxp.path">
      <path id="jaxp.classpath" />
  </target>
  
  <target name="test.classpath" depends="jaxp.classpath">
    <path id="test.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 refid="jaxp.classpath" />
    </path>
  </target>

  <target name="core.test.classpath" depends="test.classpath">
    <path id="core.test.classpath">
      <path refid="test.classpath" />
      <fileset dir="${catalina.base}/webapps/${orchestra.servlet.path}/WEB-INF/bundle" includes="*.jar" />
    </path>
  </target>
  
  
  <!--
    TOMCAT TASKS
  -->

  <target name="start" description="Install Orchestra engine if not yet done and start Tomcat" depends="jaxp.classpath">
    <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 file="${catalina.base}/webapps/${orchestra.servlet.path}/WEB-INF/server-config.wsdd" failonerror="false" />
    <copy toDir="${catalina.base}/webapps/${orchestra.servlet.path}/WEB-INF/" flatten="true" failonerror="false">
      <javaresource name="org/apache/axis/server/server-config.wsdd">
        <classpath>
          <fileset dir="${catalina.base}/webapps/${orchestra.servlet.path}/WEB-INF/bundle">
            <include name="**/*.jar"/>
          </fileset>
        </classpath>
      </javaresource>
    </copy>
    
    <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" />
        <path refid="jaxp.classpath" />
      </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}" />
    </java>
  </target>
  
  <target name="stop" description="Stop Tomcat" depends="jaxp.classpath">
    <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" />
        <path refid="jaxp.classpath" />
      </classpath>
        <jvmarg value="-Dcatalina.home=${catalina.home}" />
        <jvmarg value="-Dcatalina.base=${catalina.base}" />
        <arg line="stop"/>
    </java>
  </target>

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

</project>
