RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Enhance Eclipse RCP with Scripting : Page 2

JSR-223 defines various kinds of interactions between scripting languages and the Java platform. Learn how to enhance the Eclipse platform and its Rich Client Platform applications with JSR-223 scripting capabilities.

The Scripting Plug-in and Its Fragments
In the accompanying source code for this article, the merge between scripting languages and Eclipse (see Sidebar 1. The Eclipse Rich Client Platform) goes through the definition of the com.devx.scripting plug-in (see Sidebar 2. Anatomy of an Eclipse Plug-in), which provides the rest of the platform with common access to scripting resources and a set of plug-in fragments—one for every scripting language the application is going to support as shown in Figure 1.

Click to enlarge

Figure 1. The com.devx.scripting Plug-in Architecture

Every fragment contributes a given interpreter (Ruby, JavaScript, AppleScript, etc.) and its respective JSR-223 engine, which exposes the interpreter through the javax.script scripting API. Interpreters and JSR-223 wrappers can be bundled together or developed and shipped separately.

This plug-in setup offers various advantages, including the following:

  • You can control explicitly which languages are added to the platform by limiting the number of distributed fragments.
  • The user can slim down your application's installation by selecting from the update site only the scripting languages he or she needs.

The plug-in defines the IScript interface, which represents the script shown in following listing:

public interface IScript {
   // @return the URI which points to the script code.
   public String getURI();
   // @return the script extension 
   public String getExtension();
   // @return a Reader which points to the script code
   public Reader getReader() throws IOException;
   // @return the script unique id
   public String getId();
   // @return the namespace (plug-in id) of the script
   public String getNamespace();
	// @return run the script in a modal context?
	public boolean isModal();

The scripting plug-in exposes the com.devx.scripting.ScriptSupport class, which defines public methods for common script-related needs that arise within the Eclipse platform. These needs include running a script in the context of a progress monitor (such as the one shown while compiling sources in Eclipse), or retrieving a list of the supported languages by querying the ScriptEngineManager. The following listing shows part of the public interface of the class (Refer to the source code for the implementation):

public void runScript(final IScript script,
   Map<String,Object> params) throws ScriptException;
public List<Language> getSupportedLanguages();

Execution of External Scripts
With only these basic elements, you can already provide a way to run custom scripts inside your Eclipse application. As an example, you can contribute an Eclipse action that allows the user to choose a script from the file system and run it within the platform. Figure 2 and Figure 3 preview the final results.

Figure 2. The 'Run Script' Action
Figure 3. The 'Run Script' File Selector Allowing Scripts of All Supported Types

The action shows a file selector that filters only the available scripting languages by querying the com.devx.scripting.ScriptSupport class, which in turn asks the javax.script.ScriptEngineManager for the supported languages. Finally, the javax.script.ScriptEngineManager scans the plug-in classpath and its fragments using the Service Provider mechanism.

To obtain the previewed result, you define an extension to the org.eclipse.ui.actionSets extension point, which provides an additional menu action to the application, implemented by the com.devx.scripting.actions.RunScriptAction class shown in Listing 1.

That's it. You can now run Ruby, Groovy, and other scripts within your application, taking advantage of their power and peculiarities. You can prepare scripts that perform a bulk change on part of the workspace or you can have part of your build and deploy process written in a scripting language and invoked by the developer from within the platform when needed.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date