<?xml version="1.0"?>
<project name="jMoby: Remote service deployment" default="deploy" basedir=".">

   <!-- ================================================================== -->
   <!--                            Properties                              -->
   <!-- ================================================================== -->

   <!-- General properties -->
   <property environment="env"/>
   <property name="project.home" location="."/>
   <!-- Give user a chance to override without editing this file
        (and without typing -D each time it compiles it) -->
   <property file="${project.home}/build.properties"/>
   <property file="${user.home}/build.properties"/>

   <!-- The directory containing things to be deployed -->
   <property name="wsdd.deploy.dir"    location="${project.home}/deploy-wsdd"/>
   <property name="wsdd.undeploy.dir"  location="${project.home}/undeploy-wsdd"/>
   <property name="lib.dir"            location="${project.home}/lib"/>
   <property name="lib.dir.2"          location="${project.home}/lib2"/>
   <property name="classes"            location="${project.home}/classes"/>

   <!-- Tomcat/Axis/servlets related properties -->
   <property name="tomcat.home"        value="@TOMCAT_HOME@"/>
   <property name="tomcat.port"        value="@TOMCAT_PORT@"/>
   <property name="tomcat.host"        value="@TOMCAT_HOST@"/>
   <property name="axis.relative.path" value="@AXIS_RELATIVE_PATH@"/>
   <property name="axis.admin.url"     value="@AXIS_ADMIN_URL@"/>

   <property name="tomcat.webapps" value="@TOMCAT_WEBAPPS@"/>

   <!-- ================================================================== -->
   <!-- Message what and where will be deployed                            -->
   <!-- ================================================================== -->
   <target name="info" depends="init"
     description="Print what and where can be deploy">
     <echo taskname="info">
===========================================================
The following properties define where and how the services
will be deployed. You can still define new values for them.

tomcat.home = ${tomcat.home}
   All libraries (.jar files) will be moved here.

tomcat.host = ${tomcat.host}
   Where is your Tomcat installed. If this is not localhost
   you may need to allow remote control in server-config.wsdd
   before deploying services.

tomcat.port = ${tomcat.port}
   Where is your Tomcat listening.
   
tomcat.webapps = ${tomcat.webapps}
   Where the deployment directory is 'e.g. webapps'

axis.relative.path = ${axis.relative.path}
   Where is Axis installed within Tomcat's 'webapps' dir.
   
axis.admin.url = ${axis.admin.url}
   How to access your Tomcat/Axis.
   
For the list of services that will be deployed when you call
'ant deploy' (or undeploy when you call 'ant undeploy')
see the contents of 'deploy-wsdd' or 'undeploy-wsdd'.
===========================================================
     </echo>
   </target>

   <!-- ================================================================== -->
   <!-- Initialize (whatever needs to be done)                             -->
   <!-- ================================================================== -->
   <target name="init">

     <condition property="real.catalina.home" value="${env.CATALINA_HOME}">
       <isset property="env.CATALINA_HOME"/>
     </condition>
     <property name="real.catalina.home" value="${tomcat.home}"/>
 	 <condition property="real.server.host" value="${tomcat.host}">
  		<isset property="tomcat.host"/>
  	</condition>
  	<condition property="real.server.port" value="${tomcat.port}">
  		<isset property="tomcat.port"/>
  	</condition>
  	<condition property="real.server.host" value="${server.host}">
  		<isset property="server.host"/>
  	</condition>
  	<condition property="real.server.port" value="${server.port}">
  		<isset property="server.port"/>
  	</condition>

     <!-- properties set from other properties set in conditions above -->
     <property name="deploy.axis.dir"
       location="${real.catalina.home}/${tomcat.webapps}/${axis.relative.path}/WEB-INF"/>
     
     <!-- Classpath -->
     <path id="build.classpath">
       <fileset dir="${lib.dir}">
         <include name="*.jar"/>
       </fileset>
     </path>
     
     <!-- contributed tasks -->
     <taskdef resource="net/sf/antcontrib/antlib.xml">
       <classpath refid="build.classpath"/>
     </taskdef>
     
   </target>

   <!-- ================================================================== -->
   <!-- Do it (deploy)                                                     -->
   <!-- ================================================================== -->
   <target name="deploy" depends="init,tomcat-running,tomcat-populate"
     description="Deploy BioMoby services.">

     <foreach
       inheritall="yes"
       target="axis-admin"
       param="wsdd.file">
       <fileset dir="${wsdd.deploy.dir}" includes="*.wsdd"/>
     </foreach>

   </target>

   <!-- ================================================================== -->
   <!-- Undeploy it                                                        -->
   <!-- ================================================================== -->
   <target name="undeploy" depends="init,tomcat-running"
     description="Undeploy BioMoby services.">

     <foreach
       inheritall="yes"
       target="axis-admin"
       param="wsdd.file">
       <fileset dir="${wsdd.undeploy.dir}" includes="*.wsdd"/>
     </foreach>

   </target>

   <!-- ================================================================== -->
   <!-- Make sure that a Tomcat has been started                           -->
   <!-- ================================================================== -->
    <target name="tomcat-running" depends="init">
      <condition property="tomcat.is.running">
        <socket server="${real.server.host}" port="${real.server.port}"/>
      </condition>
      <fail unless="tomcat.is.running">-
