As Barney Boisvert has demonstrated with his CFGroovy custom tags, scripting in Java is surprisingly easy. As he explains in the CFGroovy project, any scripting language that is JSR-223 compliant can be executed inside of a Java-based context with mostly-seamless communication between the Java context and the scripting context. The CFGroovy project was designed originally (I think) to work with Groovy; but, it can now be used run code through any JSR-223 compliant script engine implementation in your Java classpaths.
Because the set of available script engine implementations depends on your JRE, we need to ask Java to see what's available. We can get this information from the Javax script engine manager:
<!--- Get the script manager class. ---> <cfset scriptManager = createObject( "java", "javax.script.ScriptEngineManager" ) /> <!--- Get the factories that are natively available. ---> <cfset scriptFactories = scriptManager.getEngineFactories() /> <!--- Loop over the factories and output the names that each can build. ---> <cfloop index="scriptFactory" array="#scriptFactories#"> <!--- Output factory name and version. ---> <cfoutput> [ #scriptFactory.getEngineName()# - #scriptFactory.getEngineVersion()# ] <br /> <!--- Output the names that this factory will respond to (the names for which it can create valid script engines). ---> <cfloop index="alias" array="#scriptFactory.getNames()#"> - #alias#<br /> </cfloop> </cfoutput> </cfloop>
Here, we are asking the script engine manager for its factories, and then, from each factory, we ask it for the list of "names" for which it can create script engines. When we run the above code (on my personal JRE), we get the following output:
[ Mozilla Rhino - 1.6 release 2 ]
When we run the above code, we get the following output:
Message: Katie is hilarious!
Want to use code from this post? Check out the license.