Scripting power
saves the day
for your Java
apps
Tutorial Details:
Scripting power saves the day for your Java apps
Scripting power saves the day for your Java apps
By: By Ramnivas Laddad
Improve your user's satisfaction with support for user-defined scripting in your Java applications
he developers of today's software products must face some hard realities. There is increasing demand for new features from users, and increasing pressure on vendors to develop products in ever-shorter timeframes. Demands for features also tend to be widely varied and unique, making it nearly impossible to satisfy all users just by adding a fixed list of new capabilities to a piece of software. A typical user will have repetitive tasks that an easy-to-create macro can automate. Moreover, some tasks are inherently complex in nature, and in such a situation, wizard-like guidance in simplifying the task can be helpful.
Most applications today, including Java applications, attempt to satisfy user needs by adding newer functionality to successive versions. With this approach, users are forced to wait for the next product release for their needs to be met -- while their customization needs remain unfulfilled even with newer versions.
If a software tool could allow users to interact with it through user-written scripts, the value of the tool would improve tremendously. Using scripts, users could ease their routine tasks by writing macros, by creating wizard-like functionality to ease complex operations, and by modifying the behavior of the tool to suit their needs. How many Java applications provide such support?
Emacs is a good example of an application that offers rich support for user scripting. Emacs' approach is to provide services that offer users the ability to custom-create the functionality they need, rather than cramming new features into the tool itself. For example, the Emacs core does not offer Java application compiling/debugging functionality, but through the use of scripts, one can transform Emacs into a powerful IDE. Similarly, scripts can configure Emacs to function as a news reader, email client, and so on. Other good examples of script-supporting applications include such Microsoft products as Microsoft Office and Microsoft Visual Studio.
These applications provide rich services and support for user scripting. By providing hooks into an application, we can transform it into an environment. User scripts use service provided by such an environment to write their applications.
In this article, we'll look at a way to infuse applications with scripting support. For that purpose, we'll first write a simple application without scripting support, then incorporate such support into it. This two-step approach demonstrates how to add scripting support to (relatively!) long-standing Java applications. We will also develop a framework for scripting support, which makes support for a new scripting language, or change to a language's interpreter, a simple task.
An application without scripting support
First, let's develop a simple application designed without a scripting extension in mind. For this purpose, we'll develop a very simple drawing application that can draw shapes on a drawing surface -- a trimmed-down version of a typical CAD tool. And since there's no point to using object-oriented design and Java if we can't reuse existing components, we're going to use the slate component developed in " Java Tip 75: Use nested classes for better organization " as the basic building block for our application.
In this section, you will find no reference to scripting support. Remember, this is done deliberately in order to demonstrate that you can add scripting support to an application designed without any prior consideration for scripting.
The application has a frame containing a drawing surface and a menu bar. The menu bar has options for clearing the drawing surface and switching between various shape modes. The drawing surface provides a simple mouse interface; a new shape object is inserted into the drawing area that is bounded points at which mouse is pressed and released. It is not the most intuitive user interface, but a more advanced UI would have shifted the focus away from the main goal of this article -- to demonstrate the incorporation of the scripting facility.
SlateApp , the application's main class, sets up a JFrame with a menu bar and slate components in it. SlateAppMenuBar , the menu bar for the application, extends JMenuBar , adding a few simple menus for clearing the drawing surface and switching between modes to draw various shapes.
(Source code for these first versions of the SlateApp.java and SlateAppMenuBar.java classes is available to download.)
This completes the first step. Now we have an application with no scripting support. This application lets users create simple pictures by drawing rectangles, ovals, and lines via a mouse interface. It also allows the whole drawing surface to be erased. While this application is useful for simple drawings, it is not adequate for creating complex drawings.
Overview of embedding scripting support
Increasing interest in Java's "write once, run anywhere" promise has made it the language of choice for many new scripting-language interpreters. Most of these interpreters allow interaction with Java objects by the scripts. If we embed such interpreters in our Java application, scripts can interact with the application's objects. Thus, to enable scripting support for Java applications, we need to embed the interpreter so that it either runs in same virtual machine as the application, or can interact with the application's objects in some other fashion. We also need to make the application's objects available to the interpreter.
Scripting languages vary widely in their capabilities and suitability for particular tasks. In addition, users frequently prefer one scripting language over another -- many times due to nontechnical reasons. As such, applications should not force users to learn a new scripting language; rather, they should support multiple languages. This enables users to write their scripts in any of the supported languages, which results in higher user satisfaction. Keep in mind, though, that while such support for multiple language is a desirable goal, it is also desirable that such support should require a minimal per-language development effort. A well-designed scripting framework can help achieve both of these goals.
For the purpose of this article, I have chosen to provide support for four popular languages: JavaScript (ECMAScript), Python, Tcl, and Scheme. JavaScript, with its Java-like flavor, is popular for dynamic Web page creation. It also supports some object-oriented features, which makes interacting with Java more natural. Python is another popular object-oriented scripting language that is easy to learn. Tcl is a scripting language designed specifically to provide scripting extensions to applications. Scheme, a compact and elegant dialect of LISP, supports functional programming concepts.
I have chosen FESI (Free EcmaScript Interpreter) as the JavaScript interpreter, JPython as the Python interpreter, Jacl as the Tcl interpreter, and Skij as the Scheme interpreter. Each of these interpreters is written in 100% Pure Java. Each also supports natural Java object interaction for the scripting language it supports. As an added bonus, all four are available for free (though you should, of course, check license agreements).
None of the core definitions of these languages support interaction with Java objects; most of them existed long before Java was born. These interpreters, therefore, amend language specifications to let scripts create new Java objects and call methods on Java classes and objects. Users already familiar with these scripting languages should find these simple extensions easy to learn.
Scripting framework
Let's establish some design goals for our project. Our scripting framework should:
Allow for easy addition of new scripting languages and do so without any impact on the rest of the system
Allow for the scripting language interpreters to be changed as needed
We design the scripting framework based on the JDBC driver-manager framework. In the same way that the JDBC framework allows support for multiple databases and drivers, our scripting framework allows support for multiple scripting languages with a minimal per-language effort. To add support for a new scripting language, the developer just needs to write a driver for that language and load it in the application. As an added benefit, our framework also isolates changes made to the script interpreter from the rest of the system.
The framework consists of an InterpreterDriver interface implemented by each driver class for our supported scripting languages, and an InterpreterDriverManager class that manages all such drivers. We'll look at the InterpreterDriverManager first.
InterpreterDriverManager
The InterpreterDriverManager isolates the core application from the scripting interpreters and allows applications to support multiple scripting languages with no per-language development effort. In addition, InterpreterDriverManager is responsible for managing drivers and delegating script execution to the appropriate driver.
Under our framework, each driver, upon loading, must register an instance of itself with InterpreterDriverManager . Upon registration, the InterpreterDriverManager queries the driver for its supported languages and script extensions. When the InterpreterDriverManager receives a request to execute a script file, it first finds an appropriate driver by looking for the required extension, then delegates the execution. For executing script strings, the caller must specify the language necessary to interpret it.
(To view the source code for InterpreterDriverManager.java , click here .)
InterpreterDriver
InterpreterDriver provides a common interface between the underlying language interpreter and the rest of the syst
Read
Tutorial at: Click here to view the tutorial
Rate Tutorial: Scripting power
saves the day
for your Java
apps
View Tutorial: Scripting power
saves the day
for your Java
apps
Related
Tutorials:
Accelerate your Java apps! - JavaWorld - September 1998
Accelerate your Java apps! - JavaWorld - September 1998 |
Java Tip 42: Write
Java apps that work with
proxy-based firewalls - JavaWorld - December 1997
Java Tip 42: Write
Java apps that work with
proxy-based firewalls - JavaWorld - December 1997 |
Scripting power
saves the day
for your Java
apps
Scripting power
saves the day
for your Java
apps |
Smart object-management saves the
day - JavaWorld November 1999
Smart object-management saves the
day - JavaWorld November 1999 |
Script JavaBeans with the Bean
Scripting Framework - JavaWorld March 2000
Script JavaBeans with the Bean
Scripting Framework - JavaWorld March 2000 |
Solve your
servlet-based presentation problems - JavaWorld November
2000
Solve your
servlet-based presentation problems - JavaWorld November
2000 |
Tcl your Java apps - JavaWorld March
2001
Tcl your Java apps - JavaWorld March
2001 |
Clean up your wire protocol with SOAP, Part 3 - JavaWorld June
2001
Clean up your wire protocol with SOAP, Part 3 - JavaWorld June
2001 |
XSLT blooms with
Java
XSLT blooms with
Java |
Java scripting languages: Which is
right for you?
Java scripting languages: Which is
right for you? |
How to build
an interpreter in Java, Part 1: The BASICs
(JavaWorld /
May 1997 / by Chuck McManis)
How to build
an interpreter in Java, Part 1: The BASICs
(JavaWorld /
May 1997 / by Chuck McManis) |
Ganymede
A log4j plugin to Eclipse that works similar to chainsaw (SocketServer). Includes color, filtering, detailed information, and saves settings. |
JDBC scripting, Part 2
JDBC scripting, Part 2
Programming and Java scripting in JudoScript
Summary
JudoScript is a rich functional scripting language, and an easy and powerful general programming and Java scripting language.
JudoScript's power comes from its synergy of |
Java Calendar Component
Java date picker component, ready to use in your Swing applications featuring. |
alt.lang.jre: Take a shine to JRuby
JRuby combines the object-oriented strength of Smalltalk, the expressiveness of Perl, and the flexibility of the Java class libraries into a single, efficient rapid development framework for the Java platform. In this third installment in the alt.lang.jre |
Prova
Prova: A Language for Rule Based Java Scripting, Information Integration, and Agent Programming |
Groovy, Java\'s New Scripting Language
Groovy, Java\'s New Scripting Language
When some Java developers hear about Groovy, their first reaction often is, as mine was, "Oh, no, not another scripting language for Java." We already have, after all, JavaScript and Rhino, Jython, Jelly, BeanShell, |
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. |
Practically Groovy: Unit test your Java code faster with Groovy
Why unit test with Groovy?
What makes Groovy particularly appealing with respect to other scripting platforms is its seamless integration with the Java platform. Because it's based on the Java language (unlike other alternate languages for the JRE, which |
Techniques used for Generating Dynamic Content Using Java Servlets.
Techniques used for Generating Dynamic Content Using Java Servlets.
Techniques used for Generating Dynamic Content
Common Gateway Interface (CGI)
For any web application high performance and timely delivery are key ingredients to competitive |
|
|
|