Monday, February 18, 2008

Installing Batik 1.7 with ColdFusion

I decided to write up a brief set of instructions on installing Batik 1.7 with ColdFusion, as a reference point for myself.


  1. Download and install the JavaLoader.cfc from riaforge.org.
    Example: I chose to install the JavaLoader directly beneath the webroot: C:\CFusionMX7\wwwroot\javaLoader\

  2. Download and install Batik 1.7 from apache.org.
    Example: My jars are installed in a folder named batik: C:\CFusionMX7\wwwroot\batik\batik-1.7



After installing the jar, restart the CF server.

Important note about using the JavaLoader.cfc

The article Using a Java URLClassLoader in CFMX Can Cause a Memory Leak recommends that you store a single instance of the JavaLoader in the server scope, rather than creating a new object each time. This prevents a memory leak caused by a bug in ColdFusion MX. I do not know if this bug exists in ColdFusion 8.

So in my Application.cfc I load the JavaLoader into the server scope, if it does not already exist. Then make the variable MyUniqueKeyForJavaLoader available to all pages in the OnRequest() function. Since I was not able to determine exactly which jars were needed I decided to use <cfdirectory> to load all jars in my /batik-1.7 directory.


<cfcomponent>
<cfset this.name = "BatikTests">
<cfset this.Sessionmanagement = true>
<cfset this.loginstorage = "session">


<cffunction name="onApplicationStart">
<!--- note, this is actually a harcoded UUID value --->
<cfset MyUniqueKeyForJavaLoader = "xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxxxxxx">

<!--- if we have not already created the javaLoader --->
<cfif NOT StructKeyExists(server, MyUniqueKeyForJavaLoader)>

<!--- get a listing of all batik jars (windows O/S) --->
<cfdirectory directory="#ExpandPath('/batik/batik-1.7/')#" filter="*.jar" recurse="true" name="jars">
<cfquery name="jarList" dbtype="query">
SELECT Directory +'\'+ Name AS PathToJar
FROM jars
</cfquery>

<!--- create an array of jar paths --->
<cfset jarPaths = listToArray(valueList(jarList.PathToJar, "|"), "|")>

<!--- get an exclusive lock on the server scope --->
<cflock scope="server" type="exclusive" timeout="10">
<!--- re-verify the javaloader was not already created --->
<cfif NOT StructKeyExists(server, MyUniqueKeyForJavaLoader)>
<cfset server[MyUniqueKeyForJavaLoader] = createObject("component", "javaloader.JavaLoader").init(jarPaths)>
</cfif>
</cflock>

</cfif>
</cffunction>

</cfcomponent>

0 comments:

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Header image adapted from atomicjeep