Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

223 lines (175 sloc) 8.168 kB
<!--- Document Information -----------------------------------------------------
Title: JavaLoader.cfc
Author: Mark Mandel
Purpose: Utlitity class for loading Java Classes
Modification Log:
Name Date Description
Mark Mandel 08/05/2006 Created
Mark Mandel 22/06/2006 Added verification that the path exists
<cfcomponent name="JavaLoader" hint="Loads External Java Classes, while providing access to ColdFusion classes">
this.instance = StructNew();
this.instance.static.uuid = "A0608BEC-0AEB-B46A-0E1E1EC5F3CE7C9C";
<!------------------------------------------- PUBLIC ------------------------------------------->
<cffunction name="init" hint="Constructor" access="public" returntype="JavaLoader" output="false">
<cfargument name="loadPaths" hint="An array of directories of classes, or paths to .jar files to load" type="array" default="#ArrayNew(1)#" required="no">
<cfargument name="loadColdFusionClassPath" hint="Loads the ColdFusion libraries" type="boolean" required="No" default="false">
<cfargument name="parentClassLoader" hint="(Expert use only) The parent java.lang.ClassLoader to set when creating the URLClassLoader" type="any" default="" required="false">
var iterator = arguments.loadPaths.iterator();
var file = 0;
var classLoader = 0;
var networkClassLoaderClass = 0;
var networkClassLoaderProxy = 0;
//arguments.parentClassLoader = createObject("java", "java.lang.Thread").currentThread().getContextClassLoader();
//can't use above, as doesn't work in some... things
arguments.parentClassLoader = getPageContext().getClass().getClassLoader();
//arguments.parentClassLoader = createObject("java", "java.lang.ClassLoader").getSystemClassLoader();
//can't use the above, it doesn't have the CF stuff in it.
//classLoader = createObject("java", "com.compoundtheory.classloader0.NetworkClassLoader").init();
networkClassLoaderClass = getServerURLClassLoader().loadClass("com.compoundtheory.classloader.NetworkClassLoader");
networkClassLoaderProxy = createJavaProxy(networkClassLoaderClass);
classLoader = networkClassLoaderProxy.init(arguments.parentClassLoader);
classLoader = networkClassLoaderProxy.init();
file = createObject("java", "").init(;
if(NOT file.exists())
throw("PathNotFoundException", "The path you have specified could not be found", file.getAbsolutePath() & " does not exist");
//pass in the system loader
return this;
<cffunction name="create" hint="Retrieves a reference to the java class. To create a instance, you must run init() on this object" access="public" returntype="any" output="false">
<cfargument name="className" hint="The name of the class to create" type="string" required="Yes">
var class = getURLClassLoader().loadClass(arguments.className);
return createJavaProxy(class);
<cffunction name="getURLClassLoader" hint="Returns the in case you need access to it" access="public" returntype="any" output="false">
<cfreturn this.instance.ClassLoader />
<cffunction name="getVersion" hint="Retrieves the version of the loader you are using" access="public" returntype="string" output="false">
<cfreturn "0.5">
<!------------------------------------------- PACKAGE ------------------------------------------->
<!------------------------------------------- PRIVATE ------------------------------------------->
<cffunction name="ensureNetworkClassLoaderOnServerScope"
hint="makes sure there is a URL class loader on the server scope that can load me up some networkClassLoader goodness"
access="private" returntype="void" output="false">
var Class = createObject("java", "java.lang.Class");
var Array = createObject("java", "java.lang.reflect.Array");
var jars = queryJars();
var iterator = jars.iterator();
var file = 0;
var urls = Array.newInstance(Class.forName(""), ArrayLen(jars));
var counter = 0;
var urlClassLoader = 0;
var key = this.instance.static.uuid & "." & getVersion();
//server scope uuid
//we have it already? escape.
if(StructKeyExists(server, key))
Array.set(urls, counter, createObject("java", "").init(;
counter = counter + 1;
urlClassLoader = createObject("java", "").init(urls);
//put it on the server scope
server[key] = urlClassLoader;
<cffunction name="createJavaProxy" hint="create a javaproxy, dependent on CF server settings" access="private" returntype="any" output="false">
<cfargument name="class" hint="the java class to create the proxy with" type="any" required="Yes">
return createObject("component", "JavaProxy")._init(arguments.class);
return createObject("java", "").init(arguments.class);
<cffunction name="initUseJavaProxyCFC" hint="initialise whether or not to use the JavaProxy CFC instead of the coldfusion java object" access="public" returntype="string" output="false">
createObject("java", "");
catch(Object exc)
<cffunction name="queryJars" hint="pulls a query of all the jars in the /resources/lib folder" access="private" returntype="array" output="false">
var qJars = 0;
//the path to my jar library
var path = getDirectoryFromPath(getMetaData(this).path) & "lib/";
var jarList = "";
var aJars = ArrayNew(1);
var libName = 0;
<cfdirectory action="list" name="qJars" directory="#path#" filter="*.jar" sort="name desc"/>
<cfloop query="qJars">
libName = ListGetAt(name, 1, "-");
//let's not use the lib's that have the same name, but a lower datestamp
if(NOT ListFind(jarList, libName))
ArrayAppend(aJars, path & "/" & name);
jarList = ListAppend(jarList, libName);
<cfreturn aJars>
<cffunction name="getServerURLClassLoader" hint="returns the server URL class loader" access="private" returntype="any" output="false">
<cfreturn server[this.instance.static.uuid & "." & getVersion()] />
<cffunction name="setURLClassLoader" access="private" returntype="void" output="false">
<cfargument name="ClassLoader" type="any" required="true">
<cfset this.instance.ClassLoader = arguments.ClassLoader />
<cffunction name="getUseJavaProxyCFC" access="private" returntype="boolean" output="false">
<cfreturn this.instance.UseJavaProxyCFC />
<cffunction name="setUseJavaProxyCFC" access="private" returntype="void" output="false">
<cfargument name="UseJavaProxyCFC" type="boolean" required="true">
<cfset this.instance.UseJavaProxyCFC = arguments.UseJavaProxyCFC />
<cffunction name="throw" access="private" hint="Throws an Exception" output="false">
<cfargument name="type" hint="The type of exception" type="string" required="Yes">
<cfargument name="message" hint="The message to accompany the exception" type="string" required="Yes">
<cfargument name="detail" type="string" hint="The detail message for the exception" required="No" default="">
<cfthrow type="#arguments.type#" message="#arguments.message#" detail="#arguments.detail#">
Jump to Line
Something went wrong with that request. Please try again.