CFML wishlist: All collections should extend Iterator

Have you ever really given a second thought to the fact that in ColdFusion/CFML you have to loop queries, arrays, and structures in completely different ways?   For example, in each of these things, we are essentially doing the same thing:

<!--- looping our query --->
<cfloop query="myQuery">
	<cfset doStuff() />
</cfloop>
<!--- looping our array --->
<cfloop array="#myArray#" index="i">
	<cfset doStuff() />
</cfloop>
<!--- or --->
<cfloop from="1" to="#ArrayLen(myArray)#" index="i">
	<cfset doStuff() />
</cfloop>

<!--- looping our structure --->
<cfloop collection="#myStruct#" item="i">
	<cfset doStuff() />
</cfloop>

In each of these cases, we are essentially doing the same thing, that being looping a collection that contains multiple items and acting on each iteration.  I have always liked the fact that the ColdFusion array can be converted to a Java iterator like this:

<cfset iterator = myArray.iterator() />
<cfloop condition="#iterator.hasNext()#">
	<cfset thisIteration = iterator.next() />
	<cfset doStuff() />
</cfloop>

However, given the fact that <cfloop array=”#myArray#” index=”i”> is already an abstraction, it doesn’t make sense to use this in most cases.   But wouldn’t it be cool if you could call myQuery.iterator() or myStruct.iterator() and have the same functionality?  Or even better, why not have those collections all extend an iterator class so that it could be simplified even futher with myQuery.hasNext() or myStruct.hasNext().

Keep in mind, this discussion is only coming from the perspective of the programming interface itself, and I am not going to get into the differences behind the scenes of how a query result is actually a set of arrays of columns, or how an array differs from a struct.  My point is simply that if we have these abstractions, it sure would be cool if they were consistent, but we were still able to call specific functions on them like ArrayFind() , StructDelete(), etc.  If we had this ability, our loops in CFSCRIPT would be a lot more consistent as well.

With that said…. I will leave you with my wishful implementation for writing loops in CFML:

<cfloop condition="#myQuery.hasNext()#">
	<cfset thisRow = myQuery.next() />
	<cfset doStuff() />
</cfloop>

<cfloop condition="#myArray.hasNext()#">
	<cfset thisItem = myArray.next() />
	<cfset doStuff() />
</cfloop>

<cfloop condition="#myStruct.hasNext()#">
	<cfset thisItem = myStruct.next() />
	<cfset doStuff() />
</cfloop>

Piecing together optional view elements at runtime with Mach-II

Often in web development, you run across a case where there is a display that contains optional view elements that are derived at runtime.  Perhaps there is a section of a form that is only available to residents of the US.  Maybe, only users with a certain level of group access have the ability to see a section of a page that can be partially viewed by other user types.  I am sure you can think of numerous cases that you have come across in your own work.   In a current project at our company, one of our developers was tasked with rewriting an old piece of legacy code in which logged in agents can select one or more of multiple reports to display.  The legacy code was a complete nightmare that would probably be worthy of an entire series of what not to do, but that is for another day!  To boil this piece down a bit, essentially the agent has a series of checkboxes of specific reports, and “to” and “from” date inputs to provide a date range.   Depending on what the agent selects, the submission page might show a single report, or a series of several reports in line.

One approach to this would be to have an event defined in which you compile each piece of data into some kind of data collection

if ( [Report1 was selected] )
     get data for Report1
if  ( [Report2 was selected] )
     get data for Report2
(... and so on...)

Then on the view, you could do something like:

if ([we have report data for Report1])
show Report1
if ([we have report data for Report2])
     show Report2
(... and so on ....)

Well, we could but it would be wrong!  Why?  For one thing, we now have conditional logic about each report built into multiple places in our application.  From a complexity and maintenance standpoint, you have just made it, at a minimum, twice as complex as it needs to be.  There is also a strong argument that could be made (and I would make it!) that your view shouldn’t be responsible for determining what it’s supposed to display.  It should simply display!

So what is another approach to this?  How could we employ MVC techniques without the individual components involved becoming intertwined, creating yet another administrative issue.  Here is the solution that I proposed to our developer:
First, let’s start with our form, which is remarkably simple:

