Skip to content

Commit

Permalink
Fix so that it works on CF7. Works on CF7 & CF8
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.riaforge.org/javaloader/trunk/javaloader@84 75c0ff33-bb23-0410-b72d-88b268fa1e2d
  • Loading branch information
markmandel committed Jan 18, 2010
1 parent b230dee commit dd3e286
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 119 deletions.
31 changes: 16 additions & 15 deletions example/spring/index.cfm
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,47 @@
</p>
<p>
We will create a Java Object called <a href="src/com/MessageReceiver.java">MessageReciever</a>, which will take a CFC named 'Message',
which has a method called 'getMessage()', which the Java object will call.
which has a method called 'getMessage()', which the Java object will call.
</p>

<cfscript>
libpaths = [];
//MUST have Spring in our classpath
ArrayAppend(libpaths, expandPath("./lib/spring.jar"));
//MUST have the cglib library for run time proxying in Spring
ArrayAppend(libpaths, expandPath("./lib/cglib-nodep-2.1_3.jar"));
//MUST have the JavaLoader's ColdFusion Dynamic Proxy
//MUST have the JavaLoader's ColdFusion Dynamic Proxy
ArrayAppend(libpaths, expandPath("/javaloader/support/cfcdynamicproxy/lib/cfcdynamicproxy.jar"));
//MUST include the JavaLoader's Spring Integration library
ArrayAppend(libpaths, expandPath("/javaloader/support/spring/lib/spring-coldfusion.jar"));
//this is the directory that stores all our Java source
srcpaths = [ expandPath("./src") ];
//We have to load the ColdFusion classpath as the ColdFusion dynamic proxy requires it.
//We have to load the ColdFusion classpath as the ColdFusion dynamic proxy requires it.
loader = createObject("component", "javaloader.JavaLoader").init(loadPaths=libpaths, loadColdFusionClassPath=true, sourceDirectories=srcpaths);
//this is the path to our XML file. Note the 'file://' prefix, this is important to Spring
path = "file://" & expandPath("./spring.xml");
//Some windows users need to use this: path = "file:/" & expandPath("./spring.xml");
//The FileSystemXMLApplicationContext I find is the easiest way to load up Spring
spring = loader.create("org.springframework.context.support.FileSystemXmlApplicationContext").init();
/*
we HAVE to set the classloader from JavaLoader as the Spring ClassLoader, so Spring knows where
to create Java Object from.
*/
spring.setClassLoader(loader.getURLClassLoader());
spring.setConfigLocation(path);
spring.refresh();
//finally, after all that, let's grab our MessageReciever Java Object!
messageReceiver = spring.getBean("messageReceiver");
messageReceiver = spring.getBean("messageReceiver");
</cfscript>
<p>
Spring Says:
Expand Down
149 changes: 87 additions & 62 deletions javaloader/JavaLoader.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Mark Mandel 22/06/2006 Added verification that the path exists
instance.static.uuid = "A0608BEC-0AEB-B46A-0E1E1EC5F3CE7C9C";
</cfscript>

<cfimport taglib="tags" prefix="jl">

<!------------------------------------------- PUBLIC ------------------------------------------->

<cffunction name="init" hint="Constructor" access="public" returntype="JavaLoader" output="false">
Expand All @@ -38,47 +40,47 @@ Mark Mandel 22/06/2006 Added verification that the path exists

<cfscript>
initUseJavaProxyCFC();

if(arguments.loadColdFusionClassPath)
{
//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.
}
}

setClassLoadPaths(arguments.loadPaths);
setParentClassLoader(arguments.parentClassLoader);

ensureNetworkClassLoaderOnServerScope();

loadClasses();

if(structKeyExists(arguments, "sourceDirectories") AND ArrayLen(arguments.sourceDirectories))
{
setJavaCompiler(createObject("component", "JavaCompiler").init(arguments.compileDirectory));
setSourceDirectories(arguments.sourceDirectories);
setCompileDirectory(arguments.compileDirectory);

compileSource();

setSourceLastModified(calculateSourceLastModified());

//do the method switching for non-trusted source
if(NOT arguments.trustedSource)
{
variables.createWithoutCheck = variables.create;

StructDelete(this, "create");
StructDelete(variables, "create");

this.create = variables.createWithSourceCheck;
}
}

