|By Charlie Arehart||
|May 18, 2005 01:00 PM EDT||
It's great to have a whole issue of CFDJ focused on integrating CFML with Java. The process is a lot easier than many might think, as the articles in this issue will show.
I'd like to review the very basics to get you started, showing some of the cool doors of opportunity that this integration opens. Perhaps more important for some, though, I'd like to also point out that direct integration of CFML with .NET is also possible.
Surprised to hear about direct integration of CFML with .NET? You may know that you can call .NET objects through web services in CFMX. But you can also call them directly (as opposed to via a web service, as well integrate CFML and ASP.NET) using BlueDragon's .NET edition. Don't worry. If you're only interested in CF, this article is still for you in its introduction to integrating CFML with Java.
Solutions for CF 4.5/5/MX and BlueDragon
Indeed, even if you're still using CF 4.5 or 5, and never transitioned to CFMX, this article will still apply. Some may be surprised to hear that integration of CFML with Java was introduced in CF 4.5. It's certainly true that the Java-based CFMX and BlueDragon engines make this integration easier; still, it is possible to do Java integration with CF 4.5 and 5. In fact, some readers of CFDJ may recall the excellent 8-part series by Guy Rish from called "A Cold Cup o' Joe", where he introduced many of us for the first time to the idea, benefits, and challenges of doing such CFML/Java integration. He showed how you can install the Java Runtime on your machine and enable access to it from within CFML with a single change to the CF Admin (you can also see the CF 4.5/5 docs for more.) It's great to see him back in this issue, with "Cold Cup o' Joe - Second Shot" and I wish him luck with this new series.
In the first series, Guy showed how to call Java objects from within CFML, as well as how to call upon and integrate CFML with JSPs and Servlets. Of course, with CFMX and BlueDragon both being built upon Java, you don't need to make any changes to leverage Java. It's just built-in. Still, most of Guy's series will still apply and would be highly recommended for further reading. The CFMX docs also now have an entire chapter on CFML/Java integration, available online at http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/java.htm. I certainly don't want to repeat all the information provided in these useful and rich resources.
Why Bother With Java?
We all know that CFML is a wonderfully high-level language. Why would we want to bother with Java? Indeed, the reason we like it and are so productive is that we can do nearly everything we want right from within CFML. Still, some hold this simplicity against CFML, lamenting that if they want to do something that CFML can't do, they feel stuck. You're not. There's an entire world of new possibilities in the form of Java objects and libraries that you can leverage to extend your CFML. These can either be the built-in libraries that come with the Java framework, or that you or others in your organization build, or that you acquire from third parties.
An important point to clarify is that it is not necessary that you know Java to do the kind of integration I'm discussing. For example, there is no CFML tag or function that will return the IP address for a given domain name. But there is a way to get this information by calling the InetAddress class in the Java API. The Java API documentation for this can be found at http://java.sun.com/j2se/1.4.2/docs/api/java/net/InetAddress.html. With a little reading, you'd learn that the GetByName method accepts a host name and returns an InetAddress object for that host name, which then has a getHostAddress method that returns the actual IP address.
The following CFML example demonstrates one way to call on this object and its methods, using CFOBJECT:
<CFOBJECT NAME="iaddrClass" CLASS="java.net.InetAddress" TYPE="JAVA" ACTION="CREATE"> <CFSET IADDR=iaddrClass.getByName("www.newatlanta.com")> <CFOUTPUT>#iaddr.getHostAddress()#</CFOUTPUT>
Recall that since CFMX and BlueDragon are built atop the Java API, you have direct access to the Java API and this java.net.InetAddress class. If you run this code (while connected to the internet, since the getByName method does require net access to search for the information about the host name), you will get the following answer:
Recall that in order to run this in CF4/5, you need to first modify the CF Admin to enable Java access. See http://livedocs.macromedia.com/coldfusion/ 5.0/Developing_ColdFusion_Applications/cfobject8.htm.
Beyond the built-in objects in the Java API libraries, you can also access any Java class that is (or has been made) available on the JVM classpath (see the CFMX or BlueDragon Admin console) or which has been placed in either of the following locations under either the CFMX or BlueDragon directories:
- WEB-INF/lib (as a Java archive or .jar file)
- WEB-INF/classes (as a .class file)
The subject of creating Java class files is beyond the scope of this article. I'm sure other articles in the issue will cover the topic, and if they don't recall that the Macromedia docs have an entire chapter on CFML/Java integration. It includes some sample Java code to create your own example class file, and discusses how to compile and place the class files. Beyond that, the documentation discusses several other useful topics regarding integration of Java with CFML, including:
- How cfobject loads the class and makes available any static objects
- How to use the built-in init() method to call the object's constructor
- How to use createObject instead of CFOBJECT, where appropriate
- Passing arguments to methods
- Handling datatype conversion
- Resolving datatype ambiguity with Javacast
- Handling case-sensitivity issues
- Handling java exceptions
- Calling EJBs
The documentation also discusses integration of CFML with JSPs and servlet, which are the Java form of web application scripting. These are also often referred to as part of the J2EE (Java 2 Enterprise Edition) framework. CFMX was built atop a J2EE server (JRun) and can be run atop others (WebLogic, WebSphere, and so on), as can BlueDragon.
What about .NET?
The previous section focused on integrating CFML with Java. What if your organization is moving to the Microsoft .NET Framework? .NET is Microsoft's response to J2EE, and it offers many of the same features that one may find in J2EE development, and more. While CFMX is only available to run atop the J2EE framework, it is possible to run your CFML natively on .NET using BlueDragon for the Microsoft .NET Framework, a product from New Atlanta (disclaimer: that's the company I work for) which offers an alternative implementation of CFML - abbreviated from hereon as BlueDragon/.NET. You don't need ColdFusion at all if you install BlueDragon, though you can also install both CF and BlueDragon at the same time for testing purposes. Why should you care? If your organization is moving to .NET, you may feel you have no choice but to rewrite your investment of CFML as ASP.NET. The fact is, with BlueDragon/.NET, you can simply run your CFML natively on the .NET framework. This is a subject for another entire article, and you can find out more at www.newatlanta.com/bluedragon/. (In fact, there will be a couple of presentations on the subject at the upcoming CFUnited conference.)
But for the sake of this article, I want to point out that you can equally leverage the underlying .NET framework classes from CFML, just as I discussed in the previous section regarding Java. You can call either the built-in classes of .NET (there are more than 4,000 of them) or ones you build or acquire, and again some of them provide information not available otherwise in CFML.
For instance, if you want to know the name of the computer on which the code is running, the following will leverage the .NET framework's Environment class in the "System" namespace, to return the MachineName. (See the .NET documentation for details on this class, at http://msdn.microsoft.com/library/default.asp?url=/ library/en-us/cpref/html/cpref_start.asp.) This CFML will only run on BlueDragon/.NET:
<CFOBJECT ACTION="create" type=".net" NAME="env" CLASS="System.Environment"> <p>Computer Name = <CFOUTPUT>#env.get_MachineName()#</CFOUTPUT>
On my machine, this returns CharlieD600, the name of my machine. Not the most compelling example (more on those in a moment), but it shows how very similar this approach is to the Java examples above. Indeed, besides using a .NET class and its method, there is just one difference.
Note the use of TYPE=".net" rather than TYPE="java" as in the previous examples. In BlueDragon/.NET, you can call any underlying .NET class this way. In fact, you could use TYPE="java" as well, and even run the example provided in the previous section, unchanged. Though it runs atop the .NET framework, BlueDragon/.NET also requires installation of the Visual J# Redistributable runtime environment, which makes most of the same Java API libraries available within .NET, and therefore available to CFML pages running on .NET via BlueDragon. Along the same lines, note that when the CFML code in the .NET example above referred to the MachineName, it used get_MachineName(). The .NET documentation for this class would show that MachineName is a property, rather than a method. But it would also show that when calling the class with Visual J#, one would need to call the property as a method instead. Because of BlueDragon/.NET's underlying reliance on Visual J#, we need to use the same form in our CFML.
Before leaving the subject of .NET integration with CFML, I would add that there is of course a whole lot more that is possible in terms of leveraging underlying .NET libraries, just as there is much more than I showed in the one example of Java integration above. While some of the other articles in this issue (and the Macromedia documentation) will highlight more about Java integration, let me take a moment to show just a little more of what's possible with .NET integration. Here's one more example, that uses CFML to display the foreign languages available in .NET for supporting globalization:
<cfobject name="culturetypes" class="System.Globalization.CultureTypes" action="CREATE" type=".net" > <cfobject name="cultures" class="System.Globalization.CultureInfo" action="CREATE" type=".net" > <cfset cultarray=cultures.getcultures(culturetypes.NeutralCultures)> <cfloop from="1" to="#arraylen(cultarray)#" index="i"> <cfoutput>#cultarray[i].get_NativeName()# (#cultarray[i].get_Name()#)
A subset of the result shown would include:
But even beyond this, the .NET framework offers so much more. There is built-in support for calendar, datagrids, adrotators, and so much more. It would be possible to show how to implement even these features within CFML, but again this is beyond the scope of this article. The discussions presented here should be enough to get you started and motivate further interest.
Other Resources for Getting Started
Before concluding, I'd like to point out a few resources to learn more about CFML integration with Java and .NET. Beyond the Macromedia documentation I mentioned above, there are other resources you can turn to in order to learn more about CFML integration and Java. There are two books that cover the topic:
Java for ColdFusion Developers, by Eban Hewitt
Reality J2EE: Integrating ColdFusion and J2EE, by Ben Forta, et al
JRun Web Application Construction Kit, by Drew Falkman
While the latter really isn't about CFML, since CFMX runs atop JRun, it provides the same kind of friendly introduction to leveraging JRun and J2EE in general that was modeled in the infamous ColdFusion Web Application Construction Kit.
For more on BlueDragon/.NET, there is an entire manual, Deploying CFML on ASP.NET Servers, with many examples. It's available with the download/install of BlueDragon/.NET, or it's available online at www.newatlanta.com/products/bluedragon/ self_help/docs/6_2/BlueDragon_62_CFML_on_ASP_NET.pdf.
You can also find all the articles in Guy Rish's excellent series from the following link: http://cfdj.sys-con.com/author/113Rish.htm.
- Where Are RIA Technologies Headed in 2008?
- The Next Programming Models, RIAs and Composite Applications
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Constructing an Application with Flash Forms from the Ground Up
- Building a Zip Code Proximity Search with ColdFusion
- Personal Branding Checklist
- CFEclipse: The Developer's IDE, Eclipse For ColdFusion
- Has the Technology Bounceback Begun?
- Adobe Flex 2: Advanced DataGrid
- i-Technology Viewpoint: We Need Not More Frameworks, But Better Programmers
- Web Services Using ColdFusion and Apache CXF
- Cloud People: A Who's Who of Cloud Computing