<h3>Select the reports you would like to view</h3>
<cfoutput>
<form name="reportform" action="#buildUrl( "viewreports" )#" method="post">
<input type="checkbox" name="reportList" value="report1" /> Report 1<br />
<input type="checkbox" name="reportList" value="report2" /> Report 2<br />
<input type="checkbox" name="reportList" value="report3" /> Report 3<br />
<input type="checkbox" name="reportList" value="report4" /> Report 4<br />
<input type="checkbox" name="reportList" value="report5" /> Report 5<br />
<input type="submit" value="run reports">
</form>
</cfoutput>

As you can see, we are going to load up an event-arg on the submission named “reportList” that will be a comma separated list of reports that we will be displaying. For instance, if we make the selections you see below, on the viewreports event, event.getArg( “reportList” ) will be: report1,report3,report5

Report Selection form output

I decided that generally I wanted it to behave with a flow like this:

Reports flow diagram

It is a good goal in application development to move as much specific knowledge of the flow of the application outside of any component (view, service, or otherwise) that is not responsible for it to avoid coupling issues.  For instance, our report display page shouldn’t understand flow should it?   (hint: “no“)   Our service layer that is responsible for retrieving data shouldn’t should it? (you guessed it: “no”)

So where does that responsibility lie?  I place it squarely on the front controller framework at hand, namely Mach-II in this case.

If that is the approach we are going to follow, then how can we have freely operating pieces, and create a composite view without the individual pieces having any knowledge of each other, nor any knowledge of their role in the bigger picture?   We achieve this by creating small encapsulated pieces that are individually responsible for their limited role, and count on our framework to do the rest.

If you look at the flow diagram above, you will see that we start with a conditional statement on our submission event: “Has form output been generated?”

In our Mach-II configuration we can achieve this by doing the following:

<event-handler event="viewreports" access="public">
 <event-mapping event="noData" mapping="multi_report1" />
 <filter name="checkForReportData" />
 <view-page name="selectreports" contentArg="form" />
 <view-page name="reports" />
</event-handler>

Let’s talk about what those pieces are doing.  First, we are defining an event-mapping “noData“.  What this means is that anywhere further in this event, if someone announces “noData“, the event that we are really going to announce is “multi_report1“.   By doing this, we don’t bury specific knowledge into our component responsible for the announcing, but more on that in a moment.  Next, we are calling a filter named checkForReportData. Filters are Mach-II components that contain a single public method filterEvent() which returns a boolean value telling Mach-II whether or not it should continue further within this event.  In the code above, if the filter returns “false”, the <view-page/> nodes will not be processed.   So let’s take a look at the filterEvent() method.

<cffunction name="filterEvent" access="public" returntype="boolean" output="false" hint="I am invoked by the Mach II framework.">
     <cfargument name="event" type="MachII.framework.Event" required="true" hint="I am the current event object created by the Mach II framework." />
     <cfargument name="eventContext" type="MachII.framework.EventContext" required="true" hint="I am the current event context object created by the Mach II framework." />

     <cfset var result = event.isArgDefined( "reportOutput" ) />

     <cfif NOT result>
          <cfset announceEvent( "noData", event.getArgs() ) />
     </cfif>

     <cfreturn result />
</cffunction>

Very simply, we are saying: Is there an event-arg named reportOutput defined? If there is, we are returning true, telling the event to continue.  If not we are going to announce an event noData, and returning false.   By announcing a generic event named “noData”, and then defining what “noData” means in the XML config, we have just insulated this filter from change.  For instance, right now the <event-mapping/> says that this means that we should announce “multi_report1“.  If this ever changes to another report, then we only have to change the config.  Additionally, we might be able to repurpose this filter another way in the future and announce a completely different event by using a different event-mapping.

So in our example, we have no reportOutput on our first pass through this method, so we are being rerouted to the event “multi_report1“.  Here is what it looks like:

<event-handler event="multi_report1" access="private">
     <event-arg name="reportName" value="report1" />
     <event-mapping event="nextEvent" mapping="multi_report2" />
     <filter name="checkIncludeReport" />
     <notify listener="ReportListener" method="getData" resultArg="data" />
     <view-page name="reports.report1" contentArg="reportOutput" append="true" />
     <announce event="nextEvent" copyEventArgs="true" />
 </event-handler>

