<!--
  ~ Licensed to the Apache Software Foundation (ASF) under one
  ~ or more contributor license agreements. See the NOTICE file
  ~ distributed with this work for additional information
  ~ regarding copyright ownership. The ASF licenses this file
  ~ to you under the Apache License, Version 2.0 (the
  ~ "License"); you may not use this file except in compliance
  ~ with the License. You may obtain a copy of the License at
  ~
  ~ http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing,
  ~ software distributed under the License is distributed on an
  ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  ~ KIND, either express or implied. See the License for the
  ~ specific language governing permissions and limitations
  ~ under the License.
  -->

<project name="databinding" basedir="." default="generate.service">

    <property environment="env"/>

    <property name="wsdl.uri" value="StockQuoteService.wsdl"/>
    <property name="service.target" value="build/service"/>
    <property name="client.target" value="build/client"/>

    <path id="axis2.classpath">
        <fileset dir="../../lib">
            <include name="*.jar"/>
        </fileset>
        <fileset dir="lib">
            <include name="*.jar"/>
        </fileset>
    </path>

    <macrodef name="CastorSourceGenerator">
       <attribute name="schema" default="NOT SET"/>
       <attribute name="package" default="NOT SET"/>
       <attribute name="output" default="NOT SET"/>
       <sequential>
          <echo/>
          <java classname="org.exolab.castor.builder.SourceGeneratorMain" fork="true">
              <classpath refid="axis2.classpath"/>
              <arg line="-i @{schema}  -package @{package} -dest @{output}" />
          </java>
       </sequential>
    </macrodef>

    <!-- Build the service, then the client. -->
    <target name="jar">
        <ant dir="service" target="jar.server"/>
        <ant dir="client"/>
    </target>

    <target name="run.client" depends="generate.stub">

        <condition property="params.ok">
            <and>
                <isset property="url"/>
                <isset property="symbol"/>
            </and>
        </condition>

        <fail message="One of the parameters are not set. Usage: StockClient -Durl=&lt;url&gt; -Dsymbol=&lt;symbol&gt;"
        unless="params.ok"/>
        
        <java classname="samples.databinding.StockClient" fork="true">
            <arg line="${url} ${symbol}"/>
            <classpath>
                <pathelement location="${client.target}/StockService-test-client.jar"/>
                <path refid="axis2.classpath"/>
            </classpath>
        </java>
    </target>

    <target name="generate.all" depends="generate.service,generate.stub"/>

    <target name="generate.service">

        <delete dir="${service.target}"/>
        <mkdir dir="${service.target}"/>
        <mkdir dir="${service.target}/classes"/>

        <CastorSourceGenerator output="${service.target}/src"
                               package="samples.databinding.data"
                               schema="StockQuote.xsd"/>
        <java classname="org.apache.axis2.wsdl.WSDL2Java" fork="true" classpathref="axis2.classpath">
            <arg line="-uri ${wsdl.uri}"/>
            <arg line="-ss"/>
            <arg line="-sd"/>
            <arg line="-ap"/>
            <arg line="-p samples.databinding"/>
            <arg line="-d none"/>
            <arg line="-o ${service.target}"/>
        </java>

        <!--copy the already written skeleton class-->
        <copy todir="${service.target}/src/"
              overwrite="yes">
            <fileset dir="service/src">
                <include name="**/*.java"/>
            </fileset>
        </copy>

        <!--First let's compile the classes-->
        <javac debug="on" memoryMaximumSize="256m" memoryInitialSize="256m" fork="true"
               destdir="${service.target}/classes" srcdir="${service.target}/src"
               classpathref="axis2.classpath">
        </javac>

        <!--aar them up -->
        <copy toDir="${service.target}/classes/META-INF" failonerror="false">
            <fileset dir="${service.target}/resources">
                <include name="*.xml"/>
                <include name="*.wsdl"/>
                <include name="*.xsd"/>
            </fileset>
        </copy>
        <jar destfile="${service.target}/StockService.aar">
            <fileset excludes="**/Test.class" dir="${service.target}/classes"/>
            <fileset dir="${basedir}">
                <include name="lib/*.jar"/>
            </fileset>
        </jar>

        <copy file="${service.target}/StockService.aar" tofile="../../repository/services/StockService.aar"
              overwrite="true"/>

    </target>

    <target name="generate.stub" unless="skip.generate">
        <delete dir="${client.target}"/>
        <mkdir dir="${client.target}"/>
        <mkdir dir="${client.target}/classes"/>


        <CastorSourceGenerator output="${client.target}/src"
                               package="samples.databinding.data"
                               schema="StockQuote.xsd"/>
        <java classname="org.apache.axis2.wsdl.WSDL2Java" fork="true" classpathref="axis2.classpath">
            <arg line="-uri ${wsdl.uri}"/>
            <arg line="-u"/>
            <arg line="-ap"/>
            <arg line="-p samples.databinding"/>
            <arg line="-d none"/>
            <arg line="-t"/>
            <arg line="-o ${client.target}"/>
        </java>

        <copy todir="${client.target}/src/"
              overwrite="yes">
            <fileset dir="client/src">
                <include name="**/*.java"/>
            </fileset>
        </copy>

        <!--now compile the stub classes-->
        <javac debug="on" memoryMaximumSize="256m" memoryInitialSize="256m" fork="true"
               srcdir="${client.target}/src"
               excludes="**/StockClient2*.*"
               destdir="${client.target}/classes"
               classpathref="axis2.classpath">
        </javac>

        <!--jar the compiled stuff-->
        <jar destfile="${client.target}/StockService-test-client.jar">
            <fileset dir="${client.target}/classes">
                <exclude name="**/META-INF/*.*"/>
                <exclude name="**/*MessageReceiver.class"/>
                <exclude name="**/*Skeleton.class"/>
            </fileset>
        </jar>
    </target>

    <!--We are not shipping castor and stax-utils jars with the release. This target can be used to
    download those jars that are required to run this sample.-->
    <target name="download.jars">
       <mkdir dir="lib"/>

        <get src="http://ws.zones.apache.org/repository/stax-utils/jars/stax-utils-20060915.jar"
             dest="lib/stax-utils-20060915.jar" verbose="true"/>
        <get src="http://dist.codehaus.org/castor/1.0.4/castor-1.0.4.jar" dest="lib/castor-1.0.4.jar"
             verbose="true"/>
        <get src="http://repo2.maven.org/maven2/xerces/xercesImpl/2.9.1/xercesImpl-2.9.1.jar"
             dest="lib/xercesImpl-2.9.1.jar" verbose="true"/>
    </target>

    <target name="clean">
        <delete dir="build"/>
    </target>

</project>
