Appendix C. The Power of Scripting

CONTENTS
  •  Scripting Versus Programming Languages
  •  Java and Scripting
  •  Integrating Scripting with Jython
  •  Which Scripting Language to Choose
  •  Hello World The Programming Rosetta Stone
  •  What Does It All Mean?

Scripting languages are dynamic, interactive environments for rapid development of Java code. Many are either object-oriented or object-based, and almost all are interpreted and use late-bound polymorphism. This makes them extremely dynamic and easy to program essential ingredients in rapid application development (RAD), component gluing, and project prototyping.

Scripting Versus Programming Languages

There's a fine line between scripting and programming languages. For example, Smalltalk is an extremely dynamic interpreted language, but it's not for scripting. A true scripting language, unlike Smalltalk, must employ late-bound polymorphism and dynamic typing.

Many UNIX programmers program in C and C++ and glue modules together with higher-level shell programming (Korn, Bourne, C, etc.). Or they use the scripting language Tcl for both programming their GUIs and gluing together their C++ classes and libraries. Another option is to use Python as the glue. It's often preinstalled with UNIX systems and is easy to extend with C.

The most prevalent object-based scripting language is Visual Basic, which is often used to glue together COM components written in different languages.

Scripting languages don't replace system languages but rather augment them in the following ways:

  • Extending applications

  • Debugging

  • Learning and experimenting with the Java API

  • Rapid prototyping

  • Gluing subsystems and components

  • Automating general testing and regression testing

For information about increasing productivity with scripting, read John Ousterhout's online paper Scripting: Higher Level Programming for the 21st Century (http://www.home.pacbell.net/ouster/scripting.html), which notes a sharp productivity increase with scripting languages on the order of 5 to 10 times higher than with a strongly typed language like Java. From my own experience, I'd say they are 2 to 3 times faster depending on the application.

Java and Scripting

I call Java a scripting language on steroids. In fact, because it uses statically typed polymorphism, it's more precisely a hybrid. This may at first seem like a disadvantage, but it turns out that Java's statically typed polymorphism, as well as its design by interface, is great for systems programming, framework definition, and component development. It would be wrong to view Java as a system programming language by classical definition. Rather, it's a virtual system programming language for a virtual system that is, a virtual machine (the Java Virtual Machine). Like its scripting language cousins, it can be very dynamic not as much as Python or Smalltalk but certainly more than C++.

Java's main drawback for scripting is that it can't glue components together. What you need to do is build components with Java and glue them together with a true scripting language.

Java's class reflection and bean introspection APIs make a great basis for integrating scripting languages. Essentially, the scripting language can get metadata about a Java class, which it uses to change its properties, handle its events, and invoke its methods. I have metaprogrammed with COM, CORBA, and Java, and of the three Java is my preference because of these features.

Some people think that the only language for the JVM is Java. They're wrong. Like many platforms (and the JVM is very much a platform), the virtual machine has many languages, and the list keeps growing. In particular, combining the JVM with scripting enhances rapid application development.

Integrating Scripting with Jython

Jython is very close to Python and has been certified as 100 percent pure Java. In fact, in a recent Web poll conducted by NetBeans (the Java IDE maker bought by Sun) on integrating a scripting language with its Java IDE, Jython won by a landslide. It also won a similar poll on JDJ and the Java channel.

