package com.marinilli.b2.c7.deploylet;
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.io.*;
import java.util.*;

/**
 * Chapter 7 - ServerDeploylet
 *
 * The client-side of the Deploylet interface.
 *
 * @author Mauro Marinilli
 * @version 1.0
 */
public class ServerDeploylet extends UnicastRemoteObject implements Deploylet {
  private Deploylet client;
  private String serverDeployletName;

  /** constructor */
  public ServerDeploylet(String deployletName) throws RemoteException {
    if (System.getSecurityManager() == null) {
      System.out.println("new RMISecurityManager()");
      System.setSecurityManager(new RMISecurityManager());
    }
    try {
      String name = "//localhost/deploylet/" + deployletName;
      Naming.rebind(name, this);
      System.out.println("ServerDeploylet bound with name \""+name+"\"");
      serverDeployletName = name;
    } catch (Exception e) {
      System.err.println("ServerDeploylet - binding: " + e.toString() );
    }
  }

  //
  // Deploylet methods
  //

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

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

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

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

  /** Implements the related Deploylet method */
  public void applicationInstallation() {
    //client.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 void startApplication() {
    //do nothing
  }

  /** Implements the related Deploylet method */
  public void stopApplication() {
    //
    try {
      String serverName = "rmi://localhost/deployletRepository";
      ServerRepository server = (ServerRepository) Naming.lookup(serverName);

      if (server!=null) {
        server.remove(this.getName());
      }
    } catch (Exception e) {
      System.out.println("ServerDeploylet during ServerRepository lookup: "+e);
    }
  }

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

  /** Implements the related Deploylet method */
  public ServiceResult requestService(DeploymentService ds) {
    System.out.println("requestedService="+ds);
    if (ds instanceof DebugService) {
      //process debug service
      return processDebugService((DebugService)ds);
    }
    return null;
  }

  /**
   *
   */
  private ServiceResult processDebugService(DebugService ds){
    Object[] s = ds.getContents();
    System.out.println("Processing Debug message:");
    System.out.println("message="+s[0].toString()+"\nDeploylet log=\n"+s[1].toString());
    return null;
  }

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

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

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

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

  /** Implements the related Deploylet method */
  public String getName() {
    return serverDeployletName;
  }

}