On the second line, all we are doing is defining an event-arg named “reportName” and assigning a value of “report1“.   We will be using this value in a moment.  Before we get to that, and now that you understand what event-mappings are doing, the third line should be clear.  We are just telling Mach-II “if someone or something announces nextEvent within the context of this event, announce multi_report2 instead“.  Again this allows our components to announce generic events which are explicitly defined in the config.   Next, we are calling a filter to see if report1 has been selected in the form by calling a filter named checkIncludeReport.   If the report was not selected in the form, we will kick out and announce nextEvent aka multi_report2.   However, if the report is included, we will continue down the line calling a method on our listener to retrieve data, and then using that data in a view named “reports.report1“.  We take that generated HTML and append it into an event-arg named “reportOutput“.   If you look at our code above, you will be reminded that this is the argument we were testing for in the checkForReportData filter.   Here is a look at our checkIncludedReport filter which makes the decision to include this report or not.

<cffunction name="filterEvent" access="public" returntype="boolean" output="false" hint="I am invoked by the Mach II framework.">
     <cfargument name="event" type="MachII.framework.Event" required="true" hint="I am the current event object created by the Mach II framework." />
     <cfargument name="eventContext" type="MachII.framework.EventContext" required="true" hint="I am the current event context object created by the Mach II framework." />    

     <cfset var result = ListFindNoCase( event.getArg( "reportList" ), event.getArg( "reportName" ) ) />

     <cfif NOT result>
          <cfset announceEvent( "nextEvent", event.getArgs() ) />
     </cfif>

     <cfreturn result />   
</cffunction>

All this filter is doing is checking in the event-arg reportList, which is a comma separated list of reports, and seeing if the value of event-arg reportName (which was defined on line 2 above) exists in the list.  Based on our example of selecting reports 1, 3, and 5, the plain English translation of this comparison is:  If the list “report1,report3,report5″ contains “report1″, return true, otherwise announce “nextEvent” and return false. As you surely know by now, in this case “nextEvent” translates to “multi_report2

Essentially we just repeat this exact pattern for the next 4 events, with a minor change in the last event:

<event-handler event="multi_report2" access="private">
    <event-arg name="reportName" value="report2" />
    <event-mapping event="nextEvent" mapping="multi_report3" />
    <filter name="checkIncludeReport" />
    <notify listener="ReportListener" method="getData" resultArg="data" />
    <view-page name="reports.report2" contentArg="reportOutput" append="true" />
    <announce event="nextEvent" copyEventArgs="true" />
</event-handler>

<event-handler event="multi_report3" access="private">
     <event-arg name="reportName" value="report3" />
     <event-mapping event="nextEvent" mapping="multi_report4" />
     <filter name="checkIncludeReport" />
     <notify listener="ReportListener" method="getData" resultArg="data" />
     <view-page name="reports.report3" contentArg="reportOutput" append="true" />
     <announce event="nextEvent" copyEventArgs="true" />
</event-handler>

<event-handler event="multi_report4" access="private">
     <event-arg name="reportName" value="report4" />
     <event-mapping event="nextEvent" mapping="multi_report5" />
     <filter name="checkIncludeReport" />
     <notify listener="ReportListener" method="getData" resultArg="data" />
     <view-page name="reports.report4" contentArg="reportOutput" append="true" />
     <announce event="nextEvent" copyEventArgs="true" />
</event-handler>

<event-handler event="multi_report5" access="private">
     <event-arg name="reportName" value="report5" />
     <event-mapping event="nextEvent" mapping="viewreports" />
     <filter name="checkIncludeReport" />
     <notify listener="ReportListener" method="getData" resultArg="data" />
     <view-page name="reports.report5" contentArg="reportOutput" append="true" />
     <announce event="nextEvent" copyEventArgs="true" />
</event-handler>

As I mentioned, there is a slight change in multi_report5 in that nextEvent is defined as “viewreports“.   By doing this, we have then ended our report generation and are redirecting the flow back to the initial event that kicked this process off.  Since we now have reportOutput data, we are directed to the page that ouputs it all.  Quite simply, our big, massive, magnificent multi-form display page looks like this:

these are the reports:

<cfoutput>#event.getArg( "reportOutput" )#</cfoutput>

There is no conditional nonsense, and the view simply outputs all of the generated output that was appended into the event-arg reportOutput.   Additionally, if you reflect on the things we have done, no where are we explicitly saying “if the user selected report1, do something“.  We have left it all fairly generic and hopefully have created some potentially reusable components.    For instance, let’s say that we now have a requirement for an event that only displays report2. No problem!  All we need to do is add an additional event like this:

