Moving from Tomcat 6 to Jboss AS5 – notes

I’m moving a bunch of single WAR sites from Tomcat 6 to Jboss AS5. Here are the configuration steps that need to be taken.

Assuming this is your tomcat config file:
%TOMCAT_HOME%/conf/server.xml:

<Host appBase="/home/wirelust/wirelust.com" autoDeploy="false"
     debug="0" deployXML="true" liveDeploy="true"
     name="domainname.com"
     unpackWARs="true">
 
    <Alias>www.wirelust.com</Alias>
 
     <Context cachingAllowed="true" cookies="true" crossContext="true" debug="0"
             displayName="wirelust" docBase="." path="" privileged="false"
             reloadable="true" swallowOutput="false" useNaming="true">
             <Resource auth="SERVLET" name="wirelustDatasource" scope="Shareable" type="javax.sql.DataSource"
                     username="username"
                     password="password"
                     driverClassName="net.sourceforge.jtds.jdbc.Driver"
                     url="jdbc:jtds:sqlserver://sql.wirelust.com/database"
                     removeAbandoned="true"
                     removeAbandonedTimeout="30"
                     logAbandoned="true"
             />
     </Context>
</Host>

Becomes the embedded tomcat config file:
%JBOSS_HOME%/server/default/deploy/jbossweb.sar/server.xml

<Host appBase="/home/wirelust/deploy/wirelust.com.war" autoDeploy="false"
     debug="0" deployXML="true" liveDeploy="true"
     name="domainname.com"
     unpackWARs="true">
 
    <Alias>www.wirelust.com</Alias>
</Host>

Notice that the appBase is now in a folder called “deploy”. You have to add this deploy folder to the config file:
%JBOSS_HOME%/server/default/conf/bootstrap/profile.xml

<bean name="BootstrapProfileFactory" class="org.jboss.system.server.profileservice.repository.StaticProfileFactory">
    <property name="bootstrapURI">${jboss.server.home.url}conf/jboss-service.xml</property>
    <property name="deployersURI">${jboss.server.home.url}deployers</property>
    <property name="applicationURIs">
        <list elementClass="java.net.URI">
            <value>${jboss.server.home.url}deploy</value>
 
            <value>file:/home/wirelust/deploy</value>
        </list>
    </property>
    <property name="attachmentStoreRoot">${jboss.server.data.dir}/attachments</property>
    <property name="profileFactory"><inject bean="ProfileFactory" /></property>
</bean>

Make sure your exploded directory ends in .war or jboss won’t see it.

Then in that new deploy directory, create a datasource file, in this case called “wirelust-ds.xml” with these contents:

<?xml version="1.0" encoding="UTF-8"?>
 
<!DOCTYPE datasources
    PUBLIC "-//JBoss//DTD JBOSS JCA Config 1.5//EN"
    "http://www.jboss.org/j2ee/dtd/jboss-ds_1_5.dtd">
<datasources>
    <local-tx-datasource>
        <jndi-name>wirelustDatasource</jndi-name>
        <connection-url>jdbc:jtds:sqlserver://sql.wirelust.com/wirelust</connection-url>
        <use-java-context>false</use-java-context>
        <driver-class>net.sourceforge.jtds.jdbc.Driver</driver-class>
        <user-name>username</user-name>
        <password>password</password>
 
        <Pool.LogAbandoned>false</Pool.LogAbandoned>
        <Pool.RemoveAbandoned>false</Pool.RemoveAbandoned>
        <Pool.RemoveAbandonedTimeout>50000</Pool.RemoveAbandonedTimeout>
   </local-tx-datasource>
</datasources>

In order to bind to the correct virtual host, you have to create a new file in your exploded WEB-INF directory called jboss-web.xml with these contents:

<!DOCTYPE jboss-web PUBLIC
    "-//JBoss//DTD Web Application 4.2//EN"
    "http://www.jboss.org/j2ee/dtd/jboss-web_4_2.dtd">
 
<jboss-web>  
    <context-root>/</context-root>  
    <virtual-host>wirelust.com</virtual-host>
</jboss-web>

Then the last change you have to make is how you are looking up your datasource in the application.
If you have code like this in your tomcat application it will fail because your jndi is not bound to java:comp/env:

Context ctx = new InitialContext();
Context envCtx = (Context) ctx.lookup("java:comp/env");
DataSource ds = (DataSource)envCtx.lookup("wirelustDatasource");
con = ds.getConnection();

This of course can be fixed with some further configuration and resource-ref settings but I couldn’t get that to work so I just changed the code to work either way:

Context ctx = null;
Context envCtx = null;
DataSource ds = null;
Connection con = null;
 
try {
	ctx = new InitialContext();
} catch (NamingException e) {
	e.printStackTrace();
}
 
if (ctx != null) {
	try {
		// look for the datasource in the tomcat location
		envCtx = (Context) ctx.lookup("java:comp/env");
		ds = (DataSource)envCtx.lookup("wirelustDatasource");
	} catch (javax.naming.NameNotFoundException nnfe) {
		try {
			// look for it in the jboss location
			ds = (DataSource)ctx.lookup(wirelustDatasource);
		} catch (Exception e) {
			e.printStackTrace();
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
 
// open the connection
if (ds != null) {
	try {
		con = ds.getConnection();
	} catch (Exception e) {
		e.printStackTrace();
	}
}