PageRenderTime 264ms CodeModel.GetById 161ms app.highlight 2ms RepoModel.GetById 98ms app.codeStats 1ms

/wheels/vendor/memcached/com/util/javaloader/JavaLoader.cfc

http://cfwheels.googlecode.com/
ColdFusion CFScript | 180 lines | 133 code | 37 blank | 10 comment | 6 complexity | 68a8b3b848f7ed502a7d22f50500f9af MD5 | raw file
  1<!--- Document Information -----------------------------------------------------
  2
  3Title:      JavaLoader.cfc
  4
  5Author:     Mark Mandel
  6Email:      mark@compoundtheory.com
  7
  8Website:    http://www.compoundtheory.com
  9
 10Purpose:    Utlitity class for loading Java Classes
 11
 12Usage:
 13
 14Modification Log:
 15
 16Name			Date			Description
 17================================================================================
 18Mark Mandel		08/05/2006		Created
 19Mark Mandel		22/06/2006		Added verification that the path exists
 20
 21------------------------------------------------------------------------------->
 22<cfcomponent name="JavaLoader" hint="Loads External Java Classes, while providing access to ColdFusion classes">
 23
 24<cfscript>
 25	instance = StructNew();
 26	instance.static.uuid = "A0608BEC-0AEB-B46A-0E1E1EC5F3CE7C9C";
 27</cfscript>
 28
 29<!------------------------------------------- PUBLIC ------------------------------------------->
 30<cffunction name="init" hint="Constructor" access="public" returntype="JavaLoader" output="false">
 31	<cfargument name="loadPaths" hint="An array of directories of classes, or paths to .jar files to load" type="array" default="#ArrayNew(1)#" required="no">
 32	<cfargument name="loadColdFusionClassPath" hint="Loads the ColdFusion libraries" type="boolean" required="No" default="false">
 33	<cfargument name="parentClassLoader" hint="(Expert use only) The parent java.lang.ClassLoader to set when creating the URLClassLoader" type="any" default="" required="false">
 34
 35	<cfscript>
 36		var iterator = arguments.loadPaths.iterator();
 37		var file = 0;
 38		var classLoader = 0;
 39		var networkClassLoaderClass = 0;
 40		var networkClassLoaderProxy = 0;
 41
 42		if(arguments.loadColdFusionClassPath)
 43		{
 44			arguments.parentClassLoader = createObject("java", "java.lang.Thread").currentThread().getContextClassLoader();
 45			//arguments.parentClassLoader = createObject("java", "java.lang.ClassLoader").getSystemClassLoader();
 46			//can't use the above, it doesn't have the CF stuff in it.
 47		}
 48
 49		//hackNetworkLoaderIntoClassPath();
 50		ensureNetworkClassLoaderOnServerScope();
 51
 52		//classLoader = createObject("java", "com.compoundtheory.classloader0.NetworkClassLoader").init();
 53		networkClassLoaderClass = getServerURLClassLoader().loadClass("com.compoundtheory.classloader.NetworkClassLoader");
 54
 55		networkClassLoaderProxy = createObject("java", "coldfusion.runtime.java.JavaProxy").init(networkClassLoaderClass);
 56
 57		if(isObject(arguments.parentClassLoader))
 58		{
 59			classLoader = networkClassLoaderProxy.init(arguments.parentClassLoader);
 60		}
 61		else
 62		{
 63			classLoader = networkClassLoaderProxy.init();
 64		}
 65
 66		while(iterator.hasNext())
 67		{
 68			file = createObject("java", "java.io.File").init(iterator.next());
 69			if(NOT file.exists())
 70			{
 71				throw("PathNotFoundException", "The path you have specified could not be found", file.getAbsolutePath() & " does not exist");
 72			}
 73			classLoader.addUrl(file.toURL());
 74		}
 75
 76		//pass in the system loader
 77		setURLClassLoader(classLoader);
 78
 79		return this;
 80	</cfscript>
 81</cffunction>
 82
 83<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">
 84	<cfargument name="className" hint="The name of the class to create" type="string" required="Yes">
 85	<cfscript>
 86		var class = getURLClassLoader().loadClass(arguments.className);
 87
 88		return createObject("java", "coldfusion.runtime.java.JavaProxy").init(class);
 89	</cfscript>
 90</cffunction>
 91
 92<cffunction name="getURLClassLoader" hint="Returns the java.net.URLClassLoader in case you need access to it" access="public" returntype="any" output="false">
 93	<cfreturn instance.ClassLoader />
 94</cffunction>
 95
 96<cffunction name="getVersion" hint="Retrieves the version of the loader you are using" access="public" returntype="string" output="false">
 97	<cfreturn "0.4">
 98</cffunction>
 99
