package org.jruby.trinidad;

import java.io.File;
import java.io.IOException;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.catalina.Context;
import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleException;

public class HotDeployObserver extends Thread {
    private final Context applicationContext;
    private final File monitor;
    private final long delay;

    private boolean interrupted;
    private long lastModified;

    public HotDeployObserver(Context applicationContext, String monitor, long delay) {
        super("TrinidadHotDeployObserver");
        this.applicationContext = applicationContext;
        this.monitor = new File(monitor);
        this.delay = delay;
        setDaemon(true);
    }

    public static void observe(Context applicationContext, String monitor, long delay) {
        HotDeployObserver observer = new HotDeployObserver(applicationContext, monitor, delay);
        observer.createMonitor();
        observer.start();
    }

    public void createMonitor() {
        try {
            monitor.createNewFile();
            lastModified = monitor.lastModified();
        } catch (IOException io) {
            System.err.println("Was not allowed to create the monitor: " + monitor.getAbsolutePath());
            interrupted = true;
        }
    }

    public void observeAndRestart() {
        boolean monitorExists;
        try {
            monitorExists = monitor.exists();
        } catch(SecurityException  e) {
            System.err.println("Was not allowed to check monitor existance: " + monitor.getAbsolutePath());
            interrupted = true;
            return;
        }

        if (monitorExists) {
            long last = monitor.lastModified();
            if (last > lastModified) {
                lastModified = last;
                restartApplicationContext();
            }
        } else {
            createMonitor();
        }
    }

    public void run() {
        while(!interrupted) {
          try {
            Thread.sleep(delay);
          } catch(InterruptedException e) {
          }
          observeAndRestart();
        }
    }

    private void restartApplicationContext() {
        interrupted = true;
        applicationContext.reload();
    }
}

@@ -1231,39 +1231,38 @@
 rr.println(%22
-Was not allowed
+%5BERROR%5D Unable
  to create t
@@ -1271,16 +1271,21 @@
  monitor
+ file
 : %22 + mo
@@ -1503,16 +1503,17 @@
  %7D catch
+ 
 (Securit
@@ -1545,70 +1545,507 @@
     
-System.err.println(%22Was not allowed to check monitor
+// double check. Capistrano removes the parent directory temporarily%0A            try %7B%0A                Thread.sleep(500);%0A            %7D catch (InterruptedException e1) %7B%0A                System.err.println(%22%5BERROR%5D Thread interrupted%22);%0A                interrupted = true;%0A                return;%0A            %7D%0A            try %7B%0A                monitorExists = monitor.exists();%0A            %7D catch (SecurityException e1) %7B%0A                System.err.println(%22%5BWARNING%5D The monitor file doesn't
  exist
-ance
 : %22 
@@ -2086,34 +2086,95 @@
         
-interrupted = true
+    System.err.println(%22%5BWARNING%5D Trying to check it again in %22 + delay + %22ms%22)
 ;%0A      
@@ -2175,32 +2175,36 @@
             
+ 
+   
 return;%0A
         %7D%0A%0A 
@@ -2183,32 +2183,46 @@
         return;%0A
+            %7D%0A
         %7D%0A%0A     

fix TRINIDAD#39: Hotdeploy issue with capistrano