Saturday, March 10, 2012

Some Nifty Adobe ColdFusion 10 Language Changes

I am reviewing the Adobe ColdFusion 10 beta documentation and wanted to point out a few small things I was happy to see.  These might not be earth shattering to you, but these will affect the way I write code so I felt it was worth sharing.

Thing 1: for - in syntax for looping queries
ColdFusion 10 (codenamed Zeus) allows us to loop over queries in cfscript using for - in syntax.  This syntax was previously used for looping keys in a structure.  I had previously mentioned this in my post Dear ColdFusion Team: Please fix this stuff in Zeus as number 4.  Maybe someone read my blog, or maybe it just made that much sense ;)

The new syntax looks like this:
for ( row in myQuery ) {
  fullname = row.fName & row.lName;
}

How does this affect my code?
It makes life so much easier and code so much more elegant (as ColdFusion is meant to be), especially when you consider the code we write in ColdFusion 9:
for ( var x=1;x<=myQuery.recordCount;x=x+1 ) {
  fullname = myQuery.fName[x] & myQuery.lName[x];
}

It also feels more like the tag based alternative, which I generally think is a good thing.

Thing 2: Retrieve Application Metadata
ColdFusion 10 adds a new function called getApplicationMetadata().  This is pretty cool as when you dump out the application scope you generally only see application variables, with the exception of the application name.  The new getApplicationMetadata() function provides insight into the application settings themselves.

getApplicationMetadata() output


How does this affect my code?
I can see several uses for this, but the first one that immediately jumps to mind is that I can sync us my JavaScript timeouts with my session timeouts.  That alone is pretty big to me, since I write HTML/Ajax apps.  This helps with pingback and timeout redirection.  Some of this I can also see being really helpful for framework and 3rd party app developers.

Thing 3: onAbort Application method
Although I now have to change my ColdFusion Application.cfc presentations to stop saying 7 application events ;), I'm happy that this function has been added.  What it does is allows you to process the results when you use <cfabort> or abort;.  The new application event handler is placed in your Application.cfc file.  Here is the syntax, directly from the documentation:
<cffunction name="onAbort"
access="public"
returntype="void"
hint="Hanldes Aborted request">
<cfargument type="String"
name="targetPage"
required=true/>
<cfoutput> Target Page: #targetPage#</cfoutput>
<!--- do stuff --->
</cffunction>
}

How will this affect my code?
To be honest, I haven't decided yet.  I imagine I will be able to start using <cfabort> for more than just debugging, but it hasn't quite solidified in my mind yet.

Thing 4: Ability to slice arrays
I don't need to go too in depth on this one.  It's something that I can do in other languages and is an important part of array handling.  Here is the syntax, right from the docs:
<cfscript>
array = [1, 2, 3, 4, 5, 6, 7, 8];
newArray = arraySlice(array, 2, 3);//returns 2,3,4
newArray = arraySlice(array, 4);//returns 4,5,6, 7, 8
newArray = arraySlice(array, -5, 3);//returns 4,5,6
</cfscript>

How will this affect my code?
Easy, I won't have to finagle my arrays.  We now have a language construct to slice.

