SYS-CON MEDIA Authors: Roberto Medrano, Dmitriy Stepanov, Gilad Parann-Nissany, Srinivasan Sundara Rajan, Sean Houghton

Related Topics: ColdFusion

ColdFusion: Article

A Cold Cup o' Joe- Java CFX Basics

A Cold Cup o' Joe- Java CFX Basics

Prior to ColdFusion 4.5 a developer could only write CFX tags in a binary language that supported the dynamic linking capabilities of the target platform.

For Windows it was C/C++ and Delphi and the like, any language that could create a DLL. For the UNIX platforms, it was pretty much the same thing - only with shared objects. Naturally anytime a CFX was distributed it needed to be compiled for the different platforms or the source code was required - which is assuming, of course, that the CFX was even designed to be ported!

Now that CFX tags can be written in Java it is a write once and run anywhere strategy, which is already one of the major pillars of the Java philosophy. This makes things simple: unless native calls are made, the CFX is guaranteed to run on both platforms without change.

Keeping It Simple
The folks at Allaire did us (the ColdFusion developer community) a wonderful service by making the Java CFAPI fairly simple. In fact, creating a basic custom tag is no more difficult than implementing a Java interface.

public interface CustomTag
{
public void processRequest(Request request, Response response)
throws Exception;
}

The CustomTag interface in the CFAPI has only one method to implement: processRequest. This method receives two arguments, a Request object and a Response object, both from the CFAPI. It is through these two objects that the Java CFX receives information and provides output.

The CFAPI Model
All of the classes and interfaces of the CFAPI are shown in the UML model (see Figure 1).

There are a number of other classes and interfaces in the CFAPI library, but most of them are not designed for direct interaction with the developer. These are the native bindings and wrappers that hook into the Cold-Fusion Server. Of all of the classes in the CFAPI, there are only four you are likely to use directly: CustomTag, Request, Response, and Query.

CustomTag
This interface is the basis for all the custom tags derived from the CFAPI. Every tag must implement this interface in order for the ColdFusion Server to be able to hook it. There is only one method signature that needs to be implemented: processRequest (see Table 1). This method receives instances of two of the other classes: Request and Response.

Request
This interface provides a way for the custom tag to receive information from the system or the calling logic (see Table 2).

Response
This interface provides a way for the custom tag to return information to the calling logic (see Table 3).

Query
This interface provides a way for the developer to manipulate a recordset (see Table 4).

Hello, World
I fear that I must exercise that tired example again; Listing 1 is a simple "Hello, World" Java CFX. As can be seen on Line 14, tag attributes can be retrieved through the Request object by means of the getAttribute method. As seen in Line 19, output can be returned to the ColdFusion Server by using the write method of the Response object.

Registering a Java CFX
As with their binary cousins, Java CFXs need to be registered in the ColdFusion Administrator. The process is fairly simple and - like almost all things in ColdFusion - does not require drilling through many screens.

The CFX Tags panel can be accessed from the ColdFusion Administrator. Starting from the left-hand navigation, under the Extensions menu is the hyperlink "CFX Tags". All the registered CFXs (binary and Java) are shown on the main CFX registration screen shown in Figure 2.

Enter a name for the CFX, set the type to be Java, and press the "Add" button. This will direct you to the second screen in the registration process, shown in Figure 3.

This screen is comprised of three entry fields: Tag Name, Class Name, and Description. The Tag Name field is the name by which the CFX will be referenced from within the template. In the case of the "Hello, World" example, created in Listing 1, I entered "Hello". So whenever the interpreter encounters a "cfx_hello" it will know to load the class file for this tag.

The Class Name entry specifies the class implementing the CFX's functionality. This class must exist somewhere in the CLASSPATH setting of the ColdFusion Server (configurable from the Java Setting panel, see Part 1 of this series in CFDJ, Vol. 3, issue 1).

When necessary, be certain to use the full class name with package path. Typically I put my CFXs (Java or otherwise) in the CFX subdirectory of the ColdFusion Server installation directory. This runs a little contrary to certain recommendations made in the ColdFusion documentation, which I will discuss later. The Description field is optional and can be used to describe the functionality of the library registered.

Testing the CFX
It is important to test the newly registered CFX since the Cold-Fusion Server does not verify any of the registration information until the CFX is first called. In the case of our "Hello, World" CFX this is rather simple. In Listing 2 I have created a simple template that called the "cfx_hello" tag in the body of an otherwise empty HTML document.

Hello, Query
Now that you have a grasp of the basics, let's step it up a bit. As a rule, CFXs cannot pass or return complex data types (the sole exception to this is the CFML query type). More specifically, the ColdFusion Server will allow for one CFML query to be passed into the CFX and one to be passed back. You can pass a CFML query in by specifying the name of the query in an attribute to the CFX named Query.

