Java Q&A
by Cliff Berg

Listing One
/** Channel.java
 * A channel that displays an image, unique for each user.
 * Copyright 1997 by Digital Focus. This code may be used for non-commercial 
 * purposes. No warrantee or guarantee is implied.
 */

public class Channel extends marimba.channel.ApplicationFrame
{
    public Channel()
    {
    }
    
    /**
     * Start the application. This method is called by the tuner to 
     * notify that the application is started. Assumes that a context exists.
     */

    public void start()
    {
        String profile;
        byte[] bp = context.getProfile();
        if ((bp == null) || (bp.length == 0))
        {
            // Ask user to set up profile.
            // Our profile consists only of the user-id, given to the user by
           // the system administrator.
            
            frame = new ProfileFrame(context);
        }
        else
        {
            profile = new String(bp, 0);
            System.out.println("Your profile is " + profile);
        }
        
        try
        {
            java.awt.MediaTracker mt = new java.awt.MediaTracker(this);
            java.net.URL imageurl = null;
            imageurl = new java.net.URL(context.getCodeBase(),"imagefile.gif");
            image = context.getImage(imageurl);
            mt.addImage(image, 0);
            try { mt.waitForAll(); } catch (InterruptedException ex) {}
            imageFrame = new ImageFrame("My Image", image);
            imageFrame.resize(image.getWidth(this), image.getHeight(this));
            imageFrame.show();
        }
        catch (Exception ex) { ex.printStackTrace(); }
    }
    
    /**
     * Destroy the Channel, hide and dispose the base frame.
     */
    public void stop()
    {
        hide();
        dispose();
    }

    /**
     * Handle events.
     */

    public boolean handleEvent(java.awt.Event event)
    {
       if (event.id == marimba.channel.Application.DATA_UPDATE_EVENT)
       {
            return true;
       }
       else if (event.id == marimba.channel.Application.DATA_AVAILABLE_EVENT)
       {
            context.restart();
            return true;
       }
       else if (event.id == marimba.channel.Application.DATA_INSTALLED_EVENT)
       {
            return true;    // will not happen; here for completeness only
       }
       else if (event.id == marimba.channel.Application.DATA_NOTIFY_EVENT)
       {
            return false;   // don't start if we are not running
        }
        else if (event.id == java.awt.Event.WINDOW_DESTROY)
        {
            context.stop();
            return true;
        }

        return false;
    }
    
    private java.awt.Frame frame;
    private java.awt.Frame imageFrame;
    private java.awt.Image image;
}

class ImageFrame extends java.awt.Frame
{
    public ImageFrame(String title, java.awt.Image image)
    {
        super(title);
        this.image = image;
    }
    
    public void paint(java.awt.Graphics g)
    {
        if (image != null) g.drawImage(image, 0, 0, this);
    }
    
    java.awt.Image image;
}

class ProfileFrame extends java.awt.Frame
{
  private java.awt.Label label = new java.awt.Label("Please enter user id:");
  private java.awt.TextField tf = new java.awt.TextField(10);
  private java.awt.Button okbutton = new java.awt.Button("OK");

   public ProfileFrame(marimba.channel.ApplicationContext context)
   {
        super("Please enter your user id");
        this.context = context;

        setLayout(new java.awt.FlowLayout());
        add(label);
        add(tf);
        add(okbutton);
        resize(200, 100);
        show();
   }
    
    public boolean handleEvent(java.awt.Event event)
    {
        if (event.id == java.awt.Event.ACTION_EVENT)
       {
            String s = tf.getText();
            byte[] b = new byte[s.length()];
            s.getBytes(0, s.length(), b, 0);
            context.setProfile(b);
            context.restart();
            dispose();
            return true;
        }
        else return false;
    }
    
    private marimba.channel.ApplicationContext context;
}

Listing Two
/** Plugin.java
 * Castanet transmitter plugin, which dynamically updates a user's download 
 * set based on settings in a user database. To accomplish this, we will only 
 * publish class files, and user this plugin to add other files as needed.
 * Copyright 1997 by Digital Focus. This code may be used for non-commercial 
 * purposes. No warrantee or guarantee is implied.
 */
 
public class Plugin extends marimba.plugin.Plugin
{
    /**
     * Processes a subscribe or update request for this plugin.  This
     * is where logging data is processed and the personalization of
     * the index occurs.
     *
     * If this returns without an exception it is assumed that the
     * logging data, if any, was processed successfully.  An
     * IOException can be thrown if there's a problem processing the
     * logging data.
     */

    public void processRequest(marimba.plugin.RequestContext context) 
                                                   throws java.io.IOException
    {
        debugPrint("a");
        super.processRequest(context);
        
        // Get the user-id from the profile.
        
        debugPrint("b");
        byte[] b = context.getProfileData();
        if ((b == null) || (b.length == 0))
       {
            // User does not have a profile; quit
            debugPrint("User has no profile");
            return;
        }
        debugPrint("c");
        String userid = new String(b, 0);
        debugPrint("userid=" + userid);
        
        // Get user data.
        
        Users.readUsers();
        
        String imagefilename = (String)(Users.users.get(userid));
        debugPrint("for " + userid + " imagefilename=" + imagefilename);
        debugPrint("There are " + Users.users.size() + " entries");
        if (imagefilename == null) return;
        debugPrint("d");

        // Put the image for that tuner in the download set.

        java.io.File imagefile = new java.io.File(imagefilename);
        context.addFile("imagefile.gif", imagefile);
    }
}

/**
 * A simple user database.
 */
 
class Users
{
    public static void readUsers()
    {
        if (users == null) users = new java.util.Hashtable();
        java.io.FileInputStream fstream = null;
        java.io.DataInputStream ostream = null;
        try
        {
           // Read in the hashtable
            
          java.io.File userfile = 
           new java.io.File ("d:\\column\\may97\\channel\\plugin\\userfile");
          fstream = new java.io.FileInputStream(userfile);
          ostream = new java.io.DataInputStream(fstream);
          for (;;)
          {
                String userid = ostream.readLine();
                if (userid == null) break;
                userid = userid.trim();
                String imagefilename = ostream.readLine();
                imagefilename = imagefilename.trim();
                users.put(userid, imagefilename);
          }
        }
        catch (java.io.IOException ex)
       {
            // done
        }
        finally
        {
            if (ostream != null) try { ostream.close(); } 
                                         catch (java.io.IOException ex) {}
            else if (fstream != null) try { fstream.close(); } 
                                          catch (java.io.IOException ex) {}
        }
    }
    
    public static void writeUsers() throws java.io.IOException
    {
        if (users == null) users = new java.util.Hashtable();
        java.io.FileOutputStream fstream = null;
        java.io.PrintStream ostream = null;
        try
        {
          java.io.File userfile = 
            new java.io.File("d:\\column\\may97\\channel\\plugin\\userfile");
          fstream = new java.io.FileOutputStream(userfile);
          ostream = new java.io.PrintStream(fstream);
          java.util.Enumeration ek = users.keys();
          for (java.util.Enumeration e = 
                               users.elements(); e.hasMoreElements();)
          {
                String userid = (String)(ek.nextElement());
                String imagefilename = (String)(e.nextElement());
                ostream.println(userid);
                ostream.println(imagefilename);
          }
        }
        catch (java.io.IOException ex)
        {
            throw new java.io.IOException();
        }
        finally
        {
            if (ostream != null) ostream.close();
            else if (fstream != null) try { fstream.close(); 
                                       } catch (java.io.IOException ex) {}
        }
    }
    static java.util.Hashtable users;
}