<event-handler event="report2" access="public">
     <notify listener="ReportListener" method="getData" resultArg="data" />
     <view-page name="reports.report2" />
</event-handler>

Easy, huh!

Lastly,  I know that some of the more astute of you may have noticed a fatal flaw in the design above.  What happens when no reports are selected?   In the interest of keeping this example as stripped down as I could, I let that one go, but it is a very simple fix.  What would you do?  Where would you put it?   Feel free to post your fix in the comments, along with any other thoughts you have on this solution.

download fully-functional example files – NOTE: doesn’t include the Mach-II framework

Creating, Modifying, Deleting with Reactor

In my last entry, I gave a showed how easy Reactor makes accessing records in your database.  Even if that was all Reactor did I think it would be a great tool.  However, as one would expect, you can do much more.  In this entry, I will show you how to use Reactor to create new records, modify data in those records.

Let say we are again working with a UserAccount record, like our example from last time.  Our UserAccount object has the following properties: UserId PK, UserName, Password, Email, DateRegistered (and of course our table as these columns as well).

The first thing we need to do is create an instance of the Reactor Factory.  As I mentioned last time, this should probably be done in the application scope or something similar so you don’t have to continually reinitialize it.  Let’s go ahead and put that in our application scope:

<cfscript>
// create an instance of Reactor
application.Reactor = CreateObject(“Component”,        “reactor.reactorFactory”).init(expandPath(“/config/reactor.xml”));
</cfscript>

Once that is instantiated, anytime we need it we can copy it into the local scope of the template we are working on.   So, let’s create a new UserAccount object in our system and populate it with our new user ‘dshuck’.

<cfscript>
// create local instance of the Reactor Factor
Reactor = application.Reactor;

// create a new empty instance of our UserAccount object
UserAccount = Reactor.createRecord(“UserAccount”);

// now populate the UserAccount object properties
UserAccount.setUserName(“dshuck”);
UserAccount.setPassword(“secret”);
UserAccount.setEmail(“dshuck@gmail.com”);
UserAccount.setDateRegistered(createODBCdatetime(now()));
</cfscript>

So now we have a UserAccount object that whose properties hold the data that we added.  If you wanted to at this point you could output something such as the username by:

<cfoutput>#UserAccount.getUserName()#</cfoutput>

If you look in the database at this point you will notice something though.  There is no ‘dshuck’ user in the UserAccount table.   The reason is that the UserAccount is currently held in memory, but we need to actually persist it to the database.  To do so, we call the save() method in the UserAccount object like so:

<cfscript>
UserAccount.save();
</cfscript>

Now if you look in the table you will see our new record.  Pretty cool!

<infomercialAnnouncerVoice>
But wait… there’s more!
</infomercialAnnouncerVoice>

Often times when we do some sort of insert action like this, we need to do something with the primary key value that is created on insert.  Typically this is done with some sort of “SELECT max(UserId) …” query, or doing a “SELECT @@identity….” via a stored procedure.  Well as you can probably guess, Reactor makes this easy as well.  How do you do it?

You already did!

Your UserAccount object already holds the new UserId value, so if you wanted to access it, you could call it like this:

<cfoutput>#UserAccount.getUserId()#</cfoutput>

By this point you should be able to plainly see the ease and speed at which you can not only read, but create new records as well.

So say we are now on a new page and would like to change ‘dshuck’s password to something tricky like “password”.  First we would need to load the record.  Once it is loaded we would alter just the password property, then save the document.

Here it is in code, and let’s assume the UserId PK value of UserAccount is 3:

<cfscript>
// create local instance of the Reactor Factor
Reactor = application.Reactor;

// load the user ‘dshuck’ as an instance of the UserAccount class
UserAccount = Reactor.createRecord(“UserAccount”).load(UserId=3);

// change the password
UserAccount.setPassword(“password”);

// persist it to the database
UserAccount.save();
</cfscript>

So there you go.  Altering property state in 4 lines.  Of course, if you are familiar with OOP in general this isn’t exceptionally groundbreaking.  What is groundbreaking however is that still, we have not coded a single CFC.  That is just ridiculous!

