The MegObjectExamplePlugin demonstrates the use of WBI's new MegObject API. MegObjects enable Megs to pass certain kinds of objects rather than simply streamed binary or character data. This ability can be useful, for example, if all your Megs can work on the same Document Object Model (DOM), as it eliminates the need for continual parsing and reparsing of the stream. MegObjects work very much like like MegStreams: On the input side, you get a MegObject from the RequestEvent (just like you'd read from a MegInputStream or MegReader), and on the output side you put your MegObject on the RequestEvent (just like you'd write to a MegOutputStream or MegReader). Moreover, streams and objects can work together: If one Meg puts a MegObject, but the next requires stream data, WBI will handle the conversion from object to streamed data (however, translating the other way, from streamed data to object is the responsibility of the Meg author).
Try It Out
What It DoesThe MegObjectExamplePlugin sets up four Megs that demonstrate how to use MegObjects. The Megs will print messages such as "MegObject created from MegInputStream" to indicate what they are doing with the document data.
How It Works
The MegObjectExamplePlugin does not change the contents of the retrieved document, but it does change the form of the data as it runs through a series of Editors.
The following diagram shows how the MegObjectExamplePlugin processes
a response from a web server: the four
To use a MegObject, you must create a class implements the MegObject interface. To implement a MegObject, methods for transforming the object into streamed data, and for copying or cloning the object must be written. In this case, ExampleMegObject implements MegObject, simply reading data from the input stream and saving it in a StringBuffer. In this case, the getInputStream() method is implemented simply by wrapping a ByteArrayInputStream around the bytes of the characters in the StringBuffer.
TestEditor1 tries to retrieve a MegObject from the RequestEvent passed to the handleRequest() method. Specifically, it first tests whether the MegObject is of the correct type using the RequestEvent.getMegObjectType() method. If it is, then it is passed along to the next Meg in the chain using RequestEvent.putMegObject(). If there is no MegObject or if the object is not of the proper type (i.e., its class is not ExampleMegObject, this editor creates an ExampleMegOject and passes it along.
TestEditor2 simply retrieves the MegInputStream and passes the data from it to the MegOutputStream. This demonstrates that even when a MegObject has been passed from the previous Meg in the chain, WBI will automatically use the MegObject.getInputStream() method to convert the object back into streamed data. In this way, the use of objects and streams can be mixed seamlessly from the programmers point of view. Some key WBI classes that were used:
Known ProblemsThere are no known problems with this example plugin. If you should happen to experience any, please inform us!).