<?xml version="1.0" encoding="UTF-8"?>
<!--

    DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.

    Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.

    The contents of this file are subject to the terms of either the GNU
    General Public License Version 2 only ("GPL") or the Common Development
    and Distribution License("CDDL") (collectively, the "License").  You
    may not use this file except in compliance with the License.  You can
    obtain a copy of the License at
    https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
    or packager/legal/LICENSE.txt.  See the License for the specific
    language governing permissions and limitations under the License.

    When distributing the software, include this License Header Notice in each
    file and include the License file at packager/legal/LICENSE.txt.

    GPL Classpath Exception:
    Oracle designates this particular file as subject to the "Classpath"
    exception as provided by Oracle in the GPL Version 2 section of the License
    file that accompanied this code.

    Modifications:
    If applicable, add the following below the License Header, with the fields
    enclosed by brackets [] replaced by your own identifying information:
    "Portions Copyright [year] [name of copyright owner]"

    Contributor(s):
    If you wish your version of this file to be governed by only the CDDL or
    only the GPL Version 2, indicate your decision by adding "[Contributor]
    elects to include this software in this distribution under the [CDDL or GPL
    Version 2] license."  If you don't indicate a single choice of license, a
    recipient has the option to distribute your version of this file under
    either the CDDL, the GPL Version 2 or to extend the choice of license to
    its licensees as provided above.  However, if you add GPL Version 2 code
    and therefore, elected the GPL Version 2 license, then the option applies
    only if the new code is made subject to such option by the copyright
    holder.

-->