Thing 5: Method chaining in CFCs
Since ColdFusion 6 we have been able to method chain to a certain degree.  This worked across multiple CFCs (following the object's composition):
employeeName = company.getEmployee().getFullname();

This should not be confused with the new method chaining within beans, which specifically affects mutator methods in our objects (if we have accessors explicitly defined of accessors="true" in our CFC definition).  Check this out:
employee.setName('Bob').setDepartment('Accounting').setFavoriteColor('Green');

How will this affect my code?
The jury is still out on this one for me.  I use method chaining like this in jQuery often.  I personally think it makes my code more legible.  It is certainly shorter code, but is it really better?  Not sure.

Thing 6: Structures now support colon notation
#1 on my Dear ColdFusion Team: Please fix this stuff in Zeus list, I am certainly happy to see this.  Railo already did this, as did other ECMAScript based languages such as JavaScript and ActionScript.
st = {
  name: 'Richard',
  age: 44,
  favoriteColor: 'Purple'
}

Again CF team, thanks for listening!

How will this affect my code?
Easy. I won't have to troubleshoot stupid errors when I move from language to language any more.  I can always use the same accepted syntax!

Other stuff
There are plenty of other language changes in ColdFusion 10.  Just because I don't use them doesn't mean they aren't great.  I encourage you to comment on some that will change the way you code.  I bet the directoryCopy() function and <cffile> improvements alone have people thinking in new ways.  Oh, and closures.

Friday, March 9, 2012

Railo, Tomcat and Apache on Amazon EC2 Ubuntu AMI

About this guide

This guide is long because I wanted to give some background info so you can not only install Railo on an Amazon Ubuntu AMI, but so that you understand why you are doing what you are doing.  If you want to see how easy this is, just skip from gray block to gray block below.  There are 13 actual commands to install Apache, Railo and Tomcat.  There are 7 lines of configuration to paste into two config files.  It doesn't get much easier.

Background on why I tried Railo in the cloud
So I've been very fortunate in my life to have been a ColdFusion developer, Ajax developer, Adobe trainer, digital media developer, Flex developer, product manager, project manager, task lead, agile trainer and Director in my short life, which has put me in the unique position to be contacted by start ups for advice on how to get going quickly using an Agile methodology.  The exciting part about this skill set is I get to help them plan their technology stack, development and deployment methods including Scrum, continuous integration, continuous builds, etc.

Now, one thing I am always sure to say is that ColdFusion should be a part of that technology stack.  See, I use ColdFusion differently than many of you.  I use it almost exclusively as a service tier.  I use Flex or HTML/Ajax for my interaction tier and ColdFusion only exists to proxy information and handle my business logic.  ColdFusion does an awesome job of this and with most start ups needing to get to minimum viable product quickly with low overhead, ColdFusion just makes sense.

Over the past 5 years I've had the luxury of working with one start up where money was no problem.  They had the best of everything from a custom data center to 5 rows of racks filled with servers and all the software and processing power we could ever need.  That has happened exactly ONE time.  Most of the start ups I have worked with are cash poor, but idea rich.  The idea that I can take ColdFusion and deploy it to the cloud with virtually no long term commitment is just plain awesome for them.  They can shut down the next day if they had to, or they could add another 10 servers in minutes.  With Amazon I helped take one company from $80k per month in data center costs down to $3k a month.  With savings like that a few Adobe ColdFusion Enterprise licenses were easy to sell.

Recently I have wanted to test Railo in the cloud as an alternative to Adobe ColdFusion for start ups, for no particular reason.  This guide is a result of that test.

Why Amazon
So I chose Amazon as my virtual host for 3 reasons:
  1. Price - can't be beat on processor time cost, and from what I remember, all incoming bandwidth is free.  This is especially important when you are working with digital media.
  2. Familiarity - I've always used Amazon.  The Amazon Management Console makes sense to me and I understand the pricing structure.
  3. Reach - They have data centers all over the place, and Amazon has always been very reliable.
 The fourth reason I could include is more for the start up themselves, but everyone is familiar with Amazon.  Its an easy sell.

Amazon EC2
I've found that Adobe ColdFusion runs great even on Amazon micro instances, depending on load and what sort of application you are running.  I usually will go with a small or large instance though for a service tier.  Amazon recently released medium instances, but I have not been able to test them yet.  For Railo I also deployed to a micro instance, mainly since this was a test.

I'm not going to go through the process of creating a new Amazon EC2 instance, but I will say that I deployed to Ubuntu 11.04 Natty Narwhal.  I am using the 64-bit version.  Your mileage may vary with other versions.

Deploying an instance takes about 5 minutes.  When done you can assign a security group or edit the default.  If this is for a public site you'll probably also want to add an elastic IP (which acts as a static IP for DNS). 

Get Ubuntu Up To Date
The first thing to do is to update Ubuntu
$ sudo apt-get update
$ sudo apt-get upgrade

Apache Installation
Next, we need to get Apache HTTP Server set up.  You technically don't need to use Apache HTTP as Apache Tomcat can serve the pages for you just fine.  I am using Apache HTTP server for familiarity and extensibility.
$ sudo apt-get install apache2

Get Railo
To install Railo I used the Tomcat/Railo installation from getrailo.org.  You may want to have more control over it than I wanted.  I did also deploy Tomcat6 on Ubuntu the one time and downloaded Railo and deployed it manually.  It wasn't hard, but the pre-rolled installer worked just great.  The commands below are for the current version of Railo (3.3.1) at the time of this writing.  I am also using the 64-bit version.  Just go to getrailo.org to grab the latest correct version for your OS.
# get the railo tomcat installer
$ wget http://www.getrailo.org/down.cfm?item=/railo/remote/download/3.3.1.000/tomcat/linux/railo-3.3.1.000-pl1-linux-x64-installer.run -O railo-3.3.1.000-pl1-linux-x64-installer.run
# make it executable
$ sudo chmod +x railo-3.3.1.000-pl1-linux-x64-installer.run
#run the installer
$ sudo ./railo-3.3.1.000-pl1-linux-x64-installer.run

The installer works great on Ubuntu.  All of the defaults should be fine.  The only things I changed were the user Railo was running under (I called it 'railo', the default is 'root' so I suggest you change it) and the default Tomcat port.  The text below is a snapshot of those settings:
Installation Directory [/opt/railo]:
Tomcat Administrator Username [admin]:
Tomcat Administrator Password []: ##########
Tomcat Port [8888]: ####
Tomcat System User [root]: railo
Yes, Start Railo at Boot Time [Y/n]: 
Install Apache Connector? [Y/n]: 
Please choose an option [1] : (Apache 2.2) 
Please choose an option [2] : (64-bit)
Apache Control Script Location [/etc/init.d/apache2]: 
Apache Modules Directory [/usr/lib/apache2/modules]: 
Apache Configuration File [/etc/apache2/apache2.conf]:
Apache Logs Directory [/var/log/apache2]:

Test Railo
Well, you now have Railo installed on Tomcat. Go test Railo by going to http://localhost:8080 and you will see the welcome page.  If you want to test remotely, be sure to add 8080 as an incoming TCP rule in your Amazon security group for the Railo instance.

Connect Railo to Apache
The hardest part for me was getting Railo to work with Apache.  There are lots of guides around, but none simple enough for me.  I'll paste the steps below in one place as that's what I had problems finding.  During installation Apache was configured for the Tomcat connection.  There are a few things we need to do yet.

First, you'll need a few mods to help proxy requests from Apache HTTP to Apache Tomcat.  It is my understanding that this does the same job as the Adobe ColdFusion connector does.  Some of these might already be enabled, but it won't hurt to try adding them.  In older versions of Apache HTTP these would be in your httpd.conf file as includes.  Now, the a2enmod command will symlink the modules from mods-enabled to mods-available.
# add the necessary mods for Apache HTTP Server
$ sudo a2enmod proxy
$ sudo a2enmod proxy_http
$ sudo a2enmod proxy_scgi
$ sudo a2enmod proxy_balancer

If you are familiar with older versions of Apache HTTP, you'll understand we need to create a virtual site.  This virtual site will allow us to define a server name and link it to a host in our Tomcat configuration.  Your virtual host might be more complex than the one below, specifying directory Aliases and custom logs, but what you see below is the minimum.  In older versions of Apache you would be putting these entries into extra/httpd-vhosts.conf which are included in httpd.conf.  That would work as well, but in the current version you create a file in the /etc/apache2/sites-available directory with the name of your site.  The file below, for nictunney.com, is found at /etc/apache2/sites-available/www.nictunney.com.
<VirtualHost *:80>
    ServerAdmin admin@nictunney.com
    DocumentRoot "/var/www"
    ServerName www.nictunney.com
</VirtualHost>

Next, you'll need to enable that site (creating a symlink from sites-enabled to sites-available).  In older versions of Apache you just include the httpd-vhosts file in the main httpd.conf.  In the newer version you use a2ensite.
$ sudo a2ensite www.nictunney.com

Configuring Tomcat
The next thing we have to do is tell Tomcat about the site.  This is done in the server.xml file.  If you are following my instructions this is located at /opt/railo/tomcat/conf/server.xml.  To configure Tomcat you need to add a Host entry.  This needs to be done inside the AJP connector definition (search for '8009' in server.xml).  Make sure to put this inside the Engine definition for Catalina.  I put mine directly under the Host definition for 'localhost'.  Note that the docBase directory matches the directory I defined in my Apache site configuration.  Put yours wherever, but make sure they match.
<Host name="www.nictunney.com" appBase="webapps">
      <Context path="" docBase="/var/www" />
</Host>

Finishing up
All that is left to do is restart Apache HTTP server and Apache Tomcat.
$ sudo service apache2 restart
$ sudo /opt/railo/railo_ctl restart

Caveat
I had fits at this point and was getting 500 errors.  Luckily I have Mark Drew on IM and he found my issue quickly.  Permissions were wacky on my webroot (/var/www) by default.  I had to change them to 766, changed the owner to the railo user and set the group to staff.  You might have differing opinions on how to secure your own web directory.

Initially I had some issues getting Railo up and running as Tomcat was new to me (I deployed to Tomcat), but I figured it was worth learning since Adobe ColdFusion is also coming on a version of Tomcat starting in ColdFusion 10.  The cool thing about deploying to the cloud is I was able to terminate my existing instance and reload from scratch in about 15 minutes.  I did this 4 times.  I love cloud deployment!

I had help
Much thanks goes to Mark Drew for taking them time to sort out my error, letting me see his config files and helping me determine what mods I needed in Apache HTTP server, and to the following folks on Twitter who helped me at various points along the way, being very patient with my Tomcat n00b-ness.  Several of them have tutorials and installation scripts as well, so please give them the proper nod and thanks, in order of appearance:
@talltroym
@azawaza
@gert_railo
@kwispel
@webRat
@bluewebtech
@mikehenke
@mpwoodward
@markdrew

Wednesday, March 7, 2012

Cryptic ColdFusion ORM Hibernate Error

Troubleshooting an old Adobe ColdFusion 9 application of mine today and was getting this error message:
HIBERNATE ERROR - Exception executing batch:
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
Now I also saw an error about a foreign key at one point, but who knows why. The issue was being caused during a value update. The goal is to allow the user to create a new object of type Domain inline. It's simple really, they just enter the Domain text and on the back end I make sure the Domain doesn't already exist before creating the domain and adding it to its parent entity (which they are editing at the time).

Here is the snippet of code that checks to see if the domain already exists, and creates the object if it does not:
// try to load the domain by the passed name
var domain = entityLoad("Domain", {name=arguments.value, project = o.getProject()}, true);
// if it doesn't exist, we need to create a new domain for this project
if ( NOT structKeyExists(local, 'domain') ) {
  domain = entityNew('Domain');
  domain.setName(arguments.value);
  domain.setProject(o.getProject());
  entitySave(domain);
}
o.setDomain(domain);
So what is happening above is we try to load the Domain, if it doesn't exist we create it, set the bi-di relationship to the project and then save it. The Domain is then added to the parent object. When the error is thrown no line number is given (cryptic, remember). So the most curious thing was I had another object type that was working fine. I compared them, and saw the error in my ways. Here is the offending object:
<cfcomponent persistent="true" table="sel_domain"  output="false" entityName='Domain'>
  <!---- properties ---->
  
  <cfproperty name="id" column="id" type="numeric" ormtype="int" fieldtype="id" generator="increment" /> 
  <cfproperty name="name" column="domain" type="string" ormtype="string"  /> 
  <cfproperty name="color" column="color" type="string" ormtype="string" default="" />
  <cfproperty name="project" cfc="com.Project" fkcolumn="projectID" fieldtype="many-to-one" />
  <cfproperty name="entities" singularname="entity" fieldtype="one-to-many" cfc="com.Entity" fkcolumn="domainid"/>  

  <cffunction name="init" returntype="Domain" access="public" output="false" hint="Constructor">
    
    <cfscript>
      setID(-1);
      
    </cfscript>
    
    <cfreturn this />
  </cffunction>
</cfcomponent> 
Can anyone guess what the issue is? If not, don't feel bad. Took me about 20 minutes to figure it out with the stack trace in front of me. I forgot to add the unsavedvalue="-1" to the id field. This can be fixed by adding that attribute, or in the case of the working object, simply removing the init() function.

The moral is that the error can be thrown when we are trying to save a disconnected object with ColdFusion ORM's entitySave() that already exists in our persistence layer. Hopefully this helps someone else make some sense of their issue!