In this blog post I’ll begin to discuss an application wherein we used JavaScript in Java, by way of Rhino.

Let me start with a confession. Although I was happy with how the effort turned out technically, I wasn’t happy with the practical result.

My idea was to write the bulk of the application in Java, then collect the small exceptions we always make to accommodate our customers, and program them as JavaScript extensions to the main program.

The problem turned out to be that, as other programmers came on board, they were in a Java mindset first and foremost. I was unable to persuade them of the advantages to my scripting approach. They preferred to put customer exceptions directly into the Java code, instead of restricting the exceptions to JavaScript. Daniel Pink’s book To Sell is Human says it all.

Danial Pink, To Sell is Human

I’ll begin in this post by describing how we store the JavaScript code. I’ll give other details in subsequent posts.

We store the scripts in a SQL Server table. This matches the way we store other program options.

CREATE TABLE [dbo].[OrderEntryEventHandlers](
[EventName] [char](40) NOT NULL,
[MimeType] [char](40) NOT NULL,
[Action] [varchar](max) NOT NULL

The EventName column is the name of the script. MimeType is always application/javascript. The Action column contains the script itself. I’ve removed a few columns that are irrelevant to our discussion.

We established a naming convention. Any script whose EventName begins with AfterInsertPart_, for example, is executed every time a new part is inserted into an order. So the script AfterInsertPart_Customer123SpecialLabelForWidgets might watch for widgets to be added to orders for customer 123, so it can customize the widget label.

At program start we fetch the contents of the script table:

SELECT EventName, MimeType, Action FROM OrderEntryEventHandlers ORDER BY EventName, MimeType;

In another post I’ll describe how we store and execute the scripts within Java, and also how we create and maintain the scripts.

Leave a Reply

Your email address will not be published. Required fields are marked *

 characters available