package com.marinilli.b2.c7.deploylet;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.rmi.*;
import java.io.*;

/**
 * Chapter 7 - ClientDeploylet
 *
 * The client-side of the Deploylet interface .
 *
 * @author Mauro Marinilli
 * @version 1.0
 */
public abstract class ClientDeploylet extends JApplet implements Deploylet {
  private Deploylet server;

  //
  // Applet methods
  //

  /**Construct the applet*/
  public ClientDeploylet() {
  }

  /**Initialize the applet*/
  public final void init() {
    //
    try {
      String serverName = getParameter("name");
      server = (Deploylet) Naming.lookup(serverName);
      System.out.println("Deploylet-server="+server.getServer());
      log("*** logged: " + getClass().getName() + "\nwith ServerDeploylet: " + serverName);
      log("init()");
      initApplication();
    } catch (Exception e) {
      System.out.println("ClientDeploylet during ServerDeploylet lookup: "+e);
    }
  }

  /**Start the client applet*/
  public final void start() {
    try {
      resolve();
    } catch (Exception ex) {
      log(ex);
    }
  }

  /**Stop the applet*/
  public final void stop() {
    // first stop client deploylet
    stopApplication();
    // then stop server deploylet
    try {
      server.stopApplication();
    } catch (Exception ex) {
      log(ex);
    }
  }

  /**Destroy the applet*/
  public void destroy() {
  }

  /**Get Applet information*/
  public String getAppletInfo() {
    return "Client Deploylet version 1.0";
  }



  //
  // Deploylet methods
  //

  /** Implements the related Deploylet method */
  public void publish() {
  }

  /** Implements the related Deploylet method */
  public void resolve() {
    try {
      server.resolve();
    } catch (Exception e) {
      log(e);
    }
    startApplication();
  }

  /** Implements the related Deploylet method */
  public void applicationUpdate() {
  }

  /** Implements the related Deploylet method */
  public void applicationUninstallation() {
  }

  /** Implements the related Deploylet method */
  public void applicationInstallation() {
  }

  /** Implements the related Deploylet method */
  public void applicationConfiguration() {
  }

  /** Implements the related Deploylet method */
  public void resoucesInstallation() {
  }

  /** Implements the related Deploylet method */
  public void JREPreparation() {
  }

  /** Implements the related Deploylet method */
  public abstract void startApplication();

  /** Implements the related Deploylet method */
  public void stopApplication() {
  }

  /** Implements the related Deploylet method */
  public ServiceResult requestService(DeploymentService ds) {
    return null;
  }

  /** Implements the related Deploylet method */
  public Deploylet getServer() {
    return null;
  }

  /** Implements the related Deploylet method */
  public Deploylet getClient() {
    return this;
  }

  /** Implements the related Deploylet method */
  public void setClient(Deploylet d) {
    //do nothing
  }

  /** Implements the related Deploylet method */
  public void setServer(Deploylet d) {
    server = d;
  }

  /** a convenience method for DebugService access */
  protected void log(String m){
//    DebugService.log("[" + new java.util.Date()+ "] " + m );
    DebugService.log(" - " + m );//shorter
  }

  /** a convenience method for DebugService access */
  protected void log(Exception e){
    try {
      // in case of an exception report it to the server
      CharArrayWriter caw = new CharArrayWriter();
      PrintWriter pw = new PrintWriter(caw);
      e.printStackTrace(pw);
      String s = new String(caw.toCharArray());
      DebugService.log("Uncaught Exception trace:\n");
      DebugService.log(s);
      server.requestService(new DebugService("Uncaught Exception: "+e.getMessage()));
    }  catch (Exception exc) {
      System.out.println("ClientDeploylet.log: "+exc);
    }
  }

}