Posts in Category "ColdFusion"

ColdFusion Makes the World a Safer Place

About three weeks ago, America’s Most Wanted launch a new site with very comprehensive crime-solving and fugitive-finding functionality. And it’s all powered by ColdFusion and Flash (man, I’d love to know how much traffic they are supporting). Macromedia isn’t just changing the web — we’re helping to change the world!

Renaming Files As They Are Uploaded (how CFFILE actually works)

When receiving a file uploaded from a client, you can actually save it and rename it all in one step rather than two steps as the documentation implies.

The documentation states that the “destination” attribute of the CFFILE tag specifies the “pathname of the directory in which to upload the file.” This is misleading for two reasons:

  1. The file has actually already been uploaded by the time your CFFILE tag is encountered. The file has automatically been saved in ColdFusion’s temporary directory. Using the CFFILE tag with the action attribute set to “upload” is really just moving the file from one place to another. If no CFFILE tag is encountered, the file is simply deleted.
  2. You can actually specify a file name in addition to a directory in the destination attribute. Rather than this…


    <cffile action="upload" destination="/path/to/some/directory" ... />
    <cffile action="rename" ... />

    … you can just do this…


    <cffile action="upload" destination="/path/to/some/directory/#createUUID()#.gif" ... />

This moves the uploaded file from ColdFuson’s temporary directory into the specified directory, and renames it using a unique ID generated by ColdFusion, all in one step.

The Definitive Guide to the ColdFusion Classpath

Before you can load and use third-party Java classes from ColdFusion, you have to add them to the ColdFusion class path. Unfortunately, class paths are not much fun (learning to get your class path right is probably just as hard as learning to write Java in the first place), and on top of that, adding classes to ColdFusion’s class path is confusing because there are so many different ways of doing it. I finally decided to sit down and create the definitive guide to how it’s done to try to clarify the process.

First of all, there are different ways of doing it depending on how ColdFusion is deployed. The techniques below are broken down by deployment preference. Also note that there is a difference between adding class files to your class path, and adding jar files. I would recommend jarring up your class files to simplify the installation process, but you can deploy either jar files or class files. If you deploy the class files outside of a jar file, make sure you grab the entire directory structure representing your packages. In other words, if you have a class file called VeryCoolUtil.class in the package “com.macromedia.util”, make sure you deploy the entire directory structure (ie com/macromedia/util/VeryCoolUtil.class). If you decide to jar you class files up instead, you also need to make sure you jar up the entire directory structure representing your packages as opposed to just the class files.

ColdFuison Standalone

  1. The easiest way to add class or jar files to your class path is to simply drop them in the lib directory where they are automatically picked up. The directory is located at {cf_installation}/lib.
  2. You can also drop them in any of the Java extension directories. To find a list of the Java extension directories, open the ColdFusion administrator and click on “System Information”. Toward the bottom, you will see a system property called “Java Ext Dirs”. You can put jar and class files in any of those directories to have them picked up by the ColdFusion server.
  3. If you want more control over the order in which class files are loaded, you can add them to the ColdFusion server’s class path manually. In the ColdFusion administrator, click on “Java and JVM”, then add the absolute paths to your jar or class files in the “Class Path” field.
  4. Finally, you can manually add class and jar files to the server’s class path located in the jvm.config file. I’m not going to go into much detail about that since I recommend you use the admin interface rather than tweaking config files by hand.

J2EE Deployment

  1. The easiest way to add class or jar files to your class path is to simply drop them in the lib directory where they are automatically picked up. The directory is located at {cf_installation}/servers/lib. These class files will be available to all servers.
  2. You can also make your class files available only to the ColdFusion server by dropping them in “{cf_installation}/servers/default/cfmx/WEB-INF/lib”. (Note that putting them in {cf_installation}/servers/default/cfmx/WEB-INF/cfusion/lib will NOT work.)
  3. You can drop them in any of the Java extension directories. To find a list of the Java extension directories, open the ColdFusion administrator and click on “System Information”. Toward the bottom, you will see a system property called “Java Ext Dirs”. You can put jar and class files in any of those directories to have them picked up by the ColdFusion server.
  4. Another way to do it is to add classes through the JRun Management Console. Open up the console, and under the default server, click on settings, then add your classes to the class path list. (Note you can also click on “ColdFusion MX application” under the default server, then click on settings to make the same changes.)
  5. And finally, once again, you can edit the jvm.config file yourself, however again, I recommend you stick to using one of the methods above.