Well, the time has come to say goodbye to our friend ‘dshuck’.  Let’s say, we are now on a new page and have been passed ‘dshuck’s UserId.  As I am sure you are guessing by now, we are not in for an enormous workload to make that happen.   So…  here we go:

<cfscript>
// create local instance of the Reactor Factor
Reactor = application.Reactor
Reactor.createGateway(“UserAccount”).delete(UserId=3);
</cfscript>

And with that we have now Created, Modified, and Delete with ease.  Reactor has many more features, including Data Gateways to access multiple records and return query record sets.  I am still just scratching the surface myself, but what I have seen so far has been very impressive.

I am sure this won’t be the last time you hear me talk about it!

Maddening server problem… Resolved!!!

Starting about 4-5 days ago, one of our web servers began experiencing nightmarish performance issues. This was such a strange problem to deal with, and I wasn’t able to Google anyone having the same issue, so I thought that I better document this in hopes that it may serve someone in the future.

Our ColdFusion MX 6.1 server would run for a while, the suddenly requests would just stop returning results at all. Occasionally a user would finally get a response to the browser that was a ‘<’ sign, followed by varying strings of seemingly random characters. It would hang like this failing to answer any future requests until the services were restarted. This system has some obvious suspect trouble points. I will list a few:

  • About 120 remote (and I mean very remote) DBs, and 1 DB on the same subnet. Those remote databases are housed in various data centers and client sites.
  • Of the remote DBs, over half are using and ODBC Socket to Sybase (Yes, JConnect is a much better idea and is in the works).
  • Of the remote DBs, a handful of them are using VPN connections.
  • A proprietary framework has been used on a couple of the applications on the server that has a number of examples of questionable coding practice in it.
  • A new company was recently migrated to the proprietary framework application that has 40,000 users.
  • Client variables were being used and were being stored in the registry.

I viewed all of the above as places that could potentially have some type of effect on the problem we were experiencing.

Any guesses so far?

Well for starters we had Fusion Reactor on the system.  We had it set to notify us when a request took longer than 30 seconds, and kill that request gracefully.  There was no consistency whatsoever to the pages in the requests that it as notifying us on.  Even simple ‘welcome’ pages would be in that list.  This didn’t appear to be helping us on this problem so to isolate the issue I removed it from the server.

We finally got on the phone with Macromedia…errr.. Adobe and dealt with Ted Zimmerman (and later Swathi C.), who went out of his way to help us and even stayed well after he was supposed to have left for the day. One of the first things we did was to change the Client Variables setting in CF Admin so that ColdFusion stored client variables in a DB rather than the registry. Also since we have no real need for persisting client variables through multiple sessions, we set ColdFusion to store them for 1 day as opposed to the 90 day default. This didn’t seem to fix anything and if anything our problem just seemed worse and worse. Instead of the uptime for 10-20 minutes after restarting, if anything, it seemed to be bombing more and more rapidly byt he minute.

With Ted’s assistance, I learned a great new technique for viewing threads at a very low level and even being able to track process IDs over a period of time and seeing if they had hung at a particular memory address. We did this by running ColdFusion from the console and outputting to a text file. That is a very interesting and informative exercise!

We kept finding a process that was talking to a remote database that seemed to be just hanging indefinitely. I did some better trapping on that page by not only catching database exceptions and logging them, but by adding in a timeout to the query, which is something I have not typically done in my code. I found that by doing this I no longer saw that particular page in the metrics information as a trouble maker, but our greater problem still existed. Now, in our thread stack dumps, another page seemed to show up pretty frequently. I put the same traps around that process and still remained in the same boat. That said, even though we didn’t fix the problem with that, we did fix a couple of issues that were definitely in need of attention.

So, any guesses yet?
I need to preface this next section by stating that the way that this company is structured and due to the fact that the systems store extremely sensitive data related to both loan origination and loan servicing, I do not have any direct access to the production system under normal circumstances, and even in this circumstance, I have no access to the database server at all. (Ironic when you consider the power of code and how counterproductive this approach can be, but I will save that for another discussion).

That said, in the middle of all of this work, we received an email from our network center that the SQL Server had reported an error notification of less than 1% of memory available. I assumed that could have just been a weird spike that had happened, but nonetheless out tech group looked into it immediately. We found that our SQL Server was showing that it had 16MB of available physical memory. As if that wasn’t a big enough “Uh Oh!”, we were told that it only had 300MB of space available on the C drive which (against my wishes) is where the virtual memory resides.