return this;
</cfscript>
</cffunction>
Expand All @@ -103,7 +105,7 @@ Mark Mandel 22/06/2006 Added verification that the path exists
</cffunction>

<cffunction name="getVersion" hint="Retrieves the version of the loader you are using" access="public" returntype="string" output="false">
<cfreturn "1.0.alpha.1">
<cfreturn "1.0.b2">
</cffunction>

<!------------------------------------------- PACKAGE ------------------------------------------->
Expand All @@ -114,7 +116,7 @@ Mark Mandel 22/06/2006 Added verification that the path exists
<cfargument name="className" hint="The name of the class to create" type="string" required="Yes">
<cfscript>
var dateLastModified = calculateSourceLastModified();

/*
If the source has changed in any way, recompile and load
*/
Expand All @@ -123,10 +125,10 @@ Mark Mandel 22/06/2006 Added verification that the path exists
loadClasses();
compileSource();
}

//if all the comilation goes according to plan, set the date last modified
setSourceLastModified(dateLastModified);

return createWithoutCheck(argumentCollection=arguments);
</cfscript>
</cffunction>
Expand All @@ -138,7 +140,7 @@ Mark Mandel 22/06/2006 Added verification that the path exists
var classLoader = 0;
var networkClassLoaderClass = 0;
var networkClassLoaderProxy = 0;

networkClassLoaderClass = getServerURLClassLoader().loadClass("com.compoundtheory.classloader.NetworkClassLoader");

networkClassLoaderProxy = createJavaProxy(networkClassLoaderClass);
Expand All @@ -162,7 +164,7 @@ Mark Mandel 22/06/2006 Added verification that the path exists

classLoader.addUrl(file.toURL());
}

setURLClassLoader(classLoader);
</cfscript>
</cffunction>
Expand All @@ -171,42 +173,54 @@ Mark Mandel 22/06/2006 Added verification that the path exists
<cfscript>
var dir = 0;
var path = getCompileDirectory() & "/" & createUUID();

var paths = 0;
var jar = 0;
var file = 0;
var counter = 1;
var len = 0;
var directories = 0;
</cfscript>
<cftry>
<cfdirectory action="create" directory="#path#">

<!--- first we copy the source to our tmp dir --->
<cfloop array="#getSourceDirectories()#" index="dir">
<cfset directoryCopy(dir, path)>
</cfloop>

<!--- then we compile it, and grab that jar --->
<cfset paths = [path]>
<cfset jar = getJavaCompiler().compile(paths, getURLClassLoader())>

<cfscript>
//first we copy the source to our tmp dir
directories = getSourceDirectories();
len = arraylen(directories);
for(; counter lte len; counter = counter + 1)
{
dir = directories[counter];
directoryCopy(dir, path);
}

//then we compile it, and grab that jar

paths = ArrayNew(1); //have to write it this way so CF7 compiles
ArrayAppend(paths, path);

jar = getJavaCompiler().compile(paths, getURLClassLoader());
</cfscript>

<!--- add that jar to the classloader --->
<cfset file = createObject("java", "java.io.File").init(jar)>
<cfset getURLClassLoader().addURL(file.toURL())>

<!--- delete the files --->
<cfif directoryExists(path)>
<cfdirectory action="delete" recurse="true" directory="#path#">
</cfif>

<cfif fileExists(jar)>
<cffile action="delete" file="#jar#" />
</cfif>

<cfcatch>
<!--- make sure the files are deleted --->
<cfif directoryExists(path)>
<cfdirectory action="delete" recurse="true" directory="#path#">
</cfif>
</cfif>

<cfrethrow>
</cfcatch>
</cftry>
Expand All @@ -216,38 +230,47 @@ Mark Mandel 22/06/2006 Added verification that the path exists
<cfscript>
var lastModified = createDate(1900, 1, 1);
var dir = 0;
var qLastModified = 0;
var qLastModified = 0;
var directories = getSourceDirectories();
var len = arraylen(directories);
var counter = 0;
</cfscript>

