Not so much java for local web startups

There’s a local group of entrepreneurs and developers that meets every couple of months in Cambridge. I was curious about this month’s presenters’ choices of development platform, so I took at look at their headers and here’s what I found.

Of 7 presenters the platform stats fall out thusly:
2 Ruby on Rails (plus one suspected, but not confirmed)
1 Python (cherry py)

By way of contrast, a quick and dirty survey of jobs in boston/cambridge/brookline on craig’s list turned up the following stats
232 jobs containing Java
113 jobs containing
164 jobs containing PHP
46 jobs containing Python
34 jobs containing Ruby

Presumably the difference is because of lots of folks in the area are working at medium sized companies on older, established (i won’t say “legacy”) systems?

Gas prices, state by state, with and without state taxes

In the image below I’ve plotted the average gas price in each state for 4/25/07 (data from here) with and without state per-gallon taxes included. Without the taxes included, it becomes obvious that gas prices increase on the west coast, perhaps due to transportation costs? ( a quick search didn’t turn up any port-by-port oil import stats).


I created this using ruby-shapelib and rmagick as mentioned previously.

Loading and drawing maps with Ruby

Loading geographic map data and drawing maps is pretty easy to do with two Ruby tools – ruby-shapelib (to load the map data) and RImageMagick (to create the drawings).

I didn’t see any tutorials or sample code, so I’m posting this sample as is – it will draw every shape part of every shape in a given shape file. Note this code does not perform any geographic projections.

require 'rubygems'
require 'RMagick'
require 'rvg/rvg'
require 'shapelib'
include ShapeLib
include Magick


def drawshape shape, canvas
#each shape can have multiple shape parts...
#iterate over each shape part in this shape -
0.upto(shape.part_start.length-1) do |index|
part_begin = shape.part_start[index]
unless shape.part_start[index+1].nil? then
part_end = shape.part_start[index+1]-1
#NOTE we're assuming all the parts are polygons for now...
#draw a polygon with the current subset of the xvals and yvals point arrays
canvas.polygon(shape.xvals.slice(part_begin..part_end),shape.yvals.slice(part_begin..part_end)).styles(:fill =>"green",:stroke=>"black",:stroke_width=>0.01)

#create a viewbox with lat/long coordinate space in the correct range
def create_canvas rvg, shapefile
width = shapefile.maxbound[0] -shapefile.minbound[0]
height = shapefile.maxbound[1] -shapefile.minbound[1]
#puts "viewport #{shapefile.minbound[0]},#{shapefile.minbound[1]} - width= #{width} height= #{height}"
#invert the y axis so "up" is bigger and map the coordinate space to the shape's bounding box
canvas = rvg.translate(0,rvg.height).scale(1,-1).viewbox(shapefile.minbound[0],shapefile.minbound[1],width,height).preserve_aspect_ratio('xMinYMin', 'meet')

shapefile =,"rb")
#create a new RVG object
rvg =,100)
canvas = create_canvas rvg, shapefile
shapefile.each { |shape| drawshape(shape,canvas) }


I’m using the US State boundary file from the national atlas website.

Object Properties Using Value Expressions in JSTL

I found out something cool about JSTL’s expression language today that I didn’t expect at all – like a real scripting language, EL allows one to access an object’s fields not only like object.fieldname (which wouldn’t be changeable at runtime) but also as object[“fieldname”] and by extension with any string variable as an argument. Wow! I don’t remember seeing any examples of this in the wild – somehow i stumbled upon it in the bowels of a unified expression language tutorial.

This came in handy DRYing out some JSP code today that differed only in the fields accessed on the same kind of objects – I thought immediately, if only I had a scripting language this could all go away! Then I thought about the long and winding road one might take to do that in java, involving reflection and/or long if/else blocks. I was pleasantly surprised to find out JSTL can do that. Phew.
Here’s a poor example:

What you’d have to do w/o this capability


and with

<c :set var="fieldName" value="dog" />

i told you it was a bad example.

Hack to make layout borders visible in Flex

I put together this quick hack to show the borders of all the containers in a flex application – I found myself wishing for something like firebug’s inspector for flex while spending way too much time debugging a pixel precise layout this week This biggest problem for me was figuring out which control or container was contributing padding to the layout in places where the alignment was off, and it appeared that some of my styles in an embedded css file weren’t working.

The code inserts a canvas over the contents of the application, disables its mouse events (making it transparent to the app/user) and then listens for mouse move events on the Application object. Each time the mouse moves, it queries the display list to find out what’s under the mouse, then draws outlines around that control and all of its parents.

There’s obviously a lot more that could be added to this – like shading padded regions, having a tool tip display which control you’re over along with attributes, but as I don’t know when I’ll have the time (and more importantly the inclination) to make those enhancements happen, I’m putting it up now.

You can right click on the example to view the source. If the embed isn’t working, you can go here where all the flash player autoinstall magic will happen.

Note: this will only work in your application if your root application object has absolute layout because of the way flex containers work- i.e. only containers with absolute layout allow overlapping controls. I had some code that attempted to reparent the children of the application if it had vertical or horizontal layout, but the guts of flex were throwing an error so that code is commented out presently.

