Changes between Initial Version and Version 1 of cbGroovyLoader

Show
Ignore:
Timestamp:
06/05/09 23:01:52 (15 months ago)
Author:
lmajano (IP: 198.102.62.250)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • cbGroovyLoader

    v1 v1  
     1= The !GroovyLoader Project = 
     2[[PageOutline()]] 
     3 
     4== Introduction == 
     5This plugin is thanks to Barney Boisevert for his cfgroovy inspiration. 
     6 
     7This plugin dynamically loads a coldbox application with the groovy runtime and 
     8other java libraries you so desire.  You can configure a classpath within your application  
     9that will act as the root of what you want to execute or you can dynamically use the groovy scripting tags. 
     10 
     11The groovy loader plugin also leverages the coldbox cache in order to store the java 
     12class files the groovyloader creates from script.  Internally, the groovy class loader 
     13also caches the parsed classes so they are not re-parsed at runtime.  The plugin includes 
     14two methods to interact with these caches: 
     15 
     16 * '''clearClazzCache()''' : Cleans the coldbox cache of loaded groovy scripts 
     17 * '''clearClassLoaderCache()''' :  Cleans the actual classloader's cache, use sparingly 
     18 
     19Why would I want to clear the class loader cache?  You want to do this, whenever 
     20you make changes to the groovy files on disk.  However, be warned that classloading 
     21is a very tempestous beast and it can lead to memory leaks or JVM permGen errors 
     22as class definitions do not get garbage collected or disposed of.  However, this 
     23side effect is only visualized on development. 
     24 
     25== Release Notes == 
     26 
     27=== VERSION 2.0 === 
     28 * Added ability to load more than 1 location for groovy scripts 
     29 * Added ability to load more than 1 location for java libraries alongside groovy language 
     30 * Added the !GroovyStarter interceptor for easy loading of the runtime, fully configurable 
     31 
     32=== VERSION 1.0 === 
     33 * Initial groovy integration 
     34 
     35== Installation == 
     36 
     37=== Requirements: === 
     38 * !ColdFusion 8 and above 
     39 * Railo 3.0 and above 
     40 
     41=== Install: === 
     42 
     43Copy the plugin folder: !GroovyLoader to your custom plugins or bring them in 
     44via the !MyPluginsLocation or !PluginsExternalLocation setting. 
     45         
     46Then either configure the class path and plugin like in the sample application: 
     47 
     48{{{      
     49<cfset getMyPlugin("GroovyLoader.GroovyLoader").configureClassPath(getSetting("ApplicationPath") & "model/groovy")> 
     50}}} 
     51 
     52Or just use the !GroovyStarter interceptor, way easy: 
     53 
     54{{{ 
     55#!xml    
     56<!-- Groovy Starter: Creates & configures the GroovyLoader --> 
     57<Interceptor class="${AppMapping}.plugins.GroovyLoader.GroovyStarter"> 
     58        <!-- Paths that hold groovy libs --> 
     59        <Property name="groovyLibPaths">/${AppMapping}/model/groovy,/${AppMapping}/model/anotherPath</Property> 
     60        <!-- Paths that hold jar's for us to load automagically --> 
     61        <Property name="javalibPaths">/${AppMapping}/model/lib</Property> 
     62</Interceptor> 
     63}}} 
     64 
     65=== Interceptor Properties === 
     66 
     67|| '''groovyLibPaths''' || A comma-delimitted list of relative/absolute path locations for groovy scripts or classes || 
     68|| '''javaLibPaths''' || A comma-delimitted list of relative/absolute path locations for the java loader to load java libraries alongside the core groovy language || 
     69|| '''pluginClassPath''' || The location of the groovy loader plugin. By Default: !GroovyLoader.!GroovyLoader || 
     70 
     71 
     72== Class Path Usage == 
     73 
     74If you will be using a groovy folder for entities or for script executions, then you will have to configure  
     75the plugin's groovyLibPaths to point to the locations where your groovy scripts/classes are located. The 
     76Groovy Loader will then let you create scripts and groovy classes at runtime by just using their instantiation name from the root directory downwards.  The groovy loader plugin will search in all configured class paths for the scripts/classes, once it locates it it will create it.  This way, you can have more than 1 location for the scripts. 
     77 
     78Ex: 
     79{{{ 
     80/model 
     81 /groovy (is your in your groovyLibPaths) 
     82   - Hello.groovy 
     83   /util 
     84    - DateCompare.groovy 
     85}}} 
     86   
     87To create these classes you would use their dot notation: 
     88 
     89{{{ 
     90#!java 
     91create("Hello") 
     92create("util.DateCompare") 
     93}}} 
     94 
     95 
     96== Loading Groovy Classes == 
     97 
     98You can load any class via the ''create(clazz)'' method in the plugin.  You can use dot notation to refer 
     99to packages, starting from the root of your class path as you defined in the section above. 
     100 
     101{{{ 
     102#!java 
     103rc.oHello = getMyPlugin("GroovyLoader.GroovyLoader").create("Hello"); 
     104}}} 
     105 
     106 
     107== Executing Groovy File Scripts == 
     108 
     109You can also execute groovy as scripts by using the ''runScript()'' method and passing the name of the script 
     110using the dot-notation path as described above. 
     111 
     112{{{ 
     113#!java 
     114getMyPlugin("GroovyLoader.GroovyLoader").runScript("This is my script here..."); 
     115}}} 
     116 
     117However, you can pass an optional argument called: '''varCollection'''. This is a structure that the groovy  
     118script will be binded with at runtime, so you can use it from within your script and basically create a data 
     119bridge between your app and groovy. 
     120 
     121By default, the groovy script will be binded with the following variables/objects: 
     122 
     123|| '''groovy variable name''' || '''description''' || 
     124|| varCollection || the varCollection argument. ||  
     125|| cf_pageContext || the j2ee page context object ||  
     126|| cf_application || the application scope ||  
     127|| cf_session || if defined, the session scope ||  
     128|| cf_server || the server scope ||  
     129|| coldbox_rc || the coldbox request collection ||  
     130 
     131Example: 
     132{{{ 
     133#!java 
     134var varCollection = {name="Luis Majano",age="31"}; 
     135getMyPlugin("GroovyLoader.GroovyLoader").runScript("Test",varCollection); 
     136}}} 
     137 
     138 
     139== Executing Groovy Source == 
     140 
     141You can execute dynamic groovy source by using two methods below: 
     142 
     143 1. executing the ''runSource()'' method 
     144 1. using the custom tag: ''groovy:script'' 
     145 
     146The plugin will also bind the dynamically executed code with the same bindings as described above.  The plugin 
     147will also create a unique hash according to your source in order to keep track of your compiled sources.  The  
     148compiled sources will be stored in the coldbox cache for easy retrieval and usage.  Once the script changes, a 
     149new entry is created in the cache and the groovy class loader will also cache the java representation. 
     150Again, be careful with permGen problems as object definitions keep increasing in development environments. 
     151 
     152{{{ 
     153#!html 
     154<div class="mynotes"> 
     155<Strong>Important Note:</strong> If you get permgen errors, just restart the CF server and you are done. 
     156</div> 
     157}}} 
     158 
     159To execute on the fly source, just call the '''runSource()''' method on the plugin.  You can also pass in the  
     160optional '''varCollection ''' argument. 
     161 
     162{{{ 
     163#!java 
     164//Declare the source in CF 
     165var source = "varCollection.GroovyArray = [1,2,3,4]";    
     166//Execute the source 
     167getMyPlugin("GroovyLoader.GroovyLoader").runSource(source); 
     168}}} 
     169 
     170You can also use the tag import to execute runtime groovy.  All you need to do is add the following cfimport 
     171 
     172{{{ 
     173#!xml 
     174<!--- Import GScript ---> 
     175<cfimport prefix="groovy" taglib="../plugins/GroovyLoader/tags" /> 
     176}}} 
     177 
     178Then you can script using the following tag '''groovy:script'''  
     179 
     180{{{ 
     181#!xml 
     182<groovy:script> 
     183<cfoutput> 
     184        def today = coldbox_rc["today"] 
     185        def SubjectLine = ["Hello","my","name","is","Luis Majano.","Expressed at",today] 
     186        //Place in ColdBox event context 
     187        coldbox_rc["SubjectLine"] = SubjectLine.join("_") 
     188</cfoutput> 
     189</groovy:script> 
     190}}} 
     191 
     192You can also add the varCollection optional attribute to the script tag 
     193  
     194{{{ 
     195#!xml 
     196<groovy:script varCollection="#MyStruct#"> 
     197</groovy:script> 
     198}}} 
     199 
     200== Conclusion == 
     201You can now see the power of both Java and !ColdFusion.  !ColdBox's !GroovyLoader really makes it easy 
     202to integrate groovy in any !ColdBox application.  So now you can get Groovy with !ColdBox!