<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="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="test.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>
  </target>


  <!--
    FELIX TASKS
  -->

  <target name="start" description="Install Orchestra engine if not yet done and start Felix">
    <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" />
        </classpath>
        <jvmarg line="${java.options} ${jaxp.classpath}" />
      </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>
