Tcl your Java apps - JavaWorld March
2001
Tutorial Details:
Tcl your Java apps
Tcl your Java apps
By: By Benedict Chng
Enhance the customizability and flexibility of your enterprise Java apps
ny 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 $5 discount on any purchase above $10.
Solution 1
The typical way to go about managing this discount would be to hard code the logic into your Java code as follows:
// The result of the code will calculate the miscellaneous
// deduction to be to the purchase price and the discount
// percent to award to the customer
double miscDeduction=0.0;
double discountPercent=0.0;
// if this is a new customer, give a $5 discount
// if he/she spents above $10
if (customer.isFirstTimeCustomer()) {
if (shoppingCart.getValue()>=10.0) {
miscDeduction=5;
}
}
// get the amount of money this customer
// already spent on this site
double dollarSpent=customer.getShoppingHistory().getDollarSpent();
// decides the percentage discount to award to the customer
// based on his/her past spendings
if (dollarSpent>100.0) {
discountPercent=5.0;
} else if (dollarSpent>500) {
discountPercent=7.0;
}
System.out.println("Discount awarded: "+discountPercent+"%");
System.out.println("Additional discount: $"+miscDeduction);
The code first initializes miscDeduction and discountPercent to zero. The variable miscDeduction will hold the amount of deduction to the purchase price and the variable discountPercent is the discount to be awarded to the customer.
The code then checks whether the customer is a first-timer by calling the method isFirstTimeCustomer() from the customer object. Then the purchase price of the customer's shopping cart is obtained by calling the method getvalue() from the shoppingCart object. The creation of the shoppingCart and customer objects is not shown because they are generic enough and not important for our discussion here.
Next the dollarSpent variable is initialized with the purchase amount the customer has made since registration. Finally, the discount awarded to the customer will be determined by the dollarSpent variable.
Problems with solution 1
All would be well if your boss had not made some last-minute decision, such as increasing the discount to 8 percent for customers who spend more than $500, to compete with a competitor doing the same thing.
Solution 2
Another way to handle this would be to parameterize some of the values that are likely to change and store them into an external XML file, database, or even from a network connection, so that you can change the values without impacting the code being written, like this:
// DiscountSetting is an interface that allows you to query an external
// XML file or database for the amount of discounts to award to
// a customer
DiscountSettings discountSettings=new MyDiscountSettings();
// The result of the code will calculate the miscellaneous
// deduction to be to the purchase price and the discount
// percent to award to the customer
double miscDeduction=0.0;
double discountPercent=0.0;
// if this is a new customer, give a $5 discount
// if he/she spents above $10
if (customer.isFirstTimeCustomer()) {
if (shoppingCart.value()>=discountSettings.get("First time purchase")) {
miscDeduction=discountSettings.get("First time deduction");
}
}
// get the amount of money this customer
// already spent on this site
double dollarSpent=customer.getShoppingHistory().getDollarSpent();
// decides the percentage discount to award to the customer
// based on his/her past spendings
if (dollarSpent>discountSettings.get("First level spending")) {
discountPercent=discountSettings.get("First level discount percent");
} else if (dollarSpent>discountSettings.get("Second level spending")) {
discountPercent=discountSettings.get("Second level discount percent");
}
System.out.println("Discount awarded: "+discountPercent+"%");
System.out.println("Additional discount: $"+miscDeduction);
The code first creates a discountSettings object. This is an interface that exposes a single method to obtain a double value based on a String input:
public interface DiscountSettings {
public double get(String valueToRetrieve);
}
Implementation of the DiscountSettings interface will be responsible for retrieving the actual information from a data source, such as an XML data file or a database table.
As in the previous example, the code will award a $5 discount to first-time customers whose purchase values exceed $10. Instead of hard coding the cash value, the code now uses the discountSettings object to look up the value based on fixed string constants, "First time purchase" and "First time deduction" .
The code then goes on to award additional discounts to customers who have spent a certain amount of money. As in the above case, the discountSettings object now finds out the percentage of discount to be awarded based on how much each customer has spent.
Problems with solution 2
Solution 2 solves our problem when the reward structure is the same and the only values that change are the discount amounts. Unfortunately, we have somehow hard coded our logic and structure here. If management decides to terminate the first customer rewards, or introduce customer coupons, we would have to go back to our code once again.
Solution 3
Solution 3 to the problem above moves the reward code into an easily understood and modified script. We will make use of Jacl to parse our Tcl script. The Java code and the Tcl script is as follows:
// Creates the Tcl interpreter object
Interp interp=new Interp();
// The result of the code will calculate the miscellaneous
// deduction to be to the purchase price and the discount
// percent to award to the customer
double miscDeduction=0.0;
double discountPercent=0.0;
try {
// the file name of the script
String scriptFileName="discount_decision.tcl";
// get the amount of money this customer
// already spent on this site
double dollarSpent=customer.getShoppingHistory().getDollarSpent();
double shoppingCartValue=shoppingCart.getValue();
// set the dollarspent variable in the Tcl script
interp.setVar(
"dollarspent",
TclDouble.newInstance(dollarSpent),
0);
// set the firsttimecustomer variable in the Tcl script
interp.setVar(
"firsttimecustomer",
TclBoolean.newInstance(customer.isFirstTimeC
Read
Tutorial at: Click here to view the tutorial
Rate Tutorial: Tcl your Java apps - JavaWorld March
2001
View Tutorial: Tcl your Java apps - JavaWorld March
2001
Related
Tutorials:
Scripting power
saves the day
for your Java
apps
Scripting power
saves the day
for your Java
apps |
Enhance your Java application with Java Native Interface (JNI)
Enhance your Java application with Java Native Interface (JNI) |
JavaMail quick start
JavaMail quick start |
Cut down on
logging errors with Jylog
Cut down on
logging errors with Jylog |
XSLT blooms with
Java
XSLT blooms with
Java |
Publish
Publish event-driven Web content with JSP custom tags |
Picture
this
Picture
this |
Worth
reading
Worth
reading |
Data Models for Desktop Apps
Data Models for Desktop Apps
This is the third article in a series that presents the prototype of a Java desktop application called JImaging. The first article described the three major Java GUI toolkits: AWT, Swing, and SWT. In the second article, I int |
Software Download Central
compatible.
GNU getopt - Java port
A while back I found myself in need of a Java command line option parser. Unsatisfied with free versions I was able to find on the net, I volunteered to port the GNU getopt family of functions from C to Java. The current release |
Creating Custom Desktop Components
This article presents a drawing component used by an image-annotation application named JImaging. Some of the JImaging code has already been described in two other articles, titled "Prototyping Desktop Applications" and "Data Models for Desktop Apps." |
Clean Up Your Mess: Managing Temp Files in Java Apps
Clean Up Your Mess: Managing Temp Files in Java Apps
Creating and managing temporary files in a Java application can be a little tricky due to some open JVM bugs. Develop a workaround with some custom code and a clever design. |
Encapsulate reusable functionality in JSP tags
JavaServer Pages (JSP) are a great mechanism for delivering dynamic Web-based content. JSP provides a set of predefined tags, but you can also define your own tag extensions that encapsulate common functionality. |
A first look at Apache Geronimo
Start developing and deploying J2EE apps on the first open source J2EE server. When released, Geronimo will be the first J2EE-certified open source server. This article will give you the basics you need for developing and deploying J2EE applications on Ge |
Struts, JavaServer Faces, and Java Studio Creator:
The Evolution of Web Application Frameworks Sun Microsystems' Craig McClanahan, the creator of the Apache Struts Framework, co-specification lead for JavaServer Faces 1.0, and prime architect for Sun Java Studio Creator's new release, explains all three. |
Running Wine on the Sun Java Desktop System, Release 2
A BigAdmin reader describes how to install and use Wine, an open source implementation of the Windows API, on the Sun Java Desktop System, Release 2. |
BioInformatics Tools
BioInformatics Tools
BioInformatics Tools
The Bioinformatics tools are the software programs for the saving, retrieving and analysis of Biological data and extracting the information from them.
Factors that must be taken into consideration when |
Web Hosting Guide. What is Web Hosting Plan?
Web Hosting Guide. What is Web Hosting Plan?
What is Web Hosting Plan?
Web hosting plan is the different plans provided by Hosting Companies for hosting your web site. Web hosting plans include the storage limit, bandwidth, access to server |
Chat Transcript: Java Web Services Developer Pack (Java WSDP) 1.5
Learn about the exciting new web services features in the recently-released Java WSDP 1.5. |
Solaris 10 OS Certification Beta Exams
If you are an expert in system and network administration, you can get involved in the creation of three new Solaris 10 certification exams. These Beta exams count toward official Solaris Certification and allow you to provide comments and technical feedb |
|
|
|