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