changeset 1536:f358ea7a6377

Added detection of JDK from PATH * rust-launcher/src/hardcoded_paths.rs: minor fix of Err to err * rust-launcher/src/jars_helper.rs: removed redundant declaration of i * rust-launcher/src/main.rs:resolving of jre moved to utils * rust-launcher/src/os_access.rs: declared new method, get_exec_suffixes to obtain java's executable suffix implemented for linux as "" * rust-launcher/src/utils.rs: new method find_jre, extracted form main, enhanced for call to get_jdk_from_path_conditionally new method get_jdk_from_path, for bundled build and both, searching fo jre on path. Based on java executable on path, resolves jre
author Jiri Vanek <jvanek@redhat.com>
date Mon, 14 Jan 2019 10:24:14 +0100
parents cc006da2a3f6
children 6daad5c31f52
files ChangeLog rust-launcher/src/hardcoded_paths.rs rust-launcher/src/jars_helper.rs rust-launcher/src/main.rs rust-launcher/src/os_access.rs rust-launcher/src/utils.rs
diffstat 6 files changed, 143 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Mon Jan 14 09:54:38 2019 +0100
+++ b/ChangeLog	Mon Jan 14 10:24:14 2019 +0100
@@ -1,3 +1,15 @@
+2019-01-14  Jiri Vanek <jvanek@redhat.com>
+
+	Added detection of JDK from PATH
+	* rust-launcher/src/hardcoded_paths.rs: minor fix of Err to err
+	* rust-launcher/src/jars_helper.rs: removed redundant declaration of i
+	* rust-launcher/src/main.rs:resolving of jre moved to utils
+	* rust-launcher/src/os_access.rs: declared new method, get_exec_suffixes to obtain java's executable suffix
+	implemented for linux as ""
+	* rust-launcher/src/utils.rs: new method find_jre, extracted form main, enhanced for call to get_jdk_from_path_conditionally
+	new method get_jdk_from_path, for bundled build and both, searching fo jre on path. Based on java executable on path, resolves
+	jre
+
 2019-01-14  Jiri Vanek <jvanek@redhat.com>
 
 	Added support for -J options
--- a/rust-launcher/src/hardcoded_paths.rs	Mon Jan 14 09:54:38 2019 +0100
+++ b/rust-launcher/src/hardcoded_paths.rs	Mon Jan 14 10:24:14 2019 +0100
@@ -99,7 +99,7 @@
             Ok(result_of_override_to_enum) => {
                 return result_of_override_to_enum;
             }
