13. Invoking Scheme II (Lookup)

I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals.

William Churchill

In a previous section Section 11, “Invoking Scheme I (creating a Public API)” we defined a Scheme Interpreter Public API as a simple interface.

We also learned how to fire threads for execution, and how to print out stuff to the console window, and how to cancel tasks (see Section 12, “Noodle Threads (threading, progress, console)”).

And we also bundled our preferred Scheme interpreter in a Library Wrapper module (see Section 10, “Wrapping Scheme into our IDE (Library Wrappers)”).

So we have all the tools required to implement that generic Scheme Interpreter interface.

In this section we'll implement that Public API, and we'll learn how to find the implementations of that interface using one of the most important tools in the NetBeans Platform: the Lookup library.

13.1. Invoking Scheme (finally!!!)

Invoking the SISC Scheme Interpreter from Java is not that difficult. There're there's documentation on how to do it, so I won't cover the details here. (I may make a blog entry on that later on).

But let me clarify how I'm doing it with the NetBeans Platform, just in case you're interested in trying out yourself.

The very first step is (of course!) to build a new NetBeans module. I'll be calling it the Scheme SISC Interpreter Module. We will be needing dependencies on the SISC Library Wrapper Module (that contains the SISC libraries) and with the Scheme Interpreter API Module (the one that contains the Scheme Interpreter Interface). [13]

In this new "SISC Interpreter Module" we'll be creating an implementation of the Scheme Interpreter Interface (that was defined a while ago, see Section 11.2.2, “One single interface to rule them all”). The implementation should look something like this:

import (sisc libraries here);
public class SISCSchemeInterpreter
  implements SchemeInterpreter
{
  public String getName() { return "SISC"; }
  public void eval( SchemeCallback aCallback, java.io.File aFile )
  {
    // evaluate a scheme file here using sisc libraries
  }
  public void eval( SchemeCallback aCallback, java.io.Reader someExpressions )
  {
    // evaluate a Reader here using sisc libraries
  }
}

Of course I've omitted the lengthy details here (but I'll be releasing the source code really soon, in case you're interested. If you're really interested you may want to download a preliminar version).

13.2. Looking-up for a Scheme implementation

Figure 52. Looking up for a Scheme implementation

Looking up for a Scheme implementation

So we have a Scheme Interpreter implementation inside our module. To use this exact implementation in another module all we need to do is to add a new dependency with this implementation, then create a "new SISCSchemeInterpreter()" and start using our implementation.

Simple, right?

But, hold down just a minute.

What happens if we have another Scheme interpreter? Shall we add a dependency with that other Scheme intepreter and then invoke it directly? What if we have ten different Scheme interpreters?

What if we want to iterate over all Scheme intepreters available in the system?

The answers to all these questions are included in the NetBeans Lookup Library (the "solution to communication between components"). A great library with lots of functionalities available as a simple jar file [14] that you can reuse in your standalone applications (even in your server-side applications!).

The NetBeans Lookup Library has lots of interesting functionalities. We'll see different applications of the library in upcoming sections. But at the moment we'll concentrate in this great feature: the Lookup Library is compliant with the Service Provider Interface as defined in the Jar File Specification (yes, NetBeans people love following standards), and can be used to register and to search implementations of services in the classpath.

So let's see how we can register our Scheme Interpreter implementation and then how to look-it-up using the Lookup Library.



[13] If you need help on how to define a new module, or on how to add a dependency with an existing module, you may want to go take a look up your answers at the Section 14, “Alphabetical Index”

[14] The "platform6/lib/org-openide-util.jar" file in your NetBeans installation directory


blog comments powered by Disqus