/*
    Start applet
    
    This class is *not* part of the installation, but instead
    becomes part of your distribution files.

    Starts a web browser with the applet's URL on the command line. If this URL
    is an applet, then starts the applet.

    Notes: 
     1. Include the StartApplet.class file from the Samples subdirectory 
        in your list of files on the Files panel.
     2. Create a menu item with the following settings:
         Java command: Yes
         Swing 1.1: Yes
         Executable file: StartApplet.class
         Command arguments: (the URL which you want launched when this class is activated)
     3. Add "GetBrowser" and "PromptForBrowser" (without the "") to the Customs panel as 
        the last 2 class in the "After uninstaller created" field.
     4. Copy GetBrowser.class, PromptForBrowser.class, and PromptForBrowser$Action.class
        in the JExpressInstaller subdirectory.
     5. You probably want to search for !!! in this file and handle the exception 
        if the browser couldn't be launched.
     6. You are welcome to adapt this to meet your needs as long 
        as the product that uses this class is being installed with JExpress

    Copyright 1997-2010 DeNova
    
*/

import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.util.*;


public class StartApplet {

    static public void main ( String args[] ) {

        if ( args. length > 0 ) {

            appletUrl = args [ 0 ];

            logFile = new File ( LogFilename );
            logging = logFile. exists ();

            if ( logging ) {
                try {
                   out = new RandomAccessFile ( logFile, "rw" );
                   out. seek ( out. length () );
                }
                catch ( IOException e ) {
                    logging = false;
                    System. err. println ( "Exception: " + e );
                    e. printStackTrace ();
                }
            }
        
            if ( getBrowserName () ) {
            
                launchApplet ();

            }
        
            if ( logging ) {
                try {
                    out. close ();
                }
                catch ( IOException e ) {
                    System. err. println ( "Exception: " + e );
                    e. printStackTrace ();
                }
            }
        }
    }


    static boolean getBrowserName () {

        boolean ok = false;
        
        try {
            File f = new File ( BrowserProperties );
            FileInputStream in = new FileInputStream ( f );

            if ( in != null ) {

                Properties properties = new Properties ();
                properties. load ( in );
                browserName = properties. getProperty ( BrowserPathname );
                in. close ();
                
                if ( browserName != null &&
                     browserName. length () > 0 ) {
                         
                    ok = true;
                    log ( "Found browser: " + browserName );
                }
                
                else {
                    log ( "Unable to find browser: " + BrowserPathname );
                }
            }
        }
        catch ( Exception pe ) {
            log ( pe. getMessage () );
        }
        
        return ok;
    }


    static void launchApplet () {

        String command = browserName + " " + appletUrl;

        try {
            String classpath = System. getProperty ( "java.class.path", "");
            String envp[] = { "classpath=" + classpath };

            log ( "About to start browser: " + command );
            log ( "     classpath= " + classpath );
            
            Process p = Runtime. getRuntime(). exec ( command, envp );
            p. waitFor ();
            
            log ( "Finished running browser" );
        }
            
        catch ( Exception e ) {
            // !!! handle this exception
            log ( e. getMessage () );
        }
    }

    
    static void log ( String message ) {
        if ( logging ) {
            try {
      	        out. writeBytes ( message );
                    out. writeBytes ( "\n" );
            }
            catch ( IOException e ) {
                System. err. println ( "Exception: " + e );
                e. printStackTrace ();
            }
        }
    }
    
       

    static String appletUrl;    
    static String browserName;
    static File logFile;
    static RandomAccessFile out;    
    static boolean logging;

    static private final String LogFilename = "startapplet.log";
    static private final String BrowserProperties = "browser.properties";
    static private final String BrowserPathname = "browserPathname";
}