Skip to content

iBatis and ColdFusion

One of my colleagues has been writing Java web apps using Spring, Struts and the iBatis Data Mapper.

A lot of his code would be very useful to use in my ColdFusion apps, so I wanted to find out if there was a way to integrate ColdFusion and iBatis.

Back in July 2006, Charlie Arehart blogged about this possibility however as far as I am aware no-one provided a working solution.

However with the help of Mark Mandel’s Javaloader, and a blog post on integrating ColdFusion and Spring from Kevan Stannard I have been able to put together a demo of iBatis and ColdFusion working together.

My sample app code can be downloaded from http://www.abm.id.au/downloads/cfibatisdemo.zip

To get this to work you need to download a few dependencies:

Once you’ve downloaded these extract them to an appropriate place in your file system (I put them all at c:\javadev).  Then update the build.xml in my sample app to reference where you put these libraries.  You should also ensure ant is on your path.

You will also need to set up a sample database and connection to run this demo.  A mysql script is included in the db/ directory of the sample code.

Setting up the database connection is a bit more complicated.  My solution was to set up a datasource using JNDI.  I have ColdFusion 8 installed in a multiserver config so this had to be set up in the JRun Management Console.

The following screenshot shows the settings I used to create this datasource.  Note that I had to change the MySQL Driver Class Name from the default to get it to work with MySQL 5.x.  Also note that you will need to update web/beans.xml to use the correct name you give this datasource.

JNDI Datasource settings

If you are using another server, you will need to check your app server documentation to find out how to set up a JNDI datasource in your environment.

A couple of gotcha’s I found with the JNDI datasource are that if the database is not running when you start the server, it doesn’t seen to initialise the datasource correctly.  Similarly if the database is stopped and restarted, I found you need to restart the JRun server to get things working again.  Obviously this would be a problem in a production environment, and needs further investigation.

Finally to get things up and running you need to install Javaloader into your ColdFusion root directory or create an appropriate mapping.

Then build the sample app by issuing the command following command in the directory where you extracted the sample code:

ant dist

You can then copy the contents of the /dist dir from the sample app over to your ColdFusion server, and run the sample.

Please feel free to leave comments on this post if you have any problems or suggestions.

Tagged

Railo on JBoss (Multi-site configuration)

Earlier this year Sean Corfield blogged about a “multi-web” install of Railo on Tomcat.

I decided to see if I could set up a similar configuration on JBoss 5.1.0 and was able to do it successfully by following Sean’s steps with a few modifications.  These modifications are documented below.

My solution also includes some ideas I learnt from this blog post – http://help.shadocms.com/blog/2009/running-railo-and-adobe-coldfusion-on-the-same-context-root-in-jboss.cfm

Firstly, I took a copy of the jboss “web” instance and named it railo, ie:

copy <JBOSS_HOME>/server/web to <JBOSS_HOME>/server/railo

This will become our railo server instance.

NB: <JBOSS_HOME> is the directory where I installed JBoss – in my case c:/jboss/jboss-5.1.0.GA

Now I started following Sean’s instructions.  The first issue I ran into was when I came to edit the conf/catalina.properties file.  I could not find the JBoss equivalent (there may be one, but I just couldn’t find it).  However I found that by putting the Railo JARs into <JBOSS_HOME>/server/railo/lib this had the same effect.

Next Sean mentioned editing the Tomcat conf/web.xml file.  The corresponding JBoss file to edit is:

<JBOSS_HOME>/server/railo/deployers/jbossweb.deployer/web.xml

At this point we we can start the server and have a look at the server administrator.  To do this (on windows at least) go into the command prompt, cd to <JBOSS_HOME> and run this command:

bin\run -c railo

Once it starts up, you can then browse to http://localhost:8080/railo-context/admin.cfm to view the Railo Server Administrator.

After confirming it runs, stop it again (I just use CTRL-C to stop the JBoss instances – there may be a cleaner way but I am not aware of it).

The next thing in JBoss that differs from the Tomcat instructions is adding your new host definitions.  In Tomcat this is done in conf/server.xml, but in JBoss 5.1.0 it is done in <JBOSS_HOME>/server/railo/conf/bootstrap/profile.xml.

The change I made to this file is shown below:

