<!--

    Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.

    This program and the accompanying materials are made available under the
    terms of the Eclipse Distribution License v. 1.0, which is available at
    http://www.eclipse.org/org/documents/edl-v10.php.

    SPDX-License-Identifier: BSD-3-Clause

-->

<property file="build.properties"/>
<!--<property file="../build.properties"/>-->
<property name="lib.sample.home" value="${basedir}/../lib"/>
<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="build.lib.home" value="${build.home}/lib"/>
<!--target name="copy-am-files">   
        <echo message="Copying AM Files"/>
        <copy file="${basedir}/../../am/AMConfig.properties"
                tofile="${build.classes.home}/AMConfig.properties" />
        <mkdir dir="${build.lib.home}"/>
        <copy file="${basedir}/../../am/amclientsdk.jar"
            tofile="${build.lib.home}/amclientsdk.jar" />
        <copy file="${basedir}/../../am/jaas.jar"
            tofile="${build.lib.home}/jaas.jar" />
        <copy file="${basedir}/../../am/jdk_logging.jar"
            tofile="${build.lib.home}/jdk_logging.jar" />
</target-->

<target name="setup-tc" if="tomcat.home">
    <echo message="In setup-TC in targets.xml"/>
    <mkdir dir="${build.home}"/>
    <mkdir dir="${build.classes.home}"/>
    <mkdir dir="${build.war.home}"/>
    <echo>TC</echo>
    &tomcat;
</target>

<target name="setup-glassfish" if="glassfish.home">
    <echo message="In setup-Glassfish in targets.xml"/>
    <mkdir dir="${build.home}"/>
    <mkdir dir="${build.classes.home}"/>
    <mkdir dir="${build.war.home}"/>
    <echo>GlassFish</echo>
    &glassfish;
</target>

<target name="server" depends="setup-tc, setup-glassfish, compile-callbacks">	
    <antcall target="clean" />
    <antcall target="compile-callbacks" />
    <antcall target="build-server-wsdl" />
    <antcall target="create-server-war" />
    <antcall target="deploy-server" />
</target>

<target name="build-server-java" depends="setup-tc, setup-glassfish">
    <apt
        fork="true"
        debug="${debug}"
    	verbose="${verbose}"
    	destdir="${build.classes.home}"
    	sourcedestdir="${build.classes.home}"
    	sourcePath="${basedir}/${sample.name}">
        <classpath>
        <path refid="jaxws.classpath"/>
            <pathelement location="${basedir}/src/${sample.name}"/>
        </classpath>
        <option key="r" value="${build.home}"/>
        <option key="verbose" value="${verbose}"/>
        <source dir="${basedir}/src/${sample.name}">
            <include name="**/server/*.java"/>
	    <include name="**/common/*.java"/>
            <include name="**/${ant.project.name}/server/*.java"/>
        </source>
    </apt>
    <!-- copy handlers descriptor file -->
    <copy todir="${build.classes.home}">
        <fileset dir="${basedir}/src/${sample.name}">
            <include name="**/server/**/*.xml"/>
        </fileset>
    </copy>
</target>

<target name="server-j2se" depends="setup-tc, setup-glassfish">

    <antcall target="clean" />
    <antcall target="build-server-java" />
    <java fork="true" classname="${j2se.sei}">
        <classpath>
            <path refid="jaxws.classpath"/>
            <pathelement location="${build.classes.home}"/>
        </classpath>
        <jvmarg value="-Djakarta.xml.ws.EndpointFactory=com.sun.xml.ws.transport.http.server.EndpointFactoryImpl"/>
    </java>

</target>

<target name="build-server-wsdl" depends="setup-tc">
    <echo message="In build-server-wsdl in targets.xml"/>
    <echo> server wsdl is : ${server.wsdl}</echo>
	<wsimport
        fork="false"
        debug="${debug}"
        verbose="${verbose}"
        keep="${keep}"
        destdir="${build.classes.home}"
        extension="${extension}"
        wsdl="${basedir}/${server.wsdl}">
        <binding dir="${basedir}" includes="${server.binding}"/>
        <jvmarg value="-Dhttp.proxyHost=${http.proxyHost}"/>
        <jvmarg value="-Dhttp.proxyPort=${http.proxyPort}"/>
        <jvmarg value="-Dhttps.proxyHost=${https.proxyHost}"/>
        <jvmarg value="-Dhttps.proxyPort=${https.proxyPort}"/>
    </wsimport>
    <echo message="In javac in build-server-wsdl in targets.xml"/>
    <javac
        fork="true"
        debug="${debug}"
        srcdir="${basedir}"
        destdir="${build.classes.home}"
        includes="**/server/**">
    	<classpath>
    	    <path refid="jaxws.classpath"/>
    	</classpath>
    </javac>
