package com.nitorcreations.willow.deployer;

import static com.nitorcreations.willow.deployer.PropertyKeys.ENV_DEPLOYER_HOME;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.hyperic.sigar.Humidor;
import org.hyperic.sigar.SigarProxy;

import com.google.inject.AbstractModule;
import com.google.inject.Provider;
import com.nitorcreations.willow.deployer.download.FileUtil;
import com.nitorcreations.willow.messages.WebSocketTransmitter;

public class DeployerModule extends AbstractModule {
  private static final class HumidorProvider implements Provider<SigarProxy> {
    Humidor humidor = Humidor.getInstance();
    @Override
    public SigarProxy get() {
      return humidor.getSigar();
    }
  }
  @Override
  protected void configure() {
    extractNativeLib();
    bind(WebSocketTransmitter.class).toInstance(new WebSocketTransmitter());
    bind(ExecutorService.class).toInstance(Executors.newCachedThreadPool());
    bind(SigarProxy.class).toProvider(new HumidorProvider());
  }
  protected void extractNativeLib() {
    String deployerHome = System.getenv(ENV_DEPLOYER_HOME);
    if (deployerHome == null) {
      deployerHome = System.getProperty("user.home") + File.separator + ".deployer" + File.separator + "repository";
    }
    File libDir = new File(new File(deployerHome), "lib");
    System.setProperty("java.library.path", libDir.getAbsolutePath());
    String arch = System.getProperty("os.arch");
    String os = System.getProperty("os.name").toLowerCase(Locale.ENGLISH);
    StringBuilder libName = new StringBuilder();
    if (os.contains("win")) {
      libName.append("sigar-");
    } else {
      libName.append("libsigar-");
    }
    if (os.contains("mac") || os.contains("darwin")) {
        if (arch.contains("64")) {
            libName.append("universal64");
        } else {
            libName.append("universal");
        }
    } else {
        libName.append(arch);
    }
    if (os.contains("win")) {
      libName.append("-winnt").append(".dll");
    } else if (os.contains("mac") || os.contains("darwin")) {
      libName.append("-macosx.dylib");
    } else if (os.contains("sunos")) {
      libName.append("-solaris.so");
    } else {
      libName.append("-").append(os).append(".so");
    }
    File libFile = new File(libDir, libName.toString());
    if (!(libFile.exists() && libFile.canExecute())) {
      InputStream lib = Main.class.getClassLoader().getResourceAsStream(libName.toString());
      FileUtil.createDir(libDir);
      if (lib != null) {
        try (OutputStream out = new FileOutputStream(libFile)) {
          byte[] buffer = new byte[1024];
          int len;
          while ((len = lib.read(buffer)) != -1) {
            out.write(buffer, 0, len);
          }
          libFile.setExecutable(true, false);
        } catch (FileNotFoundException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        } finally {
          try {
            lib.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      } else {
        throw new RuntimeException("Failed to find " + libName);
      }
    }
  }

}

@@ -145,46 +145,8 @@
 le;%0A
-import java.io.FileNotFoundException;%0A
 impo
@@ -166,32 +166,32 @@
 leOutputStream;%0A
+
 import java.io.I
@@ -367,16 +367,81 @@
 ecutors;
+%0Aimport java.util.logging.Level;%0Aimport java.util.logging.Logger;
 %0A%0Aimport
@@ -708,16 +708,16 @@
 itter;%0A%0A
-
 public c
@@ -761,16 +761,88 @@
 odule %7B%0A
+  private Logger log = Logger.getLogger(getClass().getCanonicalName());%0A
   privat
@@ -2696,16 +2696,21 @@
 %7B%0A      
+try (
 InputStr
@@ -2782,26 +2782,30 @@
 .toString())
-;%0A
+) %7B%0A  
       FileUt
@@ -2828,24 +2828,26 @@
 Dir);%0A      
+  
 if (lib != n
@@ -2849,24 +2849,26 @@
  != null) %7B%0A
+  
         try 
@@ -2930,16 +2930,18 @@
         
+  
 byte%5B%5D b
@@ -2974,16 +2974,18 @@
         
+  
 int len;
@@ -2985,16 +2985,18 @@
 nt len;%0A
+  
         
@@ -3034,24 +3034,26 @@
 )) != -1) %7B%0A
+  
             
@@ -3081,34 +3081,38 @@
 len);%0A          
-%7D%0A
+  %7D%0A  
           libFil
@@ -3141,32 +3141,34 @@
 false);%0A        
+  
 %7D catch (FileNot
@@ -3164,20 +3164,10 @@
 ch (
-FileNotFound
+IO
 Exce
@@ -3191,62 +3191,69 @@
     
-e.printStackTrace();%0A        %7D catch (IOException e) %7B
+  log.log(Level.INFO, %22Failed to extract native library%22, e);
 %0A   
@@ -3259,36 +3259,17 @@
         
-e.printStackTrace();
+%7D
 %0A       
@@ -3275,15 +3275,12 @@
   %7D 
-finally
+else
  %7B%0A 
@@ -3293,42 +3293,74 @@
    t
-ry %7B%0A            lib.close(
+hrow new RuntimeException(%22Failed to find %22 + libName
 );%0A    
+    %7D%0A
     
@@ -3399,131 +3399,67 @@
     
-    e.printStackTrace();%0A          %7D%0A        %7D%0A      %7D else %7B%0A        throw new RuntimeException(%22Failed to find %22 + libNam
+log.log(Level.INFO, %22Failed to close native library file%22, 
 e);%0A
@@ -3476,11 +3476,10 @@
   %7D%0A  %7D%0A
-%0A
 %7D%0A

Refactor to log errors instead of just printing stacktraces