There are also ways to load your class files programatically in ColdFusion, or even to load them remotely, which you can find instructions on here. And finally, keep in mind that changes to you class path always require you to restart ColdFusion and/or JRun for them to be picked up.

Free ColdFusion Applications

Need an application, and need it fast? There is no more efficient technology for developing web applications than ColdFusion, so of course, you can build just about anything you need quickly enough, but now you can also just download a complete application for free from Free ColdFusion Applications (by the folks at EasyCFM). There are only about 12 applications at this point to choose from, but it looks like a lot of the basics are covered (weblog, calendars, forums, etc.) I have no idea how good any of these applications are, but it seems like they have a lot of potential. I can think of several uses:

  1. Download and use as-is.
  2. Download and customize.
  3. Download and cannibalize.
  4. At the very least, get some ideas for the fully customized version you are writing yourself.

Anyone have any experiences with any of these apps? What do you think?

Know Your List Functions

If you use ColdFusion list functions, make sure you know the difference between listContains and listFind. Using listContains where you should be using listFind might appear to work at first, but can introduce hard-to-find bugs in your applications down the road.

listContains returns the index of the first item in the list which contains a substring of the string you are searching for. For instance, consider the following code:

<cfset myList = "abc,def,ghi"/>
<cfoutput>#listContains(myList, "e")#</cfoutput>

The substring “e” is contained in the second item in the list, so listContains returns 2 rather than 0. Now consider the code below which uses listFind:

<cfset myList = "abc,def,ghi"/>
<cfoutput>#listFind(myList, "e")#</cfoutput>

listFind looks for an exact match rather than just a substring, so 0 is returned since there is no item in the list which matches “e” exactly. (The search is case-sensitive — for a case-insensitive search, use listFindNoCase.)

Most of the time, you are probably going to want to use listFind. Either way, just make sure you are aware of the difference.

Loading Class File Remotely in ColdFusion

Spike Milligan of Spike-fu made a very cool post recently entitled “Loading java class files from a relative path” which demonstrates a technique for loading class files through a local file URL so that you don’t have put them in your classpath. I thought that was very innovative, and decided to take it a step further.

The point of Spike’s post was to make server configuration easier. To make configuration easier still, why not store all your class files on one central server, and use a similar technique to load them remotely? I wrote a component called RemoteClassLoader that does it for you:

<cfcomponent displayName="RemoteClassLoader" output="no">

<cfset my = structNew()/>

<cffunction name="setRemoteClassPaths" returnType="void" output="no">
<cfargument name="classPaths" type="array" required="true"/>
<cfset var urls = arrayNew(1)/>
<cfset var arrayFactory =
createObject("java", "java.lang.reflect.Array")/>
<cfset var urlClass =
createObject("java", "java.net.URL").init("http://macromedia.com")/>
<cfset var urlArray = arrayFactory.newInstance(urlClass.class, 0)/>
<cfloop from="1" to="#arrayLen(arguments.classPaths)#" index="i">
<cfset urls[i] =
createObject("java", "java.net.URL").init(arguments.classPaths[i])/>
</cfloop>
<cfset my.loader =
createObject("java",
"java.net.URLClassLoader").init(urls.toArray(urlArray))/>
</cffunction>

<cffunction name="getRemoteClass">
<cfargument name="class" type="string" required="true"/>
<cfreturn my.loader.loadClass(class)/>
</cffunction>
</cfcomponent>

Using the component above, I was able to load Spike’s HelloWorld class right from his server, instantiate it, and call functions on it. The code looks like this:

<cfscript>
remoteClassLoader =
createObject("component",
"com.macromedia.net.RemoteClassLoader");
urlArray = arrayNew(1);
urlArray[1] = "http://www.spike.org.uk/downloads/";
remoteClassLoader.setRemoteClassPaths(urlArray);
helloWorld = remoteClassLoader.getRemoteClass("HelloWorld");
</cfscript>

<html>
<cfoutput>#helloWorld.newInstance().sayHello()#</cfoutput>
</html>

