1
- <!--- Document Information -----------------------------------------------------
1
+ <!--- Document Information -----------------------------------------------------
2
2
3
3
Title: JavaLoader.cfc
4
4
@@ -17,6 +17,7 @@ Purpose: Utlitity class for loading Java Classes
17
17
instance .static .uuid = " A0608BEC-0AEB-B46A-0E1E1EC5F3CE7C9C" ;
18
18
< / cfscript >
19
19
20
+
20
21
<!------------------------------------------- PUBLIC ------------------------------------------->
21
22
22
23
< cffunction name = " init" hint = " Constructor" access = " public" returntype = " JavaLoader" output = " false" >
@@ -95,8 +96,122 @@ Purpose: Utlitity class for loading Java Classes
95
96
< cfreturn instance .ClassLoader / >
96
97
< / cffunction >
97
98
99
+ < cffunction name = " getClassLoadPaths" access = " public" returntype = " array" output = " false" >
100
+ < cfreturn instance .classLoadPaths / >
101
+ < / cffunction >
102
+
103
+
104
+ < cffunction name = " switchThreadContextClassLoader" hint = " Sometimes you will need to switch out the ThreadContextClassLoader with the classloader used by JavaLoader.<br/>
105
+ It has :
106
+ switchThreadContextClassLoader(function object, [struct function arguments], [classLoader=getURLClassLoader()])
107
+ switchThreadContextClassLoader(function name, [struct function arguments], [classLoader=getURLClassLoader()])
108
+ switchThreadContextClassLoader(object, function name, [struct function arguments], [classLoader=getURLClassLoader()])
109
+ This method can be used in 3 different ways:
110
+ <ol>
111
+ <li>Pass it the UDF itself</li>
112
+ <li>Pass it the current object and method name that you wish to have called</li>
113
+ <li>Inject it into your CFC/Page that you want to use, and call it from there, telling it what function to call (you will need to pass in the URLClassLoader)</li>
114
+ </ol>"
115
+ access = " public" returntype = " any" output = " false" >
116
+ < cfscript >
117
+ var local = {};
118
+ var func = 0 ; // need this as cf8 doesn't like the structure with functions.
119
+ var System = createObject (" java" , " java.lang.System" );
120
+ var Thread = createObject (" java" , " java.lang.Thread" );
121
+ var currentClassloader = Thread .currentThread ().getContextClassLoader ();
122
+ var classLoader = " " ;
123
+
124
+ if (structCount (arguments ) == 4 )
125
+ {
126
+ // the last 2 arguments are the function arguments and class loader
127
+ classLoader = arguments [4 ];
128
+ local .funcArgs = arguments [3 ];
129
+ }
130
+ else if (structCount (arguments ) == 3 )
131
+ {
132
+ // 2nd argument could be classloader or function arguments
133
+ if (isInstanceOf (arguments [2 ]," java.lang.ClassLoader" ))
134
+ {
135
+ classLoader = arguments [2 ];
136
+ }
137
+ else if (isStruct (arguments [2 ]))
138
+ {
139
+ local .funcArgs = arguments [2 ];
140
+ }
141
+
142
+ // 3rd argument could be classloader or function arguments
143
+ if (isInstanceOf (arguments [3 ]," java.lang.ClassLoader" ))
144
+ {
145
+ classLoader = arguments [3 ];
146
+ }
147
+ else if (isStruct (arguments [3 ]))
148
+ {
149
+ local .funcArgs = arguments [3 ];
150
+ }
151
+ }
152
+ else if (structCount (arguments ) == 2 )
153
+ {
154
+ // the 2nd argument could be a class loader or function arguments
155
+ if (isInstanceOf (arguments [2 ]," java.lang.ClassLoader" ))
156
+ {
157
+ classLoader = arguments [2 ];
158
+ }
159
+ else if (isStruct (arguments [2 ]))
160
+ {
161
+ local .funcArgs = arguments [2 ];
162
+ }
163
+ }
164
+
165
+ if (! structKeyExists (local ," funcArgs" ))
166
+ {
167
+ local .funcArgs = {};
168
+ }
169
+
170
+ if (isSimpleValue (classLoader ))
171
+ {
172
+ classLoader = getURLClassLoader ();
173
+ }
174
+ < / cfscript >
175
+
176
+ < cftry >
177
+ < cfscript >
178
+ Thread .currentThread ().setContextClassLoader (classloader );
179
+ < / cfscript >
180
+
181
+ < cfif isSimpleValue (arguments [1 ])>
182
+ < cfinvoke method = " #arguments [1 ] #" returnvariable = " local.return" argumentCollection = " #local .funcArgs #" / >
183
+ < cfelseif isCustomFunction (arguments [1 ])>
184
+ < cfscript >
185
+ func = arguments [1 ];
186
+ local .return = func (argumentCollection = local .funcArgs );
187
+ < / cfscript >
188
+ < cfelseif isObject (arguments [1 ]) AND isSimpleValue (arguments [2 ])>
189
+ < cfinvoke component = " #arguments [1 ] #" method = " #arguments [2 ] #" returnvariable = " local.return" argumentCollection = " #local .funcArgs #" / >
190
+ < cfelse >
191
+ < cfthrow type = " javaloader.InvalidInvocationException" message = " Unable to determine what method to invoke" detail = " Please check the documentation for switchThreadContextClassLoader." / >
192
+ < / cfif >
193
+
194
+ < cfcatch >
195
+ < cfscript >
196
+ Thread .currentThread ().setContextClassLoader (currentClassloader );
197
+ < / cfscript >
198
+ < cfrethrow >
199
+ < / cfcatch >
200
+ < / cftry >
201
+
202
+ < cfscript >
203
+ // need to do this twice, as cf8 has no finally.
204
+ Thread .currentThread ().setContextClassLoader (currentClassloader );
205
+
206
+ if (structKeyExists (local , " return" ))
207
+ {
208
+ return local .return ;
209
+ }
210
+ < / cfscript >
211
+ < / cffunction >
212
+
98
213
< cffunction name = " getVersion" hint = " Retrieves the version of the loader you are using" access = " public" returntype = " string" output = " false" >
99
- < cfreturn " 1.0 " >
214
+ < cfreturn " 1.2 " >
100
215
< / cffunction >
101
216
102
217
<!------------------------------------------- PACKAGE ------------------------------------------->
@@ -199,7 +314,7 @@ Purpose: Utlitity class for loading Java Classes
199
314
for (; counter lte len ; counter = counter + 1 )
200
315
{
201
316
dir = directories [counter ];
202
- directoryCopy (dir , path );
317
+ $ directoryCopy (dir , path );
203
318
}
204
319
205
320
// then we compile it, and grab that jar
@@ -254,14 +369,9 @@ Purpose: Utlitity class for loading Java Classes
254
369
var counter = 0 ;
255
370
< / cfscript >
256
371
257
- <!--- cf7 syntax. Yuck. --->
258
372
< cfloop from = " 1" to = " #len #" index = " counter" >
259
373
< cfset dir = directories [counter ]>
260
-
261
- < cfdirectory
262
- action = " list"
263
- directory = " #dir #"
264
- recurse = " true"
374
+ < cfdirectory action = " list" directory = " #dir #" recurse = " true"
265
375
type = " file"
266
376
sort = " dateLastModified desc"
267
377
name = " qLastModified" >
@@ -366,11 +476,11 @@ Purpose: Utlitity class for loading Java Classes
366
476
< cfdirectory action = " list" name = " qJars" directory = " #path #" filter = " *.jar" sort = " name desc" / >
367
477
< cfloop query = " qJars" >
368
478
< cfscript >
369
- libName = ListGetAt (name , 1 , " -" );
479
+ libName = ListGetAt (qJars . name , 1 , " -" );
370
480
// let's not use the lib's that have the same name, but a lower datestamp
371
481
if (NOT ListFind (jarList , libName ))
372
482
{
373
- ArrayAppend (aJars , path & " /" & name );
483
+ ArrayAppend (aJars , path & " /" & qJars . name );
374
484
jarList = ListAppend (jarList , libName );
375
485
}
376
486
< / cfscript >
@@ -379,10 +489,6 @@ Purpose: Utlitity class for loading Java Classes
379
489
< cfreturn aJars >
380
490
< / cffunction >
381
491
382
- < cffunction name = " getClassLoadPaths" access = " private" returntype = " array" output = " false" >
383
- < cfreturn instance .classLoadPaths / >
384
- < / cffunction >
385
-
386
492
< cffunction name = " setClassLoadPaths" access = " private" returntype = " void" output = " false" >
387
493
< cfargument name = " classLoadPaths" type = " array" required = " true" >
388
494
< cfset instance .classLoadPaths = arguments .classLoadPaths / >
@@ -476,7 +582,7 @@ Copies a directory.
476
582
@author Joe Rinehart ([email protected] )
477
583
@version 1, July 27, 2005
478
584
--->
479
- < cffunction name = " directoryCopy" access = " private" output = " true" >
585
+ < cffunction name = " $ directoryCopy" access = " private" output = " true" >
480
586
< cfargument name = " source" required = " true" type = " string" >
481
587
< cfargument name = " destination" required = " true" type = " string" >
482
588
< cfargument name = " nameconflict" required = " true" default = " overwrite" >
@@ -494,9 +600,9 @@ Copies a directory.
494
600
< cfif contents .type eq " file" >
495
601
< cffile action = " copy" source = " #arguments .source #/#name #" destination = " #arguments .destination #/#name #" nameconflict = " #arguments .nameConflict #" >
496
602
< cfelseif contents .type eq " dir" >
497
- < cfset directoryCopy (arguments .source & dirDelim & name , arguments .destination & dirDelim & name ) / >
603
+ < cfset $ directoryCopy (arguments .source & dirDelim & name , arguments .destination & dirDelim & name ) / >
498
604
< / cfif >
499
605
< / cfloop >
500
606
< / cffunction >
501
607
502
- < / cfcomponent >
608
+ < / cfcomponent >
0 commit comments