Using Java data sources

You can use a Java class as a data source. Custom Java class files can be installed in the classes subdirectory of the Generator installation directory, or you can specify a directory in the User Classpath field of the Generator tab using JRun Administrator. Generator looks for external Java class data sources in the classes directory before searching in any directories included in the User Classpath field.

The Java class must meet the following requirements:

It must implement a static public method called getStream that accepts a single Hashtable type argument and returns an object of type InputStream.
The getStream method must return either an InputStream object containing a valid Generator data source, or a NULL object if a valid data source cannot be provided when the Java class is called at runtime.

If the Java class is included in an archive (JAR) file, be sure to include the archive name and .jar extension in the User Classpath entry. If the Java class references external libraries or .dlls, append the path to those dependencies to your system's path environment variable so they can be found at run time.

JRun loads a Java class on the first call for it; after that it remains loaded by JRun and the instance is reused for subsequent calls to the class. When testing newer Java classes for use with Generator, you need to stop and restart JRun to purge the outdated class from memory and cause the newer class file to be loaded on the next call to it.


 
Sample Java data source class

A sample Java data source class is shown here. This data source can be any Java class, but it must have the getStream method as defined in the sample class below. The getStream method is called by Generator, and it must return an inputStream object or a NULL object if the data source can not be returned.

import java.io.*;
import java.util.*;

     //
     // Sample data source class.
     //
     public class BlimpData
     {
         // This method must be present in a Generator data source class.
         public static InputStream getStream(Hashtable hashtable)
         {
             // Enumerate each key/value pair passed as an argument to this
             // data source. These are the key value pairs passed on the URL.
             Enumeration hashEnum = hashtable.keys();

             // Enumerate each key within the hash table.
             while (hashEnum.hasMoreElements())
             {
                 // Get the hash table key.
                 String hashKey = (String) hashEnum.nextElement();

                 // Using the hash table key, get the hash table value.
                 String hashValue = (String) hashtable.get(hashKey);

                 // Use the hash table key/value pair as arguments for
                 // accessing the custom data source. In this example,
                 // these values are ignored and we pass back the static
                 // data strings shown below to Generator.
             }

             // Create a string buffer to contain the data source.
             StringBuffer buffer = new StringBuffer();

             // Reset the buffer size.
             buffer.setLength(0);

             // The first line of a data source always contains the column names.
             buffer.append("Sport, Date, City1, City2, Score1, Score2, Details\n");

             // All other lines of a data source contain the comma-separated data.
             // Instead of listing data as shown in this example, 
             // you may want to create a data source class that reads
             // the data from an external source and converts it to a data stream
             // to be passed back to Generator. This data is formatted for using 
             // with the Replicate template command. It would need to be in a different
           		// format if it were used with the Generator Environment Variable button
		// or Plot template command.
		buffer.append("NFL, 10/16/97, San Diego, Kansas City, 3, 3, Final\n");
             buffer.append("NFL, 10/19/97, Jacksonville, Dallas, 22, 26, Final\n");
             buffer.append("NFL, 10/19/97, Carolina, New Orleans, 13, 0, Final\n");
             buffer.append("NFL, 10/19/97, New England, New York, 19, 24, Final\n");
             buffer.append("NFL, 10/19/97, Arizona, Philadelphia, 10, 13, Final / OT\n");
             buffer.append("NFL, 10/19/97, Seattle, St Louis, 17, 9, Final\n");
             buffer.append("NFL, 10/19/97, Washington, Tennessee, 14, 28, Final\n");
             buffer.append("NFL, 10/19/97, San Francisco, Atlanta, 35, 28, Final\n");
             buffer.append("NFL, 10/19/97, Miami, Baltimore, 24, 13, Final\n");
             buffer.append("NFL, 10/19/97, Pittsburgh, Cincinnati, 26, 10, Final\n");
             buffer.append("NFL, 10/19/97, New York, Detroit, 26, 20, Final / OT\n");
             buffer.append("NFL, 10/19/97, Denver, Oakland, 25, 28, Final\n");
             buffer.append("NFL, 10/20/97, Buffalo, Indianapolis, 20, 9, Final\n");

             // Convert the string buffer to an input stream.
             InputStream inputStream = new ByteArrayInputStream(buffer.toString().getBytes());

             // Return the input stream to be parsed by Generator.
             return inputStream;
         }
     }

To use a Java data source class:

1 Make sure that Flash 4 and Generator are properly installed on your system or Web server.
2 Copy the compiled Java data source class to the classes directory within the Generator installation directory. For a standard Generator installation in Windows, this directory is C:\Program Files\Macromedia\Generator\classes. On UNIX systems, it would be similar to <generator parent>/classes.
3 In Flash 4, display the template command or Generator object with which you want to associate the Java class.
4 Specify a URL like the following in the data source field:
fgjava:///JavaClassName?SampleArg1=SampleValue1&SampleArg2=SampleValue2
This URL is a special Generator extension that directs Generator to load a special module that interprets the arguments of the URL and calls a Java class.
When the parameter names and values in the URL are processed by Generator, any + characters are turned to spaces, and characters specified in URL encoding (%xx) are converted back to characters. Also, the portion of the URL normally occupied by a host name must be empty.

Each section of this URL is described below:

fgjava:/// specifies to Generator that it should interpret the rest of the URL as a Java data source.

JavaClassName identifies the actual Java class to invoke as the data source. Notice that the class extension is not specified.

?SampleArg1=SampleValue1&SampleArg2=SampleValue2 specifies arguments to be passed to the Java data source class. These parameters are passed as members of a hash table to the data source class.