Using JavaLoader in Mach-II

There are probably dozens of ways to incorporate Mark Mandel’s JavaLoader library into Mach-II, but the way I’m going to focus on today involves writing a custom Property to handle configuration and access.

First off, let’s look at the source code for the Property:

<cfcomponent
   displayname="JavaLoaderProperty"
   output="false"
   extends="MachII.framework.Property">
 
  <cfset variables.loadPaths = ArrayNew(1)/>
  <cfset variables.javaLoader = ""/>
 
  <cffunction name="configure" access="public" output="false" returntype="void"
              hint="Called automatically by Mach-II on application initialization">
    <cfif isParameterDefined("loadPaths")>
      <cfset setLoadPaths(getParameter("loadPaths"))/>
    </cfif>
 
    <cfset prepareJavaLoader()/>
  </cffunction>
 
  <cffunction name="getJavaLoader" access="public" output="false" returntype="javaloader.JavaLoader">
    <cfreturn variables.javaLoader/>
  </cffunction>
 
  <cffunction name="prepareJavaLoader" access="private" output="false" returntype="void">
    <cfset var pathArray = ArrayNew(1)/>
    <cfset var i = 0/>
    <cfloop from="1" to="#ArrayLen(variables.loadPaths)#" index="i">
      <cfset pathArray[i] = expandPath(variables.loadPaths[i])/>
    </cfloop>
    <cfset variables.javaLoader = createObject("component", "javaloader.JavaLoader").init(pathArray)/>
  </cffunction>
 
  <cffunction name="setLoadPaths" access="public" output="false" returntype="void">
    <cfargument name="loadPaths" type="array" required="true"/>
    <cfset variables.loadPaths = arguments.loadPaths/>
  </cffunction>
  <cffunction name="getLoadPaths" access="public" output="false" returntype="array">
    <cfreturn variables.loadPaths/>
  </cffunction>
 
 
</cfcomponent>

The first thing you’ll notice is that this is a very simple Property. It only has five methods, 3 of which are accessors/mutators.

The configuration method, per Mach-II Property convention, accesses configured properties through the ‘isParameterDefined/getParameter’ methods. These are convenience methods for pulling configuration data out of the mach-ii.xml file.

Speaking of which, let’s take a look at the configuration of this particular parameter within the mach-ii.xml file:

    <property name="javaLoader" type="path.to.JavaLoaderProperty">
      <parameters>
	<parameter name="loadPaths">
	  <array>
	    <element value="lib/core.jar"/>
	    <element value="lib/javase.jar"/>
	    <element value="lib/jai_codec.jar"/>
	    <element value="lib/jai_core.jar"/>
	    <element value="lib/barbecue.jar"/>
	    <element value="lib/PDFRenderer.jar"/>
	  </array>
	</parameter>
      </parameters>
    </property>

The most important thing to notice is that the configuration parameter is called ‘loadPaths’, which is exactly what we were trying to access from the configuration method in the Property class.

Once the Property is configured, it sits in the PropertyManager and waits for Listeners to call it via the getProperty function, like so:

  <cffunction name="prepareBarcode" access="public" returntype="void" output="false">
    <cfargument name="event" type="MachII.framework.Event" required="yes"/>
 
    <cfset var javaLoader = getProperty("javaLoader").getJavaLoader()/>
    <cfset var barcodeFactory = javaLoader.create("net.sourceforge.barbecue.BarcodeFactory")/>
     . . .
  </cffunction>

Very simply, we access the javaLoader Property via getProperty("javaLoader").getJavaLoader(). Once we have that, we can load any Class that is contained within the Jar files we used during the Property configuration.

I hope this method of accessing a JavaLoader property is useful. If there are any questions or comments, please feel free to leave them below.

Leave a Reply