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

<project name="kirussys" default="deploy.weblogic">
    <!-- =================================================================== -->
    <!--   Properties                                                        -->
    <!-- =================================================================== -->
	
	<!-- load project specific user properties first -->
	<property file="build.properties"/>
	
	<!-- load global user properties next -->
	<property file="${user.home}/.build.properties"/>
	
	<!-- declare properties to override global defaults next -->
	<property name="src.dir" value="Source"/>
	
	<!-- load global defaults -->
	<property file="build-default.properties"/>
	
	<!-- declare project specific properties next -->
	
    <property name="build.dir.ejb"            value="${build.dir}/ejb"/>
	<property name="build.dir.war"             value="${dist.dir.ear}/war"/>
	<property name="dist.dir.ear"             value="${dist.dir}/ear"/>
	<property name="resource.ant.dir"         value="${resource.dir}/ant"/>
	<property name="test.kirus.servername"    value="localhost"/>
	<property name="test.kirus.serverport"    value="7001"/>
	<property name="test.kirs.servletcontext" value="kirussys"/>
	<property name="weblogic.dir"             value="c:/bea/weblogic701"/>
	
    <!-- =================================================================== -->
    <!--   Paths                                                             -->
    <!-- =================================================================== -->
    
	<path id="src.path">
		<pathelement path="${src.dir}"/>
	</path>
	
	<path id="compile.classpath">
		<pathelement location="${java.home}/lib/jaws.jar"/>
		<pathelement location="${lib.dir}/jakarta-cactus-1.4.1.jar"/>
		<pathelement location="${lib.dir}/httpunit-1.2.4.jar"/> 
		<pathelement location="${lib.dir}/jakarta-log4j-1.2.8.jar"/>
		<pathelement location="${lib.dir}/jakarta-poi-1.5.1.jar"/>
		<pathelement location="${lib.dir}/jdom-b8.jar"/>
		<pathelement location="${lib.dir}/junit-3.8.1.jar"/>
		<pathelement location="${lib.dir}/manifestsystem/fedex/FDXParser.jar"/>
		<pathelement location="${lib.dir}/manifestsystem/fedex/FedExAPI.jar"/>
		<pathelement location="${lib.dir}/oracle.jar"/>
		<path refid="weblogic.classpath"/>
    </path>
    
    <path id="test.classpath">
		<pathelement location="${src.dir}"/> <!-- to find the jndi.properties file there -->
		<pathelement location="${buildto.dir}"/>
		<pathelement location="${lib.dir}/jdom-b8.jar"/>
		<pathelement location="${lib.dir}/httpunit-1.2.4.jar"/> 		
		<pathelement location="${lib.dir}/jakarta-cactus-1.4.1.jar"/>
		<pathelement location="${lib.dir}"/> <!-- to find the cactus.properties file there -->
		<pathelement location="${lib.dir}/xerces.jar"/>
		<pathelement location="${lib.dir}/jakarta-log4j-1.2.8.jar"/>
		<pathelement location="${lib.dir}/Tidy.jar"/>
    	<pathelement location="${lib.dir}/clover-1.1.jar"/>
    	<pathelement location="${lib.dir}/aspectjrt-1.0.5.jar"/>
		<pathelement location="${lib.dir}/jakarta-commons-httpclient-2.0a3.jar"/>
		<pathelement location="${lib.dir}/jakarta-commons-logging-1.0.2.jar"/>	
		<path refid="weblogic.classpath"/>
    </path>
    
    <path id="taskdef.classpath">
    	<pathelement location="${lib.dir}/clover-1.1.jar"/>

    	<pathelement location="${lib.dir}/jakarta-commons-collections-2.0.jar"/>
    	<pathelement location="${lib.dir}/jakarta-commons-logging-1.0.2.jar"/>
    	<pathelement location="${lib.dir}/jakarta-log4j-1.2.8.jar"/>
		<pathelement location="${lib.dir}/xdoclet-1.2b2.jar"/>
    	<pathelement location="${lib.dir}/xdoclet-bea-module-1.2b2.jar"/>
    	<pathelement location="${lib.dir}/xdoclet-ejb-module-1.2b2.jar"/>
    	<pathelement location="${lib.dir}/xdoclet-jboss-module-1.2b2.jar"/>
    	<pathelement location="${lib.dir}/xdoclet-jmx-module-1.2b2.jar"/>
    	<pathelement location="${lib.dir}/xdoclet-web-module-1.2b2.jar"/>
    	<pathelement location="${lib.dir}/xdoclet-xjavadoc-1.2b2.jar"/>
    	<pathelement location="${lib.dir}/j2ee-1.3.1.jar"/>
    </path>
    
    <!-- =================================================================== -->
    <!--   Task Definitions                                                  --> 
    <!-- =================================================================== -->

	<taskdef resource="clovertasks" classpathref="taskdef.classpath"/>
    <taskdef name="webdoclet"  classname="xdoclet.modules.web.WebDocletTask" classpathref="taskdef.classpath"/>
	
    <!-- =================================================================== -->
    <!--   Targets                                                           --> 
    <!-- =================================================================== -->
    
	<!-- Cleans all objects created by build and dist targets -->
	<target name="clean">
		<delete dir="${build.dir.war}"/>
		<delete dir="${build.dir}"/>
		<delete dir="${dist.dir}"/>
		<!-- clean up the garbage files left behind by failed JUnit runs -->
		<delete>
			<fileset dir="." includes="junit*.properties"/>
		</delete>
	</target>
    <!-- Creates any directories that are needed -->
    <target name="init">
    	<tstamp/>
        <mkdir dir="${build.dir}"/>
        <mkdir dir="${dist.dir}"/>
    </target>
    
    
    <!-- Compiles all java source files -->
	<target name="compile" depends="init">
        <mkdir dir="${build.dir.classes}"/>
	    <!-- first compile everything -->
	    <javac destdir="${build.dir.classes}" deprecation="${javac.deprecation}" debug="${javac.debug}" fork="${javac.fork}" memoryInitialSize="${javac.memoryInitialSize}" memoryMaximumSize="${javac.memoryMaximumSize}">
	        <classpath refid="compile.classpath"/>
	        <src refid="src.path"/>
	        <include name="**/*.java"/>
	    </javac>
	    <!-- copy the conf files over -->
	    <copy todir="${build.dir.classes}/conf">
			<fileset dir="${src.dir}">
				<include name="storageAccessConfig.properties"/>
				<include name="commandProcessor.properties"/>
				<include name="Ses.properties"/>
			</fileset>
			<fileset dir="${src.dir}/../config">
				<include name="timer.properties"/>
				<include name="log4j.xml"/>
				<include name="log4j.dtd"/>
			</fileset>
		</copy>
	</target>
	
	
	<!-- Creates the applet jar file -->
	<target name="applet" depends="compile">
		<jar destfile="${build.dir}/applets.jar">
			<!-- get the image files -->
			<fileset dir="${src.dir}/.." includes="graphics/*"/>
			<!-- now get the i18n resource files -->
			<fileset dir="${src.dir}" includes="com/kirus/system/resource/*.properties"/>
			<!-- now get all of the class files -->
			<fileset dir="${build.dir.classes}" includesfile="${resource.ant.dir}/applet-includes.txt">
				<exclude name="com/kirus/client/ui/kirussys/route/RouteHeader*.class"/>
				<exclude name="com/kirus/client/ui/kirussys/route/RouteNodeApplet.class"/>
				<exclude name="com/kirus/client/ui/kirussys/route/RouteNodePanel.class"/>
				<exclude name="com/kirus/client/ui/kirussys/route/RouteNodeCode*.class"/>
	            <exclude name="com/kirus/client/ui/kirussys/pbe/**/*.class"/>
	            <exclude name="com/kirus/ui/**/printing/**/*.class"/>
	            <exclude name="com/kirus/ui/**/reporting/**/*.class"/>
	        </fileset>
		</jar>
		<jar destfile="${build.dir}/printing.jar">
			<fileset dir="${build.dir.classes}">
				<include name="com/kirus/printing/**/*.class"/>
				<include name="com/kirus/system/util/Utils.class"/>
				<include name="com/kirus/system/util/StringParser.class"/>
				<include name="com/kirus/utility/Log.class"/>
				<include name="com/kirus/ui/**/printing/**/*.class"/>
				<include name="com/kirus/ui/**/reporting/**/*.class"/>
				<include name="com/kirus/ui/**/reporting/clientReportConfig.cfg"/>
				<exclude name="com/kirus/ui/**/reporting/mgr/ReportMgr.class"/>
				<exclude name="com/kirus/ui/**/reporting/mgr/win/WinReportMgr.class"/>
			</fileset>
		</jar>
		</target>
	
	
	<!-- Creates all of the ejb jar files -->
	<target name="ejb" depends="compile">
        <mkdir dir="${build.dir.ejb}"/>
        <!-- this target is huge, so we extract it into a separate script for clarity -->
        <ant antfile="${resources.ant.dir}/build.ejb.jar.xml"/>
	</target>
	
	
	<!-- this is the external target for the war archive - the 'war.development' and
		 'war.production' targets should ONLY be called by this target -->
	<target name="war" depends="war.development, war.production"/>
	
	<!-- creates the war file in exploded format that can be used for dev. time deployments -->
	<target name="war.development" depends="compile, applet">
        <mkdir dir="${build.dir.war}"/>
        <!-- generate the *web.xml files using XDoclet-->
   		<webdoclet destdir="${build.dir.war}/WEB-INF" mergeDir="${resource.dir}/war">
   			<fileset dir="${src.dir}">
				<include name="com/kirus/system/controller/ExcelServlet.java"/>
				<include name="com/kirus/system/controller/ServletController.java"/>
				<include name="com/kirus/web/controller/ClientAppHttpController.java"/>
   			</fileset>
            <deploymentdescriptor/>
            <jbosswebxml version="3.0" contextroot="/"/>
            <weblogicwebxml version="7.0"/>
   		</webdoclet>
        <mkdir dir="${build.dir}/war2"/>
   		<webdoclet destdir="${build.dir}/war2">
   			<fileset dir="${src.dir}">
				<include name="com/kirus/b2b/system/controller/XMLServlet.java"/>
				<include name="com/kirus/b2b/system/controller/OutgoingTestServlet.java"/>
				<include name="com/kirus/b2b/system/controller/servlet/XMLSyncController.java"/>
   			</fileset>
            <deploymentdescriptor/>
            <jbosswebxml version="3.0" contextroot="/kirus-seoul"/>
            <weblogicwebxml version="7.0"/>
   		</webdoclet>
   		<!-- copy over the required class files -->
   		<copy todir="${build.dir.war}/WEB-INF/classes">
   			<fileset dir="${build.dir.classes}"/>
   			<fileset dir="${src.dir}" includes="com/**/*.properties"/>
   		</copy>
   		<!-- copy over the 3rd party jars -->
   		<copy todir="${build.dir.war}/WEB-INF/lib">
   			<fileset dir="${lib.dir}">
   				<include name="jdom-b8.jar"/>
   				<include name="jakarta-log4j-1.2.8.jar"/>
   			</fileset>
   		</copy>
        <!-- copy the configuration files -->
        <copy todir="${build.dir.war}/WEB-INF/conf">
        	<fileset dir="${src.dir}" includes="sesConfig.cfg, commandProcessor.properties"/>
        	<fileset dir="config"     includes="triggers/*"/>
        </copy>
        <!-- this flattens the tlds all into the one directory --> 
		<copy todir="${build.dir.war}/WEB-INF/tlds" flatten="true">
			<fileset dir="${src.dir}/com/kirus/ses/ui/html/tagext" includes="**/*.tld"/>
		</copy>
		<copy todir="${build.dir.war}">
            <!-- the javascript and image files are actually in the right place.
            	 the css files should be put under a directory called css though --> 
            <fileset dir="${src.dir}/com/kirus/client/web/html">
                <include name="javascript/**" /> 
                <include name="images/**" /> 
                <include name="*.css" /> 
            </fileset>
			<!-- now get the jsp files. these should really go under /WEB-INF/jsp since clients
				 never refer to them directly and it also gives additional access security -->
            <fileset dir="${src.dir}/com/kirus/presentation/kirussys" includes="**/*.jsp"/>
            <!-- now add the applet jar file -->
            <fileset dir="${build.dir}" includes="applets.jar, printing.jar"/>         
		</copy>
	</target>
	
	<!-- creates an actual war archive for production deployment. this target only executes
		 if the property 'production.mode' has a value -->
	<target name="war.production" depends="war.development" if="production.mode">
		<signjar jar="${build.dir}/printing.jar" alias="mycertkirus" storepass="~GodIsGood!" keystore="${src.dir}/../dev_tools/.keystore"/>
		<signjar jar="${dist.dir.war}/applets.jar" alias="mycertkirus" storepass="~GodIsGood!" keystore="${src.dir}/../dev_tools/.keystore"/>
		<zip destfile="${dist.dir}/kirus-kiev.war" basedir="${build.dir.war}"/>
	</target>
	
	
	<!-- this is the external target for the ear archive - the 'ear.development' and
		 'ear.production' targets should ONLY be called by this target -->
	<target name="ear" depends="ear.development, ear.production"/>
	<!-- creates the ear file in exploded format that can be used for dev. time deployments -->
	<target name="ear.development" depends="ejb, war">
		<mkdir dir="${dist.dir.ear}"/>
		<!-- create a jar of all the classes needed, that are not part of the ejb.jar files -->
		<jar destfile="${dist.dir.ear}/kirussys.jar">
			<fileset dir="${build.dir.classes}" excludes="test/**"/>
			<fileset dir="${src.dir}" includes="com/kirus/system/resource/*.properties"/>
			<!--<fileset dir="${src.dir}/../config" includes="conf/log4j.xml"/>-->
		</jar>
		<!-- copy over the needed 3rd party jars -->
		<copy todir="${dist.dir.ear}">
			<fileset dir="${lib.dir}">
				<include name="jdom.jar"/>
        		<include name="jakarta-log4j.jar"/>
			</fileset>
		</copy>
		<!-- get the deployment descriptors -->
		<copy todir="${dist.dir.ear}/META-INF">
			<fileset dir="${resources.dir}/ear" includes="application.xml, weblogic-application.xml"/>
		</copy>
		<!-- set the reference to the war file for development time. replace production path first
			 incase we have deployed in that configuration previously -->
		<replace file="${dist.dir.ear}/META-INF/application.xml" token="${ant.project.name}.war" value="@web-uri@"/>
		<replace file="${dist.dir.ear}/META-INF/application.xml" token="@web-uri@" value="war"/>
		<!-- now get the ejb jar files -->
		<copy todir="${dist.dir.ear}">
			<fileset dir="${build.dir.ejb}" includes="*.jar"/>
		</copy>			
	</target>
	<!-- creates an actual ear archive for production deployment. this target only executes
		 if the property 'production.mode' has a value -->
	<target name="ear.production" depends="ear.development" if="production.mode">
		<!-- get the war file -->
		<copy todir="${dist.dir.ear}">
			<fileset dir="${dist.dir}" includes="${ant.project.name}.war"/>
		</copy>
		<!-- set the reference to the war file for production time. replace development path first
			 incase we have deployed in that configuration previously -->
		<replace file="${dist.dir.ear}/META-INF/application.xml" token="war" value="@web-uri@"/>
		<replace file="${dist.dir.ear}/META-INF/application.xml" token="@web-uri@" value="${ant.project.name}.war"/>
		<!-- create the ear archive -->
		<zip destfile="${dist.dir}/${ant.project.name}.ear" basedir="${dist.dir.ear}" excludes="war/**"/>
	</target>

	
	<target name="runtests">
		<path id="taskdef.classpath">
			<pathelement location="${ant.home}/optional.jar"/>
			<pathelement location="${lib.dir}/junit.jar"/>
		</path>
		<taskdef name="junit" classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask" classpathref="taskdef.classpath"/>
		<taskdef name="junitreport" classname="org.apache.tools.ant.taskdefs.optional.junit.XMLResultAggregator" classpathref="taskdef.classpath"/>
		<junit printsummary="yes" fork="yes" haltonfailure="true">
		    <classpath refid="test.classpath"/>
		    <!--<formatter type="xml"/>-->
		    <jvmarg value="-Dtest.kirus.serverport=${test.kirus.serverport}"/>
		    <jvmarg value="-Dtest.kirus.servername=${test.kirus.servername}"/>
		    <jvmarg value="-Dtest.kirus.servletcontext=${test.kirus.servletcontext}"/>
			<jvmarg value="-Xms128m"/>
			<jvmarg value="-Xmx128m"/>
		    <batchtest todir="${test.output.dir}">
		        <fileset dir="${build.dir.classes}">
		            <include name="test/**/*Test.class"/>
		            <exclude name="**/b2b/**/*.class"/>
		            <!-- these classes match the pattern, but don't define any tests, so JUnit gets upset -->
		            <exclude name="test/kirus/data/persistent/kirussys/outbound/order/OutboundOrderLinePOTest.class"/>
		            <exclude name="test/kirus/data/persistent/kirussys/requisition/order/RequisitionOrderLinePOTest.class"/>
		        </fileset>
		    </batchtest>
		</junit>
		<junitreport todir="${test.output.dir}">
			<fileset dir="${test.output.dir}">
		    	<include name="TEST-*.xml"/>
		  	</fileset>
			<report format="frames" todir="."/>
		</junitreport>
	</target>
	
	<target name="test" depends="ear">
		<parallel>
			<antcall target="start.weblogic"/>
			<sequential>
				<waitfor maxwait="3" maxwaitunit="minute" checkevery="500">
    				<http url="http://${test.kirus.servername}:${test.kirus.serverport}/sesController"/>
				</waitfor>
				<antcall target="deploy.weblogic"/>
				<antcall target="runtests"/>
				<antcall target="stop.weblogic"/>
			</sequential>
		</parallel>
	</target>
	
	
	<property name="bea.dir"              value="c:/bea"/>
	<property name="weblogic.domain.dir"  value="c:/bea/user_projects/mydomain"/>
	<property name="weblogic.server.name" value="myserver"/>
	<property name="weblogic.username"    value="root"/>
	<property name="weblogic.password"    value="password"/>
	<path id="weblogic.classpath">
		<pathelement location="${lib.dir}/xerces.jar"/>
		<pathelement location="${java.home}/lib/tools.jar"/>
		<pathelement location="${weblogic.dir}/server/lib/weblogic_sp.jar"/>
		<pathelement location="${weblogic.dir}/server/lib/weblogic.jar"/>
	</path>
	
	<target name="start.weblogic" unless="test.attended">
		<java classname="weblogic.Server" fork="yes" dir="${weblogic.domain.dir}">
			<classpath refid="weblogic.classpath"/>
			<jvmarg value="-hotspot"/>
			<jvmarg value="-Xms256m"/>
			<jvmarg value="-Xmx256m"/>
			<jvmarg value="-Dweblogic.security.SSL.trustedCAKeyStore=${weblogic.dir}/server/lib/cacerts"/>
			<jvmarg value="-Dweblogic.Name=${weblogic.server.name}"/>
			<jvmarg value="-Dbea.home=${bea.dir}"/>
			<jvmarg value="-Dweblogic.management.username=${weblogic.username}"/>
			<jvmarg value="-Dweblogic.management.password=${weblogic.password}"/>
			<jvmarg value="-Dweblogic.ProductionModeEnabled=false"/>
			<jvmarg value="-Dweblogic.management.discover=false"/>
			<jvmarg value="-Djava.security.policy=${weblogic.dir}/server/lib/weblogic.policy"/>
		</java>
	</target>
	
	<!-- delegates to the weblogic specific Ant script -->
	<target name="deploy.weblogic" unless="test.attended">
		<!--<ant antfile="${resources.ant.dir}/deploy.weblogic.xml"/>-->
		<java classname="weblogic.Deployer" fork="yes">
			<classpath refid="weblogic.classpath"/>
			<arg line="-adminurl http://${test.kirus.servername}:${test.kirus.serverport} -name kirus.production -source ${dist.dir}/kirussys.ear -targets ${weblogic.server.name} -activate -password ${weblogic.password} -user ${weblogic.username}"/>
		</java>		
	</target>
	
	<target name="stop.weblogic" unless="test.attended">
		<java classname="weblogic.Admin" fork="yes" dir="${weblogic.domain.dir}">
			<classpath refid="weblogic.classpath"/>
			<arg line="-url t3://${test.kirus.servername}:${test.kirus.serverport} -username ${weblogic.username} -password ${weblogic.password} SHUTDOWN"/>
		</java>
	</target>
	
</project>