A reimplementation of the "Hello, World" CFX, shown in Listing 3, demonstrates how to work with an incoming CFML query. The template, shown in Listing 4, will query a database to retrieve a list of names, which it will pass to the CFX through the Query attribute.

The CFX is not that much of a departure from the "Hello, World" code in Listing 1. However, instead of receiving a single name in the attribute Name, the incoming query is looped over and the Name column is concatenated.

Reloading Problems
During development, code often gets changed rapidly and must be tested just as rapidly - and CFX tags are no exception. However, trying to rerun a Java CFX is not as simple as rerunning a newly compiled application. Once the CFX has been loaded by the JVM, it stays cached and the ColdFusion Server must be restarted to flush it from the JVM. The DebuggingFactory I created for Part 4 of this series (CFDJ, Vol.3, issue 6) is of little use here since the ColdFusion Server is invoking the CFX class and cannot be made to use that class.

There is actually a way to hijack Allaire's CFX class loader, but it's a sticky operation at best. The ColdFusion 4.5 documentation (Developing Web Applications with ColdFusion, Chapter 18: "Building Custom CFAPI Tags") states that if CFXs are deployed in the same configured directory as the cfx.jar file, the ColdFusion Server will check the timestamp on the class file and reload it if there have been changes. This can be further ad-justed by passing an additional attribute to the CFX called Reload. For CFXs stored in the configured directory, the Reload attribute indicates to the ColdFusion Server whether the timestamp should be checked and what the reload rule should be (see Table 5).

This same documentation recommends that all Java CFX tags should be both debugged and deployed from this location. I would recommend against deploying production class files from the location for two reasons:

1. There is an additional performance hit to check the CFX class file each time for modifications.
2. It is a potential security risk to have a deployed production environment module reload without an explicit administrator request.

This situation gets a little better in ColdFusion 5.0, but the points I've made still remain.

CFX Limitations
For all their power (all CFXs - binary and Java alike), there are inherent limitations in the CFX model. These limitations have existed since the introduction of the CFX architecture and will continue to persist through version 5.0 (the Windows version being just newly available at the time of the writing of this article).

Singular Tags
CFX tags can only be singular tags. This means there's no end tag, and thus no body or subordinate tags. This effectively makes CFX tags analogous to simply executing external programs.

Limited Relationship to Calling Logic
Undoubtedly CFX tags enhance the available power of the ColdFusion Server, but their implementation model hobbles them some. There is little connection with the calling logic of the CFX. There is no way for the CFX to find out information about its parent tag, it cannot trace the tag stack or access the Caller scope as other CFML-based tags do.

Complex Data Types
With the sole exception of a single incoming and outgoing query, no complex data types may be passed in or out of the CFX tag. This is a severe limitation that has not been corrected even in version 5.0 (there is a work around that I will discuss later in this series).

Difficulty in Debugging
Since the ColdFusion Server does not have specific hooks in it for stepping specific binary code or Java bytecode into either platform, debugging CFX tags can be difficult. Allaire has provided a number of classes to assist with this, but it isn't a silver bullet to the problem.

Wrapping It Up
Given the simplicity of writing CFXs in Java and the extensive libraries available, it makes sense to write tags in this manner. They are often more robust in functionality and frequently faster in execution than tags written in CFML. Naturally tags written in C/C++ will have better performance, but in many cases the loss of platform independence and thus the need to maintain two different code bases outweighs that gain.

In Part 6, I'll introduce you to some of the more advanced features of the CFX model, the recommended Allaire debugging techniques, and a couple of the benefits of using the JVM.

More Stories By Guy Rish

