<!--
========================================================================
 Copyright (c) 2003 Mort Bay Consulting (Australia) Pty. Ltd.
 $Id: build.xml,v 1.20 2005/03/13 20:16:51 janb Exp $
========================================================================
========================================================================

The default target is "all", which will build everything.

Other targets:

  clean              - Remove all generated files.
  prepare            - Set up build directory structure.
  run.jaas.demo      - run the JAAS demo webapp
  run.jettyplus.demo        - run the jettyplus demo webapp

-->

<project name="JettyPlus" default="all" basedir=".">

  <!-- ==================================================================== -->
  <property name="package"          value="jetty.plus" />
  <property environment="env"/>
  <property file="../../ant.properties" />
  <property file="../build.package.properties" />

  <!-- ==================================================================== -->
  <property name="jettyplus.webapp"             value="${demo.webapps}/jettyplus"/>
  <property name="jettyplus.webapp.webinf"      value="${jettyplus.webapp}/WEB-INF"/>
  <property name="jettyplus.webapp.classes"     value="${jettyplus.webapp.webinf}/classes"/>
  <property name="jaas.webapp"           value="${demo.webapps}/jaas"/>
  <property name="jaas.webapp.webinf"    value="${jaas.webapp}/WEB-INF"/>
  <property name="jaas.webapp.classes"   value="${jaas.webapp.webinf}/classes"/>
  <property name="jaas.jar"              value="${lib}/org.mortbay.jaas.jar"/>
  <property name="policy.file"           value="${etc}/jaas.policy"/>
  


  <!-- ==================================================================== -->
  <target name="is.jdk1.4.available">
    <available property="jdk1.4.available" classname="java.util.logging.Handler" />
    <echo message="JDK1.4 available=${jdk1.4.available}" level="info"/>
  </target>

  <!-- ==================================================================== -->
  <!-- Classpath                                                            -->
  <!-- ==================================================================== -->
  <target name="classpath" depends="is.jdk1.4.available" if="jdk1.4.available">
    <path id="extpath">
     <fileset dir="${jetty.ext}">
        <include name="*.jar"/>
        <include name="*.JAR"/>
        <include name="*.zip"/>
        <include name="*.ZIP"/>
     </fileset>
      <fileset dir="${ext}">
        <include name="*.jar"/>
        <include name="*.JAR"/>
        <include name="*.zip"/>
        <include name="*.ZIP"/>
     </fileset>
    </path>

    <path id="resourcepath">
      <pathelement path="${basedir}/../resources"/>
    </path>

    <path id="libpath">
     <fileset dir="${lib}">
        <include name="org.mortbay.jaas.jar"/>
        <include name="org.mortbay.jetty.plus.jar"/>
     </fileset>
    </path>
    <path id="classpath">
      <pathelement location="${jetty.jar}" />
      <pathelement location="${servlet.jar}" />
      <pathelement location="${mortbay.jmx.jar}" />
      <path refid="extpath" />
    </path>



    <property name="expanded.classpath" refid="classpath"/>
    <echo message="classpath=${expanded.classpath}" level="info"/>
  </target>


  <!-- ==================================================================== -->
  <!-- Prepare                                                              -->
  <!-- ==================================================================== -->
  <target name="prepare" depends="classpath" description="Make build directories">
    <mkdir dir="${classes}" />
    <mkdir dir="${lib}" />
    <mkdir dir="${logs}" />
  </target>


  <!-- ==================================================================== -->
  <!-- Tidy                                                                 -->
  <!-- ==================================================================== -->
  <target name="tidy" description="Remove classes">
    <delete dir="${classes}" quiet="true"/>
    <delete includeEmptyDirs="true">
      <fileset dir="${demo.webapps}" includes="**/classes/**"/>
    </delete>
    <ant dir="${test}" antfile="${test}/build.xml" inheritAll="true" target="tidy"/>
  </target>


  <!-- ==================================================================== -->
  <!-- Clean                                                                -->
  <!-- ==================================================================== -->
  <target name="clean" depends="tidy"
     description="Remove generated files">
    <delete quiet="true">
      <fileset dir="${logs}">
        <include name="**/*.log"/>
      </fileset>
    </delete>
    <delete file="${jaas.jar}"/>
    <delete file="${package.jar}"/>
    <ant dir="${test}" antfile="${test}/build.xml" inheritAll="false" target="clean"/>
  </target>


  <!-- ==================================================================== -->
  <!-- Classes                                                              -->
  <!-- ==================================================================== -->
  <target name="classes" depends="prepare" description="Compile classes">
    <copy todir="${classes}">
      <fileset dir="${src}">
        <include name="**/*.properties" />
      </fileset>
    </copy>
    <javac srcdir="${src}"
           destdir="${classes}"
           debug="true"
           optimize="fale"
           deprecation="${javac.deprecation}">
       <classpath>
         <path refid="classpath"/>
       </classpath>
       <include name="**/*.java"/>
    </javac>
  </target>


  <!-- ==================================================================== -->
  <!-- jars                                                                 -->
  <!-- ==================================================================== -->
  <target name="jars" 
          depends="classes, build.jaas.jar, build.package.jar" 
	  description="Make jars">
  </target>



  <!-- ==================================================================== -->
  <!-- build.package.jar                                                    -->
  <!-- ==================================================================== -->
  <target name="build.package.jar" description="Make the JettyPlus jar file">
    <jar basedir="${classes}" 
         destfile="${package.jar}"
	 excludes="**/jaas/**">
    </jar>
  </target>

  <!-- ==================================================================== -->
  <!-- build.jaas.jar                                                       -->
  <!-- ==================================================================== -->
  <target name="check.jaas.available" depends="classes">
    <available property="jaas.available" file="${src}/org/mortbay/jaas/JAASPrincipal.java"/>
    <echo message="jaas available:${jaas.available}"/>
  </target>

  <target name="build.jaas.jar" 
          depends="classes, check.jaas.available" 
	  if="jaas.available"
	  description="Make the jaas jar file">
    <jar basedir="${classes}"
         destfile="${jaas.jar}"
	 includes="**/jaas/**">
    </jar>
  </target>


  <!-- ==================================================================== -->
  <!-- build.webapp.jaas                                                    -->
  <!-- ==================================================================== -->
  <target name="check.jaas.demo.available" >
    <available property="jaas.demo.available" file="${demo.src}/org/mortbay/webapps/jaas"/>
    <echo message="JAAS demo available: ${jaas.demo.available}"/>
  </target>

  <target name="build.jaas.webapp" 
          depends="jars, check.jaas.demo.available" 
	  if="jaas.demo.available" 
	  description="JAAS demo webapp">
    <mkdir dir="${jaas.webapp.classes}"/>
    <javac srcdir="${demo.src}"
           destdir="${jaas.webapp.classes}"
           debug="${javac.debug}"
           optimize="${javac.optimize}"
           deprecation="${javac.deprecation}">
      <classpath>
        <path refid="classpath" />
	<pathelement location="${jaas.jar}"/>
      </classpath>
      <include name="**/jaas/**.java" />
    </javac>
  </target>




  <!-- ==================================================================== -->
  <!-- webapp-security-prep                                                 -->
  <!-- ==================================================================== -->
  <target name="webapp-security-prep">
    <available property="policy.file.available" file="${policy.file}"/>
    <available property="login.conf.available" file="${etc}/login.conf"/>
  </target>



  <!-- ==================================================================== -->
  <!--  jdk-prep                                                            -->
  <!-- ==================================================================== -->
  <target name="check.jdk.env" unless="${jdk.home}">
    <property name="jdk.home" value="${env.JDK_HOME}"/>
  </target>
  <target name="check.java.env" unless="${jdk.home}">
    <property name="jdk.home" value="${env.JAVA_HOME}"/>
  </target>
  <target name="jdk-prep" depends="check.jdk.env, check.java.env" description="Check if JDK has been set">
    <echo message="jdk.home=${jdk.home}"/>
  </target>

  <!-- ==================================================================== -->
  <!-- build.jettyplus.webapp                                                      -->
  <!-- ==================================================================== -->
  <target name="build.jettyplus.webapp" 
          depends="jars" 
	  description="JettyPlus demo webapp">
    <mkdir dir="${jettyplus.webapp.classes}"/>
    <javac srcdir="${demo.src}"
           destdir="${jettyplus.webapp.classes}"
           debug="${javac.debug}"
           optimize="${javac.optimize}"
           deprecation="${javac.deprecation}">
      <classpath>
        <path refid="classpath" />
	<pathelement location="${package.jar}"/>
      </classpath>
      <include name="**/jettyplus/**.java" />
    </javac>
  </target>

  <!-- ==================================================================== -->
  <!-- JettyPlus demo webapp                                                -->
  <!-- ==================================================================== -->
  <target name="run.jettyplus.demo" depends="build.jettyplus.webapp" 
          description="Run a JettyPlus webapp demo"> 
     <java fork="true" jar="${jetty.home}/start.jar" dir="${jetty.home}"> 
       <jvmarg value="-DSTART=${etc}/start-plus.config"/>
       <!-- jvmarg value="-Dmain.class=org.mortbay.jetty.plus.Server"/ -->      
       <arg value="${etc}/jettyplus.xml"/>
     </java>
  </target>


  <!-- ==================================================================== -->
  <!-- JAAS demo webapp                                                     -->
  <!-- ==================================================================== -->
  <target name="run.jaas.demo" depends="jdk-prep, webapp-security-prep, build.jaas.webapp" if="jaas.demo.available">
     <fail message="Set up a test.policy file in ${policy.file}" unless="policy.file.available"/>
     <fail message="Set up a login config file in ${etc}/login.conf" unless="login.conf.available"/>
     <fail message="-Djdk.home not set" unless="jdk.home"/>
     <fail message="-Djetty.home not set" unless="jetty.home"/>
     <dirname property="install.dir" file="${ant.file}"/>
     <echo message="install.dir=${install.dir}"/>
     <echo message="dbUserName=sa"/>
     <echo message="dbPassword="/>
     <echo message="dbDriver=org.hsqldb.jdbcDriver"/>
     <echo message="dbUrl=jdbc:hsqldb:."/>
     <echo message="login config=${etc}/login.conf"/>
     <echo message="policy file=${etc}/jaas.policy"/>
     <echo message="java.io.tmpdir=${java.io.tmpdir}"/>
     <copy file="${install.dir}/demo/webapps/jaas/WEB-INF/template.xml"
           tofile="${install.dir}/demo/webapps/jaas/WEB-INF/web.xml"
	   overwrite="true"/>
     <replace file="${install.dir}/demo/webapps/jaas/WEB-INF/web.xml" 
              token="@dbDriver" value="org.hsqldb.jdbcDriver"/>
     <replace file="${install.dir}/demo/webapps/jaas/WEB-INF/web.xml" 
              token="@dbUrl" value="jdbc:hsqldb:."/>
     <replace file="${install.dir}/demo/webapps/jaas/WEB-INF/web.xml" 
              token="@dbUserName" value="sa"/>
     <replace file="${install.dir}/demo/webapps/jaas/WEB-INF/web.xml" 
              token="@dbPassword" value=""/>