Select part of the text in a textbox with javascript

There was a brief discussion on the Tracks mailing list over the way its autocomplete works (the project uses Scriptaculous’s nice Autocomplete widget in local mode): should it automatically fill in the entire word once enough text is entered to match one of the choices (annoying if you have to backspace because the guess was wrong) or make you hit tab or enter to fill in the suggestion. To make a long story short, I suggested a third approach – fill in the remainder of the match selected, so if its wrong, the user can type over it, like this:


This isn’t rocket science and has been done forever, but I didn’t find a single good reference on how to select just part of the text in a text box. Scriptaculous and prototype don’t support it at the present time, and like anything good in browser-land, there’s at least two different ways to skin this cat. The IE way, and everyone else’s way (actually I don’t know which way safari and opera work, but the code here does work for them too).

I saw one of the YUI widgets supported “typeahead” so I looked through its code to find the relevent javascript calls – for IE its “createTextRange” and for firefox its “setSelectionRange”. Combine those and you get the following, which I’ve tested as working on IE 6 (or was it 7?), Firefox (mac), opera (mac) and safari.

function selectSomeText(element,begin,end)
if (element.setSelectionRange)
else if (element.createTextRange)
var range = element.createTextRange();

One of these days I’ll refactor the code I wrote to test this with scriptactulous and throw that up here as well.

Loading Flex from a Flash 8 swf; Actionscript-only version checking

Not too long ago, I was poking around the internets trying to figure out how to load an Actionscript 3 (Flex) SWF file from a Flash 8-compatible (AS2) swf after version checking the player to see if it is version 9 or greater. I managed to piece together what I needed and went on my merry way.

Yesterday someone who had seen my queries on the flexcoders mailing list wrote to me to ask if I had found an actionscript-only solution after all, so I will post the solution here for future reference. I certaintly couldn’t have come up with this without tidbits from here and there…

The key to this working is not loading anything onto the stage before running this code. In fact, in my solution, I use a flash project that only does version checking and loads SWFs externally for both cases where flex is supported and where it isn’t. Insert something like the following in the first frame of your timeline and you should be all set.

var FLEX_NOT_SUPPORTED_SWF ="absolute path to a flash 8 or below swf";
var FLEX_SUPPORTED_SWF = "absolute path to a flash 9 swf";
var versionInfo = getVersionInfo(); // use your favorite implementation of this
if (versionInfo.majorVersion >= 9)

Flex and REST don’t get along

I was a bit dismayed on Friday to find that Flex is severely crippled in the way it handles fault responses via the HTTPService component. In short, it doesn’t provide the response body if the HTTP status code indicates an error. It doesn’t even tell you the status code so you can create an intelligent error message. This is easy to do in javascript, so its lame to find that Flex and actionscript are actually less capable in this regard. I found another post describing this issue where blame is placed on the browser implementation.

I had planned to to return HTTP 404 plus an xml fragment containing the error message when a resource wasn’t found via a given id number, but now that’s out the window. Instead I’m always returning OK, and switching based on the body XML content. Oh well.

In order to do so, I needed to determine the root node of the xml – was it “error” or something else? Using e4x, navigating the dom is usually child’s play – just nodename.childname.text() for example. This is where I made a “rookie mistake” in not realizing that the XML object actually represents the root node, not some super-root. So I was testing for the existence of x.error (where x is the XML object, and error would be a first level child) rather than just checking the localname of the xml object, which in fact is the node named “error”. Maybe this will show up in google and help someone experiencing a similar brain-fart one day.

Apollo alpha available

I saw (via that Adobe Apollo is available at adobe labs. It remains to be seen if the whole I need to install tens of targeted rich clients thing will take off (java web start, anyone?), but if it does, this looks like a great tool to be using.

When I was working at Lotus on Workplace Client, I often wondered if Java on the Eclipse platform was really the best way to deploy rich client applications. The only compelling things it had to offer over browser-based applications was an offline experience and desktop integration. (plus some hand waving about leveraging existing Java skills). Now that flex has a more traditional programming model around the flash runtime, and apollo provides desktop integration and offline support (with synchronization) as well as update/provisioning, the advantages of using the Eclipse platform with layers of overpriced IBM code piled on top are evaporating. One could still make a case for the lipstick on a legacy-code pig project (see Hanover/notes 8 ) building on the Java/eclipse stack, but I can’t see any reason why a company would invest in something like Lotus Expeditor for green-field development.

Can’t share session contents between applications on Tomcat

I got stuck for a little while on this problem today – I was pushing some content into the session from a Flex app into one web app, and then trying to read it from another. It turns out this won’t work under tomcat. I thought at first it was because the cookie paths were different, so I added emptySessionPath=true to my server.xml file, but that actually doesn’t fix the problem. Thinking about it, it is perfectly reasonable that two web applications that aren’t associated in some way (as being in the same ear) can’t get at each others session information.
Lesson learned: either share state in some non-session mechanism (like the database) or move the resources into the same application.