Jul 08

Reserved ColdFusion function names

Posted by James Netherton | Tuesday 08 July 2008 8:55 PM | In ColdFusion

Most people are aware that you cannot give a name to a ColdFusion function that is equal to the name of one of the ColdFusion "built in" functions.

This started me thinking that as ColdFusion templates and CFC's are compiled down to Java servlets, there must be additional function names that are 'reserved'. Each template and CFC page will naturally extend java.lang.Object and therefore inherit all of Object's public methods.

For example, try the following:

<cffunction name="getClass">
</cffunction>

The result is "The name getClass is the name of a built-in ColdFusion function". You get the same result when declaring a function with the names "wait", "notify" and "equals", all of which are public methods on java.lang.Object.

These are only a few examples, I'm assuming that there must be more reserved function names given the number of classes that ColdFusion template and CFC servlets could potentially extend from.

The getClass method opens up the potential for some fun with ColdFusion:

Get the current template class name

<cfoutput>#getClass().getName()#</cfoutput>

Get the current template class properties

<cfset fields = getClass().getFields()/>

<cfloop array="#fields#" index="field">
   <cfoutput>#field.getName()#<br/></cfoutput>
</cfloop>

Get the current template class methods

<cfset methods = getClass().getDeclaredMethods()/>

<cfloop array="#methods#" index="method">
   <cfoutput>#method.getName()#<br/></cfoutput>
</cfloop>

Get the name of the class that the current template extends from

<cfoutput>#getClass().getSuperclass().getName()#</cfoutput>

 

Jul 07

Flex coding standards and best practices

Posted by James Netherton | Monday 07 July 2008 1:00 PM | In Flex

Adobe have added a list of Flex standards and best practices on their open source wiki.

Apparently it's not 100% complete yet but it's a nice set of guidelines nonetheless.

http://opensource.adobe.com/wiki/display/flexsdk/Coding+Conventions

 

Jul 01

Air IIS log reader

Posted by James Netherton | Tuesday 01 July 2008 7:00 AM | In AIR

After an hour or so hacking, I've put together a very simple Adobe Air application to read IIS log files that are in the W3C extended log file format.

Usage of the application is fairly obvious once it's up and running so I won't go into any detail on how it works. Here are some screen grabs that explain the basics. Click the thumbnails to see the full sized images.

Viewing a log file

Viewing a log file

Show / hide data columns

Show / hide data columns

View data row (double click datagrid row)

View data row

Here is the Zipped Air file and MXML source:

WebLogReader.zip
Air File

WebLogReader-Source.zip
Source Code


 

Jun 28

Disabling the Firefox smart location bar

Posted by James Netherton | Saturday 28 June 2008 11:51 AM | In FireFox

I've finally upgraded to Firefox 3, and after 10 minutes, I've found that I can't live with the "awesome" smart location bar.

Here's how to disable it:

1. Navigate to about:config and continue past the warning message

2. Search for the setting browser.urlbar.matchOnlyTyped and change its value to true

3. Restart Firefox

 

Jun 12

Disabling ColdFusion administrator authentication

Posted by James Netherton | Thursday 12 June 2008 11:51 AM | In ColdFusion

I find having to log into the ColdFusion administrator a pain when I'm working on applications in my development environment. You can disable the authentication process by way of the following:

1. Open up WEB-INF\cfusion\lib\neo-security.xml on multiserver installations or cfinsntallationdir\lib\neo-security.xml on standalone installs

2. Edit the admin.security.enabled property so that it appears as follows:

<var name="admin.security.enabled">
   <boolean value="false"/>
</var>

3. Save the file and restart ColdFusion

Obviously you would not want to be doing this in a production or publicly accessible environment!

 

Jun 04

Using jrunx classes in ColdFusion

Posted by James Netherton | Wednesday 04 June 2008 8:34 PM | In ColdFusion

I've been screwing around with classes within the jrunx Java package that comes bundled with ColdFusion (not sure if it exists if you aren't running CF on JRun). There are some useful and interesting utilities and I've put together a very brief set of demos for a few of them.

jrunx.util.NetUtil

Lookup an IP address from a DNS string:

<cfset variables.netUtil = createObject("java", "jrunx.util.NetUtil")/>