Guy Rish is a ColdFusion and .NET developer at Vente as well as President at Gestaltech. He is an active developer and writer for various languages and technologies, and has contributed work in books on ColdFusion MX, Flash MX, and Dreamweaver MX. He blogs at ccoj.coldfusionjournal.com.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Latest Stories
DevOps Summit 2015 New York, co-located with the 16th International Cloud Expo - to be held June 9-11, 2015, at the Javits Center in New York City, NY - announces that it is now accepting Keynote Proposals. The widespread success of cloud computing is driving the DevOps revolution in enterprise IT. Now as never before, development teams must communicate and collaborate in a dynamic, 24/7/365 environment. There is no time to wait for long development cycles that produce software that is obsolete...
WebRTC defines no default signaling protocol, causing fragmentation between WebRTC silos. SIP and XMPP provide possibilities, but come with considerable complexity and are not designed for use in a web environment. In his session at @ThingsExpo, Matthew Hodgson, technical co-founder of the Matrix.org, discussed how Matrix is a new non-profit Open Source Project that defines both a new HTTP-based standard for VoIP & IM signaling and provides reference implementations.
The 3rd International Internet of @ThingsExpo, co-located with the 16th International Cloud Expo - to be held June 9-11, 2015, at the Javits Center in New York City, NY - announces that its Call for Papers is now open. The Internet of Things (IoT) is the biggest idea since the creation of the Worldwide Web more than 20 years ago.
"There is a natural synchronization between the business models, the IoT is there to support ,” explained Brendan O'Brien, Co-founder and Chief Architect of Aria Systems, in this SYS-CON.tv interview at the 15th International Cloud Expo®, held Nov 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA.
SYS-CON Events announced today Isomorphic Software, the global leader in high-end, web-based business applications, will exhibit at SYS-CON's DevOps Summit 2015 New York, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Isomorphic Software is the global leader in high-end, web-based business applications. We develop, market, and support the SmartClient & Smart GWT HTML5/Ajax platform, combining the productivity and performance of traditional desktop software ...
The definition of IoT is not new, in fact it’s been around for over a decade. What has changed is the public's awareness that the technology we use on a daily basis has caught up on the vision of an always on, always connected world. If you look into the details of what comprises the IoT, you’ll see that it includes everything from cloud computing, Big Data analytics, “Things,” Web communication, applications, network, storage, etc. It is essentially including everything connected online from ha...
"SAP had made a big transition into the cloud as we believe it has significant value for our customers, drives innovation and is easy to consume. When you look at the SAP portfolio, SAP HANA is the underlying platform and it powers all of our platforms and all of our analytics," explained Thorsten Leiduck, VP ISVs & Digital Commerce at SAP, in this SYS-CON.tv interview at 15th Cloud Expo, held Nov 4-6, 2014, at the Santa Clara Convention Center in Santa Clara, CA.
SAP is delivering break-through innovation combined with fantastic user experience powered by the market-leading in-memory technology, SAP HANA. In his General Session at 15th Cloud Expo, Thorsten Leiduck, VP ISVs & Digital Commerce, SAP, discussed how SAP and partners provide cloud and hybrid cloud solutions as well as real-time Big Data offerings that help companies of all sizes and industries run better. SAP launched an application challenge to award the most innovative SAP HANA and SAP HANA...
Connected devices and the Internet of Things are getting significant momentum in 2014. In his session at Internet of @ThingsExpo, Jim Hunter, Chief Scientist & Technology Evangelist at Greenwave Systems, examined three key elements that together will drive mass adoption of the IoT before the end of 2015. The first element is the recent advent of robust open source protocols (like AllJoyn and WebRTC) that facilitate M2M communication. The second is broad availability of flexible, cost-effective ...
How do APIs and IoT relate? The answer is not as simple as merely adding an API on top of a dumb device, but rather about understanding the architectural patterns for implementing an IoT fabric. There are typically two or three trends: Exposing the device to a management framework Exposing that management framework to a business centric logic Exposing that business layer and data to end users. This last trend is the IoT stack, which involves a new shift in the separation of what stuff happe...
Scott Jenson leads a project called The Physical Web within the Chrome team at Google. Project members are working to take the scalability and openness of the web and use it to talk to the exponentially exploding range of smart devices. Nearly every company today working on the IoT comes up with the same basic solution: use my server and you'll be fine. But if we really believe there will be trillions of these devices, that just can't scale. We need a system that is open a scalable and by using ...
An entirely new security model is needed for the Internet of Things, or is it? Can we save some old and tested controls for this new and different environment? In his session at @ThingsExpo, New York's at the Javits Center, Davi Ottenheimer, EMC Senior Director of Trust, reviewed hands-on lessons with IoT devices and reveal a new risk balance you might not expect. Davi Ottenheimer, EMC Senior Director of Trust, has more than nineteen years' experience managing global security operations and asse...
SYS-CON Events announced today that Gridstore™, the leader in hyper-converged infrastructure purpose-built to optimize Microsoft workloads, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Gridstore™ is the leader in hyper-converged infrastructure purpose-built for Microsoft workloads and designed to accelerate applications in virtualized environments. Gridstore’s hyper-converged infrastructure is the ...
What do a firewall and a fortress have in common? They are no longer strong enough to protect the valuables housed inside. Like the walls of an old fortress, the cracks in the firewall are allowing the bad guys to slip in - unannounced and unnoticed. By the time these thieves get in, the damage is already done and the network is already compromised. Intellectual property is easily slipped out the back door leaving no trace of forced entry. If we want to reign in on these cybercriminals, it's hig...
SYS-CON Events announced today that Cloudian, Inc., the leading provider of hybrid cloud storage solutions, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Cloudian, Inc., is a Foster City, California - based software company specializing in cloud storage software. The main product is Cloudian, an Amazon S3-compliant cloud object storage platform, the bedrock of cloud computing systems, that enables c...