</target>

<target name="create-server-war">
    <echo message="In create-server-war in targets.xml"/>
    <war warfile="${build.war.home}/jaxws-${ant.project.name}.war" webxml="../../etc/service/web.xml">
	<webinf dir="../../etc/service" includes="sun-jaxws.xml"/>
	<zipfileset
            dir="etc/service"
            includes="PingService.wsdl, *.xsd"
            prefix="WEB-INF/wsdl"/>
         <zipfileset
            dir="../../etc/service"
            includes="wsit-server.xml"
            prefix="WEB-INF/classes"/>
	<classes dir="${build.classes.home}"/>
    </war>
</target>

  <!--Tomcat -->
  <target name="check-if-server-deployed-tom" if="tomcat.home">
    <echo message="Checking for deployed webapp at context path /${server.path}"/>
    <condition property="tom.webapp.deployed">
      <http url="http://${endpoint.host}:${endpoint.port}/${server.path}/"/>
    </condition>
    <echo message="${tom.webapp.deployed}" />
  </target>

 <target name="undeploy-tomcat-proper" if="webapp.deployed">
      <echo message="Undeploying existing webapp at ${path}"/>
      <undeploy-catalina url="http://${endpoint.host}:${endpoint.port}/manager/" username="${username}" password="${password}" path="/${path}"/>
  </target>
                                                                                                                          
  <target name="undeploy-tomcat-war-present" if="war.present">
      <echo message="Undeploying existing webapp at ${path}"/>
      <undeploy-catalina url="http://${endpoint.host}:${endpoint.port}/manager/" username="${username}" password="${password}" path="/${path}"/>
  </target>
                                                                                                                          
  <target name="undeploy-server-tomcat" if="tomcat.home">
   <echo message="Undeploying existing webapp at ${server.path}"/>
    <condition  property="war.present">
        <available file="${tomcat.home}/webapps/jaxws-${ant.project.name}.war" />
    </condition>
    <!--antcall target="undeploy-tomcat-war-present" /-->
    <!--<condition property="webapp.deployed">
      <http url="http://${endpoint.host}:${endpoint.port}/${path}/"/>
    </condition>
    <antcall target="undeploy-tomcat-proper" />-->
 </target>

<target name="deploy-server-tc" depends="check-if-server-deployed-tom, undeploy-server-tomcat" if="tomcat.home">

    <echo message="Deploying war for ${ant.project.name} to tomcat"/>

<!--deploy-catalina url="http://localhost:8080/manager" username="${username}" password="${password}"
            path="/${path}" war="file:${build.war.home}/jaxws-${ant.project.name}.war"/-->
     <copy file="${build.war.home}/jaxws-${ant.project.name}.war" todir="${tomcat.home}/webapps"/>
</target>

<!--Glassfish -->
<target name="deploy-server-glassfish" if="glassfish.home">
    <copy file="${build.war.home}/jaxws-${ant.project.name}.war"
        todir="${glassfish.home}/domains/${domain}/autodeploy"/>
</target>

<target name="deploy-server" depends="deploy-server-tc, deploy-server-glassfish"/>

    <target name="compile-callbacks" depends="setup-tc, setup-glassfish" >
        <javac
            fork="true"
            debug="${debug}"
            srcdir="${basedir}/.."
            destdir="${build.classes.home}" 
            includes="common/SampleUsernamePasswordCallbackHandler.java, common/SampleUsernamePasswordValidator.java, common/SampleSTSAttributeProvider.java">
            <classpath refid="jaxws.classpath"/>
        </javac>
    </target>

 <!--target name="compile-callbacks" depends="setup-tc, setup-glassfish" >
        <javac
            fork="true"
            debug="${debug}"
            srcdir="${basedir}/.."
            destdir="${build.classes.home}" 
            includes="common/SampleUsernamePasswordCallbackHandler.java, common/SampleAMSTSAuthorizationProvider.java, common/SampleAMUsernamePasswordValidator.java">
            <classpath refid="jaxws.classpath"/>
        </javac>
