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

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

        <!-- Start the servlet engine, wait for it to be started, run the
             unit tests, stop the servlet engine, wait for it to be stopped.
             The servlet engine is stopped if the tests fail for any reason -->
        <antcall target="cactus.runservertests.tomcat4x" inheritRefs="true"/>

    </target>

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

    <target name="cactus.runservertests.task.tomcat4x" 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.tomcat4x"
            stopTarget="cactus.stop.tomcat4x">
          <test>
          	<cactus.testTask/>
          </test>
        </runservertests>

    </target>

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

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

    </target>

    <!--
       ========================================================================
         Start Tomcat 4.x
       ========================================================================
    -->
    <target name="cactus.start.tomcat4x" description="Start Tomcat 4.x"
        depends="cactus.check.tomcat4x" if="cactus.home.tomcat4x">

        <java jvm="${cactus.jvm}" classname="org.apache.catalina.startup.Bootstrap" fork="yes">

            <sysproperty key="catalina.home" file="${cactus.home.tomcat4x}"/>
            <sysproperty key="catalina.base" file="${cactus.target.tomcat4x}"/>
            <arg value="start"/>

            <classpath>
              <!-- These are ignored if -Dbuild.sysclasspath=only is used -->
              <pathelement path="${java.home}/../lib/tools.jar"/>
              <fileset dir="${cactus.home.tomcat4x}">
                  <include name="bin/bootstrap.jar"/>
              </fileset>
            </classpath>

        </java>

    </target>

    <!--
       ========================================================================
         Stop Tomcat 4.x
       ========================================================================
    -->
    <target name="cactus.stop.tomcat4x" description="Stop Tomcat 4.x"
        depends="cactus.check.tomcat4x" if="cactus.home.tomcat4x">

        <java jvm="${cactus.jvm}" classname="org.apache.catalina.startup.Bootstrap" fork="yes">

            <sysproperty key="catalina.home" file="${cactus.home.tomcat4x}"/>
            <sysproperty key="catalina.base" file="${cactus.target.tomcat4x}"/>
            <arg value="stop"/>

            <classpath>
              <!-- These are ignored if -Dbuild.sysclasspath=only is used
                   (this is the case for Gump builds) -->
              <fileset dir="${cactus.home.tomcat4x}">
                  <include name="bin/bootstrap.jar"/>
              </fileset>
            </classpath>

        </java>

    </target>

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

        <echo></echo>
        <echo>******************************************************</echo>
        <echo>WARNING : The 'cactus.home.tomcat4x' 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.tomcat4x" if="cactus.home.tomcat4x"
        description="Deploy the webapp on Tomcat 4.x">

        <!-- Copy the webapp -->
        <copy todir="${cactus.target.tomcat4x}/webapps/${cactus.context}">
            <fileset dir="${cactus.target.dir}/${cactus.context}"/>
        </copy>

    </target>
    
    <!--
       ========================================================================
         Set up a valid Tomcat 4.x directory structure in the
         cactus.target.tomcat4x directory.
       ========================================================================
    -->
    <target name="cactus.setup.tomcat4x"
        depends="cactus.init, cactus.check.tomcat4x" if="cactus.home.tomcat4x"
        description="Set up a Tomcat 4.x directory structure">

        <!-- Create work and conf directories and copy configuration files -->
        <mkdir dir="${cactus.target.tomcat4x}/conf"/>
        <mkdir dir="${cactus.target.tomcat4x}/work"/>
        <mkdir dir="${cactus.target.tomcat4x}/webapps"/>

        <!-- Delete some config file so that they will be copied every time -->
        <delete file="${cactus.target.tomcat4x}/conf/server.xml"/>

        <!-- Remove the auto deployed webapp so that it is redeployed every
             time -->
        <delete dir="${cactus.target.tomcat4x}/webapps/${cactus.context}"/>

        <copy todir="${cactus.target.tomcat4x}/conf" filtering="on">
            <fileset dir="${cactus.conf.tomcat4x}"/>
        </copy>

        <!-- Copy the Tomcat web.xml - We need to copy this file as it has
             changed between Tomcat 4.x versions and one version of it doesn't
             work with another version of Tomcat 4 -->
        <copy file="${cactus.home.tomcat4x}/conf/web.xml"
            todir="${cactus.target.tomcat4x}/conf"/>

    </target>

    <!--
       ========================================================================
         Clean generated files (including distributables)
       ========================================================================
    -->
    <target name="cactus.clean.tomcat4x" depends="cactus.init,cactus.clean" 
    	description="Clean target directory">
        <delete includeEmptyDirs="true">
            <fileset dir="${cactus.target.tomcat4x}"/>
        </delete>
    </target>
