<project name="Nova Orchestra" default="usage" basedir=".">
  <description>Nova Orchestra tasks.</description>
  <!-- set global properties for this build -->
  <import file="${basedir}/common.xml"/>

  <property file="${basedir}/conf/config.properties" prefix="felixconf" />

  <condition property="felix.shutdown.port" value="${felixconf.osgi.shell.telnet.port}" else="6666">
    <isset property="felixconf.osgi.shell.telnet.port" />
  </condition>

  <!--
    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="bundle" includes="*.jar" />
      <path refid="jaxp.classpath"/>
    </path>
  </target>


  <!--
    FELIX TASKS
  -->

  <target name="start" description="Install Orchestra engine if not yet done and start Felix" depends="jaxp.classpath">
    <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}" />
    <fail message="org.osgi.service.http.port property in felix configuration has not the same value as orchestra.servlet.port">
      <condition>
        <not><equals arg1="${orchestra.servlet.port}" arg2="${felixconf.org.osgi.service.http.port}"/></not>
      </condition>
    </fail>
    <parallel failonany="true">
      <java classname="org.apache.felix.main.Main" fork="true">
        <classpath>
          <fileset dir="${basedir}/felix" />
          <path refid="jaxp.classpath"/>
        </classpath>
        <jvmarg line="${java.options}" />
      </java>
      <sequential>
        <waitfor maxwait="2" maxwaitunit="minute">
          <and>
            <socket server="${orchestra.servlet.host}" port="${orchestra.jmx.port}" />
            <socket server="${orchestra.servlet.host}" port="${orchestra.servlet.port}" />
          </and>
        </waitfor>
      </sequential>
    </parallel>
  </target>
  
  <target name="stop" description="Stop Felix">
    <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}" />
    <telnet port="${felix.shutdown.port}" server="localhost">
      <read>-&gt;</read>
      <write>shutdown</write>
      <!--<read timeout="10">Good Bye!</read>-->
    </telnet>
    <waitfor maxwait="2" maxwaitunit="minute">
      <and>
        <not>
          <socket server="${orchestra.servlet.host}" port="${orchestra.jmx.port}" />
        </not>
        <not>
          <socket server="${orchestra.servlet.host}" port="${orchestra.servlet.port}" />
        </not>
      </and>
    </waitfor>
  </target>
  
</project>
