alanwilliamson

OpenBD CFML with inline pure Java with cfscript

This week we are gearing up for our usual 6 monthly release of OpenBlueDragon with 1.4. As we do this, I have gone and slipped in a new feature that has some of us on the Steering Committee jumping up and down with excitement. This feature has been quietly baking away, as a plugin, for a number of months now. It's ready.

We all know the power, beauty and simplicity of CFML. With its subtle mixture of tags and inline language syntax with CFSCRIPT, CFML has much to offer. However for all its strengths, one of the core blunders the original architects made was bastardizing CFSCRIPT. It is neither Javascript, ActionScript or Java. It's a sort of halfway house between them all with quirks left, right and center.

Today though, we tighten some of this area up.

cfscript lang="java"

We've extended the core language support, by adding the lang attribute to the CFSCRIPT tag. This lets us hook in other language implementations that can be embedded within any CFML page or CFC.

Now you can drop pure-raw Java code inside CFSCRIPT and the OpenBD engine will automatically compile and optimize it on-the-fly and execute it when the request gets there. Let's take a look at this power.

<cfset s = ["A","B","C","D","E","F"]>

<cfscript lang="java">
  // Retrieve the CFML object
  cfArray d = cf.getArray("s");

  for ( int x=0; x < d.size(); x++ ){
    cf.print( d.get(x) );
  }

  cf.print( cf.get("CGI.SCRIPT_NAME") );

  // Creating an array in Java, and then setting it for the CFML
  List fromJava	= Arrays.asList(new String[] { "aa", "bb", "cc", "dd" });
  cf.set( "abc", fromJava );
</cfscript>

<cfdump var="#abc#">

As you can see here, we have a pure Java code inside the CFSCRIPT tags. We interact with the existing CFML context, using the Java object cf that is available when the tag code is executing.

Through this context object, we can get/set CFML objects, while maintaining a high-level of performance and low over head of marshaling data in and out only when absolutely required.

We provide standard facade objects for CFML arrays (cfArray), structures (cfStruct) and queries (cfQuery) that lets you interact with CFML data from inside Java at minimal cost. Any operations on these objects, effect the underlying CFML object they represent.

Extend beyond CFML

It was/is possible to hook into Java from CFML using the CreateObject() function. However this introduces a lot of extra overhead and gets somewhat messy when converting data types in-and-out of the CFML/Java world as attributes and return values travel through out the execution tree.

By offering pure Java as a single block, you now gain all the optimizations garnered from the underlying Java HotSpot compiler and do not suffer any of the data marshal overhead. An example of this would be any heavy string manipulation you may be doing, that say using a java.lang.StringBuilder could benefit from.

Another area is of course is being able to easily use the full Java API without having to worry about all the logistics of compiling and deployment. Simply write your Java and hit the browsers "REFRESH" button! I mean how easier can we make Java development?

There is much innovation that will go into this area as we expand this out. But already we have replaced some of our heavy CFSCRIPT blocks with the cleaner and easier to read Java versions and we are seeing a huge gain.

Ready now

Regularly followers will know I have been working on expanding this world for nearly a year now. My first swing at the ball, was the plugin CFJS that offers server-side Javascript. We learned a lot going through that process and will be adding lang="javascript" very soon to round out the language choice.

However, you don't have to wait for the lang="java" feature. This is enabled in the 1.4 branch of OpenBlueDragon which is available for download today as the nightly build.

You can find out more information on this new addition to the OpenBD family by heading over http://www.openbluedragon.org/manual/?/cfscript_java

Let us know how you get on.


 

Recent Cloud posts

Recent JAVA posts

Latest CFML posts


 
Site Links