This example illustrates how to create a pluglet and package it into a jar which you simply place in the browser's "plugins" directory. Before we get started, let's see it run!



Required Code Artifacts

Creating a pluglet requires creating several code arifacts:

  1. A Java class that implements PlugletFactory.

    Details
  2. A Java class that implements Pluglet, instances of which are created and returned by the PlugletFactory.

    Details
  3. A Java class that implements PlugletStreamListener, if the Pluglet uses the getURL() or postURL() methods on the PlugletManager passed to plugletFactory.initialize().

    Details

  4. A Java JAR file containing the .class files compiled from the above Java files. The manifest for the JAR must contain the following entries:

    Manifest attribute name Explanation Value for this example
    MIMEDescription The mime-type to be handled by this pluglet. application/x-simple-pluglet
    Pluglet-Class The fully qualified class name of the Java class implementing PlugletFactory. simple.SimplePluglet
  5. Some HTML markup that includes the pluglet. Note: this step is exactly the same as for any other native plugin.

    Details

Calling a Pluglet from JavaScript

This release of pluglets allows a limited degree of communication between JavaScript and the Pluglet class. Through the magic of Netscape's XPConnect code, it is possible to invoke, from JavaScript in the page in which the pluglet is embedded, an arbitrary method on the pluglet, provided the method returns String, and takes anywhere between zero and ten String arguments. Such a method is called a "conforming method" for the rest of this discussion.

In the above example, the four plain ole' HTML buttons labeled "Call Pluglet N arg" (where N is a number between 0 and 3) call a Java method on the pluglet. Click the Details button on the right to see the JavaScript code.

Details

Line 1 is standard JavaScript to get the 0th <EMBED> element in the page, which happens to be our pluglet. The functions callPluglet0, callPluglet1, callPluglet2 and callPluglet3, declared on lines 3, 16, 29, and 42, respectively, are called from the onclick handler of the HTML buttons in the page. Each of these functions calls a Java method implemented on the pluglet using the special callPlugletMethod JavaScript function defined on the XPConnect API for pluglet. The general form of this JavaScript method is:


  1. /** The return value of this function is a JavaScript associative array containing
  2.   * an entry named 'value', whose value is a JavaScript String that is
  3.   * the Java String returned by the conforming method named in the first argument.
  4.   */
  5. function callPlugletMethod(<name of a conforming method implemented on Pluglet Class>,
  6.                            <JavaScript associative array containing an
  7.                            entry named 'value', whose value is
  8.                            JavaScript array (of length zero thru ten) of
  9.                            JavaScript strings.  Each element in the
  10.                            array will be passed to the corresponding
  11.                            String argument in the conforming method.>,
  12.                            <JavaScript associative array containing an
  13.                            entry named 'value', whose value is a
  14.                            JavaScript number representing the length of
  15.                            the array.>);

The Java code for the methods calledFromJavaScriptN (where N is a number between 0 and 3) can be seen by expanding the Details button on the right. These methods are defined on the class SimplePlugletInstance.

Details

NOTE: One convenient interchange format would be to return JSON strings from your conforming methods.