A couple of notes:

  1. The code above assumes that the RemoteClassLoader component is in the package “com.macromedia.net” (which is where I put it after I wrote it before checking it into CVS).
  2. I haven’t tested this with jar files, but it should work fine.
  3. I wrote this in about 5 minutes, so you might want to bulletproof it before using it in a production environment.

Thanks for the inspiration, Spike!

Logging Classes Loaded by the JVM

Good ol’ Debbie has just published an interesting TechNote entitled “Determining which class files are loaded by ColdFusion MX” which explains how to get ColdFusion (actually, Java) to log all the classes loaded by the JVM. This can be handy for resolving classpath conflicts, and for some types of optimization. Be warned, however, that the log file will get very big, very quickly, and that this should not be used in a production environment due to performance degradation unless that is the only way to debug your specific issue.

CFMX, OS X, and Java 1.4.2

Back in February, I posted some information on how to get ColdFusion to run on OS using Java 1.4.2. I made that post before realizing that, although CFMX does start up with the right version of Java, there is no way that I know of to add libraries to Java’s (and therefore, ColdFusion’s) classpath. Adding paths to the -cp or -classpath flags doesn’t work because ColdFusion gets its classpath from the admin application. I thought that starting both CFMX and the admin application was fixing the problem until Sean Corfield pointed out that starting the admin application with CFMX forced CFMX to use Java 1.3 rather than 1.4.2. Oops.

Well, now I have a complete solution. The instructions I posted back in February are still valid, but now we also have a way to add libraries, as well. Rather than using the classpath to make additional libraries available to Java, put jar files in a directory specified by the java.ext.dirs system property. With my configuration, the directory “/Library/Java/Extensions” works well. For a list of valid directories, log into the ColdFusion administrator, click on the “System Information” link at the top, and scroll down to “Java Ext Dirs”. Pick a directory in that list, and copy all your third-party jar files there. If you don’t like to jar up your classes, you should also be able to put your classes (in their appropriate packages) right into an extension directory, as well.

The java.ext.dirs and java.class.path system properties cannot be changed dynamically after Java has started up, so don’t bother going that route (I tried it just to be sure). At this point, I think using one of the Java extension directories is your best bet. There are still advantages to using a classpath on other platforms when you can since the classpath gives you the added advantage of sequencing your jars files (so that classes appearing earlier in your classpath take precedence over classes appearing later), but if that’s all I have to give up in order to be able to fully run CFMX on OS X using the latest and greatest version of Java, that’s perfectly fine with me.

Macromedia ColdFusion Forums RSSified

Roger Benningfield of Big Damn Heroes has rssified all the ColdFusion forums except, mysteriously enough, the security forum. You can get the feed URLs from his post entitled “Minimalist RSS for the Macromedia CF Forums“. Be responsible, however. Roger warns, “Abuse them by pounding my server every few minutes and I’ll just drop the service entirely… the feeds only update every few hours, so don’t bother polling them more than that.”

This is a very cool service. Thanks, Roger.

ColdFusion MacroChat Today

I think this is likely to be the coolest MacroChat yet. At 12:00 PM Pacific (3:00 PM Eastern), Ben Forta, Tim Buntel and Dave Gruber talk ColdFusion and answer community questions. To participate, go to:

http://macromedia.breezecentral.com/r36158305/

Other MacroChats for today (Thursday):

CSS for Dreamweaver
A free presentation by Macromedia Dreamweaver Product Evangelist, Greg Rewis
Thursday, May 20, 2004
9:00 AM PT/12:00 PM ET
http://macromedia.breezecentral.com/r82016586/

Delegating Web Content Updates with Macromedia Contribute
A free presentation by Macromedia Product Manager, Lawson Hancock
Thursday, May 20, 2004
10:00 AM PT/1:00 PM ET
http://macromedia.breezecentral.com/r84286274/

Director MX 2004 New Features, Putting It All Together
A free presentation by Macromedia Product Engineer, Thomas Higgins
Thursday, May 20, 2004
3:00 PM PT/6:00 PM ET
http://macromedia.breezecentral.com/r81881694/

Customizing and Extending Dreamweaver MX 2004
A free presentation by Team Macromedia member, Danilo Celic
Thursday, May 20, 2004
4:00 PM PT/7:00 PM ET
http://macromedia.breezecentral.com/r17184370/