package com.neverwinterdp.server.shell;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;

import com.neverwinterdp.server.gateway.ClusterGateway;

public class ShellContext {
  private Map<String, Object> variables = new HashMap<String, Object>() ;
  private Console console ;
  private Timer   timer ;
  private ClusterGateway cluster ;
  private ExecuteContext currentExecuteContext ;
  private ExecuteContext lastExecuteContext ;
  
  public Map<String, Object> getVariables() { return this.variables ; }
  
  public Console console() { return this.console ; }
  
  public Timer getTimer() { return this.timer ; }
  
  public ClusterGateway getClusterGateway() { return this.cluster ; }
  
  public ExecuteContext getExecuteContext() { return this.currentExecuteContext ; }
  
  public ExecuteContext getLastExecuteContext() { return this.lastExecuteContext ; }
  
  public void connect(String ... members) {
    console = new Console(System.out) ;
    if(cluster == null) cluster = new ClusterGateway(members) ;
    else cluster.connect(members);
    timer = new Timer() ;
  }
  
  public void onStartCommand(ShellCommand shellCommand, ShellSubCommand shellSubCommand) {
    lastExecuteContext = currentExecuteContext ;
    currentExecuteContext = new ExecuteContext() ;
    console.newConsoleOutput() ;
  }
  
  public void onFinishCommand(ShellCommand group, ShellSubCommand command) {
    currentExecuteContext.setConsoleOutput(console.getTextOutput()) ;
  }
  
  public boolean isClose() { return cluster == null ; }
  
  public void close() {
    timer.purge() ;
    timer.cancel() ; 
    timer = null ;
    if(cluster != null) {
      cluster.close(); 
      cluster = null ;
    }
    console = null ;
  }
} 

@@ -465,24 +465,101 @@
 text ;%0D%0A  %0D%0A
+  public ShellContext() %7B%0D%0A    console = new Console(System.out) ;%0D%0A  %7D%0D%0A  %0D%0A
   public Map
@@ -1039,49 +1039,8 @@
  %7B%0D%0A
-    console = new Console(System.out) ;%0D%0A
     
@@ -1804,30 +1804,8 @@
  %7D%0D%0A
-    console = null ;%0D%0A
   %7D%0D

fix broken test