<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Id: build.xml 562 2006-08-11 15:49:10Z tgrall $ -->
<project name="how-to-simplembeans" default="all" basedir="." xmlns:oracle="antlib:oracle" >

    <!-- Import for OC4J ant integration. -->
    <import file="ant-oracle.xml"/>

   <description>
    How-To Simple MBeans
   </description>

	<!-- ****************************************************************
         ** Creates and deploys all deployable units for the           **
         ** application.                                               **
         ** ************************************************************* -->
    <target name="all" depends="package-app, deploy" description="Creates all deployable units for the application">
        <echo message="-----> Creates and deploys all deployable units for the application"/>
    </target>

    <target name="oracle-env-check" unless="oracle.home">
        <fail message="oracle.home property must be set."/>
    </target>

    <target name="java-env-check" unless="env.JAVA_HOME">
        <fail message="java.home property must be set."/>
    </target>

    <!-- ******************************************************
         ** Initialize all properties relevant to the        **
         ** execution of this how-to demonstration.          **
         ** *************************************************** -->
    <target name="init" depends="common,oracle-env-check,java-env-check" >
        <echo message="-----> Initializing project properties" />
        <property name="app.name" value="how-to-simplembeans" />
        <property name="context.root" value="${app.name}" />
        <property name="ear.name" value="${app.name}" />
        <property name="web.name" value="${app.name}-web" />
        <property name="jar.mbeans.name" value="simplembeans.jar"/>

        <property name="src.dir" value="./src" />
        <property name="bld.dir" value="./build" />
        <property name="lib.dir" value="./lib" />
        <property name="etc.dir" value="./etc" />
        <property name="log.dir" value="./log" />

        <property name="bld.ear.dir" value="${bld.dir}/${ear.name}" />
        <property name="bld.mbeans.dir" value="${bld.dir}/simplembeans" />
        <property name="bld.compiler" value="classic" />
        <property name="class.files" value="**/*.class" />
        <property name="bak.files" value="**/*~" />

        <property name="src.mbeans.dir" value="${src.dir}/mbeans"/>
        <property name="src.web.dir" value="${src.dir}/web" />
        <property name="src.web.content.dir" value="${src.dir}/web/content" />
        <property name="src.web.java.dir" value="${src.dir}/web/java" />
        <property name="bld.web.dir" value="${bld.dir}/${web.name}" />

    </target>

    <!-- ******************************************************
         ** Removes the how-to build process artifacts       **
         ** *************************************************** -->
    <target name="clean" depends="init" description="Removes the contents of the 'build', 'lib' and others files">
        <echo message="-----> Removing the contents of the ${bld.dir} and ${lib.dir} directories as well as other miscellaneous files used during the creation of the application"/>
        <delete dir="${bld.dir}"/>
        <delete dir="${lib.dir}"/>
        <delete dir="${log.dir}"/>
        <delete>
            <fileset dir="." includes="${bak.files}" defaultexcludes="no"/>
        </delete>
    </target>

    <!-- ****************************************************************
         ** Sets up required directories and configuration for how-to  **
         ** ************************************************************* -->
    <target name="setup" depends="init">
       <echo message="-----> Creating the required sub-directories" />
        <mkdir dir="${lib.dir}" />
        <mkdir dir="${bld.dir}" />
        <mkdir dir="${log.dir}" />
        <mkdir dir="${bld.ear.dir}" />
        <mkdir dir="${bld.ear.dir}/META-INF" />
        <mkdir dir="${bld.mbeans.dir}"/>
        <mkdir dir="${bld.web.dir}"/>
        <mkdir dir="${bld.web.dir}/WEB-INF"/>
        <mkdir dir="${bld.web.dir}/WEB-INF/classes"/>
    </target>

    <!--                     APPLICATIONS RELATED TARGETS                   -->

    <!-- ****************************************************************
         ** Package the EAR File                                       **
         ** *************************************************************  -->
    <target name="build" depends="package-app" description="Package the application in an ear file">
  	    <echo message="Location of the ear file: ${lib.dir}/${app.name}.ear"  />
    </target>

    <target name="package-app" depends="package-mbeans, package-web, ear-descriptor">
        <ear destfile="${bld.dir}/${app.name}.ear" appxml="${etc.dir}/application.xml">
            <fileset dir="${bld.ear.dir}" includes="**/*" />
        </ear>
        <copy file="${bld.dir}/${app.name}.ear" todir="${lib.dir}" />
    </target>

    <target name="package-mbeans" depends="setup, compile-mbeans">
      <jar basedir="${bld.mbeans.dir}" includes="**/*" destfile="${bld.ear.dir}/${jar.mbeans.name}"/>
    </target>

    <target name="compile-mbeans" depends="setup">
      <javac destdir="${bld.mbeans.dir}" srcdir="${src.mbeans.dir}" includes="**/*.java">
        <classpath path="${common.j2ee.class.path}"/>
      </javac>
    </target>

   <!-- ****************************************************************
         ** Compile all the source and put them into the web dir  **
         ** ************************************************************* -->
    <target name="compile-web-classes" depends="setup">
        <echo message="-----> Compile Web Classes" />
        <javac srcdir="${src.web.java.dir}"
               includes="**/*.java"
               destdir="${bld.web.dir}/WEB-INF/classes"
               debug="on">
            <classpath>
              <pathelement location="${bld.web.dir}/WEB-INF/classes"/>
              <pathelement path="${common.j2ee.class.path}"/>
            </classpath>
        </javac>
    </target>

    <!-- ****************************************************************
         ** Package the Web Application                                **
         ** *************************************************************  -->
    <target name="package-web" depends="compile-web-classes, web-descriptor">
        <war destfile="${bld.ear.dir}/${web.name}.war" webxml="${etc.dir}/web.xml">
            <fileset dir="${bld.web.dir}" includes="**/*" />
            <fileset dir="${src.web.content.dir}" includes="**/*" />
        </war>
    </target>

    <!-- ****************************************************************
         ** Add OC4J Web Descriptor	                                   **
         ** *************************************************************  -->
	<target name="web-descriptor" depends="setup">
        <echo message="-----> Add OC4J descriptor" />
		<copy todir="${bld.web.dir}/WEB-INF">
			<fileset dir="${etc.dir}" includes="orion-web.xml"/>
		</copy>
	</target>

    <!-- ****************************************************************
         ** Add OC4J application Descriptor	                                   **
         ** *************************************************************  -->
	<target name="ear-descriptor" depends="setup">
		<copy todir="${bld.ear.dir}/META-INF">
			<fileset dir="${etc.dir}" includes="orion-application.xml"/>
        </copy>
	</target>

    <!--              DEPLOYMENT/UNDEPLOYMENT RELATED TARGETS             -->

    <!-- ****************************************************************
         ** Deploys the Application to the server.                     **
         ** ************************************************************* -->
    <target name="deploy" depends="setup, check-oc4j-available, skip-deploy, deploy-ear" description="Deploys the application to your OC4J instance">
        <echo message="-----> Deployed the application components required by OC4J for the execution of this application"/>
    </target>

    <!-- ****************************************************************
         ** Undeploys the web service from the server.                 **
         ** ************************************************************* -->
    <target name="undeploy" depends="setup, check-oc4j-available, skip-deploy, undeploy-ear" description="Undeploys the application from your OC4J instance">
        <echo message="-----> Undeployed the application components required by OC4J for the execution of this application"/>
    </target>

    <!-- ****************************************************************
         ** Checks if the server is up, if not goes to skip.deploy     **
         ** ************************************************************* -->
    <target name="check-oc4j-available">
        <echo message="------> Checking to see if OC4J is available so deployment can be done."/>
        <echo message="        [oracleas.host=${oracleas.host}]"/>
        <echo message="        [oracleas.admin.port=${oracleas.admin.port}]"/>
          <echo message="        If OC4J is not started, please start it now."/>
        <waitfor checkeveryunit="second"
                 maxwaitunit="second"
                 maxwait="60"
                 checkevery="1"
                 timeoutproperty="skip.deploy" >
            <socket server="${oracleas.host}" port="${oracleas.admin.port}"/>
        </waitfor>
    </target>

    <!-- ****************************************************************
         ** Calles when deployment is skipped, echo messages           **
         ** ************************************************************* -->
    <target name="skip-deploy" if="skip.deploy">
        <echo message="The deployment phase was skipped because the target OC4J is not running."/>
        <echo message="Start OC4J before executing next time so deployment can take place."/>
    </target>


    <!-- ****************************************************************
         ** Undeploys the ear file from the server.                    **
         ** ************************************************************* -->
    <target name="undeploy-ear" depends="setup">
        <echo message="-----> Undeploying the application module deployment (ear) file"/>
        <echo message="       Target: ${oracleas.deployer.uri}"/>
        <oracle:undeploy deployerUri="${oracleas.deployer.uri}"
            userId="${oracleas.admin.user}"
            password="${oracleas.admin.password}"
            deploymentName="${app.name}"
            logFile="${log.dir}/undeploy-ear.log"/>
    </target>

    <!-- ****************************************************************
         ** Binds the context root for the web service.                **
         ** ************************************************************* -->
    <target name="bind-web-app" depends="setup,deploy-ear" unless="skip.deploy">
        <echo message="-----> Binding the web context for the application"/>
        <echo message="       Target: ${deployer.uri}"/>
        <oracle:bindWebApp
            deployerUri="${deployer.uri}"
            deploymentName="${app.name}"
            userId="${oracleas.admin.user}"
            password="${oracleas.admin.password}"
            webModule="${web.name}"
            webSiteName="${oracleas.binding.module}"
            contextRoot="/${app.name}"/>
    </target>

    <!-- ****************************************************************
         ** Deploys the ear file to the server.                        **
         ** ************************************************************* -->
    <target name="deploy-ear" depends="package-app" unless="skip.deploy">
        <echo message="-----> Deploying the application module deployment (ear) file"/>
        <echo message="       Target: ${oracleas.deployer.uri}"/>
        <oracle:deploy
            deployerUri="${oracleas.deployer.uri}"
            userId="${oracleas.admin.user}"
            password="${oracleas.admin.password}"
            file="${lib.dir}/${app.name}.ear"
            deploymentName="${app.name}"
            bindAllWebApps="${oracleas.binding.module}" />
    </target>

    <!-- ****************************************************************
     ** Packages the how-to for distribution                       **
     ** *************************************************************  -->
    <target name="package-how-to" depends="init, clean">
      <zip destfile="../${app.name}.zip">
        <zipfileset dir="." prefix="${app.name}" excludes=".svn/**, .settings/**, .classpath, .project"/>
      </zip>
    </target>

</project>