-            _Err => {
+            _err => {
                 let mut info = String::new();
                 write!(&mut info, "ITW-LIBS provided, but have invalid value of {}. Use BUNDLED, DISTRIBUTION or BOTH", result_of_override_var);
                 logger.info(&info);
@@ -113,7 +113,7 @@
         Ok(v) => {
             return v
         }
-        _Err=> {
+        _err=> {
             panic!("itw-lib search out of range");
         }
     }
--- a/rust-launcher/src/jars_helper.rs	Mon Jan 14 09:54:38 2019 +0100
+++ b/rust-launcher/src/jars_helper.rs	Mon Jan 14 10:24:14 2019 +0100
@@ -144,7 +144,6 @@
 
 fn compose_class_path(members: Vec<std::path::PathBuf>, os: &os_access::Os) -> String {
     let mut result = String::new();
-    let mut i = 0;
     for (i, mb) in members.iter().enumerate()  {
         result.push_str(&dirs_paths_helper::path_to_string(&mb));
         if i < members.len() - 1 {
--- a/rust-launcher/src/main.rs	Mon Jan 14 09:54:38 2019 +0100
+++ b/rust-launcher/src/main.rs	Mon Jan 14 10:24:14 2019 +0100
@@ -28,41 +28,10 @@
     //TODO verbose will be populated by also from deployment properties
     let os = os_access::Linux::new(is_debug_on());
     os.log(&dirs_paths_helper::path_to_string(&dirs_paths_helper::current_program()));
-    let java_dir: std::path::PathBuf;
     let mut info1 = String::new();
     write!(&mut info1, "itw-rust-debug: trying jdk over properties ({})", property_from_file::JRE_PROPERTY_NAME).expect("unwrap failed");
     os.log(&info1);
-    match property_from_files_resolver::try_jdk_from_properties(&os) {
-        Some(path) => {
-            java_dir = std::path::PathBuf::from(path);
-            os.log("itw-rust-debug: found and using");
-        }
-        None => {
-            os.log("itw-rust-debug: nothing");
-            os.log("itw-rust-debug: trying jdk JAVA_HOME");
-            match env::var("JAVA_HOME") {
-                Ok(war) => {
-                    java_dir = std::path::PathBuf::from(war);
-                    os.log("itw-rust-debug: found and using");
-                }
-                Err(_e) => {
-                    os.log("itw-rust-debug: nothing");
-                    os.log("itw-rust-debug: trying jdk from registry");
-                    match os.get_registry_jdk() {
-                        Some(path) => {
-                            java_dir = path;
-                            os.log("itw-rust-debug: found and using");
-                        }
-                        None => {
-                            os.log("itw-rust-debug: nothing");
-                            os.log("itw-rust-debug: failing down to hardcoded");
-                            java_dir = std::path::PathBuf::from(hardcoded_paths::get_jre());
-                        }
-                    }
-                }
-            }
-        }
-    }
+    let java_dir = utils::find_jre(&os);
     let mut info2 = String::new();
     write!(&mut info2, "selected jre: {}", java_dir.display()).expect("unwrap failed");
     os.info(&info2);
--- a/rust-launcher/src/os_access.rs	Mon Jan 14 09:54:38 2019 +0100
+++ b/rust-launcher/src/os_access.rs	Mon Jan 14 10:24:14 2019 +0100
@@ -22,6 +22,7 @@
     // see https://doc.rust-lang.org/std/env/fn.home_dir.html
     fn get_home(&self) -> Option<std::path::PathBuf>;
     fn get_classpath_separator(&self) -> char;
+    fn get_exec_suffixes(&self) -> &'static [&'static str];
 }
 
 pub struct Linux {
@@ -118,6 +119,11 @@
     }
 
     fn get_classpath_separator(&self) -> char {
-        return ':';
+         ':'
+    }
+
+    //on linux, java is known to be compiled witout any suffix, on windows, it should be .exe
+    fn get_exec_suffixes(&self) -> &'static [&'static str] {
+        &[""]
     }
 }
--- a/rust-launcher/src/utils.rs	Mon Jan 14 09:54:38 2019 +0100
+++ b/rust-launcher/src/utils.rs	Mon Jan 14 10:24:14 2019 +0100
@@ -1,3 +1,120 @@
+use std;
+use env;
+use dirs_paths_helper;
+use os_access;
+use std::fmt::Write;
+use hardcoded_paths;
+use property_from_files_resolver;
+
+pub fn find_jre(os: &os_access::Os) -> std::path::PathBuf {
+    match property_from_files_resolver::try_jdk_from_properties(os) {
+        Some(path) => {
+            os.log("itw-rust-debug: found and using");
+            return std::path::PathBuf::from(path);
+        }
+        None => {
+            os.log("itw-rust-debug: nothing");
+            os.log("itw-rust-debug: trying jdk JAVA_HOME");
+            match env::var("JAVA_HOME") {
+                Ok(war) => {
+                    os.log("itw-rust-debug: found and using");
+                    return std::path::PathBuf::from(war);
+                }
+                Err(_e) => {
+                    os.log("itw-rust-debug: nothing");
+                    os.log("itw-rust-debug: trying jdk from registry");
+                    match os.get_registry_jdk() {
+                        Some(path) => {
+                            os.log("itw-rust-debug: found and using");
+                            return path;
+                        }
+                        None => {
+                            os.log("itw-rust-debug: nothing");
+                            os.log("itw-rust-debug: trying jdk from path");
+                            match get_jdk_from_path_conditionally(os) {
+                                Some(path) => {
+                                    os.log("itw-rust-debug: found and using");
+                                    return path;
+                                }
+                                None => {
+                                    os.log("itw-rust-debug: nothing");
+                                    os.log("itw-rust-debug: failing down to hardcoded");
+                                    return std::path::PathBuf::from(hardcoded_paths::get_jre());
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+}
+
+fn get_jdk_from_path_conditionally(os: &os_access::Os) -> Option<std::path::PathBuf> {
+    let libsearch = hardcoded_paths::get_libsearch(os);
+    if libsearch == hardcoded_paths::ItwLibSearch::DISTRIBUTION {
+        os.log("itw-rust-debug: skipping jdk from path, your build is distribution");
+        None
+    } else {
+        if libsearch == hardcoded_paths::ItwLibSearch::BOTH {
+            os.info("your build is done as BOTH distribution and bundled, jdk from PATH may be not what you want!");
+        }
+        get_jdk_from_path(os)
+    }
+}
+
+fn get_jdk_from_path(os: &os_access::Os) -> Option<std::path::PathBuf> {
+    env::var_os("PATH").and_then(|paths| {
+        env::split_paths(&paths).filter_map(|dir| {
+            for suffix in os.get_exec_suffixes() {
+                let mut bin_name = String::new();
+                write!(&mut bin_name, "java{}", suffix).expect("unwrap failed");
+                let full_path = dir.join(bin_name);
+                let mut info1 = String::new();
+                write!(&mut info1, "itw-rust-debug: trying {}", full_path.to_str().expect("unwrap failed")).expect("unwrap failed");
+                os.log(&info1);
+                if dirs_paths_helper::is_file(&full_path) {
+                    let can = match full_path.canonicalize() {
+                        Ok(resolved) => {
+                            //.../bin/java
+                            resolved
+                        }
+                        _error => {
+                            full_path.clone()
+                        }
+                    };
+                    //.../bin/java -> bin
+                    let jre_bin_dir: std::path::PathBuf = std::path::PathBuf::from(&can.parent().expect("file should always have parent"));
+                    let jre_dir: std::path::PathBuf;
+                    //will panic if the file was /java - not fixing
+                    if jre_bin_dir.file_name().expect("java's parent should have name") == "bin" {
+                        jre_dir = std::path::PathBuf::from(jre_bin_dir.parent().expect("java's  bin dir should have parent"))
+                    } else {
+                        os.info("Error: JRE from path seems to not have bin dir");
+                        jre_dir = match jre_bin_dir.parent() {
+                            Some(p) => {
+                                //.../bin/ -> ...
+                                std::path::PathBuf::from(p)
+                            }
+                            None => {
+                                //??
+                                jre_bin_dir.clone()
+                            }
+                        }
+                    }
+                    let mut info2 = String::new();
+                    write!(&mut info2, "itw-rust-debug: found {} resolving as {}", full_path.to_str().expect("unwrap failed"), can.to_str().expect("unwrap failed")).expect("unwrap failed");
+                    os.log(&info2);
+                    //returning owner of /bin/java as needed by find_jre
+                    return Some(jre_dir);
+                }
+            }
+            None
+        }).next()
+    })
+}
+
+
 #[cfg(test)]
 pub mod tests_utils {
     use std;
@@ -66,6 +183,10 @@
             panic!("not implemented");
         }
         fn get_classpath_separator(&self) -> char { ':' }
+
+        fn get_exec_suffixes(&self) -> &'static [&'static str] {
+            panic!("not implemented");
+        }
     }