<cfset variables.ip = variables.netUtil.resolveIp("www.google.com")/>

<cfoutput>#variables.ip#</cfoutput>

Validate an IP address:

<cfset variables.netUtil = createObject("java", "jrunx.util.NetUtil")/>

<cfset variables.hostValid = variables.netUtil.validateHostIp("127.0.0.1")/>

<cfoutput>#variables.hostValid#</cfoutput>

jrunx.util.ZipTool

Zip a directory:

<cfset variables.zipUtil = createObject("java", "jrunx.util.ZipTool")/>
<cfset variables.zipUtil.zipDir("C:/Test", "C:/Test.zip", true)/>

Zip a single file:

<cfset variables.zipUtil = createObject("java", "jrunx.util.ZipTool")/>
<cfset variables.zipUtil.unZipSingleFile("C:/Test.zip", "C:/TestUnzip", "myHTMLFile.html")/>

Unzip an entire archive:

<cfset variables.zipUtil = createObject("java", "jrunx.util.ZipTool")/>
<cfset variables.zipUtil.unzipToPath("C:/Test.zip", "C:/TestUnzip")/>

jrunx.xml.XMLScript

Change an XML entity value within an XML file: XML Input File

<root>
   <person id="1">
      <name>James</name>
      <age>00</age>
      <occupation>Developer</occupation>
   </person>
   <person id="2">
      <name>Bob</name>
      <age>00</age>
      <occupation>Developer</occupation>
   </person>
</root>

<cfset variables.xmlScript = createObject("java", "jrunx.xml.XMLScript").init()/>

<cfset variables.inputFile = expandPath("./input.xml")/>
<cfset variables.outputFile = expandPath("./output.xml")/>

<cfset variables.args = arrayNew(1)/>

<cfset variables.args.add("-i")/>
<cfset variables.args.add(variables.inputFile)/>
<cfset variables.args.add("-o")/>
<cfset variables.args.add(variables.outputFile)/>
<cfset variables.args.add("-s")/>
<cfset variables.args.add("/root/person[@id='1']/name/text()")/>

<cfset variables.args.add("Joe")/>

<!--- Hackery to create a java string array --->

<cfset variables.dummyString = repeatString(" : ", arrayLen(variables.args) - 1)/>
<cfset variables.stringArray = variables.dummyString.split(":")/>

<cfloop from="1" to="#arrayLen(variables.args)#" index="i">
   <cfset variables.stringArray[i] = variables.args[i]/>
</cfloop>

<cfset variables.xmlScript.command(variables.stringArray)/>

XML Output File

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <person id="1">
      <name>Joe</name>
      <age>00</age>
      <occupation>Developer</occupation>
   </person>
   <person id="2">
      <name>Bob</name>
      <age>00</age>
      <occupation>Developer</occupation>
   </person>
</root>

Admittedly that's a very lame example, there's a proper overview of what the class can really do on the Adobe Jrun guide There's a full overview of jrunx classes and packages here.

 

May 22

Updated Bookshelf Page

Posted by James Netherton | Thursday 22 May 2008 10:04 PM | In Blogging

I've finally got around to redesigning my bookshelf page. All of my latest purchases are on there and I've removed the old crappy books that I really need to take off my book shelf at home.

 

May 18

Eclipse Memory Manager Plugin

Posted by James Netherton | Sunday 18 May 2008 12:09 PM | In Java

I thought I'd mention this as it ties in quite nicely with my previous post.

Currently in incubation phase, Eclipse has a memory manager plugin which can analyse and provide some nice graphs and statistics from Java heap dumps.

The plugin can be downloaded here. There is an additional dependency on the BIRT charting and reporting plugin. Instructions for installing this are here.

Once everything is installed you can begin heap analysis. Use the jmap utility to create a heap dump file from a running JVM instance. You'll need the JVM process ID in order to do this.

Open up a command prompt or terminal session and execute the command below. Be warned that the size of the file produced can be quite large (over 100Mb).

jmap -dump:format=b,file=heapdump.hprof <process id>

From eclipse you can use the memory manager to open up and analyse the dump file.

Java 1.6 includes a simpler heap analysis tool named jhat. To analyse the heap dump we obtained from jmap, execute the following command:

