<!--
  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="CXF common build file" default="build">


    <property environment="env"/>
    <property name="build.dir" location ="${basedir}/build"/>
    <property name="testbase.classes.dir" location ="${basedir}/../base/build/classes"/>
    <property name="build.classes.dir" location ="${build.dir}/classes"/>
    <property name="build.src.dir" location ="${build.dir}/src"/>
    <property name="build.var.dir" location ="${build.dir}/var"/>
    <property name="codegen.timestamp.file" value="${build.src.dir}/.CODEGEN_DONE"/>
    <property name="wsdl.dir" location="${basedir}/wsdl"/>
    <property name="war.dir" location="${build.dir}/war"/>


    <!-- Setup for building in a source build -->
    <condition property="srcbuild_paths.file" value="./srcbuild_paths.xml"> 
        <available file="../../lib/cxf-manifest.jar"/>
    </condition> 
    <!-- default setting --> 
    <property name="srcbuild_paths.file" location="../../../distribution/target/srcbuild_env.ant"/> 
    <loadfile property="srcbuild.classpath" srcFile="${srcbuild_paths.file}" failonerror="false"/>
    <property name="srcbuild.classpath" value=""/>    

    <uptodate property="codegen.notrequired" value="true">
        <srcfiles dir="${wsdl.dir}" includes="*.wsdl"/>
        <srcfiles dir="${wsdl.dir}" includes="*.xsd"/>
        <mapper type="merge" to="${codegen.timestamp.file}"/>
    </uptodate>
        
    <!-- find the tools jar, which on darwin, tools.jar does not exist -->
    <available property="tools.jar" value="$${env.JAVA_HOME}/lib/tools.jar"
       file="${env.JAVA_HOME}/lib/tools.jar"/>
    <path id="jdk.tools.classpath">
        <pathelement location="${tools.jar}"/>
    </path>

    <!-- Determine cxf.home, either from the environment variable CXF_HOME
       - or using ../..
       -->
    <condition property="cxf.home" value="${env.CXF_HOME}">
        <isset property="env.CXF_HOME"/>
    </condition>
    <property name="cxf.home" location="../.."/>
    <property name="activemq.home" location="${cxf.home}/lib/activemq/3.2"/>
    <property name="cxf.etc.dir" location="${cxf.home}/etc"/>


    <path id="cxf.classpath">
        <pathelement location="${build.classes.dir}"/>
        <pathelement path="${srcbuild.classpath}"/>
        <pathelement location="${cxf.home}/lib/cxf-manifest.jar"/>
	<pathelement location="${testbase.classes.dir}"/>
    </path>
    <condition property="thirdparty.classpath" value="">
        <not> <isset property="thirdparty.classpath"/> </not>
    </condition>

    <target name="maybe.generate.code" unless="codegen.notrequired">
        <antcall target="generate.code"/>
        <touch file="${codegen.timestamp.file}"/>
    </target>

    <target name="compile" depends="maybe.generate.code">
        <mkdir dir="${build.classes.dir}"/>
        <mkdir dir="${build.src.dir}"/>
        <javac destdir="${build.classes.dir}" debug="true">
            <src path="${build.src.dir}"/>
            <src path="${basedir}/src"/>
            <classpath>
                <path refid="cxf.classpath"/>
                <pathelement path="${thirdparty.classpath}"/>
            </classpath>
        </javac>

        <copy todir="${build.classes.dir}">
            <fileset dir="${basedir}/src" includes="**/*.xml" />
            <fileset dir="${build.src.dir}" includes="**/*.xml" />
        </copy>
    </target>

    <target name="checkstyle">
        <checkstyle config="${checkstyle.config.file}">
            <fileset dir="${basedir}/src" includes="**/*.java"/>
            <classpath>
                <path refid="cxf.classpath"/>
                <pathelement path="${thirdparty.classpath}"/>
            </classpath>
        </checkstyle>
    </target>

    <target name="clean">
        <delete dir="${build.classes.dir}"/>
        <delete dir="${build.src.dir}"/>
        <delete file="${codegen.timestamp.file}"/>
        <delete file="demo.log"/>
        <delete file="${wsdl.dir}/async_binding.xml"/>
        <delete dir="${build.var.dir}"/>
        <delete dir="${activemq.home}/var"/>
        <delete dir="${build.dir}"/>
    </target>

            
    <target name="build"  depends="compile" description="build demo client and server"/>

    <macrodef name="cxf.server.run">
        <attribute name="classname"/>
        <attribute name="argline" default=""/>
        <sequential>
            <java classname="@{classname}" fork="yes" maxMemory="512M" >
                <jvmarg value="-Xms512M"/>
                <classpath refid="cxf.classpath"/>
                <arg line="@{argline}"/>
                <sysproperty key="java.util.logging.config.file" value="${cxf.etc.dir}/logging.properties"/>
		<sysproperty key="log4j.configuration" value="file:///${cxf.etc.dir}/log4j.properties"/>
            </java>
        </sequential>
    </macrodef>

    <macrodef name="cxf.client.run">
        <attribute name="classname"/>
        <attribute name="argline" default=""/>
         <sequential>
            <java classname="@{classname}" fork="yes" maxMemory="512M" >
                <jvmarg value="-Xms256M"/>
                <classpath refid="cxf.classpath"/>
                <arg line="@{argline}"/>
                <sysproperty key="java.util.logging.config.file" value="${cxf.etc.dir}/logging.properties"/>
                <sysproperty key="log4j.configuration" value="file:///${cxf.etc.dir}/log4j.properties"/>
            </java>
        </sequential>
    </macrodef>

    <macrodef name="cxfrun">
        <attribute name="logging-properties-file" default="${cxf.etc.dir}/logging.properties"/>
        <attribute name="classname"/>
        <attribute name="param1" default=""/>
        <attribute name="param2" default=""/>
        <attribute name="param3" default=""/>
        <attribute name="param4" default=""/>
        <attribute name="param5" default=""/>
        <attribute name="jvmarg1" default="-D"/>
        <attribute name="jvmarg2" default="-D"/>
        <attribute name="jvmarg3" default="-D"/>
        <attribute name="jvmarg4" default="-D"/>
        <attribute name="jvmarg5" default="-D"/>
        <sequential>
            <java classname="@{classname}" fork="yes">
                <classpath>
                    <path refid="cxf.classpath"/>
                    <pathelement path="${thirdparty.classpath}"/>
                </classpath>
                <arg value="@{param1}"/>
                <arg value="@{param2}"/>
                <arg value="@{param3}"/>
                <arg value="@{param4}"/>
                <arg value="@{param5}"/>
                <jvmarg value="@{jvmarg1}"/>
                <jvmarg value="@{jvmarg2}"/>
                <jvmarg value="@{jvmarg3}"/>
                <jvmarg value="@{jvmarg4}"/>
                <jvmarg value="@{jvmarg5}"/>
                <assertions>
                    <enable package="org.apache.cxf"/>
                </assertions>
                <sysproperty key="java.util.logging.config.file" value="${cxf.etc.dir}/logging.properties"/>
                <sysproperty key="log4j.configuration" value="file:///${cxf.etc.dir}/log4j.properties"/>
            </java>
        </sequential>
    </macrodef>
        
    <macrodef name="wsdl2java">
        <attribute name="srcdestdir" default="${build.src.dir}"/>
        <attribute name="destdir" default="${build.classes.dir}"/>
        <attribute name="file"/>
        <attribute name="bindingfile" default=""/>
        <attribute name="dir" default="${wsdl.dir}"/>
        <attribute name="package" default="NOT_SPECIFIED"/>           
        <sequential>
            <mkdir dir="@{destdir}"/>
            <mkdir dir="@{srcdestdir}"/>
            <condition property="package.arg.@{file}" value="-p @{package}">
                <not>
                    <equals arg1="@{package}" arg2="NOT_SPECIFIED"/>
                </not>
            </condition>
            <property name="package.arg.@{file}" value=""/>
            <condition property="binding.arg" value="-b @{bindingfile}">
                <not>
                    <equals arg1="@{bindingfile}" arg2=""/>
                </not>
            </condition>
            <property name="binding.arg" value=""/>

            <java failonerror="true" classname="org.apache.cxf.tools.wsdlto.WSDLToJava" fork="yes">
                <classpath>
                    <path refid="cxf.classpath" />
                </classpath>
                <sysproperty key="java.util.logging.config.file" value="${cxf.etc.dir}/logging.properties"/>
                <arg line="${package.arg.@{file}}"/>
                <arg line="${binding.arg}"/>
                <arg value="-verbose"/>
                <arg value="-d"/>
                <arg value="@{srcdestdir}"/>
                <arg value="@{dir}/@{file}"/>
            </java>  
        </sequential>
    </macrodef>    
        
    <macrodef name="cxfwar">
        <attribute name="filename"/>
        <attribute name="wsdl"/>
        <attribute name="dir" default="${war.dir}"/>
        <attribute name="classesdir" default="${build.classes.dir}"/>
        <sequential>
            <mkdir dir="@{dir}"/>
            <war destfile="@{dir}/@{filename}" webxml="${cxf.home}/etc/web.xml">
                <classes dir="@{classesdir}"/>
                <webinf dir="${wsdl.dir}">
                    <include name="cxf-servlet.xml"/>
                </webinf>
                <webinf dir="${wsdl.dir}/..">
                    <include name="wsdl/@{wsdl}"/>
                </webinf>
            </war>
        </sequential>
    </macrodef>
    
</project>
