Improve bazel detection

  - Try to locate bazel from the PATH
  - Correctly report bazel as missing in error when the path is not
    pointing to a binary

Fixes #29.

Change-Id: I8c75137018f2c14b0be0e1665a0127ecc6613da5
diff --git a/java/com/google/devtools/bazel/e4b/Activator.java b/java/com/google/devtools/bazel/e4b/Activator.java
index c47cd68..840256a 100644
--- a/java/com/google/devtools/bazel/e4b/Activator.java
+++ b/java/com/google/devtools/bazel/e4b/Activator.java
@@ -35,8 +35,6 @@
 
   private BazelCommand command;
 
-  public static final String DEFAULT_BAZEL_PATH = "/usr/local/bin/bazel";
-
   /**
    * The constructor
    */
@@ -47,6 +45,7 @@
    *
    * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
    */
+  @Override
   public void start(BundleContext context) throws Exception {
     plugin = this;
     super.start(context);
@@ -68,6 +67,7 @@
    *
    * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
    */
+  @Override
   public void stop(BundleContext context) throws Exception {
     plugin = null;
     this.command = null;
diff --git a/java/com/google/devtools/bazel/e4b/command/BazelCommand.java b/java/com/google/devtools/bazel/e4b/command/BazelCommand.java
index 9dd1cd5..b1c5603 100644
--- a/java/com/google/devtools/bazel/e4b/command/BazelCommand.java
+++ b/java/com/google/devtools/bazel/e4b/command/BazelCommand.java
@@ -53,7 +53,7 @@
   private final List<String> aspectOptions;
 
   private final Map<File, BazelInstance> instances = new HashMap<>();
-  private String bazel = null;
+  private File bazel = null;
 
   /**
    * Create a {@link BazelCommand} object, providing the implementation for locating aspect and
@@ -71,17 +71,18 @@
   }
 
   private String getBazelPath() throws BazelNotFoundException {
-    if (bazel == null) {
+    if (bazel == null || !bazel.exists() || !bazel.canExecute()) {
       throw new BazelNotFoundException.BazelNotSetException();
     }
-    return bazel;
+
+    return bazel.toString();
   }
 
   /**
    * Set the path to the Bazel binary.
    */
   public synchronized void setBazelPath(String bazel) {
-    this.bazel = bazel;
+    this.bazel = new File(bazel);
   }
 
   /**
diff --git a/java/com/google/devtools/bazel/e4b/preferences/BazelPreferenceInitializer.java b/java/com/google/devtools/bazel/e4b/preferences/BazelPreferenceInitializer.java
index 519538b..4daf3dc 100644
--- a/java/com/google/devtools/bazel/e4b/preferences/BazelPreferenceInitializer.java
+++ b/java/com/google/devtools/bazel/e4b/preferences/BazelPreferenceInitializer.java
@@ -14,6 +14,8 @@
 
 package com.google.devtools.bazel.e4b.preferences;
 
+import java.io.File;
+
 import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
 import org.eclipse.jface.preference.IPreferenceStore;
 
@@ -25,10 +27,20 @@
  */
 public class BazelPreferenceInitializer extends AbstractPreferenceInitializer {
 
+  private static String which(String name, String def) {
+    for (String dirname : System.getenv("PATH").split(File.pathSeparator)) {
+      File file = new File(dirname, name);
+      if (file.isFile() && file.canExecute()) {
+        return file.getAbsolutePath();
+      }
+    }
+    return def;
+  }
+
   @Override
   public void initializeDefaultPreferences() {
     IPreferenceStore store = Activator.getDefault().getPreferenceStore();
-    store.setDefault("BAZEL_PATH", Activator.DEFAULT_BAZEL_PATH);
+    store.setDefault("BAZEL_PATH", which("bazel", "/usr/local/bin/bazel"));
   }
 
 }