<?xml version="1.0"?>
<!--

    DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.

    Copyright (c) 2011 Oracle and/or its affiliates. All rights reserved.

    The contents of this file are subject to the terms of either the GNU
    General Public License Version 2 only ("GPL") or the Common Development
    and Distribution License("CDDL") (collectively, the "License").  You
    may not use this file except in compliance with the License.  You can
    obtain a copy of the License at
    https://glassfish.java.net/public/CDDL+GPL_1_1.html
    or packager/legal/LICENSE.txt.  See the License for the specific
    language governing permissions and limitations under the License.

    When distributing the software, include this License Header Notice in each
    file and include the License file at packager/legal/LICENSE.txt.

    GPL Classpath Exception:
    Oracle designates this particular file as subject to the "Classpath"
    exception as provided by Oracle in the GPL Version 2 section of the License
    file that accompanied this code.

    Modifications:
    If applicable, add the following below the License Header, with the fields
    enclosed by brackets [] replaced by your own identifying information:
    "Portions Copyright [year] [name of copyright owner]"

    Contributor(s):
    If you wish your version of this file to be governed by only the CDDL or
    only the GPL Version 2, indicate your decision by adding "[Contributor]
    elects to include this software in this distribution under the [CDDL or GPL
    Version 2] license."  If you don't indicate a single choice of license, a
    recipient has the option to distribute your version of this file under
    either the CDDL, the GPL Version 2 or to extend the choice of license to
    its licensees as provided above.  However, if you add GPL Version 2 code
    and therefore, elected the GPL Version 2 license, then the option applies
    only if the new code is made subject to such option by the copyright
    holder.

-->

