<?xml version="1.0"?>
<!--
 -
 - 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="QMF2 API Implementation" default="api" basedir=".">
	<!-- =================================================================== -->
	<!-- Note that this requires the QPID_HOME environment variable          -->
	<!-- to be set and it installs the plugin to $QPID_HOME/lib/plugins      -->
	<!-- =================================================================== -->

    <property environment="env"/>
    <property name="qpid.home"        location="${env.QPID_HOME}"/>
    <property name="qpid.lib"         location="${qpid.home}/lib"/>
    <property name="qpid.lib.plugins" location="${qpid.lib}/plugins"/>

	<!-- =================================================================== -->
	<!-- Builds the QMF2 and REST APIs                                       -->
    <!-- Use this if you have Qpid version 0.12+ as this doesn't override    -->
    <!-- the now working latest version of AMQMessageDelegate_0_10.          -->
	<!-- =================================================================== -->
	<target name="api" depends="main-jar, restapi-jar, api-doc">
    </target>

	<!-- =================================================================== -->
	<!-- Builds the QMF2 and REST APIs and the AMQMessageDelegate_0_10 patch.-->
	<!-- Use this if you have a Qpid version < 0.12 as it includes the patch -->
    <!-- for AMQMessageDelegate_0_10.java that is needed to work with older  -->
    <!-- Qpid versions. N.B. This task won't compile on later Qoid versions. -->
	<!-- =================================================================== -->
	<target name="api-patched" depends="main-jar, patch-jar, restapi-jar, api-doc">
    </target>

	<!-- =================================================================== -->
	<!-- Builds the QMF2 and REST APIs plus the QMF2 Agent Plugin to enable  -->
    <!-- the Java Broker to talk QMF2.                                       -->
	<!-- Note that this task only compiles on Qpid Java version > 0.20       -->
	<!-- =================================================================== -->
	<target name="all" depends="main-jar, restapi-jar, qpid-broker-plugins-management-qmf2-jar, all-doc">
		<mkdir dir="${qpid.lib.plugins}"/>

        <copy file="build/lib/qmf2.jar" todir="${qpid.lib.plugins}"/>
        <copy file="build/lib/qpid-broker-plugins-management-qmf2.jar" todir="${qpid.lib.plugins}"/>
    </target>

	<!-- =================================================================== -->
	<!-- Compiles the main API code                                          -->
	<!-- =================================================================== -->
	<target name="main">
		<mkdir dir="build/scratch/qmf2/classes"/>

		<javac srcdir="src/main/java"
			   destdir="build/scratch/qmf2/classes"
			   debug="off"
			   deprecation="on"
               includeantruntime="false"
			   optimize="on">
                <classpath location="${qpid.lib}/qpid-all.jar"/>
		</javac>
	</target>

	<!-- =================================================================== -->
	<!-- Compiles the test code                                              -->
	<!-- =================================================================== -->
	<target name="test" depends="main">
		<mkdir dir="build/scratch/qmf2/testclasses"/>

		<javac srcdir="src/test/java"
			   destdir="build/scratch/qmf2/testclasses"
			   debug="off"
			   deprecation="on"
               includeantruntime="false"
			   optimize="on">
                <classpath>
                    <pathelement location="${qpid.lib}/qpid-all.jar"/>
                    <pathelement location="build/scratch/qmf2/classes"/>
                </classpath>
		</javac>
	</target>

	<!-- =================================================================== -->
	<!-- Compiles the patch needed for Qpid versions older than 0.12         -->
	<!-- =================================================================== -->
	<target name="patch">
		<mkdir dir="build/scratch/patch"/>

		<javac srcdir="src/patch/java"
			   destdir="build/scratch/patch"
			   debug="off"
			   deprecation="on"
               includeantruntime="false"
			   optimize="on">
                <classpath location="${qpid.lib}/qpid-all.jar"/>
		</javac>
	</target>

	<!-- =================================================================== -->
	<!-- Compiles the Java Broker QMF2 Agent Plugin                          -->
	<!-- =================================================================== -->
	<target name="qpid-broker-plugins-management-qmf2">
		<mkdir dir="build/scratch/qpid-broker-plugins-management-qmf2"/>

		<javac srcdir="src/qpid-broker-plugins-management-qmf2/java"
			   destdir="build/scratch/qpid-broker-plugins-management-qmf2"
			   debug="off"
			   deprecation="on"
               includeantruntime="false"
			   optimize="on">
                <classpath>
                    <pathelement location="${qpid.lib}/qpid-all.jar"/>
                    <pathelement location="build/scratch/qmf2/classes"/>
                </classpath>
		</javac>
	</target>

	<!-- =================================================================== -->
	<!-- Compiles the HTTP REST API Server                                   -->
	<!-- =================================================================== -->
	<target name="restapi" depends="main"> 
		<mkdir dir="build/scratch/restapi"/>

		<javac srcdir="src/restapi/java"
			   destdir="build/scratch/restapi"
			   debug="off"
			   deprecation="on"
               includeantruntime="false"
			   optimize="on">
                <classpath>
                    <pathelement location="${qpid.lib}/qpid-all.jar"/>
                    <pathelement location="build/scratch/qmf2/classes"/>
                </classpath>
		</javac>
	</target>

	<!-- =================================================================== -->
	<!-- Generates the main jars                                             -->
	<!-- =================================================================== -->
	<target name="main-jar" depends="main, test">
		<mkdir dir="build/lib"/>

		<jar destfile="build/lib/qmf2.jar" basedir="build/scratch/qmf2/classes">
		</jar>

		<jar destfile="build/lib/qmf2test.jar" basedir="build/scratch/qmf2/testclasses">
		</jar>
	</target>

	<!-- =================================================================== -->
	<!-- Generates the jar for the patch needed by Qpid versions < 0.12      -->
	<!-- =================================================================== -->
	<target name="patch-jar" depends="patch">
		<mkdir dir="build/lib"/>
		<jar destfile="build/lib/qpid-client-patch.jar" basedir="build/scratch/patch">
		</jar>
	</target>

	<!-- =================================================================== -->
	<!-- Generates the jar for the Java Broker QMF2 Agent Plugin             -->
	<!-- =================================================================== -->
	<target name="qpid-broker-plugins-management-qmf2-jar" depends="qpid-broker-plugins-management-qmf2">
		<mkdir dir="build/lib"/>
		<jar destfile="build/lib/qpid-broker-plugins-management-qmf2.jar"
             basedir="build/scratch/qpid-broker-plugins-management-qmf2">

            <service type="org.apache.qpid.server.plugin.PluginFactory" 
                     provider="org.apache.qpid.server.qmf2.QmfManagementFactory"/>
		</jar>
	</target>

	<!-- =================================================================== -->
	<!-- Generates the jar for the the HTTP REST API Server                  -->
	<!-- =================================================================== -->
	<target name="restapi-jar" depends="restapi">
		<mkdir dir="build/lib"/>
		<jar destfile="build/lib/restapi.jar" basedir="build/scratch/restapi">
		</jar>
	</target>

	<!-- =================================================================== -->
	<!-- Generates Javadoc                                                   -->
	<!-- =================================================================== -->
	<target name="api-doc">
        <javadoc packagenames="org.apache.qpid.qmf2.*, org.apache.qpid.restapi.*"
                 sourcepath="src/main/java:src/restapi/java"
                 destdir="docs/api"
                 windowtitle="QMF2 API">
        </javadoc>
	</target>

	<target name="all-doc">
        <javadoc packagenames="org.apache.qpid.qmf2.*, org.apache.qpid.restapi.*, org.apache.qpid.server.qmf2.*"
                 sourcepath="src/main/java:src/restapi/java:src/qpid-broker-plugins-management-qmf2/java"
                 destdir="docs/api"
                 windowtitle="QMF2 API">
        </javadoc>
	</target>


	<!-- =================================================================== -->
	<!-- Cleans up classes but keeps jars and docs around                    -->
	<!-- =================================================================== -->
	<target name="clean">
		<delete includeemptydirs="true">
			<fileset dir="." includes="**/*.class"/>
		</delete>
	</target>

	<!-- =================================================================== -->
	<!-- Removes everything except the sources enabling clean build.         -->
	<!-- =================================================================== -->
	<target name="cleanall" depends="clean, cleanplugin">
		<delete includeemptydirs="true">
			<fileset dir="." includes="**/*.*~ **/*~" defaultexcludes="no"/>
		</delete>

		<delete dir="build"/>
		<delete dir="docs"/>
	</target>

	<!-- =================================================================== -->
	<!-- If plugins directory exists delete the plugin jars.                 -->
	<!-- =================================================================== -->
	<target name="cleanplugin" depends="test-for-plugin" if="dir.exists">
		<delete includeemptydirs="true">
			<fileset dir="${qpid.lib.plugins}" includes="qpid-broker-plugins-management-qmf2.jar qmf2.jar" 
                     defaultexcludes="no"/>
		</delete>
	</target>

	<!-- =================================================================== -->
	<!-- Test for the presence of the plugins directory.                     -->
	<!-- =================================================================== -->
    <target name="test-for-plugin">
        <condition property="dir.exists">
            <available file="${qpid.lib.plugins}" type="dir"/>
        </condition>
    </target>

</project>