Upon learning this, our network tech immediately robbed memory from an internal development server and drove down to our NOC to upgrade the memory. Immediately the system went back to normal and has been blazing ever since!

It was interesting thinking back to when we altered the client variables setting. The change didn’t seem obvious at the moment, but in hindsight, it definitely exasperated the problem, although it was clearly the right thing to do.

So, now… 18 hours after the memory upgrade, things are still rocking!

Fusion Reactor

We purchased a copy of Fusion Reactor last week and today I had my first chance to use it.   Right off the bat I was extremely impressed.  The GUI is very similar to the ColdFusion Administrator and is very easy to get around.

Within the past couple of weeks our production server started experiencing some strange performance issues.  We have been seeing some frequent occurrences where the request times would just get longer and longer, and eventually stop responding all together.  There are about 6 high volume applications on that server and  try as we might, we were never able to reproduce this in a controlled way and point to any particular point that was breaking.

Within about 5 minutes of installing Fusion Reactor, I was able to pinpoint a specific piece of code that was in desperate need of refactoring.    Until I could get the piece of code in place, I was able to set Crash Protection in Fusion Reactor which would gracefully kill any troublesome request if it took longer than X seconds.  This worked really flawlessly and put the application right back on its feet again when it stumbled into the code.

Fusion Reactor includes many very useful features including:

  • Requests
    • View currently running requests, including the ability to see stack traces and to kill specific threads.
    • View request history
  • Threads view current threads
  • Rich memory charts/graphs
  • Request Metrics reporting
  • Filters – This is a cool feature.  You can have Fusion Reactor constantly look for specific strings and alter them at request time if you wish.  A useful time for this might be a discovery of a typo, or a moved url in a link.
  • Crash Protection – It approaches this from several different angles.  You can set it for specific request issues, server memory levels, etc.
  • There are several more features but those are the ones that stuck in my memory after 20 minutes or so of using it.

Keep in mind the standard version of this is only $99.00 which is very undervalued in my opinion.

Head First Design Meditations

OK, not to beat a dead horse, but since we are on the subject I found a great link.  The Head First team is at it again.   To quote their site:

…this time we’re working on a creative design card deck for the software world. Designed to be used as a brainstorming and inspiration tool, the card deck will contain small bits of software design wisdom, insights, idioms, inspiring quotes and perhaps even a chuckle or two.

The are also accepting submissions, if you have a piece of wisdom you would like to share.

The first one I clicked was:

Computers allow you to make more mistakes faster than any other invention in human history with the possible exception of handguns and tequila.
– Mitch Ratcliffe

Awesome!

Helms and Peters Outloud

Anyone who knows me knows that I have a lot of respect for Hal Helms.  I took a 1 day class by him at CFUnited this year (his Domain Model OO class) and walked away with my head spinning with all the ideas that I had just tapped into.  His class is part code, part programming theory and part Hal’s philosophy.  Since then anyone who has asked me about training has received a reply “If you can find a class by Hal Helms.  You won’t regret it.”

Well, now you can for free!  (sort of..)  OK, not really a class, but using the new podcast audio series Helms and Peters Outloud, you can get a taste of what his class offers.  In fact, the first one that I listened to “Design Patterns” covered some of the subjects we covered in class, and included an excercise (employees… hourly vs. salary) that we did in class.  FYI, our group in class actually got it right    The left side of the room didn’t fair so well. :)

They don’t seem to have a full library listed on their site www.helmsandpeters.com, but there is an RSS podcast feed that lists several of their sessions.   Considering I have a 1 hour commute to work, I don’t think it will take me long to get through the ones that are there.  I hope more are on the way soon!

Loading… Loading…. Loading… 0 How to Make Friendly URLs

What are friendly URLs?

The idea is instead of something like this in the address bar:

http://www.yourunfriendlysite.com/index.cfm?event=someAction

you have something a) perhaps more easily remembered, and b) easier on the eyes like this:

http://www.yourfriendlysite.com/someAction/

Pete Frietag has written a great example of how to use friendly urls in your application. Expecially for those of us that use frameworks such as Mach-ii, Fusebox, Model Glue, etc., this is a nice touch to add to an application.

ColdFusion moving on up!

Sean Corfield blogged about a new rating that places ColdFusion as the 19th most popular language.

