<!--
  ~ 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="faulthandling" basedir="." default="generate.service">

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

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

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

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

        <fail message="One of the parameters are not set. Usage: BankClient -Durl=&lt;url&gt; -Daccount=&lt;account&gt; -Damt=&lt;amount&gt;"
        unless="params.ok"/>
        
        <java classname="example.BankClient" fork="true">
            <arg line="${url} ${account} ${amt}"/>
            <classpath>
                <pathelement location="${client.target}/BankService-test-client.jar"/>
                <fileset dir="../../lib">
                    <include name="*.jar"/>
                </fileset>
            </classpath>
        </java>
    </target>

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

    <target name="generate.service">

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

        <java classname="org.apache.axis2.wsdl.WSDL2Java" fork="true">
            <arg line="-uri ${wsdl.uri}"/>
            <arg line="-ss"/>
            <arg line="-sd"/>
            <arg line="-o ${service.target}"/>
            <!-- Output directory in which to generate files --> <!-- NB it generates to the src folder under this directory!! -->
            <classpath>
                <fileset dir="../../lib">
                    <include name="*.jar"/>
                </fileset>
            </classpath>
        </java>

        <!--copy the already written skeleton class-->
        <copy file="service/src/example/BankServiceSkeleton.java"
              todir="${service.target}/src/example"
              overwrite="yes"/>

        <!--First let's compile the classes-->
        <javac debug="on" memoryMaximumSize="256m" memoryInitialSize="256m" fork="true"
               destdir="${service.target}/classes" srcdir="${service.target}/src">
            <classpath>
                <fileset dir="../../lib">
                    <include name="*.jar"/>
                </fileset>
            </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}/BankService.aar">
            <fileset excludes="**/Test.class" dir="${service.target}/classes"/>
        </jar>

        <copy file="${service.target}/BankService.aar" tofile="../../repository/services/sample-faulthandling.aar"
              overwrite="true"/>

    </target>

    <target name="gen.stub">
        <delete dir="${client.target}"/>
        <mkdir dir="${client.target}"/>
        <mkdir dir="${client.target}/classes"/>
        <java classname="org.apache.axis2.wsdl.WSDL2Java" fork="true">
            <arg line="-uri ${wsdl.uri}"/>
            <arg line="-u"/>
            <arg line="-o ${client.target}"/>
            <classpath>
                <fileset dir="../../lib">
                    <include name="*.jar"/>
                </fileset>
            </classpath>
        </java>

        <copy file="client/src/example/BankClient.java" todir="${client.target}/src/example"/>

        <!--now compile the stub classes-->
        <javac debug="on" memoryMaximumSize="256m" memoryInitialSize="256m" fork="true"
               destdir="${client.target}/classes">
            <src path="${client.target}/src"/>
            <classpath>
                <fileset dir="../../lib">
                    <include name="*.jar"/>
                </fileset>
            </classpath>
        </javac>

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

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

</project>
