| By Tugdual Grall | Article Rating: |
|
| November 16, 2005 05:00 PM EST | Reads: |
13,678 |
As developers or system administrators, we all have common tasks that must be replicated again and again. A few examples might be creating a new feature test, populating a test database with data, copying data from a file system to a database, or creating new user accounts.... You get the idea.
Scripting languages share a number of characteristics that make them ideal for managing such redundant tasks:
- Their built-in abstract data types let them easily manipulate lists, arrays, or other composite structures. Also, the languages are often extended with regular expression support and built-in sorting capabilities.
- They support dynamic typing.
- They offer automated memory and exception management.
- They don't need a compiler and can be run directly from a command line or interpreter - meaning you can just write and run.
Java is more than a simple language. It contains many low-level features, including networking, input-output, XML manipulation, and threading, that have contributed to its success. It's also an enterprise-ready solution based on the services provided by the J2EE platform, including persistence, transaction management, security, Web services, and open connectivity using J2EE Connector Architecture.
Java scripting languages extend the benefits of scripting development with the power of Java - with the goal of making Java development easier and more productive.
Java Scripting Language Offerings
Over the past few years, multiple Java scripting solutions have emerged, and many are encountering good adoption in the developer community. Most of today's Java scripting languages are based on open source projects that either port an existing scripting language to Java or simplify Java to offer a more high-level development approach.
Table 1 lists some of the most popular languages built around Java.
Scripting in Action
If you're not familiar with scripting languages, you might be skeptical about the benefits they provide. Let's look at some simple examples that illustrate these benefits.
Listing 1 provides a small Java program that processes a list of strings and prints those that are shorter than six characters.
Here's the same program written as a Groovy script:
list = ["BeanShell","Groovy","Jacl","JRuby","JudoScript","Jython","Rhino"]
shorts = list.findAll { it.size() >= 6 }
shorts.each { println it }
As you can see, using Groovy dramatically reduces the number of lines of code that you have to write. It's also easier to read.
In addition to a very concise syntax, most scripting languages provide high-level constructs for dealing with most of the components that current systems use. For example, many provide APIs for manipulating XML documents, simplifying database operations, or creating user interfaces. Some languages such as BeanShell, Groovy, and JudoScript also have native integration with Apache Ant to facilitate build and test tasks, as discussed in the next section.
Scripting Languages and Apache Ant
Using Ant to automate tasks such as building and testing development projects has become almost standard practice in the Java development cycle. However, there are often times when using Ant for complex tasks isn't feasible. In such cases, it may be easier to write a quick script using Groovy, JudoScript, or Bean-Shell and embed it in the Ant build.xml file.
The following example shows how BeanShell instructions can be embedded in a build.xml file:
You can also replicate Ant scripting using Groovy. In this case, you wouldn't use XML to manipulate the different tasks; instead, you'd use a real scripting language, which would allow better constructs such as loops and logical branching. You can even mix Ant and Groovy operations in a single script file.
<project name="antBeanShell" default="execScript" basedir=".">
<target name=" execScript ">
<!-- Run script from a file -->
<script language="beanshell" src="demo.bsh"/>
<!-- Run script in-line -->
<script language="beanshell"><![CDATA[
for(int i=0; i<10; i++ )
print( "i="+i );
]]></script>
</target>
</project>
Where Should You Use Scripting Languages?
As we've seen, scripts are well suited to automating redundant tasks such as building and testing applications. They can also be useful when you need to create quick prototypes for a specific feature.
Although some developers often question its use, you can use scripts to build complex applications - or at least use them inside complex applications or systems. Outside of the Java world, developers have used languages such as PHP, Perl, Python, and Tcl to create multiple Web sites or as part of complex integration projects.
The story is much the same with regard to Java scripting languages. While you might not be able to completely build a complex application using one of the scripting languages I've mentioned, these technologies can help you create more agile solutions.
As a developer, you may often have to add new business logic to your application due to constantly changing requirements. However, you may also need your application to be extensible so that advanced users or third-party developers can plug in their own modules.
Administrators and developers can use scripts to connect to a Java application (such as J2SE or J2EE) using JMX so they can configure and deploy it. This is another example where scripts leverage a low-level Java API to easily perform complex tasks.
For such uses, scripting languages are ideal. Already, some Web applications, such as Portal or Wiki, let developers add new features by publishing their own scripts to the product. Desktop applications based on Swing, SWT, or even native languages also offer extensibility based on scripts.
Another example is the service-oriented architecture world, where the goal is to interconnect various services. Here, using scripting technologies to provide the plumbing in a loosely coupled manner is especially practical, since scripts are easy to change and completely dynamic.
Wrapping It Up
Java scripting languages can provide the ideal solution for Java developers or system administrators who need to automate repeated tasks or create extensible applications, while still leveraging the power of Java and the J2EE platform. Along with reducing code, scripting languages integrate well with commonly used tools such as Ant. The dynamic nature of scripts also makes them ideal to use when you're adding features to existing products.
Another sign of the importance of scripting in current development projects is that scripting technologies will be directly integrated into Java; Java 6 (code name Mustang) will contain a JavaScript interpreter based on Rhino. Also, the JSR 223, Scripting for the Java Platform, is a JCP specification that defines a low-level API to expose a scripting language engine to the Java platform. The current reference implementation integrates PHP, JavaScript, and Groovy.
Published November 16, 2005 Reads 13,678
Copyright © 2005 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Tugdual Grall
Tugdual Grall is a principal product manager for J2EE and Web services at Oracle. He joined Oracle in January 1999 initially with Oracle France in consulting, and, since April 2002, he has worked with Oracle Application Server product management. His current areas of focus include J2EE and Web services with Oracle Containers for J2EE (OC4J).
- Kindle 2 vs Nook
- Why IBM’s Server Chief Got Busted
- Is Cloud Computing Like Teenage Sex?
- Industry Experts Discuss the State of Cloud Computing
- Performance Tuning Essentials for Java
- Confessions of a Ulitzer Addict
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- It's the Java vs. C++ Shootout Revisited!
- Cloud Computing Can Revitalize Your Career as Software Developer
- IBM Could "Reinvent" Java: Mills
- Oracle & Cloud Computing: Exclusive Q&A with SVP Richard Sarwal
- A Brief History of Cloud Computing
- Kindle 2 vs Nook
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- Why IBM’s Server Chief Got Busted
- Is Cloud Computing Like Teenage Sex?
- Industry Experts Discuss the State of Cloud Computing
- Performance Tuning Essentials for Java
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing Expo: Exclusive Q&A with Yahoo! SVP Cloud Computing
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- My Thoughts on Ulitzer
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- A Cup of AJAX? Nay, Just Regular Java Please
- Java Developer's Journal Exclusive: 2006 "JDJ Editors' Choice" Awards
- The i-Technology Right Stuff
- JavaServer Faces (JSF) vs Struts
- Rich Internet Applications with Adobe Flex 2 and Java
- Java vs C++ "Shootout" Revisited
- Bean-Managed Persistence Using a Proxy List
- Reporting Made Easy with JasperReports and Hibernate
- Creating a Pet Store Application with JavaServer Faces, Spring, and Hibernate
- What's New in Eclipse?
- Why Do 'Cool Kids' Choose Ruby or PHP to Build Websites Instead of Java?
- i-Technology Predictions for 2007: Where's It All Headed?








































