Friday, October 14, 2005

example of common use of web application context with struts

<!—web.xml -->

<web-app>

<display-name>Config Filter Application</display-name>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/webContext-hibernate.xml</param-value>

</context-param>

<servlet>

<servlet-name>SpringContextServlet</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Standard Action Servlet Configuration (with debugging) -->

<servlet>
<servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>

<param-name>config</param-name>

<param-value>/WEB-INF/struts-config.xml</param-value>

</init-param>

<init-param>

<param-name>debug</param-name>

<param-value>2</param-value>

</init-param>

<init-param>

<param-name>detail</param-name>

<param-value>2</param-value>

</init-param>

<load-on-startup>2</load-on-startup>

</servlet>

<!-- Standard Action Servlet Mapping -->

<servlet-mapping>

<servlet-name>action</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

<!-- The Usual Welcome File List -->

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<!-- Struts Tag Library Descriptors -->

<taglib>

<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>

</taglib>

<taglib>

<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-html.tld</taglib-location>

</taglib>

<taglib>

<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>

</taglib>

<taglib>

<taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>

</taglib>

<taglib>

<taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>

</taglib>

<taglib>

<taglib-uri>/WEB-INF/c.tld</taglib-uri>

<taglib-location>/WEB-INF/c.tld</taglib-location>

</taglib>


</web-app>

//ACTION BASE CLASS

import javax.servlet.ServletContext;
import org.apache.struts.action.Action;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public abstract class SpringBeanAccessorAction extends Action {

/** To be cast by client
*/

protected Object makeBLServiceBean(String name)
{

ServletContext servletContext = this.servlet.getServletContext();
WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
Object blservice = context.getBean(name);
return blservice;

}

}

Tuesday, October 11, 2005

simple ant project


<project basedir="." default= "web-war" name="contextuon">
<target name="init">
<property name="name" value="contextuon">
<property name="src.dir" value="src">
<property name="dist.dir" value="dist">
<property name="deploy.dir" value="C:/jboss-4.0.1sp1/server/default/deploy">
<property name="web.dir" value="web">
<property name="project.lib" value="lib">
<property name="build.dir" value="build">
<property name="config.dir" value="config">
<property name="build.compiler" value="modern">

<!-- jdbc -->
<property name="jdbc.url" value="jdbc:postgresql://pathfinder/amo">
<property name="database.driver" value="org.postgresql.Driver">
<property name="database.username" value="avexnet">
<property name="database.password" value="avexnet">
<property name="database.schema" value="">
<property name="database.catalog" value="">


<!--war info-->
<property name="build.dir.web" value="${build.dir}/web">
<property name="build.web.inf" value="${build.dir.web}/WEB-INF">
<property name="build.web.classes" value="${build.web.inf}/classes">

<property name="warfile" value="${name}.war">
<property name="web.orig.dir" value="web">
<property name="web.lib" value="${web.orig.dir}/WEB-INF/lib">
<property name="web.classes" value="${web.orig.dir}/WEB-INF/classes">

<!--javadoclet properties-->
<property name="javadoc.dir" value="${web.orig.dir}/html/docs">



<!-- Configure the custom Ant tasks for the Manager application -->
<echo message="JAVA_HOME=${java.home}">
<mkdir dir="${dist.dir}">

 <path id="project.class.path">
<fileset dir="${java.home}/lib">
<include name="*.jar">
</fileset>
<fileset dir="${project.lib}">
<include name="*.jar">
</fileset>
<fileset dir="${web.lib}">
<include name="*.jar">
</fileset>
<fileset dir="${dist.dir}">
<include name="*.jar">
</fileset>
</path>

<tstamp>
<format pattern="d-MM-yy" property="TODAY">
</tstamp>


</target>

<target name="prepare" depends="init"
description="Create build directories.">
<mkdir dir="${dist.dir}">
<mkdir dir="${build.dir}">
<mkdir dir="${build.web.classes}">
<mkdir dir="${build.dir.web}/WEB-INF">
<mkdir dir="${build.dir.web}/WEB-INF/lib">
<mkdir dir="${build.dir.web}/html">
<mkdir dir="${build.dir.web}/images">
<mkdir dir="${build.dir.web}/js">
</target>


<target name="hibernate-gui" depends="init, prepare" description="Runs hibernate ide">
<java classname="net.sf.hibern8ide.Hibern8IDE"
fork="yes" spawn="no">
<classpath refid="project.class.path">
<classpath>
<pathelement location="dist/${name}.jar">
</classpath>
</java>
</target>