<project basedir="." default="help" name="rmdemo">


    <property environment="env"/>


    <property file="build.properties"/>
    <property name="WSIT_HOME" value="${basedir}/../../../../dist/image/metro"/>
    <property name="lib.home" value="${WSIT_HOME}/lib"/>
    <!--<property name="dist.home" value="${WSIT_HOME}/dist/image"/>-->
    <property name="build.home" value="${basedir}/build"/>
    <property name="build.classes.home" value="${build.home}/classes"/>
    <property name="build.war.home" value="${build.home}/war"/>
    <!--<property name="wsit.dir" value="${WSIT_HOME}/metro"/>-->
    <property name="wsit.dir" value="${WSIT_HOME}"/>

     <!-- Test Execution Defaults -->
    <condition property="usetomcat" value="true">
      <not>
             <isset property="useglassfish"/>
      </not>
    </condition>
    <path id="wsrm.classpath">
        <pathelement location="${java.home}/../lib/tools.jar"/>
        <fileset dir="${lib.home}">
            <include name="*.jar"/>
        </fileset>
         <!--<fileset dir="${dist.home}/metro/lib">
            <include name="*.jar"/>
        </fileset>-->
    </path>

    <taskdef name="wsimport" classname="com.sun.tools.ws.ant.WsImport">
        <classpath refid="wsrm.classpath"/>
    </taskdef>

    <target name="setup">
        <mkdir dir="${build.home}"/>
        <mkdir dir="${build.classes.home}"/>
        <mkdir dir="${build.classes.home}/src"/>
        <mkdir dir="${build.war.home}"/>
    </target>

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

    <target name="build-server-wsdl" depends="setup">
        <wsimport
                debug="true"
                verbose="true"
                keep="true"
                extension="true"
                destdir="${build.classes.home}"
                fork="true"
                sourcedestdir="${build.classes.home}/src"
                wsdl="${server.wsdl}">
            <binding dir="${server.dir}/etc" includes="${server.binding}"/>
        </wsimport>
        <javac
                fork="true"
                srcdir="${build.classes.home}/src"
                destdir="${build.classes.home}"
                includes="**/server/*.java">
            <classpath refid="wsrm.classpath"/>
        </javac>
    </target>

    <target name="create-war" depends="build-tests">
        <war warfile="${build.war.home}/${warName}.war" webxml="${server.dir}/etc/web.xml">
            <webinf dir="${server.dir}/etc" includes="sun-jaxws.xml"/>
            <zipfileset
                    dir="${server.dir}/etc"
                    includes="*.wsdl, *.xsd"
                    prefix="WEB-INF/wsdl"/>
            <classes dir="${build.classes.home}"/>
        </war>
    </target>

    <target name="build-tests"
        depends="setup"
        description="builds the tests">

        <javac
            srcdir="${server.dir}"
            includes="**/*.java"
            destdir="${build.classes.home}"
            source="1.5"
            debug="${debug}"
            deprecation="${deprecation}"
            optimize="${optimize}">
            <classpath refid="wsrm.classpath" />

        </javac>
    </target>

    <target name="generate-client" depends="setup">
        <wsimport
                debug="${debug}"
                verbose="true"
                keep="true"
                extension="true"
                destdir="${build.classes.home}"
                wsdl="${test.wsdl}">
            <binding dir="${client.dir}/etc" includes="${client.binding}"/>
        </wsimport>
    </target>

    <target name="client" depends="generate-client">
        <javac
                fork="true"
                srcdir="${client.dir}"
                destdir="${build.classes.home}"
                includes="**/*.java">
            <classpath refid="wsrm.classpath"/>
        </javac>
    </target>

    <target name="run">
        <java fork="true" classname="${client}">
            <classpath>
                <path refid="wsrm.classpath"/>
                <pathelement location="${build.classes.home}"/>
                <pathelement location="${basedir}/etc"/>
            </classpath>
            <!--Uncomment me if you want to see the detail
                WS RM protocol messages flowing on the wire

            <jvmarg value="-Dcom.sun.xml.ws.transport.http.client.HttpTransportPipe.dump=true"/>
            -->
        </java>
    </target>

    <target name="help">
        <echo message="server:       Builds and deploy the service endpoint WAR"/>
        <echo message="client:       Builds the client"/>
        <echo message="run:          Runs the client"/>
    </target>


    <target name="server" depends="setup">

        <antcall target="clean"/>

        <antcall target="build-server-wsdl"/>

        <antcall target="create-war"/>

        <antcall target="deploy"/>
    </target>

    <target name="install" depends="clean">
        <antcall target="install-glassfish"/>
        <antcall target="install-tomcat"/>
     </target>

    <target name="install-glassfish" if="useglassfish">
     <!-- remove old test WAR files -->
     <delete verbose="true">
       <fileset dir="${env.AS_HOME}/domains/domain1/autodeploy"
               includes="rm*.war"/>
     </delete>
     <!-- install WSRM & other needed JAXWS-related files -->
     <!--todo save the installation file-->
     <ant dir="${wsit.dir}"
           antfile="metro-on-glassfish.xml" target="install"
          inheritAll="false"/>
     </target>

     <target name="install-tomcat" if="usetomcat">
     <!-- remove old test WAR files -->
     <delete verbose="true">
       <fileset dir="${env.CATALINA_HOME}/webapps"
               includes="rm*.war"/>
     </delete>
     <!-- install WSRM & other needed JAXWS-related files -->
     <!--todo save the installation file-->
     <ant dir="${wsit.dir}"
           antfile="metro-on-tomcat.xml" target="install"
           inheritAll="false"/>
     </target>

     <target name="deploy" depends="create-war">
        <antcall target="deploy-war-tomcat"/>
        <antcall target="deploy-war-glassfish"/>
     </target>

     <target name="deploy-war-tomcat" depends="create-war" if="usetomcat">
        <property name="catalina.home" value="${env.CATALINA_HOME}"/>
        <echo message="catalina.home = ${catalina.home}"/>

        <copy todir="${catalina.home}/webapps"
              file="${build.war.home}/${warName}.war" verbose="false"
              overwrite="true"/>

     </target>

     <target name="deploy-war-glassfish" depends="create-war" if="useglassfish">
        <property name="as.home" value="${env.AS_HOME}"/>
        <echo message="as.home = ${env.AS_HOME}"/>

        <copy todir="${as.home}/domains/domain1/autodeploy"
              file="${build.war.home}/${warName}.war"
              verbose="false" overwrite="true"/>

     </target>
</project>