By the way, you can develop Java Server Pages in Jython. They're called Python Server Pages, and they run in a Java servlet. They're also open source. (Find out more about them at http://www.ciobriefings.com.)

Tcl

The Java command language (Jacl) implements Tcl8.x for writing scripts for Java components and APIs. Another Tcl blend allows Java objects to be manipulated directly from Tcl. Because Tcl is the premier RAD language, many Jacl users have claimed significant reduction in development costs with its use. (Find out more about Jacl at http://www.tcl.tk/software/java/.)

JavaScript (Rhino)

Rhino is an implementation of JavaScript v.1.5, a very powerful object-based language. Freely available and open source, Rhino is a natural for rapid application and prototyping in the JVM.

Instant Basic

Halcyon's Instant Basic is a Visual Basic clone that allows quick porting of existing Visual Basic applications to the Java platform (the IDE, database components, etc.). Its iASP, a clone of JSP, works with Java so that you can use VBScript and JavaScript to access JavaBeans, CORBA, EJB, and so forth. Learn more about Instant Basic at http://www.halcyonsoft.com.

Java BeanShell

BeanShell is interpreted Java with a syntax very similar to real Java's that executes Java statements and expressions. Like other scripting languages, it's dynamically typed so much of the Java syntax for type declaration and casting is optional. BeanShell is great for writing prototypes and experimenting with unfamiliar APIs. Also, it supports beans, it's very easy to use, and it's open source. To find out more about BeanShell, visit http://www.Beanshell.org.

Smalltalk/Bistro

Bistro is a Smalltalk variant with extensions for Java features and integration. It offers software developers the ability to code in a readable and expressive syntax. Although it's dynamically typed, it has the option of static typing for closer Java integration, which means that you can mix and match statically and dynamically typed systems. Bistro combines Java type safeness with Smalltalk fast development.

Scheme/Skij

Skij is a small Scheme interpreter implemented in Java. A variant of Lisp, it enables rapid prototyping in the Java environment and has many advanced features such as macros and first class continuations. There are at least 15 Scheme ports to the JVM.

You can download a copy of Skij at http://www.alphaworks.ibm.com/tech/skij. If it's not your favorite Java Scheme variant, let me know why.

Need More?

If I didn't mention your favorite language, or the if the ones I covered don't tickle your fancy, see Robert Tolksdorf's comprehensive list of JVM programming languages.

 

Dynamic to Static Typing and Back

BeanShell is essentially a Java variant with dynamic and optional static typing. Bistro is a Smalltalk variant with static and optional dynamic typing. Both languages are on the right track, given that Visual Basic, the granddaddy of all scripting languages, provides both static and dynamic typing support.

Which Scripting Language to Choose

In choosing a scripting language, ease of use is a primary criterion. For example, a language may be easy to learn because it resembles another language that most developers know. Or it may just have an easily grasped syntax.

A particular benefit of scripting languages is that they can be embedded in a large application to make it more extensible. The question is how embeddable a particular language is and how well it integrates with Java.

Resemblance to the parent language is important because it can affect how code ports from a legacy system (i.e., non-Java). The degree of resemblance can influence how quickly developers can get up to speed.

A language's unique features might be those that gear it to a particular problem domain. For example, does it have a library for generating XML and HTML documents? Is it easy to integrate with JSP (making it particularly suitable for Web programming)?

Some languages excel at common tasks like string parsing; some don't.

A certain scripting language may be better than Java at tasks like manipulating strings and collections. Features such as extensive class libraries and useful built-in language constructs enhance programming productivity.

A key criterion is a language's ease of integration with Java classes and APIs. For example, a language that permits its classes to subclass Java classes has good integration. So does a language that allows methods to have type signatures.

Some scripting languages are better than others at code debugging. Some have a more pleasant development environment.

Hello World The Programming Rosetta Stone

To give you a feel for scripting, we're going to compare how it's done in three languages, Java, JavaScript, and Python. We'll make the comparison more interesting by adding a Say Hello button, which when pressed causes a window to pop up and display "Hello World" in 18-point bold type.

The Java Version

import javax.swing.*; import java.awt.Font; import java.awt.event.*; class MyFrame extends JFrame{   public MyFrame(){       JButton sayHello;       sayHello = new JButton("say hello");       sayHello.setMnemonic('h');       this.getContentPane().add(sayHello);       this.setVisible(true);       this.pack();       sayHello.addActionListener(new ActionListener(){       public void actionPerformed(ActionEvent ae){           JButton b = (JButton)ae.getSource();           b.setEnabled(false);           sayHello();       }   } ); } private void sayHello(){       JFrame helloFrame;       JLabel helloLabel;       Font font;       helloFrame = new JFrame("Hello Frame");       helloLabel= new JLabel("Hello World");       font = new Font("Arial", Font.BOLD, 20);       helloLabel.setFont(font);       helloFrame.getContentPane().add(helloLabel);       helloFrame.pack();       helloFrame.setVisible(true);   }   public static void main(String [] args){       MyFrame frame = new MyFrame();       frame.setTitle("My Frame");   } }

Here's the JavaScript (Rhino at JavaScript 1.4) version.

function MyFrame(){   sayHello = new Packages.javax.swing.JButton("say hello");   sayHello.setMnemonic('h');   this.frame = new Packages.javax.swing.JFrame();   this.frame.getContentPane().add(sayHello);   this.frame.setVisible(true);   this.frame.pack();   sayHello.addActionListener( new Packages.java.awt.event.ActionListener() {       __parent__ : this,       actionPerformed : function(ae) { ae.getSource().setEnabled(false);            this.__parent__.sayHello();       }   } );   function sayHello(){       helloFrame = new Packages.javax.swing.JFrame("Hello Frame");       helloLabel= new Packages.javax.swing.JLabel("Hello World");       font = new java.awt.Font("Arial", java.awt.Font.BOLD, 20);       helloLabel.setFont(font);       helloFrame.getContentPane().add(helloLabel);       helloFrame.pack();       helloFrame.setVisible(true);   }   this.sayHello=sayHello; } function main(){   frame = new MyFrame();   frame.frame.setTitle("My Hello");   } main();

The Jython Version

from javax.swing import JFrame, JButton, JLabel from java.awt import Font class MyFrame (JFrame):   def __init__(self):       sayHello = JButton("say hello", mnemonic='h')       self.contentPane.add(sayHello)       self.visible=1       self.pack()       sayHello.actionPerformed = self.__sayHello   def __sayHello(self, ae):       ae.source.enabled=0       helloFrame = JFrame("Hello Frame")       helloLabel= JLabel("Hello World", font=Font("Arial", Font.BOLD, 20))       helloFrame.contentPane.add(helloLabel)       helloFrame.pack()       helloFrame.visible=1 if __name__=="__main__":       frame = MyFrame(title = "My Frame")

Notice how well Jython integrates with the JavaBean properties and event model. Also notice how much it packs into a small package (it's about two-thirds the size of the JavaScript version).

What Does It All Mean?

Basically that Python is the best scripting language for the JVM. Here's why:

  • Ease of use. Python was designed to be easy for beginners. In Virginia, high school students are taught how to program with it.

  • Embeddability. On a scale from 1 to 10, Python scores 10 for embeddability.

  • Resemblance to the parent language. Jython is syntactically identical to Python.

  • Features. Python has some of the best features of Smalltalk, Scheme, Icon, and Java.

  • String parsing. Python has libraries for regular-expression string parsing (see Appendix E) and slice notation, as well as other features that make string parsing easy.

  • Productivity. Python has an extensive class library as well as built-in language support for collection objects (including collection literals for defining collections). These make Python strikingly productive.

  • Working well with Java classes and APIs. In Jython you instantiate and subclass Java classes and interfaces, invoke Java methods, set up bean events, and work with JavaBean properties. You can also compile Jython into Java classes to create JavaBeans, servlets, and applets.

  • Development environment and debugging. Jython has a good interactive interpreter, but its development environment is its weak point. Python has some mature IDEs, but Jython has nothing. If it did, it would give Java a serious run for its money as the most popular language for the JVM.

CONTENTS


Python Programming with the JavaT Class Libraries. A Tutorial for Building Web and Enterprise Applications with Jython
Python Programming with the Javaв„ў Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython
ISBN: 0201616165
EAN: 2147483647
Year: 2001
Pages: 25

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net