jhat heapdump.hprof

Now point your web browser at http://localhost:7000 to see view the heap analysis results.

All of this may be useful if you're experiencing the Java OutofMemory exception. You can set the JVM parameter -XX:+HeapDumpOnOutOfMemoryError to dump the heap on out of memory exceptions.

 

May 16

JVM Monitoring Tools For ColdFusion

Posted by James Netherton | Friday 16 May 2008 11:29 AM | In ColdFusion,Java

I've been experimenting with JVM monitoring tools out of curiosity for what impact some applications have on memory and CPU usage. Here is a brief rundown on some of the tools that I used. All of these tools should be included within the bin directory of whatever JDK distribution you are using, providing the Java version is 1.5 or greater.

You may read this post and wonder why I bothered to research any of this, seeing as ColdFusion now has built in monitoring and profiling tools, together with many third party alternatives. Those tools are all good at providing a relatively high level of detail, showing what's going on inside of the application server but the tools outlined below can offer a more granular level of detail.

jps

Some of the tools require an argument to specify the id of the JVM that you want to interrogate, jps can be used to retrieve this id:

jps localhost

358 Jps
268

Other tools require that you pass an argument for the JVM process ID. This information can be easily obtained by using Task Manager, Activity Monitor, top, ps or whatever your tool of choice is for your environment.

JConsole

JConsole is a graphical tool that displays the current state of the JVM in terms of memory usage, thread usage and classes loaded. There are a couple of other items such as MBean management, but that's outside the scope of this post.

To start JConsole you need to the process ID of the JVM. I'm going to use the PID of JRun (2219) which of course is my ColdFusion JVM instance:

jconsole 2219

You'll then see graphs representing the current JVM memory usage. Execute some CF pages or fire up an application and you should see the memory graphs rise and fall as memory is allocated and garbage is collected. You can even force garbage collection by clicking the 'Perform GC' button.

jstack

jstack produces a stack trace dump of all threads within the JVM.

jstack 2219

Note that the output is very verbose so it's probably better to pipe the output to a file.

jmap

jmap can be quite useful to gather details about JVM memory details. For example, you can view heap statistics relating to number of objects loaded and the amount of memory allocated. Again, it's best to pipe the output to a text file:

jmap -histo 2219 > heap_stats.txt

jstat

jstat displays performance statistics for a JVM instance. The tool requires the id of the target JVM to be passed as a command line argument. You can also set the data refresh rate by specifying a value in milliseconds. The example below outputs a summary of garbage collection statistics every 5 seconds:

jstat -gcutil 1700 5000

There a many different switches that can be used to obtain different monitoring statistics. The Sun jstat web page has an overview of these options.

Some other tools that I looked at:

visualgc - similar in some ways to JConsole but focuses on memory and garbage collection statistics.

profiler4j - I couldn't get this working with JRun.

Eclipse Profiler - I got the Eclipse plugin installed ok but JRun choked whenever I tried to start ColdFusion after adding the specified profiler arguments into the jvm.config file. I only tried this with CF 8 so it may work with CF 7 and Java 1.4.2.

JRockit Mission Control - A product from BEA. It has been used to good effect in the past to find memory leaks in ColdFusion:

Mike Schierberl - ColdFusion Memory Leaks

Jason Sheedy - Cold Fusion Memory Leak

I got bored of trying to hunt down a free version of JRockit on the BEA website, but this tool can potentially offer some nice insights into what's happening under the hood of an application.

 

May 04

Converting Unix timestamp values in ColdFusion

Posted by James Netherton | Sunday 04 May 2008 4:58 PM | In ColdFusion

I've been working on an application that integrates with a third party payment gateway solution. The payment service sends back a transaction timestamp after a purchase is successfully completed in the unix time format.

I spent some time trying to figure out how to covert this into a 'normal' date format with ColdFusion. The solution is pretty simple as the following code sample demonstrates.

<!--- Unix Timestamp representing 4th May 2008 00:00:00 AM --->
<cfset variables.unixTimeStamp = "1209859200"/>

<cfoutput>#dateAdd("s", variables.unixTimeStamp, "01/01/1970")#</cfoutput>