forked from jamiekrug/JavaLoaderFactory
-
Notifications
You must be signed in to change notification settings - Fork 1
/
JavaLoaderFactory.cfc
121 lines (95 loc) · 5.25 KB
/
JavaLoaderFactory.cfc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<!---
Title: JavaLoaderFactory.cfc
Source: https://github.com/jamiekrug/JavaLoaderFactory
Author: Jamie Krug
http://identi.ca/jamiekrug
http://twitter.com/jamiekrug
http://jamiekrug.com/blog/
(converted to CF8-compliant tag-based structure by Rich Rein)
Purpose: Factory to provide facade to server-scoped instance of
JavaLoader (http://javaloader.riaforge.org/).
Example ColdSpring bean factory configuration:
<bean id="javaLoaderFactory" class="JavaLoaderFactory.JavaLoaderFactory" />
<bean id="javaLoader" factory-bean="javaLoaderFactory" factory-method="getJavaLoader">
<constructor-arg name="loadPaths">
<list>
<value>/opt/XOM/xom-1.2.6.jar</value>
</list>
</constructor-arg>
<constructor-arg name="loadRelativePaths">
<list>
<value>/../jars/opencsv-2.2.jar</value>
</list>
</constructor-arg>
</bean>
Example usage:
javaLoader = getBeanFactory().getBean( 'javaLoader' );
csvReader = javaLoader.create( 'au.com.bytecode.opencsv.CSVReader' );
--->
<cfcomponent displayname="JavaLoaderFactory" output="false" hint="Factory to provide facade to server-scoped instance of JavaLoader (http://javaloader.riaforge.org/)">
<cffunction name="init" access="public" output="false" returntype="any" hint="Pseudo-constructor">
<cfargument name="lockTimeout" required="false" default="60" type="numeric" hint="" />
<cfargument name="serverKey" required="false" default="" type="string" hint="" />
<cfset variables.lockTimeout = arguments.lockTimeout />
<cfset variables.serverKey = arguments.serverKey />
<cfif StructKeyExists(arguments, "serverKey") and arguments.serverKey neq "">
<cfset variables.serverKey = arguments.serverKey />
</cfif>
<cfreturn this />
</cffunction>
<cffunction name="getJavaLoader" access="public" output="false" returntype="any" hint="returns a reference to the appropriate javaloader">
<cfargument name="loadPaths" required="false" default="#ArrayNew(1)#" type="array" hint="" />
<cfargument name="loadColdFusionClassPath" required="false" default="false" type="boolean" hint="" />
<cfargument name="parentClassLoader" required="false" default="" type="string" hint="" />
<cfargument name="sourceDirectories" required="false" default="#ArrayNew(1)#" type="array" hint="" />
<cfargument name="compileDirectory" required="false" default="" type="string" hint="" />
<cfargument name="trustedSource" required="false" default="false" type="boolean" hint="" />
<cfargument name="loadRelativePaths" required="false" default="#ArrayNew(1)#" type="array" hint="" />
<cfset var javaLoaderInitArgs = buildJavaLoaderInitArgs(argumentCollection = arguments) />
<cfset var _serverKey = calculateServerKey(javaLoaderInitArgs) />
<cfif not structKeyExists(server, _serverKey)>
<cflock name="server.#_serverKey#" timeout="#variables.lockTimeout#">
<cfif not structKeyExists(server, _serverKey)>
<cfset server[ _serverKey ] = createObject('component', 'com.compoundtheory.JavaLoader').init(argumentCollection = javaLoaderInitArgs) />
</cfif>
</cflock>
</cfif>
<cfreturn server[ _serverKey ] />
</cffunction>
<cffunction name="buildJavaLoaderInitArgs" access="private" output="false" returntype="struct" hint="">
<cfargument name="loadPaths" required="false" default="#ArrayNew(1)#" type="array" hint="" />
<cfargument name="loadColdFusionClassPath" required="false" default="false" type="boolean" hint="" />
<cfargument name="parentClassLoader" required="false" default="" type="string" hint="" />
<cfargument name="sourceDirectories" required="false" default="#ArrayNew(1)#" type="array" hint="" />
<cfargument name="compileDirectory" required="false" default="" type="string" hint="" />
<cfargument name="trustedSource" required="false" default="false" type="boolean" hint="" />
<cfargument name="loadRelativePaths" required="false" default="#ArrayNew(1)#" type="array" hint="" />
<cfset var initArgs = {} />
<cfset var argName = "" />
<cfset var lstPossibleArgs = "loadPaths,loadColdFusionClassPath,parentClassLoader,sourceDirectories,compileDirectory,trustedSource" />
<cfset var relPath = "" />
<cfloop index="argName" list="#lstPossibleArgs#">
<cfif structKeyExists(arguments, argName)>
<cfset initArgs[ argName ] = arguments[ argName ] />
</cfif>
</cfloop>
<cfif structKeyExists( arguments, 'loadRelativePaths' ) && arrayLen( arguments.loadRelativePaths )>
<cfif not structKeyExists( initArgs, 'loadPaths' )>
<cfset initArgs.loadPaths = [] />
</cfif>
<cfloop index="relPath" array="#arguments.loadRelativePaths#">
<cfset arrayAppend(initArgs.loadPaths, expandPath(relPath)) />
</cfloop>
</cfif>
<cfreturn initArgs />
</cffunction>
<cffunction name="calculateServerKey" access="private" output="false" returntype="string" hint="">
<cfargument name="javaLoaderInitArgs" required="false" default="" type="struct" hint="" />
<!--- variables.serverKey takes precedence, if exists --->
<cfif structKeyExists(variables, "serverKey") and variables.serverKey neq "">
<cfreturn variables.serverKey />
</cfif>
<!--- hash init args, to generate unique key based on precise JavaLoader instance --->
<cfreturn hash(serializeJSON(arguments.javaLoaderInitArgs)) />
</cffunction>
</cfcomponent>