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.

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):

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.

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.