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:
A Java class that implements PlugletFactory.
A Java class that implements Pluglet, instances of which are created and returned by the PlugletFactory.
A Java class that implements PlugletStreamListener, if the Pluglet uses the getURL() or postURL() methods on the PlugletManager passed to plugletFactory.initialize().
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 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.
Line 1 is standard JavaScript to get the 0th
<EMBED>element in the page, which happens to be our pluglet. The functionscallPluglet0,callPluglet1,callPluglet2andcallPluglet3, declared on lines 3, 16, 29, and 42, respectively, are called from theonclickhandler of the HTML buttons in the page. Each of these functions calls a Java method implemented on the pluglet using the specialcallPlugletMethodJavaScript function defined on the XPConnect API for pluglet. The general form of this JavaScript method is:
- /** The return value of this function is a JavaScript associative array containing
- * an entry named 'value', whose value is a JavaScript String that is
- * the Java String returned by the conforming method named in the first argument.
- */
- function callPlugletMethod(<name of a conforming method implemented on Pluglet Class>,
- <JavaScript associative array containing an
- entry named 'value', whose value is
- JavaScript array (of length zero thru ten) of
- JavaScript strings. Each element in the
- array will be passed to the corresponding
- String argument in the conforming method.>,
- <JavaScript associative array containing an
- entry named 'value', whose value is a
- JavaScript number representing the length of
- 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 classSimplePlugletInstance.NOTE: One convenient interchange format would be to return JSON strings from your conforming methods.