<?xml version="1.0"?>

<project name="base" default="abort" basedir=".">

  <target name="abort">
    <echo message="This buildfile cannot be executed on its own"/>
    <echo message="It must be included"/>
  </target>

  <!-- some useful properties shorthands -->
  <property name="package" value="org.ecoinformatics.ecogrid"/>
  <property name="package.dir" value="org/ecoinformatics/ecogrid"/>

  <!-- directories used for generated files -->
  <property name="build.dir" value="./build"/>
  <property name="build.src" value="${build.dir}/src"/>
  <property name="build.lib" value="${build.dir}/lib"/>
  <property name="build.classes" value="${build.dir}/classes"/>
  <property name="build.schema" value="${build.dir}/schema"/>
  <property name="schema.dir" value="src/xsd"/>
  <property name="registry.schema.dir" value="src/xsd_reg"/>
  <property name="lib.dir" value="lib"/>
	
	<!--testing properties -->
	<property name="test.src" value="tests"/>
	<patternset id="test.files">
	    <include name="org/ecoinformatics/ecogrid/client/**"/>
	</patternset>
	
	  <!-- requiredjars is a patternset which is used to add jars to
	       the compileclasspath and to the webapp gar file -->
	<patternset id="requiredjars" />
  

  <property name="mapping.file" value="lib/namespace2package.mappings"/>

  <property name="axis.dir" value="${basedir}/${lib.dir}/axis-1_3"/>

  <target name="clean">
    <delete dir="${build.dir}"/>
  </target>

  <target name="mkdirs">
    <mkdir dir="${build.classes}"/>
    <mkdir dir="${build.src}"/>
    <mkdir dir="${build.lib}"/>
    <mkdir dir="${build.schema}"/>
  </target>

  <path id="compile.classpath">
    <fileset dir="${axis.dir}">
      <exclude name="axis-ant.jar"/>
      <include name="*.jar"/>
    </fileset>
    <fileset dir="${basedir}/${lib.dir}" includes="*.jar">
      <patternset refid="requiredjars"/>
    </fileset>
  	<pathelement location="${basedir}/${lib.dir}/metacat.jar"/>
  	<pathelement location="${basedir}/${lib.dir}/utilities.jar"/>
    <pathelement location="${tomcat.root}/common/lib/servlet-api.jar"/>
    <pathelement location="${basedir}/${lib.dir}/xalan.jar"/>
  </path>

  <path id="axis.classpath">
    <path refid="compile.classpath"/>
    <pathelement location="${axis.dir}/axis-ant.jar"/>
  </path>
	
	<path id="test.classpath">
		<path refid="compile.classpath"/>
		<fileset dir="${build.lib}">
			<include name="*stub*"/>
		</fileset>
	    <pathelement location="${basedir}/${lib.dir}/junit.jar"/>
	</path>

  <taskdef resource="axis-tasks.properties" classpathref="axis.classpath"/>

  <target name="setQueryClientProperties">
    <echo message="Setting properties for Query Client"/>

    <!-- define the interface name. -->
    <property name="interface.name" value="QueryService"/>

    <property name="stubjar.name" value="${package}.${interface.name}-stub.jar"/>

    <!-- client.files is a patternset used in the stubCompile and stubJar targets
         to add client files to the stub jar -->
    <selector id="client.files">
      <or>
      <filename name="${package.dir}/EcogridObjType.*"/>
      <filename name="${package.dir}/EcogridUtils.*"/>
      <filename name="${package.dir}/EcogridUtilsNamedNodeIterator.*"/>
      <filename name="${package.dir}/queryservice/**"/>
      </or>
    </selector>

    <!-- wsdl.file is the main wsdl file -->
    <property name="wsdl.file" value="${interface.name}.wsdl"/>

    <!-- schema.files is a patternset used to specify the schemas -->
    <patternset id="schema.files">
      <include name="${wsdl.file}"/>
      <include name="query.xsd"/>
      <include name="resultset.xsd"/>
    </patternset>
    
    <!-- schema.files is a patternset used to specify the schemas -->
    <patternset id="registry.schema.files">
      <include name="RegistryService.wsdl"/>
      <include name="RegistryEntryType.xsd"/>
      <include name="ecogridregistry.205.22.dtd"/>
    </patternset>

    <property name="deploy.wsdd" value="QueryServiceDeploy.wsdd"/>
    <property name="undeploy.wsdd" value="QueryServiceUndeploy.wsdd"/>
    
    <property name="registry.deploy.wsdd" value="RegistryDeploy.wsdd"/>
    <property name="registry.undeploy.wsdd" value="RegistryUndeploy.wsdd"/>

  </target>

  <!-- This implementation of setClientProperties is just a place
       to override and not use the QueryClientProperties -->
  <target name="setClientProperties" depends="setQueryClientProperties"/>

  <target name="setProperties" depends="setClientProperties">
    <echo message="base.setProperties"/>
    <property name="impljar.name" value="${package}.${interface.name}.${impl.class.name}.jar"/>
  </target>
	
	<target name="compileTest" depends="mkdirs,setProperties">
		<ant antfile="buildfiles/metacatImpl.xml"  dir="." target ="stubJar" inheritall="false">
					 <property name="target" value="metacatImpl"/>  	   	
		</ant>
		<ant antfile="buildfiles/metacatPut.xml"  dir="." target ="stubJar" inheritall="false">
							 <property name="target" value="metacatPut"/>  	   	
		</ant>
		<ant antfile="buildfiles/metacatAuth.xml"  dir="." target ="stubJar" inheritall="false">
									 <property name="target" value="metacatAuth"/>  	   	
		</ant>
		<ant antfile="buildfiles/metacatAuthorize.xml"  dir="." target ="stubJar" inheritall="false">
									 <property name="target" value="metacatAuthorize"/>  	   	
		</ant>
		<ant antfile="buildfiles/registry.xml"  dir="." target ="stubJar" inheritall="false">
											 <property name="target" value="registry"/>  	   	
		</ant>
		<ant antfile="buildfiles/AuthenticatedQueryService.xml"  dir="." target ="stubJar" inheritall="false">
													 <property name="target" value="AuthenticatedQueryService"/>  	   	
	    </ant>
		<ant antfile="buildfiles/identifierService.xml"  dir="." target ="stubJar" inheritall="false">
															 <property name="target" value="identifierService"/>  	   	
		</ant>
	    <javac srcdir="${test.src}" destdir="${build.classes}" debug="${debug}"
	           deprecation="${deprecation}"
	           source="1.4"
	           classpathref="test.classpath">
	      <patternset refid="test.files"/>
	    </javac>
	  </target>
	
  <target name="serverJar" depends="compileServer">
    <jar jarfile="${build.lib}/${impljar.name}">
      <fileset dir="${build.classes}">
        <patternset refid="impl.files"/>
      </fileset>
      <fileset dir="src">
    	<include name="xsd/xslt/**"/>
      </fileset>
    </jar>
  </target>

  <target name="compileServer" depends="mkdirs,setProperties,compileStubs">
    <javac srcdir="${build.src}" destdir="${build.classes}" debug="${debug}"
           deprecation="${deprecation}"
           source="1.4"
           classpathref="compile.classpath">
      <patternset refid="impl.files"/>
    </javac>
    <javac srcdir="src" destdir="${build.classes}" debug="${debug}"
           deprecation="${deprecation}"
           source="1.4"
           classpathref="compile.classpath">
      <patternset refid="impl.files"/>
    </javac>
  </target>

  <target name="stubJar" depends="compileStubs">
    <jar jarfile="${build.lib}/${stubjar.name}" index="true">
      <fileset dir="${build.classes}">
        <selector refid="client.files"/>
      </fileset>
      <fileset dir="${build.src}">
        <selector refid="client.files" if="client.files.jarall"/>
      </fileset>
      <fileset dir="src">
        <selector refid="client.files" if="client.files.jarall"/>
      </fileset>
    </jar>
  </target>

  <target name="compileStubs" depends="mkdirs,setProperties,generateStubs">
    <javac srcdir="${build.src}" destdir="${build.classes}" debug="${debug}"
      deprecation="${deprecation}"
      source="1.4"
      classpathref="compile.classpath">
      <selector refid="client.files"/>
    </javac>
    <javac srcdir="src" destdir="${build.classes}" debug="${debug}"
      deprecation="${deprecation}"
      source="1.4"
      classpathref="compile.classpath">
      <selector refid="client.files"/>
    </javac>
  </target>

  <target name="generateStubs" depends="mkdirs,setProperties,copySchemaFiles">
    <echo message=" Generating stubs from ${interface.name}/${wsdl.file} " />
    <!-- this task requires the namespace to package mapping file to be
         the default file for axis WSDL2Java translator which is
         NStoPkg.properties -->
    <axis-wsdl2java
      verbose="true"
      output="${build.src}"
      wrapArrays="true"
      namespacemappingfile="${mapping.file}"
      url="${build.schema}/${interface.name}/${wsdl.file}"/>
  </target>

  <target name="copySchemaFiles" depends="setProperties">
    <copy toDir="${build.schema}/${interface.name}" overwrite="true">
       <fileset dir="${schema.dir}">
         <patternset refid="schema.files"/>
       </fileset>
      <filterset>
        <filter token="ServiceName" value="http://localhost:8080/"/>
      </filterset>
    </copy>
  </target>

  <target name="filterSchemaAndDeployFiles" depends="mkdirs,setProperties">
    <copy toDir="${build.schema}/${interface.name}" overwrite="true">
       <fileset dir="${schema.dir}">
         <patternset refid="schema.files"/>
       </fileset>
      <filterset>
        <filter token="ServiceName" value="${service.name}"/>
      </filterset>
    </copy>

    <copy toDir="${build.dir}" overwrite="true">
      <fileset dir="lib">
        <include name="${deploy.wsdd}"/>
	<include name="${undeploy.wsdd}"/>
      </fileset>
      <filterset>
        <filter token="ServiceName" value="${service.name}"/>
	<filter token="ServiceImplementationClassName" value="${impl.package.name}.${impl.class.name}"/>
      </filterset>
    </copy>
  </target>
  
  <target name="deployRegistrySchemaFiles" depends="mkdirs,setProperties">
    <copy toDir="${build.schema}/RegistryService" overwrite="true">
       <fileset dir="${registry.schema.dir}">
         <patternset refid="registry.schema.files"/>
       </fileset>
    </copy>
    
    <copy toDir="${build.dir}" overwrite="true">
      <fileset dir="lib">
        <include name="${registry.deploy.wsdd}"/>
		<include name="${registry.undeploy.wsdd}"/>
      </fileset>
    </copy>
  </target>

  <!-- This target generates a straight forward war file for deployment in tomcat.
       It will not be deployable in the globus container.  Also, it currently works
       with Globus 3 and probably will need significant tweakage to support GT4.
       You gotta admit, it's pretty tidy -->
  <target name="war" depends="serverJar,stubJar,filterSchemaAndDeployFiles">
     <condition property="context" value="${impl.class.name}">
      <not>
        <isset property="context"/>
      </not>
    </condition>
    <war destfile="${context}.war"
         webxml="conf/${impl.class.name}/web.xml">
      <fileset dir="${build.dir}/" includes="schema/**"/>
      <fileset dir="conf/${impl.class.name}" excludes="web.xml" includes="*"/>
      <fileset dir="${build.dir}/" includes="*.wsdd"/>
      <webinf dir="conf/${impl.class.name}" includes="server-config.wsdd"/>
      <classes dir="conf/${impl.class.name}/classes" includes="**"/>
      <lib dir="${build.lib}" includes="*.jar"/>
      <lib dir="${basedir}/lib">
        <patternset refid="requiredjars"/>
      </lib>
      <lib dir="${axis.dir}">
        <include name="axis.jar"/>
        <include name="commons-discovery-0.2.jar"/>
        <include name="commons-logging-1.0.4.jar"/>
        <include name="jaxrpc.jar"/>
        <include name="log4j-1.2.8.jar"/>
        <include name="saaj.jar"/>
        <include name="wsdl4j-1.5.1.jar"/>
      </lib>
    </war>
    <echo message="Deployment instructions:"/>
    <echo message="The war file ${impl.class.name}.war needs to be placed in Tomcat webapps directory."/>
    <echo message="If running Tomcat 4.x, place a copy of xalan.jar in Tomcat/common/endorsed/."/>
    <echo message="Restart Tomcat."/>
    <echo message="In this directory, execute the ant deploy target."/>
  </target>

  <target name="testQuery" depends="setProperties">
    <antcall target="testInternal">
      <param name="mainclass" value="org.ecoinformatics.ecogrid.queryservice.QueryServiceClient"/>
    </antcall>
  </target>

  <target name="testGet" depends="setProperties">
    <antcall target="testInternal">
      <param name="mainclass" value="org.ecoinformatics.ecogrid.queryservice.QueryServiceGetToStreamClient"/>
      <param name="args" value="http://localhost:8080/knb/services/QueryService bowdish.292.7 testGet.xml"/>

    </antcall>
  </target>

  <target name="testInternal" depends="setProperties">
    <java classname="${mainclass}">
      <arg line="${args}"/>
      <classpath>
        <path refid="compile.classpath"/>
        <pathelement location="${build.lib}/${stubjar.name}"/>
        <pathelement location="${tomcat.root}/common/endorsed/xercesImpl.jar"/>
      </classpath>
    </java>
  </target>
	
	<target name="testAll" depends="compileTest">
		   <junit printsummary="yes" haltonfailure="no" fork="yes"
            haltonerror="no">
		   	<formatter type="plain" />
			<batchtest fork="yes" todir="${build.dir}">
				<fileset dir="${build.classes}">
					<include name="**/*Test.class"/>
					<exclude name="**/*MetacatImplTest.class"/> 
					<exclude name="**/*EcoGridFTPTransferTest.class"/> 
				</fileset>
			</batchtest>
			<classpath>
	    		<path refid="test.classpath"/>
	    		<pathelement location="${build.classes}"/>
	    	</classpath>
		</junit>
	  </target>
	
	<target name="runonetest" depends="compileTest">
		      <echo>testtorun: ${testtorun}</echo>
			   <junit printsummary="yes" haltonfailure="no" fork="yes"
	            haltonerror="no">
			   	<formatter type="plain" />
				<batchtest fork="yes" todir="${build.dir}">
					<fileset dir="${build.classes}">
						<include name="**/${testtorun}.class"/>
					</fileset>
				</batchtest>
				<classpath>
		    		<path refid="test.classpath"/>
		    		<pathelement location="${build.classes}"/>
		    	</classpath>
			</junit>
		  </target>

  <target name="deploy" depends="setProperties">
    <condition property="context" value="${impl.class.name}">
      <not>
        <isset property="context"/>
      </not>
    </condition>
    <condition property="hostname" value="localhost">
      <not>
        <isset property="hostname"/>
      </not>
    </condition>
    <condition property="port" value="8080">
      <not>
        <isset property="port"/>
      </not>
    </condition>
    <axis-admin port="${port}" hostname="${hostname}" servletpath="${context}/services/AdminServlet"
                failonerror="true"
		xmlfile="${build.dir}/${deploy.wsdd}"
		/>
  </target>
</project>