|
|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SOA World Conference
Virtualization Conference $200 Savings Expire May 16, 2008... – Register Today!
SYS-CON.TV |
TOP THREE LINKS YOU MUST CLICK ON General Java
Extending Your Applications with Bean Scripting Framework
By: Rick Hightower
Digg This!
Part 4 of a series discussing the many languages that compile and/or run on the Java platform Do you remember the operating system religious wars? Mac OS versus Windows, Windows NT versus UNIX, OS/2 versus Windows NT. Or how about the text editor wars VI versus Emacs? It may seem silly for programmers to become involved so passionately with the technology they work with, but if you spend more time with your VI text editor than with your family, I guess you do get kind of attached. I'm certain, for example, that we all have our favorite programming language.... Recently, for example, I was explaining to a colleague why I like to prototype things in JPython. He stared at me blankly and asked what at first seemed an innocent enough question: "Why?" I spent the next five minutes explaining I didn't even take a breath. To which he again responded "why?" before going on to expound on how Java was the perfect language and how you don't need another one. At this point I found it better not to continue the conversation. I try to avoid religious attachment to technology. (See the March issue of JDJ [Vol. 5, issue 3] for more about JPython.) I've had similar experiences when talking to VB, Perl, Delphi, C++ and Rexx programmers. Perl programmers seem particularly attached to their language. Developers have favorite languages just like developers have their favorite editor. (Mine is Emacs.) Java is one of my favorite programming languages. However, it's more than just a language, it's a platform, and Java the platform runs many, many programming languages. (See the first article in this series to learn more about programming languages that run in the Java Virtual Machine ["Programming Languages for the JVM," Vol. 5, issue 2].) Now let's say that you wanted to make available a set of services from a Java-based application via a scripting language, as LotusScript does for Notes or VBA (Visual Basic for Applications) does for Excel. Which scripting language do you use? It's more or less impossible to pick one without leaving someone bent out of shape. Remember, everyone has their favorite. You want your application services to be inclusive, not exclusive. What if you could support all of the major scripting languages with the same or less effort than it took you to support one? Essentially, with Bean Scripting Framework (BSF) from IBM, you can support Perl, Python, NetRexx, JavaScript and even VBScript. Cool beans! What's more, BSF is going to add standard ways to debug scripts, a major flaw in some scripting languages. (I know, I know: real programmers don't use visual debuggers. But admit it, you do sometimes and so do I, but don't tell anyone.) BSF brings standard support for many programming languages to the Java platform.
Why Scripting Languages?
I believe the above echoes my same sentiments from the first article in this series. Scripting languages and components go together like a horse and carriage: having a standard way for scripts to talk to Java components opens up a lot of possibilities. BSF endeavors to provide a standard for scripts to communicate with Java components. (Please refer to the first article for more information on components and scripting languages.)
What Is BSF?
The official Java platform from Sun doesn't have a standard scripting architecture, but IBM is submitting BSF to JavaSoft as a Java Specification Request, so BSF may become the basis of the Java platform standard extension for scripting. Even if it doesn't, BSF is likely to be the de facto standard for scripting integration for the Java platform.
Why BSF?
Currently, the BSF supports the following pure Java platform languages:
The scripting language doesn't have to be implemented in Java to be supported by BSF. For example, IBM adds support for Perl and VBScript. Essentially, all active scripting languages including VBScript and JScript are supported via BSF's support for Microsoft Active Scripting Framework (MASF). BSF is the Java version of MASF, which is weird because some languages that are supported by MASF are also supported by BSF. Thus you can mix classic Python with JPython. This is good for legacy integration. In addition, every language that gets added to MASF automatically gets added to BSF. Pretty tricky, huh? BSF is to MASF as JDBC is to ODBC. Just as you can have pure database drivers in JDBC or use native ODBC drivers, you can have pure Java scripting languages or use native scripting languages in BSF. Think of all the people in the world who have done VBScript (or VBA). Now you can allow them to participate in your development efforts. Not only that, but you have another integration point with COM via MASF. With MASF, you can instantiate COM objects. (Don't worry, you 100% Pure zealots, you can still use BSF scripting in a Pure environment as well.)
Distribution
Architecture
Getting Started
The first step on every journey like this is usually a download or two and this adventure is no different. I went to IBM's alphaWorks Web site, looked up BSF and downloaded the bsf21.zip before extracting it to C:\BSF-2.1. The zip file contains all the files you need to get started: API documents, a getting started guide, source code and library files (jar files). The ReadMe file has most of the information you need to get started. The getting started guide introduces you to most of the concepts behind BSF with some code example snippets. Even with all of the above, setting up BSF is not for the averagely motivated person. The BSF guide is good but it needs a little more meat it's not a step-by-step tutorial. If you like to tinker, then BSF is for you, but if you rarely wander from the confines of your favorite Java IDE, BSF may not be for you at this stage. Eventually I got some sample code that I'd written to work. I had a problem and needed to look at the BSF source to figure out what was happening. It was a little tougher than I thought it would be (see the sidebar for details), but it wasn't impossible. One of the keys to getting started smoothly is to make sure the jar files for BSF and those for your scripting language are on the CLASSPATH. Two jar files ship with BSF that you need on your classpath: bsf.jar and bsfengines.jar. The bsf.jar has the core BSF files. The bsfengines.jar has the language drivers, i.e., the language engines. You also need the jar files for your language on the classpath, e.g., if you are using JPython you need JPython.jar on your classpath. (JPython.jar ships with the JPython distribution.) For NetRexx you need NetRexxC.jar, NetRexxR.jar and tools.jar on your classpath; for TCL (JACL) you need JACL.jar; and for JavaScript you need js.jar and jstools.jar. The example I'm going to highlight in this article will focus on integrating JPython. However, with slight modification you can incorporate TCL, JavaScript, NetRexx and so on. I'll leave the modification up to you. For the code example, we'll map in an instance of a class to the BSFManager, load a script and then execute it. The script will have code that interacts with the class instance we mapped in the BSFManager. The class we'll use should be familiar to you if you've been following this series. Basically, it's a variation of the Statistics class from the last two articles. The Stats class (see Listing 1) is a simple class that figures out the mean, mode and median for a given set of numbers. The script that we're going to use to manipulate this instance of Stats is very short (see Listing 2) it prints out the mean, mode and median price of a list of houses.
print "The mode of the houses is %2.2f" % houses.mode The houses variable is an instance of the Stats class that gets mapped into the BSFManager. The steps to use a scripting language in BSF are as follows:
The first thing that Listing 3 does is to register JPython with the BSF manager:
BSFManager manager = new BSFManager (); The manager has a method called registerScriptingEngine that takes three arguments: the name of the scripting language; the fully qualified class name of the BSF engine corresponding to the scripting language; and an array of string that corresponds to all the possible file extensions for the scripting language. Remember, the BSF ships with several engines for various languages, so you can easily change the above to work with JavaScript or NetRexx or whatever. Once you've registered the scripting language, you can load its engine and start working with scripts. To load the engine, call the manager's loadScriptingEngine method:
//Load JPython engine After the scripting engine is loaded, you can start evaluating expressions and executing scripts. A scripting language expression is evaluated by calling the eval method on the manager.
//Execute an expression. The eval method takes five arguments as follows: the name of the scripting language, the name of the file name associated with the expression, the row of the expression, the column of the expression, and last the expression. The above expression is "2+32". When the above code runs, it prints out 34. Of course, in addition to evaluating expressions, you can execute scripts. Before evaluating scripts you can map objects (or an entire object model) into the BSF manager. All objects that are mapped in the BSF manager are available to the scripts. The following code instantiates an instance of Stats, and then maps that instance into the BSF manager:
//Map some objects that the script will use. One way to map objects into the BSF manager is to call the declareBean method. The declareBean method takes three arguments: the script variable name of the bean (object), the instance and the class of the instance. After you've mapped in your application's scriptable objects into the manager, you can load and run a script as follows:
//Load the script and execute it. The manager's exec takes the same arguments as the eval method, the only difference being that the exec method doesn't return a value, i.e., it returns void. Notice that the scripting language can be decided based on the filename of the script by using the manager's getLangFromFilename method. I have showed how to register a language, map application scriptable objects and execute a script. You can easily change the above to work with JavaScript, NetRexx and so on.
Parting Shots
I hope to see BSF used in a lot of development tools like IDE, editors and modeling tools so that the developers can easily customize their environment. Java has wonderful features that make creating scripting languages easy. The class reflection and bean introspection APIs are a great basis for integrating scripting languages. Once the scripting language has metadata about a Java's class properties, events and methods, it can use this data to change properties, handle events and invoke methods. BSF complements this feature by providing you with a common way to map Java objects to scripting languages and a common interface for integrating your application with a multitude of scripting languages. Components (JavaBeans) and distributed components (e.g., CORBA, EJB and RMI) have a symbiotic relationship with high-level languages. For example, Visual Basic did well because of VBX, OCX and ActiveX components and COM/ActiveX/DCOM did well because of tools like Visual Basic, PowerBuilder and Delphi. On the Java platform we have the component models, but we need the glue, i.e., tools for the high-level languages debuggers, IDEs and so on. BSF provides a common way to "glue" components into applications using a multitude of scripting languages. BSF does for JavaBeans what MASF does for ActiveX controls. LATEST JAVA STORIES & POSTS
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING JAVA NEWS
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||