<cfloop array="#getSourceDirectories()#" index="dir">
<cfdirectory action="list" directory="#dir#" recurse="true"
type="file"
sort="dateLastModified desc"

<!--- cf7 syntax. Yuck. --->
<cfloop from="1" to="#len#" index="counter">
<cfset dir = directories[counter]>
<jl:directory action="list" directory="#dir#" recurse="true"
type="file"
sort="dateLastModified desc"
name="qLastModified">
<cfscript>
//it's possible there are no source files.
if(qLastModified.recordCount)
{
//get the latest date modified
if(dateCompare(lastModified, qlastModified.dateLastModified) eq -1)
if(dateCompare(lastModified, qlastModified.dateLastModified) eq -1)
{
lastModified = qLastModified.dateLastModified;
}
/*
This is here, because cfdirectory only ever gives you minute accurate modified
date, which is not good enough.
*/
lastModified = createObject("java", "java.util.Date").init(createObject("java", "java.io.File").init(qLastModified.directory & "/" & qLastModified.name).lastModified());
}
}
else
{
lastModified = Now();
}
</cfscript>
</cfloop>

<cfreturn lastModified />
</cffunction>

<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">
<cfscript>
access="private" returntype="void" output="false">
<cfscript>
var Class = createObject("java", "java.lang.Class");
var Array = createObject("java", "java.lang.reflect.Array");
var jars = queryJars();
Expand All @@ -257,25 +280,27 @@ Mark Mandel 22/06/2006 Added verification that the path exists
var counter = 0;
var urlClassLoader = 0;
var key = instance.static.uuid & "." & getVersion();
//server scope uuid

//we have it already? escape.
if(StructKeyExists(server, key))
{
return;
}
</cfscript>

while(iterator.hasNext())
{
Array.set(urls, counter, createObject("java", "java.io.File").init(iterator.next()).toURL());
counter = counter + 1;
}
<cfif NOT StructKeyExists(server, key)>
<cflock name="javaloader.networkclassloader" throwontimeout="true" timeout="60">
<cfscript>
if(NOT StructKeyExists(server, key))
{
while(iterator.hasNext())
{
Array.set(urls, counter, createObject("java", "java.io.File").init(iterator.next()).toURL());
counter = counter + 1;
}

urlClassLoader = createObject("java", "java.net.URLClassLoader").init(urls);
urlClassLoader = createObject("java", "java.net.URLClassLoader").init(urls);

//put it on the server scope
server[key] = urlClassLoader;
</cfscript>
//put it on the server scope
server[key] = urlClassLoader;
}
</cfscript>
</cflock>
</cfif>
</cffunction>

<cffunction name="createJavaProxy" hint="create a javaproxy, dependent on CF server settings" access="private" returntype="any" output="false">
Expand Down Expand Up @@ -427,13 +452,13 @@ Copies a directory.

<cfset var contents = "" />
<cfset var dirDelim = createObject("java", "java.lang.System").getProperty("file.separator")>

<cfif not(directoryExists(arguments.destination))>
<cfdirectory action="create" directory="#arguments.destination#">
</cfif>

<cfdirectory action="list" directory="#arguments.source#" name="contents">

<cfloop query="contents">
<cfif contents.type eq "file">
<cffile action="copy" source="#arguments.source#/#name#" destination="#arguments.destination#/#name#" nameconflict="#arguments.nameConflict#">
Expand Down
Binary file removed javaloader/lib/classloader-20090826111308.jar
Binary file not shown.
Binary file added javaloader/lib/classloader-20100118113328.jar
Binary file not shown.
Binary file modified javaloader/lib/classloader-src.zip
Binary file not shown.
4 changes: 2 additions & 2 deletions javaloader/readme.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
JavaLoader v1.0 Alpha
JavaLoader v1.0
Author: Mark Mandel
Date: 31 August 2009
Date: 22 December 2009

Documentation can now be found at:
http://www.compoundtheory.com/javaloader/docs/
Loading

0 comments on commit dd3e286

Please sign in to comment.