'

    ERROR: Server does not seem to be running.
    ------------------------------------------
    I have just checked ${real.server.host}:${real.server.port}...
    You may perhaps consider to start it.
    Or to set differently the following properties:
    "server.host" (current value: ${real.server.host}), and
    "server.port" (current value: ${real.server.port})
      </fail>
    </target>

   <!-- ================================================================== -->
   <!-- Populate local Tomcat with files needed for BioMoby Web Services   -->
   <!-- ================================================================== -->
   <target name="tomcat-populate" depends="init">

     <!-- merge jar files in ${lib.dir.2} with the same-named jar files in Tomcat/Axis -->
     <foreach
       inheritall="yes"
       target="-merge-jars"
       param="jarfile.name">
       <fileset dir="${lib.dir.2}" includes="*.jar"/>
     </foreach>

     <!-- copy jar files to Tomcat/Axis - lib directory -->
     <copy todir="${deploy.axis.dir}/lib">
       <fileset dir="${lib.dir}"/>
       <fileset dir="${lib.dir.2}"/>
     </copy>

     <!-- copy other files to Tomcat/Axis - classes directory -->
     <copy todir="${deploy.axis.dir}/classes">
       <fileset dir="${classes}"/>
     </copy>

   </target>

   <!-- parameter: ${jarfile.name} -->
   <target name="-merge-jars">
     <basename property="jarfile.base.name" file="${jarfile.name}"/>
     <if>
       <available file="${deploy.axis.dir}/lib/${jarfile.base.name}" type="file"/>
       <then>
         <tempfile property="merging.dir" destDir="${java.io.tmpdir}/mobydeploy-${user.name}"/>
         <mkdir dir="${merging.dir}"/>
         <unjar dest="${merging.dir}" src="${deploy.axis.dir}/lib/${jarfile.base.name}"/>
         <unjar dest="${merging.dir}" src="${jarfile.name}"/>
         <jar destfile="${jarfile.name}">
           <fileset dir="${merging.dir}"/>
           <manifest>
             <attribute name="Built-By" value="${user.name}"/>
           </manifest>
         </jar>
         <delete dir="${merging.dir}"/>
       </then>
     </if>

   </target>


   <!-- ================================================================== -->
   <!-- Calling an Axis Admin client to do the real [un]deploying.         -->
   <!--    parameter: ${wsdd.file} ... a file with a [un]deployment descr. -->
   <!-- ================================================================== -->
   <target name="axis-admin" depends="tomcat-running">
     
     <basename property="deploy.task.name" file="${wsdd.file}" suffix="wsdd"/>

     <!-- deploy (or undeploy) services (using given deployment descriptor) -->
     <java classname="org.apache.axis.client.AdminClient" taskname="${deploy.task.name}"
       classpathref="build.classpath" fork="true" failonerror="true">
       <arg value="-h"/>
       <arg value="${tomcat.host}"/>
       <arg value="-p"/>
       <arg value="${tomcat.port}"/>
       <arg value="-s"/>
       <arg value="${axis.admin.url}"/>
       <arg file="${wsdd.file}"/>
     </java>
     
   </target>
   
</project>