This comes from the TIOBE Programming Community Index for November 2005.  Now on the surface, 19th doesn’t sound exceptionally impressive, but this isn’t “web language”, this is all languages as defined by the Wikipedia definition for “language”.  The most notable thing to me is how dramatically it has jumped (note the green arrows).  It think that is far more significant than the position itself.

Java J2EE vs. Microsoft .NET

What follows is a very high level view article that I put together for my boss to answer clients’ questions of “What is the difference between Java and .Net”. Much of this article is borrowed from several sources, of which I listed the sites at the bottom.  By no means is this an end-all-be-all definitive guide, but is intented to be more of an unbiased guide for someone at a management level.  It is an open document and I completely welcome input on where I could have made it better or more accurate.


  • Java (J2EE) – When people speak of Java web applications, they are referring to “The Java 2 Platform, Enterprise Edition (J2EE)”.  It was designed to simplify complex problems with the development, deployment, and management of multi-tier enterprise solutions. J2EE is not a product, but rather an industry standard, and is the result of a large industry initiative led by Sun Microsystems.    Since it is a platform rather than a product, you cannot just “download J2EE”.  Instead, one needs to choose one of the many J2EE platforms to run on, and install that on top of the Java JDK (several implementations include the JDK).

    There are a number of widely used open source J2EE runtime environments, in addition to some of the purchasable solutions such as Websphere, BEA, Oracle, etc.  Once installed, the various implementations all perform virtually the same from functionality to speed, whether open source or “paid version”.

  • .NET – Per their marketing documents, Microsoft.NET8 is product suite that enables organizations to build smart, enterprise-class web services. Note the important difference between the two: .NET is a product strategy, whereas J2EE is a standard to which products are written.

    Microsoft.NET is largely a rewrite of Windows DNA, which was Microsoft’s previous platform for developing enterprise applications. Windows DNA includes many proven technologies that are in production today, including Microsoft Transaction Server (MTS) and COM+, Microsoft Message Queue (MSMQ), and the Microsoft SQL Server database. The.NET Framework more or less replaces these technologies, and includes a web services layer as well as improved language support.

    Microsoft.NET offers language-independence and language-interoperability. This is one of the most differing and fundamental aspects of the .NET platform. A single .NET component can be written, for example, partially in VB.NET, the .NET version of Visual Basic, and C#.

  • Application layer - An application under either architecture is hosted within a container, which provides qualities of service necessary for enterprise applications, such as transactions, security, and persistence services.   From a high level view and without talking about specific components, application flow under both architectures virtually the same.
  • Business logic layer – The differences really begin to show at this layer.  This layer performs business processing and data logic.

o        J2EE – In large-scale J2EE applications, business logic is built using Enterprise JavaBeans (EJB) components. This layer performs business processing and data logic. It connects to databases using Java Database Connectivity (JDBC) or SQL/J, or existing systems using the Java Connector Architecture (JCA). It can also connect to business partners (B2B) using web services technologies (SOAP, UDDI, WSDL, ebXML) through the Java APIs for XML (the JAX APIs).

o        Microsoft .NET – The business layer of the .NET application is built using .NET managed components. This layer performs business processing and data logic. It connects to databases using Active Data Objects (ADO.NET) and existing systems using services provided by Microsoft Host Integration Server 2000, such as the COM Transaction Integrator (COM TI). It can also connect to business partners using web services technologies (SOAP, UDDI, WSDL).

  • Client layer

o        J2EE – ‘Thick’ clients such as applets or applications connect directly to the EJB layer through the Internet Inter-ORB Protocol (IIOP) rather than web services, since generally the thick clients are written by the same organization that authored J2EE application, and therefore there is no need for XML-based web service collaboration.

Web browsers and wireless devices connect to JavaServer Pages (JSPs) which can render user interfaces in HTML, XHTML, or WML.

o        Microsoft .NET – ‘Thick’ clients, web browsers, wireless devices connect to Active Server Pages (ASP.NET) which render user interfaces in HTML, XHTML, or WML. Heavyweight user interfaces can be built using Windows Forms.

  • Lanquage Support

o        J2EE - J2EE promotes Java-centric computing, and as such all components deployed into a J2EE deployment (such as EJB components and servlets) must be written in the Java language. There can be kind of a middle-ware layer of languages such as ColdFusion that produce Java byte code for runtime, but the end result must be Java language.  Other languages can be bridged into a J2EE solution through web services, CORBA, JNI, or the JCA, as previously mentioned.

