<?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="util" default="help" basedir=".">
    <property environment="env"/>
    <property name="activemq.home" value="${env.ACTIVEMQ_HOME}"/>
    <property name="axis2.xml" value="../../repository/conf/axis2.xml"/>
    <property name="libdir" value="../../lib"/>
    
    <taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask">
        <classpath>
            <pathelement location="xmltask-1.14.jar"/>
        </classpath>
    </taskdef>
    
    <target name="help">
    <echo>
    ant setupActiveMQ
        Set up the JMS transport in Synapse for use with ActiveMQ. This task copies the required
        libraries to the lib folder and configures the JMS transport in axis2.xml. Note that for
        this to work, the ActiveMQ installation directory must be specified either using the
        ACTIVEMQ_HOME environment variable or the activemq.home property:
        
        ant setupActiveMQ -Dactivemq.home=/usr/local/apache-activemq-4.1.2
    </echo>
    </target>
    
    <target name="setupActiveMQ">
        <copy todir="${libdir}">
            <fileset dir="${activemq.home}/lib">
                <include name="activeio-core-*.jar"/>
                <include name="activemq-core-*.jar"/>
                <include name="geronimo-j2ee-management_*_spec-*.jar"/>
                <exclude name="*-tests.jar"/>
            </fileset>
        </copy>
        <xmltask source="${axis2.xml}" dest="${axis2.xml}">
            <remove path="/axisconfig/transportReceiver[@name='jms']"/>
            <insert path="/axisconfig/transportReceiver[last()]" position="after">
    <![CDATA[
    <transportReceiver name="jms" class="org.apache.axis2.transport.jms.JMSListener">
        <parameter name="myTopicConnectionFactory">
            <parameter name="java.naming.factory.initial">org.apache.activemq.jndi.ActiveMQInitialContextFactory</parameter>
            <parameter name="java.naming.provider.url">tcp://localhost:61616</parameter>
            <parameter name="transport.jms.ConnectionFactoryJNDIName">TopicConnectionFactory</parameter>
            <parameter name="transport.jms.ConnectionFactoryType" locked="false">topic</parameter>
        </parameter>

        <parameter name="myQueueConnectionFactory">
            <parameter name="java.naming.factory.initial">org.apache.activemq.jndi.ActiveMQInitialContextFactory</parameter>
            <parameter name="java.naming.provider.url">tcp://localhost:61616</parameter>
            <parameter name="transport.jms.ConnectionFactoryJNDIName">QueueConnectionFactory</parameter>
            <parameter name="transport.jms.ConnectionFactoryType" locked="false">queue</parameter>
        </parameter>

        <parameter name="default">
            <parameter name="java.naming.factory.initial">org.apache.activemq.jndi.ActiveMQInitialContextFactory</parameter>
            <parameter name="java.naming.provider.url">tcp://localhost:61616</parameter>
            <parameter name="transport.jms.ConnectionFactoryJNDIName">QueueConnectionFactory</parameter>
            <parameter name="transport.jms.ConnectionFactoryType" locked="false">queue</parameter>
        </parameter>
    </transportReceiver>
    ]]>
            </insert>
            <remove path="/axisconfig/transportSender[@name='jms']"/>
            <insert path="/axisconfig/transportSender[last()]" position="after">
    <![CDATA[
    <transportSender class="org.apache.axis2.transport.jms.JMSSender" name="jms">
        <parameter name="myTopicConnectionFactory">
            <parameter name="java.naming.factory.initial">org.apache.activemq.jndi.ActiveMQInitialContextFactory</parameter>
            <parameter name="java.naming.provider.url">tcp://localhost:61616</parameter>
            <parameter name="transport.jms.ConnectionFactoryJNDIName">TopicConnectionFactory</parameter>
        </parameter>
    
        <parameter name="myQueueConnectionFactory">
            <parameter name="java.naming.factory.initial">org.apache.activemq.jndi.ActiveMQInitialContextFactory</parameter>
            <parameter name="java.naming.provider.url">tcp://localhost:61616</parameter>
            <parameter name="transport.jms.ConnectionFactoryJNDIName">QueueConnectionFactory</parameter>
        </parameter>
    
        <parameter name="default">
            <parameter name="java.naming.factory.initial">org.apache.activemq.jndi.ActiveMQInitialContextFactory</parameter>
            <parameter name="java.naming.provider.url">tcp://localhost:61616</parameter>
            <parameter name="transport.jms.ConnectionFactoryJNDIName">QueueConnectionFactory</parameter> 
        </parameter>
    </transportSender>
    ]]>
            </insert>
        </xmltask>
    </target>
</project>