</target-->

<target name="generate-client" depends="setup-tc, setup-glassfish" >
    <wsimport
        fork="false"
        debug="${debug}"
        verbose="${verbose}"
        extension="${extension}"
        keep="${keep}"
        destdir="${build.classes.home}"
        wsdl="${client.wsdl}">
        <binding dir="${basedir}" includes="${client.binding}"/>
    </wsimport>
    <javac
        fork="true"
        debug="${debug}"
        srcdir="${build.classes.home}"
        destdir="${build.classes.home}"
        includes="**/client/**">
        <classpath>
            <pathelement location="${build.classes.home}"/>
            <path refid="jaxws.classpath"/>
        </classpath>
    </javac>
</target>

<target name="client" depends="generate-client, compile-callbacks">
    <javac
        fork="true"
        debug="${debug}"
        srcdir="${basedir}"
        destdir="${build.classes.home}"
        includes="**/client/**, **/util/** ">
        <classpath>
            <pathelement location="${build.classes.home}"/>
            <path refid="jaxws.classpath"/>
        </classpath>
    </javac>
</target>

<target name="run-tc-s1" if="tomcat.home">
   <mkdir dir="${build.classes.home}/META-INF/services"/>
   <copy file="${basedir}/../../etc/com.sun.xml.ws.policy.PlugfestPolicyMap" todir="${build.classes.home}/META-INF/services"/>
   <copy file="${basedir}/../../etc/javax.security.auth.callback.CallbackHandler.client" tofile="${build.classes.home}/META-INF/services/javax.security.auth.callback.CallbackHandler"/>
   <java fork="true" classname="${client}">
        <sysproperty key="tomcat.home" value="${tomcat.home}"/>
        <sysproperty key="endpoint.host" value="${endpoint.host}"/>
        <sysproperty key="endpoint.port" value="${endpoint.port}"/>
        <sysproperty key="service.url" value="${service.url}"/>
        <sysproperty key="DebugSecurity" value="true"/>
        <sysproperty key="javax.net.ssl.keyStore" value="${javax.net.ssl.keyStore}"/>
        <sysproperty key="javax.net.ssl.keyStoreType" value="${javax.net.ssl.keyStoreType}"/>
        <sysproperty key="javax.net.ssl.keyStorePassword" value="${javax.net.ssl.keyStorePassword}"/>
       <sysproperty key="javax.net.ssl.trustStore" value="${javax.net.ssl.trustStore}"/>
       <sysproperty key="javax.net.ssl.trustStoreType" value="${javax.net.ssl.trustStoreType}"/>
       <sysproperty key="javax.net.ssl.trustStorePassword" value="${javax.net.ssl.trustStorePassword}"/>
       <sysproperty key="javax.net.debug" value="ssl, handshake" />
        <classpath>
            <path refid="jaxws.classpath"/>
            <pathelement location="${build.classes.home}"/>
            <pathelement location="${build.classes.home}/../"/>
        </classpath>
        <!--
        <jvmarg value="-Xdebug"/>
        <jvmarg value="-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9009"/>
        -->  
    </java>  
</target>

<target name="run-tc" if="tomcat.home">
   <mkdir dir="${build.classes.home}/META-INF/services"/>
   <!--copy file="${basedir}/../../etc/javax.security.auth.callback.CallbackHandler.client" tofile="${build.classes.home}/META-INF/services/javax.security.auth.callback.CallbackHandler"/-->
   <java fork="true" classname="${client}">
        <sysproperty key="tomcat.home" value="${tomcat.home}"/>
        <sysproperty key="endpoint.host" value="${endpoint.host}"/>
        <sysproperty key="endpoint.port" value="${endpoint.port}"/>
        <sysproperty key="service.url" value="${service.url}"/>
        <sysproperty key="DebugSecurity" value="true"/>
        <!--sysproperty key="com.sun.xml.ws.runtime.client.wss.before" value="true"/>
        <sysproperty key="com.sun.xml.ws.runtime.client.wss.after" value="true"/-->
        <classpath>
            <path refid="jaxws.classpath"/>
            <pathelement location="${build.classes.home}"/>
            <pathelement location="${build.classes.home}/../"/>
        </classpath>
        <jvmarg value="-DWSIT_HOME=${tomcat.home}"/>
        <!--
        <jvmarg value="-Xdebug"/>
        <jvmarg value="-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9009"/>
        -->
    </java>  