<!--
 The profile configuration
 This contains required properties:
 The uri to jboss-service.xml.
 The uri to the deployers folder.
 A list of uris to deploy folders. -->
 <bean name="BootstrapProfileFactory">
 <property name="bindingsURI">${jboss.server.home.url}conf/bindingservice.beans</property>
 <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:///C:/www/railo-deploy</value>
 </list>
 </property>
 <property name="attachmentStoreRoot">${jboss.server.data.dir}/attachments</property>
 <property name="profileFactory"><inject bean="ProfileFactory" /></property>
 </bean>

Note that this is a URI, so it must include file:/// (this caught me out on my first attempt).  How I did this was to create a directory called c:/www/railo-deploy, then in that directory create new directories called web1.war and web2.war

Next (borrowing from the ShadoCMS blog) <JBOSS_HOME>/server/railo/deploy/jbossweb.sar/server.xml to have two more <Host> nodes:

 <Host name="web1.local">
 <Alias>web1.local</Alias>
 </Host>

 <Host name="web2.local">
 <Alias>web2.local</Alias>
 </Host>

The final piece of the puzzle was then to create a web.xml and  jboss-web.xml in each of the WEB-INF dirs, ie:

c:/www/railo-deploy/web1.war/WEB-INF/web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
 PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
 <display-name>Web App 1</display-name>
 <description>
 Railo Web 1
 </description>
</web-app>

c:/www/railo-deploy/web1.war/WEB-INF/jboss-web.xml

<jboss-web>
<context-root>/</context-root>
<virtual-host>web1.local</virtual-host>
</jboss-web>

c:/www/railo-deploy/web2.war/WEB-INF/web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
 PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
 <display-name>Web App 2</display-name>
 <description>
 Railo Web 2
 </description>
</web-app>

c:/www/railo-deploy/web2.war/WEB-INF/jboss-web.xml

<jboss-web>
<context-root>/</context-root>
<virtual-host>web2.local</virtual-host>
</jboss-web>

All going well you should now have a working multi-web setup in JBoss.

Creative Commons License
This work is licensed under a Creative Commons Attribution-Share Alike 2.5 Australia License.

Tagged , ,

Railo 3 on JBoss 5

Railo 3 will not run on JBoss 5 without some modifications, as some of the packaged jars conflict with the jars in Jboss’s classpath.  The error reported is similar to that below:


2009-09-07 20:20:09,354 ERROR [org.jboss.web.tomcat.service.deployers.JBossContextConfig] (main) XML error parsing: context.xml
org.jboss.xb.binding.JBossXBRuntimeException: Failed to create a new SAX parser
at org.jboss.xb.binding.UnmarshallerFactory$UnmarshallerFactoryImpl.newUnmarshaller(UnmarshallerFactory.java:100)
at org.jboss.web.tomcat.service.deployers.JBossContextConfig.processContextConfig(JBossContextConfig.java:549)
at org.jboss.web.tomcat.service.deployers.JBossContextConfig.init(JBossContextConfig.java:536)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:279)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.core.StandardContext.init(StandardContext.java:5436)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4148)
at org.jboss.web.tomcat.service.deployers.TomcatDeployment.performDeployInternal(TomcatDeployment.java:310)
at org.jboss.web.tomcat.service.deployers.TomcatDeployment.performDeploy(TomcatDeployment.java:142)
at org.jboss.web.deployers.AbstractWarDeployment.start(AbstractWarDeployment.java:461)
at org.jboss.web.deployers.WebModule.startModule(WebModule.java:118)
at org.jboss.web.deployers.WebModule.start(WebModule.java:97)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:157)
at org.jboss.mx.server.Invocation.dispatch(Invocation.java:96)
at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:264)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:668)
at org.jboss.system.microcontainer.ServiceProxy.invoke(ServiceProxy.java:206)
at $Proxy38.start(Unknown Source)

One solution (which seems to work perfectly) is to remove the offending jars from <railo_home>/WEB-INF/lib, as described here:

http://coldshen.com/blog/index.cfm/2008/8/2/Running-Railo-3-beta-on-JBoss-5

An alternative solution, is to create a file named jboss-classloading.xml and place this into <railo_home>/WEB-INF.  The contents of the file should be similar to that shown below:

<classloading xmlns="urn:jboss:classloading:1.0"
name="railo.war"
domain="railo_Domain"
export-all="NON_EMPTY"
import-all="false">
</classloading>

More details are available from the JBoss wiki -  http://www.jboss.org/community/wiki/useJBossWebClassLoaderinJBoss5

Tagged , ,