StreamAsset

Description

As the stream service is capable of streaming any kind of data, this stream asset comes in handy when the stream is a image or some other resource that is to be treated as an asset.

An example is when a chart is to be rendered on a page, the resource source can stream the chart data and the asset can be configured to call that specific resource source.

JFreeChart support

Tapestry Bayeux comes with a abstract JFreeChartResource (an implementation of the StreamResource) that makes it easy to stream a JFreeChart.

A SimpleJFreeChartResource implementation is also available. That implementation is sufficient in many cases. The example below uses that implementation.

All you have to do is concentrate on creating your chart and bayeux will handle all the dirty work for you.

Requirements

This component uses the StreamService service which in turn requires specific configuration of a stream resource source. Please refer to the service for configuration of that thingi.

Example

Presuming you have defined bayeux library in you application under the library name "bayeux":

The following illustrates how you can implement you own resource source capable of streaming a JFreeChart.

package com.nordija.tapestry.bayeux.stream.jfreechart;

public class JFreeChartResourceSource implements StreamResourceSource {
    public StreamResource getStreamResource(IRequestCycle cycle, Object[] parameters) throws IOException {
        //use the parameters to construct the chart you like
        DefaultKeyedValuesDataset dataset = new DefaultKeyedValuesDataset();
        dataset.setValue("one", 50);
        dataset.setValue("two", 20);
        dataset.setValue("three", 30);
        JFreeChart chart = ChartFactory.createPieChart("My Piechart", dataset, false, false, false);
        return new SimpleJFreeChartResource(chart, 100, 200, ImageFormat.JPEG);
    }
}
Then you have to configure your resource source in your application configuration:

<extension name="bayeux.stream.myjfreechart"
           class="com.nordija.tapestry.bayeux.stream.jfreechart.JFreeChartResourceSource"/>
Now you have configured a resource source capable of streaming the chart. To have it rendered you have to create a StreamAsset and hand it to a Image component.

First we create a method that creates the StreamAsset. The method could be defined in the page class for which the image is to be rendered:

public StreamAsset getMyStreamAsset() {
    return new StreamAsset(getStreamService(), "myjfreechart", new Object[]{}, this);
}
As you see the StreamAsset has to be constructed with the StgreamService. The preffered way to do so is to inject the StreamService into page class that constructs the asset.

Like this:
<inject property="streamService" object="service:com.nordija.tapestry.bayeux.StreamService"/>    
Then the StreamAsset has to be handed to a classic Tapestry Image component responsible of rendering the actual chart:
<img jwcid="@Image" image="ognl:myStreamAsset" src="#" alt="stream asset" />
Now you are done. The chart should be streamed!