alanwilliamson

Writing CFC's using pure Java

A few weeks ago we released an early access candidate for embedding pure Java into the popular CFSCRIPT block. Feedback has been good and I am pleased to release some new updates.

Writing CFC with Java

The biggest addition has been permitting the declaration of complete Java methods inside CFSCRIPT and having them available to the outside world. So if your snippet requires some inner-methods, you can now easily do this.

However, the big addition, has been the integration with CFML Components (CFC). You can now write CFC functions in either tags, cfml-script or now pure Java. Any public methods you create in Java will automatically be exported as CFC functions that can then be called by any part of the CFML eco-system. Pretty darn cool no?

Let's look at a quick example of a CFC written with a mixture of tags and Java.

<cfcomponent>

  <cffunction name="fromCfml">
    <cfreturn now()>
  </cffunction>

  <cfscript language="java">
  long start = System.currentTimeMillis();
 
  public long getStart(){
    return start;
  }

  public Date fromJava(){
    return new Date();
  }

  public String echo(String al){
    return al;
  }
  </cfscript>

</cfcomponent>

You can now create and call this CFC in the usual ways, and any public Java methods you created will be exported. There is nothing stopping you from creating complete CFC functions in Java.

Calling Built-in functions from Java

Instead of having to rewrite everything in the Java world, you can leverage the rich CFML functions inside your Java code, by calling them from the cf.call("function" [,param list] ). An example of this in action to call the ListGetAt() function.

<cfscript language="java">
  // Calling out to a cfml function
  String a = (String)cf.call( "listgetat", "a,b,c,d", 3 );
  cf.print( a );
</cfscript>

Debugging

In addition, I've also rewritten the error console when you are developing the Java blocks. Here is an example output when something has gone wrong with your Java code. Remember Java code is not as forgiving as CFML code, so it is much more strict on what you can and can't do. But as you can see, any problems are quickly pinpointed so you can get a handle on it to address quickly.

We've just got started

This whole area has got me very excited for a number of reasons. The main driving force for this has been our real need at aw2.0 to integrate OpenBD closly with MongoDB without having to write a specific set of new CFML functions.

We are also working with Gert and the Railo team to come up with a semi-base-specification for this feature that both engines will support (If any other engines wish to implement the specification they are of course welcome to). It has been good so far, with lots of good debate and discussion. This is how things should be done -- no politics, no back stabbing, just trying to move the CFML ball as far forward as possible.

Try it out now, head over to the OpenBD nightly build and give it a whirl.

Documentation cfscript language="java"


 

Recent Cloud posts

Recent JAVA posts

Latest CFML posts


 
Site Links