<project name="Metro on Tomcat" default="help">
    <property environment="env"/>
    <property name="tomcat.home" value="${env.CATALINA_HOME}"/>
    <property name="catalina.home" value="${tomcat.home}"/>
    <property name="catalina.lib.home" value="${catalina.home}/shared/lib"/>
    <property name="catalina.endorsed" value="${catalina.home}/common/endorsed"/>

    <!-- Check if we are running JAVA SE 6 --> 
    <condition property="jdk6">
        <contains string="${java.version}" substring="1.6.0"/>
    </condition>

    <condition property="catalina.home.exists">
        <available file="${catalina.home}" type="dir"/>
    </condition>

    <condition property="tomcat6">
        <not>
            <available file="${catalina.home}/common/endorsed"/>
        </not>
    </condition>
 
    <target name="catalinahome-test" unless="catalina.home.exists">
        <fail message="The environment variable CATALINA_HOME was not set or does not point to an existing directory."/>
    </target>

    <target name="init" depends="catalinahome-test"/>

    <target name="jdk6-message" if="jdk6">
        <echo message="Metro is being installed using Java SE 6."/>
        <echo/>
        <echo message="  Metro 2.1.1-b04 provides JAX-WS API 2.2 while Java SE 6 includes"/>
        <echo message="  JAX-WS API 2.1. If you want to run Metro applications"/>
        <echo message="  outside Tomcat, you will need to use the endorsed"/>
        <echo message="  standards override mechanism. For more details see"/>
        <echo message="  http://download.oracle.com/javase/6/docs/technotes/guides/standards/index.html"/>
        <echo/>
        <echo message="  If you want to enable JAX-WS 2.2 for all applications"/>
        <echo message="  running under Java SE 6, you may run the command"/>
        <echo message="  'ant install-api'."/>
        <echo message="  That command will copy the file webservices-api.jar to"/>
        <echo message="  ${java.home}/lib/endorsed."/>
        <echo message="  The command needs to be run by a user that has the"/>
        <echo message="  permissions to copy files into ${java.home}/lib/endorsed."/>
        <echo/>
        <echo message="  NOTE that any Java application that incidentally"/>
        <echo message="  uses one of the APIs included in Metro but does"/>
        <echo message="  not have all Metro JARs on its classpath may"/>
        <echo message="  experience class loading issues if you proceed with"/>
        <echo message="  'install-api'."/>
    </target>
    
    <target name="install" depends="init,uninstall,update-catalina-props,check-api,install-api-tomcat" description="Install latest Metro jars">
        <echo message="Installing Metro 2.1.1-b04 for ${catalina.home} ..."/>

        <mkdir dir="${catalina.lib.home}"/>
        <copy toDir="${catalina.lib.home}" overwrite="true">
            <fileset dir="lib">
                <include name="webservices-rt.jar"/>
                <include name="webservices-tools.jar"/>
                <include name="webservices-extra.jar"/>
                <include name="webservices-extra-api.jar"/>
            </fileset>
        </copy>
        <echo message="... installation complete."/>
    </target>

    <!-- If we install into Tomcat 5.x, install the webservices-api.jar into common/endorsed. -->
    <!-- If we install into Tomcat 6, install the webservices-api.jar into endorsed.          -->
    <!-- If we install with Java SE 6, additionally install the webservices-api.jar           -->
    <!-- into $JAVA_HOME/lib/endorsed.                                                        -->

    <target name="install-api-tomcat" description="Installs 'webservices-api.jar'" depends="install-api-tomcat5,install-api-tomcat6"/>

    <target name="install-api-tomcat5" description="Installs 'webservices-api.jar' into $CATALINA_HOME/common/endorsed" unless="tomcat6">
        <copy toDir="${catalina.endorsed}" overwrite="true">
            <fileset dir="lib">
                <include name="webservices-api.jar"/>
                <include name="jsr173_api.jar"/>
            </fileset>
        </copy>
    </target>

    <target name="install-api-tomcat6" description="Installs 'webservices-api.jar' into $CATALINA_HOME/endorsed" if="tomcat6">
        <mkdir dir="${catalina.home}/endorsed"/>
        <copy toDir="${catalina.home}/endorsed" overwrite="true">
            <fileset dir="lib">
                <include name="webservices-api.jar"/>
                <include name="jsr173_api.jar"/>
            </fileset>
        </copy>
    </target>

    <target name="test-api" description="Test if 'webservices-api.jar' is already installed in $JAVA_HOME/lib/endorsed" if="jdk6">
        <available property="api.installed" file="${java.home}/lib/endorsed/webservices-api.jar"/>
    </target>

    <target name="check-api" depends="test-api" if="api.installed"
            description="Print a warning if 'webservices-api.jar' is found in $JAVA_HOME/lib/endorsed">
        <echo message="Found the file webservices-api.jar in ${java.home}/lib/endorsed."/>
        <echo/>
        <echo message="  This installation does not automatically overwrite"/>
        <echo message="  'webservices-api.jar' in ${java.home}/lib/endorsed."/>
        <echo message="  Please execute the command 'ant install-api' to"/>
        <echo message="  update 'webservices-api.jar'."/>
        <echo/>
        <echo message="  NOTE that any Java application that incidentally uses"/>
        <echo message="  one of the APIs included in Metro but does not"/>
        <echo message="  have all Metro JARs on its classpath may experience"/>
        <echo message="  class loading issues if you install webservices-api.jar"/>
        <echo message="  into ${java.home}/lib/endorsed."/>
    </target>

    <target name="install-api" description="Installs 'webservices-api.jar' into $JAVA_HOME/lib/endorsed" if="jdk6">
        <mkdir dir="${java.home}/lib/endorsed"/>
        <copy toDir="${java.home}/lib/endorsed" overwrite="true">
            <fileset dir="lib">
                <include name="webservices-api.jar"/>
            </fileset>
        </copy>
    </target>

    <taskdef name="updatesharedloaderprop" classname="com.sun.xml.ws.installer.UpdateSharedLoaderProp">
        <classpath>
            <fileset dir="./lib" includes="webservices-rt.jar"/>
        </classpath>
    </taskdef>

    <target name="update-catalina-props">
        <updatesharedloaderprop catalinaprops="${catalina.home}/conf/catalina.properties"/>
    </target>

    <target name="uninstall" depends="init"
            description="Remove the Metro jar files.">
        <delete quiet="true">
            <fileset dir="${catalina.lib.home}" includes="webservices-rt.jar, webservices-tools.jar, webservices-extra.jar, webservices-extra-api.jar"/>
        </delete>
        <delete quiet="true">
            <fileset dir="${catalina.endorsed}" includes="webservices-api.jar, jsr173_api.jar"/>
        </delete>
        <delete quiet="true">
            <fileset dir="${java.home}/lib/endorsed" includes="webservices-api.jar, jsr173_api.jar"/>
        </delete>
    </target>

    <target name="help">
        <echo message="Usage:"/>
        <echo message="  ant -f metro-on-tomcat.xml {install,uninstall}"/>
        <echo/>
        <echo message="$CATALINA_HOME must be set to the installation directory of Tomcat."/>
        <echo/>
        <echo message="install:"/>
        <echo message="  Installs Metro 2.1.1-b04 on Tomcat"/>
        <echo/>
        <echo message="uninstall: "/>
        <echo message="  Uninstalls Metro 2.1.1-b04 from Tomcat"/>
        <echo/>
        <antcall target="jdk6-message"/>
    </target>
    
</project>