<target name="jar-all" depends="init, prepare, web-compile">
<copy todir="${build.dir}/web/WEB-INF/classes">
<fileset dir="web/WEB-INF/classes">
<include name="**/*.lcf">
<include name="**/*.xml">
<include name="**/*.properties">
</fileset>
</copy>
<copy todir="${build.dir}/web/WEB-INF/classes">
<fileset dir="src">
<include name="**/*.properties">
<include name="**/*.hbm.xml">
</fileset>
</copy>
<jar jarfile="${dist.dir}/${name}.jar">
<fileset dir="${build.dir}/web/WEB-INF/classes" includes="**/*.class">
<fileset dir="${build.dir}/web/WEB-INF/classes" includes="**/*.xml">
<fileset dir="${build.dir}/web/WEB-INF/classes" includes="**/*.lcf">
<fileset dir="${build.dir}/web/WEB-INF/classes" includes="**/*.properties">
<fileset dir="${build.dir}" includes="META-INF/**">
</jar>
</target>


<target name="javadoc" depends="init, prepare">
<javadoc destdir="${javadoc.dir}" classpath="project.class.path" additionalparam="-tag ejb:X">
<fileset dir="${src.dir}">
<include name="**/*.java">
</fileset>
</javadoc>
</target>


<target name="web-compile" depends="init, prepare" description="Compile webapp Java files and copy HTML and JSP pages">
<javac deprecation="on" destdir="${build.web.classes}" srcdir="${src.dir}">
<include name="**/*.java">
<classpath refid="project.class.path">
</javac>
</target>

 <target name="web-war" depends="init, prepare, web-compile"
description="Compile app Java files and copy HTML and JSP pages" >

<copy todir="${build.dir.web}">
<fileset dir="${web.dir}">
<include name="**/*.*">
<exclude name="${web.dir}/WEB-INF/web.xml">
</fileset>
</copy>
<copy todir="${build.dir.web}/WEB-INF/classes">
<fileset dir="${src.dir}">
<include name="**/*.*">
<exclude name="**/*.java">
</fileset>
</copy>

<war warfile="${dist.dir}/${name}.war" webxml="${web.orig.dir}/WEB-INF/web.xml">
<fileset dir="${build.dir.web}">
<include name="**/*.*">
<exclude name="**/web.xml">
</fileset>
</war>
</target>

   <target name="all" depends="init, prepare, web-war" description="Compile webapp Java files and copy HTML and JSP pages">

<target name="deploy" depends="init, prepare, web-war">
<copy todir="${deploy.dir}">
<fileset dir="${dist.dir}">
<include name="${name}.war">
</fileset>
</copy>
</target>

<target name="clean" depends="init">
<delete dir="${build.dir}">
<delete dir="${generated.src.dir}">
<delete dir="${dist.dir}">
</target>

</project>

Wednesday, October 05, 2005

Tomcat 5 as an NT Service

This really should be much easier than it is, if you fail to update the VM options the thing fails, after a day of work the following example is the only one that worked for me.

install.bat

set JAVA_HOME=C:\j2sdk1.4.2_06
set CATALINA_HOME=c:\tomcat5
set CATALINA_BASE=c:\tomcat5
set EXECUTABLE=tomcat5.exe
set SERVICE_NAME=tomcat5server
set PR_STDOUTPUT=%CATALINA_HOME%\logs\stdout.log
set PR_STDERROR=%CATALINA_HOME%\logs\stderr.log

"%EXECUTABLE%" //IS//%SERVICE_NAME%
--DisplayName=cipc --Install="%CATALINA_HOME%\bin\tomcat5.exe"
--Jvm=auto
--JvmMs=128
--JvmMx=512
--StartMode=jvm
--Classpath="%CATALINA_HOME%\bin\bootstrap.jar;%CATALINA_HOME%\bin\servlet.jar;%JAVA_HOME%\lib\tools.jar"
--StopMode=jvm
--StartClass=org.apache.catalina.startup.Bootstrap
--StartParams=start
--StopClass=org.apache.catalina.startup.Bootstrap
--StopParams=stop

"%EXECUTABLE%" //US//"%SERVICE_NAME%"
--JvmOptions "-Xms128M;-Xmx512M;-Dcatalina.base=%CATALINA_BASE%;-Dcatalina.home=%CATALINA_HOME%;-Djava.endorsed.dirs=%CATALINA_HOME%\common\endorsed"
--StartMode jvm
--StopMode jvm

"%EXECUTABLE%" //US//"%SERVICE_NAME%"
++JvmOptions "-Djava.io.tmpdir=%CATALINA_BASE%\temp"
--JvmMs 128
--JvmMx 512