o        Microsoft .NET –.NET supports development in any language that Microsoft’s tools support. With the exception of Java, all major languages will be supported. Microsoft’s C# language is most closely equivalent (with the exception of portability) to Java and is also available as a programming language within the Visual Studio.NET environment.  Due to the inter-language support, a single .NET component can therefore be written in several languages.

  • Tools
    • J2EE – The Sun J2EE Product Portfolio includes Forte, a modular and extensible Java-based IDE that pre-dates both Sun J2EE and .NET. Developers who prefer other IDEs for Java development are free to use WebGain’s Visual Caf’, IBM’s VisualAge for Java, Borland’s JBuilder, and more. Numerous 3rd party tools and open source-code products are available.  One of the more recent strong contenders is Eclipse, which is an open source IDE that has plug-ins for many languages, including ColdFusion, XML, UML, HTML.
    • Microsoft .NET – Microsoft has always been a strong tools vendor, and that has not changed. As part of its launch of .NET, Microsoft released a beta version of the Visual Studio.NET integrated development environment. Visual Studio.NET supports all languages supported by earlier releases of Visual Studio – with the notable exception of Java. In its place, the IDE supports C#.
  • Performance

o        This was by far the toughest area to research.   Most of the published benchmark testing reports have been produced by either Sun or Microsoft.  Both claim the other has inadequately represented their product and has countered with later benchmarks trying to prove their stance.  One of the reasons is that it is very difficult to create side by side comparisons.  A fair benchmark is largely based on running identical code which is somewhat impossible.  It is fair to say that under both platforms there are huge systems running with spectacular performance.  Java can boast sites such as ebay.com.  .NET can boast sites such as. Microsoft also boasts of high volume sites such as its own microsoft.com.   The bottom line is, with good programming, neither platform really owns this category.

Some basic bullet points –

  • Arguments supporting both platforms

o        Regardless of which platform you pick, new developers will need to be trained (Java training for J2EE, OO training for .NET)

o        You can build web services today using both platforms

o        Both platforms offer a low system cost, such as jBoss/Linux/Cobalt for J2EE, or Windows/Win32 hardware for .NET.

o        Both platforms offer a single-vendor solution.

o        The scalability of both solutions is theoretically unlimited.

  • Arguments for .NET and against J2EE

o        .NET has a dedicated team at Microsoft’s marketing it

o        .NET has a powerful tool set with Visual Studio.NET

o        .NET has a somewhat simpler programming model, enabling rank-and-file developers to be productive without creating dangers.  Java is argued to have more flexibility, but due to this, there is some lack of protection from yourself from a coding standpoint.

o        .NET gives you language neutrality when developing new eBusiness applications, whereas J2EE makes you treat other languages as separate applications

o        .NET benefits from being strongly interweaved with the underlying operating system

  • Arguments for J2EE and against .NET

o        J2EE is being marketed by an entire industry

o        J2EE is a more mature platform, with a few new web services APIs. .NET is comparatively still somewhat new and introduces risk as with any early-generation technology, although this is much less of an argument since the 2.0 release.

o        .NET web services are not interoperable with current industry standards. Their BizTalk framework has proprietary SOAP extensions and does not support ebXML. This is a major drawback against .NET when using web services.

o        J2EE gives you platform neutrality, including Windows. You also get good (but not free) portability.

o        J2EE lets you use any operating system you prefer, such as Windows, UNIX, or mainframe. Developers can use the environment they are most productive in.

Summary

Both J2EE and .NET are fierce contenders in the world of application development and will almost certainly be battling for the top spot through the next decade.  There will be literally millions of applications built under each of them in the future, and neither one is particularly “better” than the other.  From a cost standpoint, .NET ships with Windows, so there is no additional cost there beyond the operating system. There are also widely used open source J2EE implementations so no cost argument there either.  From a performance difference, both appear to play on a fairly level field.  There are great programming IDEs available for both, and both have high traffic implementations as proven success stories. Arguments can be made for either system, but the one strong winning point in J2EE’s favor is platform independence.

Article resources: www.theserverside.com, www.netcraft.com, www.microsoft.com, www.sun.com, www.godotnet.com, www.javaworld.com