Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Tcl your Java apps

Enhance the customizability and flexibility of your enterprise Java apps

  • Print
  • Feedback
Any viable company has to react quickly to gain or regain an edge over its competitors. For companies that rely on their Websites to generate revenue, this means frequent changes to those Websites. Some of the changes are more than cosmetic and require many code changes on the server side. Changes might be required, for example, in order to give holiday discounts, reward frequent customers with loyalty points, or send personalized marketing emails. If you use EJB technology to create your Web-based enterprise application, you know that you have to look through the Java source code, figure out which parts require changing, change the actual code, compile, and then retest your application.

All is well if you are the developer and know the system inside and out. Unfortunately, if someone else takes over, that person will experience a learning curve -- so by the time the change is rolled out, your business may have lost many customers.

The solution I propose is to isolate parts of the Java code that are likely to change and implement them in a scripting language -- Tcl, in this case -- that is easy to read, understand, and modify. If properly done, I believe even your manager could perform certain changes without paging you in the middle of the night.

Tcl's background

Tcl was developed by UC Berkeley professor John Ousterhout as a cross-platform scripting language that is easy to read and understand, and is easily extensible and embeddable. You can extend it by writing your own extensions and commands. This article will show you how, if want your application to interface with a Tcl script, you can easily embed a Tcl interpreter to process your script.

Since its creation, Tcl has empowered hundreds of thousands of users in a wide range of applications such as rapid prototyping, unit testing, and the task of tying together applications that were never meant to work together.

Not too long ago, considerable efforts had been spent integrating Java with Tcl (previously only extensible and embeddable in C). The TclBlend project, led by Moses Dejong, is an extension to the existing Tcl interpreter written in C, which allows a Tcl script to instantiate and call methods in Java objects, on top of the existing extensions written in C/C++. You can also create new commands or extensions in Java. The other project, Jacl (Java Command Language), is a total rewrite of the existing Tcl script interpreter in Java that will make it easy to embed into a Java application. I will explain in this article how, by embedding Jacl into your existing application, you will make your app more dynamic and configurable.

Caveat emptor

This article does not teach you the Tcl language or walk you through downloading and installing the necessary software. It is meant only as an introduction to the possibility of addressing a situation that you might frequently encounter. Fortunately, Tcl scripting is easy to read and understand, so you can explore it further to see whether it meets your enterprise needs. I have provided some links in Resources that discuss the principles and the development of the Tcl scripting language and its integration with Java.

Software download

From the Resources section, you can download all the source code for this article. In addition, you will need to install JDK 1.1 or above and a version of the Jacl interpreter; you will find links to both of those in Resources. I have tested my code with Jacl 1.2.6.

Scenario

Let's take a typical scenario in which you're developing a business-to-consumer Website. To attract customers during a certain period, you sometimes need to offer perks, such as holiday discounts. During one such promotional period, the company might want to give first-time customers a discount on any purchase above 0.

  • Print
  • Feedback

Resources