Posted in ColdFusion | Posted on 04-27-2009 | 5,020 views
OK, this is probably an over-the-top geeky question to ask, but Christopher Ryan suggested it to me earlier today and I thought it would be a great topic. While ColdFusion can do almost anything, there are times when we need to resort to Java in order to do something ColdFusion can't. This can be using another JAR for a feature not implement by ColdFusion, or perhaps just relying on createObject to make an instance of a built-in Java object. So let me ask - which Java class have you found to be the most useful? For me it is StringBuffer. I've only had to use it a few times, but it was incredibly helpful in cases where I was creating a large string on the fly. (Like my toXML CFC.)


I keep my CF servers on UTC time and consistently store date/time data in UTC time. Then users pick their time-zone locale (like 'America/Denver' or 'Navajo'), and CF code using those functions adjusts dates/times shown on web pages to their locale -- automatically detecting if Daylight Savings Time is in effect and applying it, no matter where they are in the world.
I really like the ability to pick-and-choose Java classes that are useful, and have CF code pull everything together. I'm curious what other Java gems CF developers have found.
java.util.regex.Pattern and Matcher are also nice to have. CF's inbuilt regular expression support is pretty good, so I don't use them very often, but very nice to have in a bind.
My all-time highest use, however, are undoubtedly the collection classes (java.util.Xxx). CF makes dealing with collections a lot harder than it needs to be, so I do quite a bit of direct [Array]List and [Hash]Map stuff, along with the odd [Hash]Set when it's appropriate.
http://javacsv.sourceforge.net/
http://www.csvreader.com/java_csv.php
The delimiter can be set. It's not just for comma-delimited.
- Gabriel
i know it's package and not a class, but i have a love/hate relationship with it. love it for the fact that you can do ALMOST anything to a pdf that cfdocument can't. hate it for the fact that the package is so big it's nightmare reading through the documentation to figure out how to do something.
Also, as of JDK 5 StringBuilder is preferred to StringBuffer in most cases (non-multi-threaded situations).
Oh, wait, I see what you asked for...
My personal favorite bit of Java in CF. Really good for code generation.
I've been using it alot for some reason lately ;)
@tony get bruno's "itext in action", the PDF version is only $30. if you use iText for anything complicated, best investment you can make: http://www.manning.com/lowagie/
http://java.sun.com/javase/6/docs/api/java/lang/Th...
For storing variables you only want to be accessed by that given thread.
I use this allll the time.
<cfset Variables.JRunObject = CreateObject("Java", "jrunx.kernel.JRun")>
<cfset Variables.InstanceName = Variables.JRunObject.getServerName()>
and for getting current host name:
<cfset Server.hostName = CreateObject("java", "java.net.InetAddress").localhost.getHostName() />
In CF it's actually faster to use arrayNew(1) and arrayAppend() and a final arrayToList() at the end. Even Fusebox has a CFC to "fake" a StringBuilder because someone thought it was faster, but seriously, stop it, just use an array.
Your code is also portable between every runtime (even that .NET one people don't talk about anymore).
As for some Java classes, the java.lang.reflect.* package is quite useful, but for actual development I'd say java.math.BigInteger. There's no way to do arbitrary precision integer math in CF. precisionEvaluate() always uses BigDecimal, which will throw nasty NaN or Infinity exceptions when doing big exp/mod calculations.
@Mark: Can you add a bit? I thought vars created within cfthread were already blackboxed?
@shakti: Have you tested file ops in CF8? They were significantly improved.
.addValidationError("fieldname allows blank","validation error number","Validation message")
then at the end of the method I throw the error
<cfif eValidate.hasValidationError()>
<cfthrow object="#eValidate#">
</cfif>
the calling app can capture the validation exceptions now
try {
oFpsGTW.validateForPassword(oFpsDTO);
} catch (uao.exceptions.Validation e) {
//return to form with messages
}
To c/p paste from the Javadoc:
This class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variable.
Think of it like a request scope variable, but encapsulated.
I used the Jakarta Net Commons WHOIS class to monitor our company's domain names. Somewhere around v7 we noticed that Macromedia rolled the commons-net Jar into the CFMX, so we no longer had to worry about deploying it when we built a new server.
I also used some sort of DNS lookup class to check that an MX record existed for email addresses submitted by users during registration. That added another layer of validation that could occur before we tried to send a user an email.
This is great for mapping applications, where you can use the Google maps API to let end users draw regions on a map (see http://code.google.com/articles/support/ezdigitize...). This generates the latitude/longitude coordinates of the boundaries of the area, which can be stored.
Then you can pass any other latitude/longitude coordinates to java.awt.Polygon and determine if the location is within the region. Works well...
<cfset objJavaSimpleDateFormat = CreateObject("java", "java.text.SimpleDateFormat").init("dd/MM/yyyy HH:mm:ss")>
<cfset objJavaIoFile = createObject("java", "java.io.File")>
<cfset parsedDateTime = objJavaSimpleDateFormat.parse("28/07/2010 16:45:03")/>
<cfset myFile = objJavaIoFile.init("c:\test\myFile.txt")>
<cfset wasLastModifiedDateChanged = myFile.setLastModified(parsedDateTime.getTime())/>
Suppose if you have two java objects bc and sc respectively of class com.abc.def.baseclass and its subclass com.abc.def.subclass, in java you can assign subclass object to superclass reference (i.e. derived is assigned to base or bc=sc;) Any idea how this can be achieved in ColdFusion code with the same two java objects?
[Add Comment] [Subscribe to Comments]