</target>

<target name="run-glassfish" if="glassfish.home">
   <mkdir dir="${build.classes.home}/META-INF/services"/>
   <!--copy file="${basedir}/../../etc/javax.security.auth.callback.CallbackHandler.client" tofile="${build.classes.home}/META-INF/services/javax.security.auth.callback.CallbackHandler"/-->
   <java fork="true" classname="${client}">
        <sysproperty key="glassfish.home" value="${glassfish.home}"/>
        <sysproperty key="endpoint.host" value="${endpoint.host}"/>
        <sysproperty key="endpoint.port" value="${endpoint.port}"/>
        <sysproperty key="service.url" value="${service.url}"/>
        <!-- sysproperty key="DebugSecurity" value="true"/ -->
        <!--sysproperty key="com.sun.xml.ws.runtime.client.wss.before" value="true"/>
        <sysproperty key="com.sun.xml.ws.runtime.client.wss.after" value="true"/-->
        <classpath>
            <path refid="jaxws.classpath"/>
            <pathelement location="${build.classes.home}"/>
            <pathelement location="${build.classes.home}/../"/>            
        </classpath>
        <jvmarg value="-DWSIT_HOME=${glassfish.home}"/>
    </java>  
</target>

<target name="run" depends="setup-tc, run-tc, setup-glassfish, run-glassfish"/>

<target name="run-s1" depends="setup-tc, run-tc-s1"/>

<target name="rundebug-tc">
   <mkdir dir="${build.classes.home}/META-INF/services"/>
   <copy file="${basedir}/../../etc/javax.security.auth.callback.CallbackHandler.client" tofile="${build.classes.home}/META-INF/services/javax.security.auth.callback.CallbackHandler"/>
   <java fork="true" classname="${client}">
        <sysproperty key="tomcat.home" value="${tomcat.home}"/>
        <sysproperty key="endpoint.host" value="${endpoint.host}"/>
        <sysproperty key="endpoint.port" value="${endpoint.port}"/>
        <sysproperty key="service.url" value="${service.url}"/>
        <sysproperty key="DebugSecurity" value="true"/>
        <classpath>
            <path refid="jaxws.classpath"/>
            <pathelement location="${build.classes.home}"/>
            <pathelement location="${build.classes.home}/../"/>
        </classpath>
        <jvmarg value="-Xdebug"/>
        <jvmarg value="-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000"/>
    </java>  
</target>

<target name="rundebug" depends="rundebug-tc" />

<target name="dispatch-client" depends="setup-tc, setup-glassfish">
    <javac
        fork="true"
        debug="${debug}"
        srcdir="${basedir}/src/${sample.name}"
        destdir="${build.classes.home}"
        includes="**/client/**,**/common/**">
        <classpath refid="jaxws.classpath"/>
    </javac>
</target>

<target name="run-dispatch-tc" if="tomcat.home">
    <java fork="true" classname="${client}">
        <sysproperty key="tomcat.home" value="${tomcat.home}"/>
        <classpath>
            <path refid="jaxws.classpath"/>
            <pathelement location="${build.classes.home}"/>
        </classpath>
    </java>
</target>

<target name="run-dispatch-glassfish" if="glassfish.home">
    <java fork="true" classname="${client}">
        <sysproperty key="glassfish.home" value="${glassfish.home}"/>
        <classpath>
            <path refid="jaxws.classpath"/>
            <pathelement location="${build.classes.home}"/>
        </classpath>
       <jvmarg value="-Dhttp.proxyHost=${http.proxyHost}"/>
        <jvmarg value="-Dhttp.proxyPort=${http.proxyPort}"/> 
    </java>
</target>

<target name="run-dispatch" depends="run-dispatch-tc, run-dispatch-glassfish"/>

<target name="clean">
    <delete dir="${build.home}" includeEmptyDirs="true" />
</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>
