<!--
 -
 - 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="qpid-jca-example" default="help" basedir="">

    <!-- Valid target platforms are currently geronimo & jboss -->
    <property name="target.platform" value="geronimo"/>

    <!-- Change to BURL for older syntax support -->
    <property name="qpid.dest_syntax" value="ADDR"/>

    <!-- Properties controlling running sample standalone client -->
    <property name="client.use.ejb" value="true"/> <!-- uses JNDI/JMS or JNDI/RMI -->
    <property name="client.message" value="Hello Qpid World"/>
    <property name="client.message.count" value="1"/>
    <property name="client.use.topic" value="false"/> <!-- Use topic/queue -->
    <property name="client.say.goodbye" value="false"/>

    <!-- Pull in environment vars as properties -->
    <property environment="env"/>

    <!-- QPID version property -->
    <import file="${basedir}/build-properties.xml"/>

    <import file="${basedir}/qpid-jca-example-properties.xml"/>

    <!-- Target specific properties/targets -->
    <import file="${basedir}/build-${target.platform}-properties.xml"/>

    <macrodef name="compile">
        <attribute name="classpath"/>
        <sequential>
            <javac srcdir="${gen.dir}"
               destdir="${build.classes.dir}"
               classpathref="@{classpath}"
               debug="true" optimize="false"/>
        </sequential>
    </macrodef>

    <echo message="Using Qpid version ${qpid.ver}"/>
    <echo message="Building for platform ${target.platform}"/>
    <echo message="Broker url is currently set to ${broker.url}"/>
	<echo message="Qpid Destination Syntax is ${qpid.dest_syntax}"/>

	<target name="init">
        <mkdir dir="${build.classes.dir}"/>
        <mkdir dir="${gen.dir}"/>
        <mkdir dir="${log.dir}"/>
    </target>

    <target name="generate" depends="init">
        <copy todir="${gen.dir}" overwrite="true">
            <fileset dir="${conf.dir}"/>
            <filterset>
                <filter token="rar.name" value="${rar.name}"/>
                <filter token="ejb.name" value="${ejb.name}"/>
                <filter token="war.name" value="${war.name}"/>

                <filter token="broker.url" value="${broker.url}"/>

                <filter token="qpid.hello.topic.dest.address" value="${qpid.hello.topic.dest.address}"/>
                <filter token="qpid.goodbye.topic.dest.address" value="${qpid.goodbye.topic.dest.address}"/>
                <filter token="qpid.hellogoodbye.topic.dest.address" value="${qpid.hellogoodbye.topic.dest.address}"/>
                <filter token="qpid.hello.queue.dest.address" value="${qpid.hello.queue.dest.address}"/>
                <filter token="qpid.goodbye.queue.dest.address" value="${qpid.goodbye.queue.dest.address}"/>
                <filter token="qpid.request.queue.dest.address" value="${qpid.request.queue.dest.address}"/>
                <filter token="qpid.response.queue.dest.address" value="${qpid.response.queue.dest.address}"/>

            </filterset>
            <filterset refid="extra.filterset"/>
        </copy>
        <copy todir="${gen.dir}">
            <fileset dir="${src.dir}"/>
            <filterset>
                <filter token="rar.name" value="${rar.name}"/>
                <filter token="broker.url" value="${broker.url}"/>
                <filter token="jndi.scheme" value="${jndi.scheme}"/>
                <filter token="qpid.xacf.jndi.name" value="${qpid.xacf.jndi.name}"/>
                <filter token="qpid.hello.topic.jndi.name" value="${qpid.hello.topic.jndi.name}"/>
                <filter token="qpid.goodbye.topic.jndi.name" value="${qpid.goodbye.topic.jndi.name}"/>
                <filter token="qpid.hello.queue.jndi.name" value="${qpid.hello.queue.jndi.name}"/>
                <filter token="qpid.goodbye.queue.jndi.name" value="${qpid.goodbye.queue.jndi.name}"/>
                <filter token="qpid.request.queue.jndi.name" value="${qpid.request.queue.jndi.name}"/>
                <filter token="qpid.ejb.jndi.name" value="${qpid.ejb.jndi.name}"/>
            </filterset>
        </copy>
    </target>

    <target name="compile" depends="generate" description="Compiles the source files for the Qpid JCA example">
        <compile classpath="compile.classpath"/>
    </target>

    <target name="package-war" depends="compile" description="Packages the WAR file for deployment.">
        <war destfile="${build.dir}/${war.name}" webxml="${gen.dir}/web.xml">
            <classes dir="${build.classes.dir}">
                <include name="org/apache/qpid/jca/example/web/**"/>
            </classes>
        </war>
    </target>

    <target name="package-ejb" depends="compile" description="Packages the EJB archive for deployment.">
        <jar destfile="${build.dir}/${ejb.name}" basedir="${build.classes.dir}">
            <include name="org/apache/qpid/jca/example/ejb/**/*.class"/>
            <metainf dir="${gen.dir}">
                <include name="jboss.xml"/>
            </metainf>
        </jar>
    </target>

    <target name="package-ear" depends="generate, package-war, package-ejb" description="Packages the EAR archive for deployment.">
        <jar destfile="${build.dir}/${ear.name}" basedir="${build.dir}">
            <include name="*.war"/>
            <include name="*.jar"/>
            <metainf dir="${gen.dir}">
                <include name="application.xml"/>
            </metainf>
        </jar>
    </target>

    <target name="run-client" depends="compile" description="Runs the RMI client.">
        <java classname="org.apache.qpid.jca.example.client.QpidTestClient">
            <classpath>
                <pathelement path="${build.classes.dir}"/>
                <path refid="run.classpath"/>
            </classpath>
            <sysproperty key="java.naming.factory.initial" value="${jndi.context}"/>
            <sysproperty key="java.naming.provider.url" value="${server.host}"/>
            <sysproperty key="qpid.ejb.name" value="${qpid.ejb.name}"/>
            <sysproperty key="qpid.cf.name" value="${qpid.cf.jndi.name}"/>
            <sysproperty key="qpid.dest_syntax" value="${qpid.dest_syntax}"/>
            <sysproperty key="qpid.dest.name" value="${qpid.hello.queue.jndi.name}"/>
            <sysproperty key="log4j.configuration" value="file://${conf.dir}/log4j.properties"/>

            <sysproperty key="qpid.message" value="${client.message}"/>
            <sysproperty key="message.count" value="${client.message.count}"/>
            <sysproperty key="use.topic" value="${client.use.topic}"/>
            <sysproperty key="use.ejb" value="${client.use.ejb}"/>
            <sysproperty key="say.goodbye" value="${client.say.goodbye}"/>
        </java>
    </target>

    <target name="run-reqresp" depends="compile">
        <java classname="org.apache.qpid.jca.example.client.QpidRequestResponseClient">
            <classpath>
                <pathelement path="${build.classes.dir}"/>
                <path refid="run.classpath"/>
            </classpath>
            <sysproperty key="java.naming.factory.initial" value="${jndi.context}"/>
            <sysproperty key="java.naming.provider.url" value="${server.host}"/>
            <sysproperty key="qpid.message" value="Hello, World"/>
            <sysproperty key="message.count" value="1"/>
            <sysproperty key="thread.count" value="5"/>
            <sysproperty key="qpid.cf.name" value="${qpid.cf.jndi.name}"/>
            <sysproperty key="qpid.dest.name" value="${qpid.responder.queue.jndi.name}"/>
            <sysproperty key="log4j.configuration" value="file://${conf.dir}/log4j.properties"/>
            <sysproperty key="qpid.dest_syntax" value="${qpid.dest_syntax}"/>
        </java>
    </target>

    <target name="clean" description="Deletes the build directory and all related files.">
        <delete dir="${build.dir}"/>
    </target>

    <target name="help">
        <echo>

           ant compile
                This will compile all the source code for the Qpid JCA example project to the ${build.classes.dir} directory.

           ant deploy-rar deploy-ear
             Deploys a particular component which could be rar, ear (or ds for JBoss)

           ant undeploy-ear undeploy-rar
             Undeploys a particular component which could be rar, ear (or ds for JBoss)

           ant run-client run-reqresp
             Runs the RMI/thin client or the request-response client example
        </echo>
    </target>

</project>
