<?xml version="1.0" encoding="UTF-8"?>
<!--

    Copyright 2005 The Apache Software Foundation

    Licensed 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.
-->

<!-- $Rev$ $Date$ -->

<project default="default"
    xmlns:j="jelly:core"
    xmlns:ant="jelly:ant"
    xmlns:deploy="geronimo:deploy"
    xmlns:velocity="jelly:velocity"
    >

    <j:set var="instDir" value="${maven.build.dir}/geronimo-${geronimo_version}"/>

    <goal name="default">
        <attainGoal name="petstore:deploy"/>
    </goal>

    <goal name="petstore:deploy" description="Deploys PetStore" prereqs="clean">
        <deploy:unpackServer
            geronimoVersion="${geronimo_version}"
            geronimoName="geronimo"/>

        <ant:copy todir="${instDir}/var">
            <ant:fileset dir="src/var"/>
        </ant:copy>

        <fileScanner var="plans">
            <fileset dir="${basedir}/src/plan">
                <include name="*-plan.xml"/>
            </fileset>
        </fileScanner>

        <ant:mkdir dir="${maven.build.dir}/plan"/>
        <j:forEach var="plan" items="${plans.iterator()}">
            <j:set var="planName" value="${plan.name}"/>
            <echo>Preprocessing ${planName}</echo>
            <velocity:merge
                basedir="${basedir}/src/plan"
                template="${planName}"
                name="${maven.build.dir}/plan/${planName}"/>
        </j:forEach>

        <ant:echo>Building Security Configuration</ant:echo>
        <ant:echo>${instDir}</ant:echo>
        <ant:java fork="true" jar="${instDir}/bin/deployer.jar" failonerror="true">
            <ant:jvmarg value="-ea"/>
            <ant:arg value="--offline"/>
            <ant:arg value="distribute"/>
            <ant:arg value="${maven.build.dir}/plan/petstore1.3.2-security-plan.xml"/>
        </ant:java>

        <ant:echo>Building JMS Configuration</ant:echo>
        <ant:java fork="true" jar="${instDir}/bin/deployer.jar" failonerror="true">
            <ant:jvmarg value="-ea"/>
            <ant:arg value="--offline"/>
            <ant:arg value="distribute"/>
            <ant:arg value="${maven.build.dir}/plan/petstore1.3.2-jms-ra-plan.xml"/>
            <ant:arg value="${maven.repo.local}/activemq/rars/activemq-ra-${activemq_version}.rar"/>
        </ant:java>

        <ant:echo>Building Database Configuration</ant:echo>
        <ant:java fork="true" jar="${instDir}/bin/deployer.jar" failonerror="true">
            <ant:jvmarg value="-ea"/>
            <ant:arg value="--offline"/>
            <ant:arg value="distribute"/>
            <ant:arg value="${maven.build.dir}/plan/petstore1.3.2-db-ra-plan.xml"/>
            <ant:arg value="${maven.repo.local}/tranql/rars/tranql-connector-derby-embed-xa-${tranql_vendors_version}.rar"/>
        </ant:java>

    <ant:echo>Distributing Java Pet Store Application</ant:echo>
        <ant:java fork="true" jar="${instDir}/bin/deployer.jar" failonerror="true">
            <ant:jvmarg value="-ea"/>
            <ant:arg value="--offline"/>
            <ant:arg value="distribute"/>
            <ant:arg value="${maven.build.dir}/plan/petstore1.3.2.ear-plan.xml"/>
            <ant:arg value="${petstore.home}/src/apps/petstore/build/petstore.ear"/>
        </ant:java>

        <ant:echo>Distributing Java Pet Store Supplier Application</ant:echo>
        <ant:java fork="true" jar="${instDir}/bin/deployer.jar" failonerror="true">
            <ant:jvmarg value="-ea"/>
            <ant:arg value="--offline"/>
            <ant:arg value="distribute"/>
            <ant:arg value="${maven.build.dir}/plan/supplier1.3.2.ear-plan.xml"/>
            <ant:arg value="${petstore.home}/src/apps/supplier/build/supplier.ear"/>
        </ant:java>

        <ant:echo>Distributing Java Pet Store OPC Application</ant:echo>
        <ant:java fork="true" jar="${instDir}/bin/deployer.jar" failonerror="true">
            <ant:jvmarg value="-ea"/>
            <ant:arg value="--offline"/>
            <ant:arg value="distribute"/>
            <ant:arg value="${maven.build.dir}/plan/opc1.3.2.ear-plan.xml"/>
            <ant:arg value="${petstore.home}/src/apps/opc/build/opc.ear"/>
        </ant:java>

        <ant:echo>Distributing Java Pet Store Admin Application</ant:echo>
        <ant:java fork="true" jar="${instDir}/bin/deployer.jar" failonerror="true">
            <ant:jvmarg value="-ea"/>
            <ant:arg value="--offline"/>
            <ant:arg value="distribute"/>
            <ant:arg value="${maven.build.dir}/plan/petstoreadmin1.4.ear-plan.xml"/>
            <ant:arg value="${petstore.home}/src/apps/admin/build/petstoreadmin.ear"/>
        </ant:java>

        <ant:echo>Creating Java Pet Store Database Schema</ant:echo>
        <j:invokeStatic className="java.lang.System" method="setProperty">
            <j:arg type="java.lang.String" value="derby.system.home"/>
            <j:arg type="java.lang.String" value="${instDir}/var/derby"/>
        </j:invokeStatic>
        <ant:sql driver="org.apache.derby.jdbc.EmbeddedDriver"
            url="jdbc:derby:PetStoreDB;create=true" userid="" password="">
            <ant:fileset dir="src/sql"/>
            <ant:classpath>
                <ant:pathelement location="${maven.repo.local}/org.apache.derby/jars/derby-10.1.1.0.jar"/>
            </ant:classpath>
        </ant:sql>
    </goal>

    <goal name="petstore:start" description="Start PetStore">
        <deploy:startRemoteServer
            geronimoTarget="${instDir}"
            debugPort="8000"
	    configs="org/apache/geronimo/PetStore1.3.2 org/apache/geronimo/Supplier1.3.2 org/apache/geronimo/PetStoreSecurity1.3.2 org/apache/geronimo/OPC1.3.2 org/apache/geronimo/PetStoreAdmin1.3.2 org/apache/geronimo/RuntimeDeployer"/>
        <ant:echo message="Waiting for server at: ${instDir}"/>
        <deploy:waitForStarted
            uri="jmx:rmi://localhost/jndi/rmi:/JMXConnector"
            username="system"
            password="manager"
            id="org/apache/geronimo/PetStore1.3.2"/>
        <deploy:waitForStarted
            uri="jmx:rmi://localhost/jndi/rmi:/JMXConnector"
            username="system"
            password="manager"
            id="org/apache/geronimo/Supplier1.3.2"/>
        <deploy:waitForStarted
            uri="jmx:rmi://localhost/jndi/rmi:/JMXConnector"
            username="system"
            password="manager"
            id="org/apache/geronimo/PetStoreSecurity1.3.2"/>
        <deploy:waitForStarted
            uri="jmx:rmi://localhost/jndi/rmi:/JMXConnector"
            username="system"
            password="manager"
            id="org/apache/geronimo/OPC1.3.2"/>
        <deploy:waitForStarted
            uri="jmx:rmi://localhost/jndi/rmi:/JMXConnector"
            username="system"
            password="manager"
            id="org/apache/geronimo/PetStoreAdmin1.3.2"/>
        <echo message="PetStore has started"/>
    </goal>

    <goal name="petstore:stop" description="Stop PetStore">
        <deploy:stopRemoteServer
            uri="jmx:rmi://localhost/jndi/rmi:/JMXConnector"
            username="system"
            password="manager"/>
        <echo message="server has stopped"/>
    </goal>

</project>