<!--
     <java fork="true" jar="${jetty.home}/start.jar" dir="${jetty.home}"> 
       <jvmarg value="-Dinstall.dir=${install.dir}"/>
       <jvmarg value="-Djdk.home=${jdk.home}"/>
       <jvmarg value="-Djava.security.manager"/>
       <jvmarg value="-Djava.security.policy=${etc}/jaas.policy"/>
       <jvmarg value="-Djava.security.auth.login.config=${etc}/login.conf"/>
       <arg value="${etc}/jaas.xml"/>
     </java>
-->
     <java fork="true" classname="org.mortbay.jetty.Server" dir="${jetty.home}"> 
       <jvmarg value="-Dinstall.dir=${install.dir}"/>
       <jvmarg value="-Djetty.home=${jetty.home}"/>
       <jvmarg value="-Djdk.home=${jdk.home}"/>
       <jvmarg value="-Djava.security.manager"/>
       <jvmarg value="-Djava.security.policy=${etc}/jaas.policy"/>
       <jvmarg value="-Djava.security.auth.login.config=${etc}/login.conf"/>
       <jvmarg value="-DSTART=${jetty.home}/extra/etc/start.config"/>
       <arg value="${etc}/jaas.xml"/>
       <classpath>
	 <path refid="resourcepath"/>
         <path refid="classpath"/>
	 <path refid="libpath"/>
       </classpath>
     </java>
  </target>


  <!-- ==================================================================== -->
  <!-- test                                                                 -->
  <!-- ==================================================================== -->
  <target name="check.tests" description="check to see if there are any tests">
    <available property="tests.available" file="${test}/build.xml"/>
  </target>

  <!-- ==================================================================== -->
  <target name="test" 
          depends="check.tests" 
          if="tests.available" 
          description="run tests">
    <ant dir="${test}" antfile="${test}/build.xml" inheritAll="false" target="test"/>
  </target>


  <!-- ==================================================================== -->
  <!-- all                                                                  -->
  <!-- ==================================================================== -->
  <target name="webapps" depends="build.jaas.webapp, build.jettyplus.webapp" description="build webapps">
  </target>

  <!-- ==================================================================== -->
  <!-- all                                                                  -->
  <!-- ==================================================================== -->
  <target name="all" depends="jars,webapps" description="default">
  </target>
</project>
