    <!-- WARNING: This script does not support the cactus.port property
    	 JBoss will be started on the port defined in the server 
    	 configuration (in jboss-service.xml) -->

    <!-- Global properties -->
    <property name="cactus.conf.jboss3x" value="${cactus.conf.dir}/jboss3x"/>
    <property name="cactus.target.jboss3x" value="${cactus.target.dir}/jboss3x"/>

    <!-- Location of the server configurations -->
    <property name="cactus.jboss3x.config.dir" 
      value="${cactus.home.jboss3x}/server"/>

    <!-- Name of the server configuration to use -->
    <property name="cactus.jboss3x.config.name" value="default"/>

    <!-- 
       ========================================================================
         Run JBoss 3.x tests
       ========================================================================
    -->
    <target name="cactus.run.jboss3x"
        depends="cactus.init,cactus.setup.jboss3x,cactus.war,cactus.deploy.jboss3x"
        if="cactus.home.jboss3x" description="Run tests on JBoss 3.x">

        <!-- Start the container, wait for it to be started, run the
             unit tests, stop the container, wait for it to be stopped.
             The container is stopped if the tests fail for any reason -->

        <antcall target="cactus.runservertests.jboss3x" inheritRefs="true"/>

    </target>

    <target name="cactus.runservertests.jboss3x" depends="cactus.runservertests.task.jboss3x, cactus.runservertests.target.jboss3x"/>

    <target name="cactus.runservertests.task.jboss3x" if ="cactus.test.task">

        <taskdef name="cactus.testTask" classname="${cactus.test.task}"/>
        <runservertests
            testURL="http://localhost:${cactus.port}/${cactus.context}/ServletRedirector?Cactus_Service=RUN_TEST"
            startTarget="cactus.start.jboss3x"
            stopTarget="cactus.stop.jboss3x">
          <test>
          	<cactus.testTask/>
          </test>
        </runservertests>

    </target>

    <target name="cactus.runservertests.target.jboss3x" unless="cactus.test.task">

        <runservertests
            testURL="http://localhost:${cactus.port}/${cactus.context}/ServletRedirector?Cactus_Service=RUN_TEST"
            startTarget="cactus.start.jboss3x"
            stopTarget="cactus.stop.jboss3x"
            testTarget="cactus.test"/>

    </target>

    <!-- 
       ========================================================================
         Start JBoss 3.x
       ========================================================================
    -->
    <target name="cactus.start.jboss3x" description="Start JBoss 3.x"
        depends="cactus.check.jboss3x" if="cactus.home.jboss3x">

        <dirname property="antfile.dir" file="${ant.file}"/>

        <java jvm="${cactus.jvm}" classname="org.jboss.Main" fork="yes"
          dir="${cactus.home.jboss3x}/bin">

            <sysproperty key="program.name"
                file="${cactus.home.jboss3x}/bin/run.bat"/>
            <sysproperty key="jboss.server.home.dir"
                file="${cactus.jboss3x.config.dir}/${cactus.jboss3x.config.name}"/>
            <sysproperty key="jboss.server.home.url"
                value="file:/${cactus.jboss3x.config.dir}/${cactus.jboss3x.config.name}"/>
             
            <arg line="-c ${cactus.jboss3x.config.name}"/>
            
            <classpath>
                <pathelement location="${cactus.home.jboss3x}/bin/run.jar"/>
                <pathelement path="${java.home}/../lib/tools.jar"/>
            </classpath>

        </java>

    </target>

    <!-- 
       ========================================================================
         Stop JBoss 3.x
       ========================================================================
    -->
    <target name="cactus.stop.jboss3x" description="Stop JBoss 3.x"
        depends="cactus.check.jboss3x" if="cactus.home.jboss3x">

        <java jvm="${cactus.jvm}" classname="org.jboss.Shutdown" fork="yes">

            <arg value="localhost"/>
            <arg value="${cactus.port}"/>
            
            <classpath>
                <pathelement location="${cactus.home.jboss3x}/bin/shutdown.jar"/>
            </classpath>

        </java>

    </target>

    <!-- 
       ========================================================================
         Display a warning message if the needed container home property
         is not set
       ========================================================================
    -->
    <target name="cactus.check.jboss3x" unless="cactus.home.jboss3x">

        <echo></echo>
        <echo>******************************************************</echo>
        <echo>WARNING : The 'cactus.home.jboss3x' property has not</echo>
        <echo>been set. No test will be run on that servlet engine.</echo>
        <echo>******************************************************</echo>
        <echo></echo>

    </target>

    <!--
       ========================================================================
         Deploy the cactified webapp
       ========================================================================
    -->
    <target name="cactus.deploy.jboss3x" if="cactus.home.jboss3x"
        description="Deploy the webapp on JBoss 3.x">

        <!-- Note: It seems JBoss requires a war file in its deploy directory. 
             It fails to deploy an expanded webapp (something may need to be
             configured to allow this?). -->
            
        <!-- Copy the webapp -->
        <mkdir dir="${cactus.target.jboss3x}/tmp/${cactus.context}"/>
        <copy todir="${cactus.target.jboss3x}/tmp/${cactus.context}">
            <fileset dir="${cactus.target.dir}/${cactus.context}"/>
        </copy>

        <!-- Add the JBoss specific configuration files -->
        <copy todir="${cactus.target.jboss3x}/tmp/${cactus.context}/WEB-INF/classes">
            <fileset dir="${cactus.conf.jboss3x}">
                <include name="*.properties"/>
            </fileset>
        </copy>

        <jar destfile="${cactus.jboss3x.config.dir}/${cactus.jboss3x.config.name}/deploy/${cactus.context}.war">
            <fileset dir="${cactus.target.jboss3x}/tmp/${cactus.context}"/>
        </jar>

    </target>

    <!--
       ========================================================================
         Set up a valid JBoss 3.x directory structure in the
         cactus.target.jboss3x directory.
       ========================================================================
    -->
    <target name="cactus.setup.jboss3x"
        depends="cactus.init, cactus.check.jboss3x" if="cactus.home.jboss3x"
        description="Set up a JBoss 3.x directory structure">

        <!-- TODO: Find how to create a valid default server configuration.
             Copying the server/ directory does not seem to be enough -->
             
    </target>
    
    <!--
       ========================================================================
         Clean generated files (including distributables)
       ========================================================================
    -->
    <target name="cactus.clean.jboss3x" depends="cactus.init, cactus.clean"
        description="Clean target directory">
        <delete includeEmptyDirs="true">
            <fileset dir="${cactus.target.jboss3x}"/>
        </delete>
    </target>