100<!------------------------------------------- PACKAGE ------------------------------------------->
101
102<!------------------------------------------- PRIVATE ------------------------------------------->
103
104<cffunction name="ensureNetworkClassLoaderOnServerScope"
105			hint="makes sure there is a URL class loader on the server scope that can load me up some networkClassLoader goodness"
106			access="public" returntype="void" output="false">
107	<cfscript>
108		var Class = createObject("java", "java.lang.Class");
109		var Array = createObject("java", "java.lang.reflect.Array");
110		var jars = queryJars();
111		var iterator = jars.iterator();
112		var file = 0;
113		var urls = Array.newInstance(Class.forName("java.net.URL"), ArrayLen(jars));
114		var counter = 0;
115		var urlClassLoader = 0;
116		var key = instance.static.uuid & "." & getVersion();
117		//server scope uuid
118
119		//we have it already? escape.
120		if(StructKeyExists(server, key))
121		{
122			return;
123		}
124
125		while(iterator.hasNext())
126		{
127			Array.set(urls, counter, createObject("java", "java.io.File").init(iterator.next()).toURL());
128			counter = counter + 1;
129		}
130
131		urlClassLoader = createObject("java", "java.net.URLClassLoader").init(urls);
132
133		//put it on the server scope
134		server[key] = urlClassLoader;
135	</cfscript>
136</cffunction>
137
138<cffunction name="queryJars" hint="pulls a query of all the jars in the /resources/lib folder" access="private" returntype="array" output="false">
139	<cfscript>
140		var qJars = 0;
141		//the path to my jar library
142		var path = getDirectoryFromPath(getMetaData(this).path) & "lib/";
143		var jarList = "";
144		var aJars = ArrayNew(1);
145		var libName = 0;
146	</cfscript>
147
148	<cfdirectory action="list" name="qJars" directory="#path#" filter="*.jar" sort="name desc"/>
149	<cfloop query="qJars">
150		<cfscript>
151			libName = ListGetAt(name, 1, "-");
152			//let's not use the lib's that have the same name, but a lower datestamp
153			if(NOT ListFind(jarList, libName))
154			{
155				ArrayAppend(aJars, path & "/" & name);
156				jarList = ListAppend(jarList, libName);
157			}
158		</cfscript>
159	</cfloop>
160
161	<cfreturn aJars>
162</cffunction>
163
164<cffunction name="getServerURLClassLoader" hint="returns the server URL class loader" access="private" returntype="any" output="false">
165	<cfreturn server[instance.static.uuid & "." & getVersion()] />
166</cffunction>
167
168<cffunction name="setURLClassLoader" access="private" returntype="void" output="false">
169	<cfargument name="ClassLoader" type="any" required="true">
170	<cfset instance.ClassLoader = arguments.ClassLoader />
171</cffunction>
172
173<cffunction name="throw" access="private" hint="Throws an Exception" output="false">
174	<cfargument name="type" hint="The type of exception" type="string" required="Yes">
175	<cfargument name="message" hint="The message to accompany the exception" type="string" required="Yes">
176	<cfargument name="detail" type="string" hint="The detail message for the exception" required="No" default="">
177		<cfthrow type="#arguments.type#" message="#arguments.message#" detail="#arguments.detail#">
178</cffunction>
179
180</cfcomponent>