From 9c154890a363be10a9ba6e09630f3c69008d670b Mon Sep 17 00:00:00 2001 From: Fabien Wernli Date: Tue, 5 May 2015 15:17:39 +0200 Subject: [PATCH] add two facts: java_libjvm_path java_default_home * dirname containing base directory of java * e.g. java binary is `${::java_default_home}/jre/bin/java` * dirname containing `libjvm.so` * Most people will use this for LD_LIBRARY_PATH --- README.markdown | 2 ++ lib/facter/java_default_home.rb | 21 ++++++++++++++++ lib/facter/java_libjvm_path.rb | 25 +++++++++++++++++++ spec/unit/facter/java_default_home_spec.rb | 29 ++++++++++++++++++++++ spec/unit/facter/java_libjvm_path_spec.rb | 21 ++++++++++++++++ 5 files changed, 98 insertions(+) create mode 100644 lib/facter/java_default_home.rb create mode 100644 lib/facter/java_libjvm_path.rb create mode 100644 spec/unit/facter/java_default_home_spec.rb create mode 100644 spec/unit/facter/java_libjvm_path_spec.rb diff --git a/README.markdown b/README.markdown index 5442157..369f896 100644 --- a/README.markdown +++ b/README.markdown @@ -79,6 +79,8 @@ The java module includes a few facts to describe the version of Java installed o * `java_major_version`: The major version of Java. * `java_patch_level`: The patch level of Java. * `java_version`: The full Java version string. +* `java_default_home`: The absolute path to the java system home directory (only available on Linux). For instance, the `java` executable's path would be `${::java_default_home}/jre/bin/java`. This is slightly different from the "standard" JAVA_HOME environment variable. +* `java_libjvm_path`: The absolute path to the directory containing the shared library `libjvm.so` (only available on Linux). Useful for setting `LD_LIBRARY_PATH` or configuring the dynamic linker. **Note:** The facts return `nil` if Java is not installed on the system. diff --git a/lib/facter/java_default_home.rb b/lib/facter/java_default_home.rb new file mode 100644 index 0000000..3584b19 --- /dev/null +++ b/lib/facter/java_default_home.rb @@ -0,0 +1,21 @@ +# Fact: java_default_home +# +# Purpose: get absolute path of java system home +# +# Resolution: +# Uses `readlink` to resolve the path of `/usr/bin/java` then returns subsubdir +# +# Caveats: +# Requires readlink +# +# Notes: +# None +Facter.add(:java_default_home) do + confine :kernel => 'Linux' + setcode do + if Facter::Util::Resolution.which('readlink') + java_bin = Facter::Util::Resolution.exec('readlink -e /usr/bin/java').strip + java_default_home = File.dirname(File.dirname(File.dirname(java_bin))) + end + end +end diff --git a/lib/facter/java_libjvm_path.rb b/lib/facter/java_libjvm_path.rb new file mode 100644 index 0000000..14fd78c --- /dev/null +++ b/lib/facter/java_libjvm_path.rb @@ -0,0 +1,25 @@ +# Fact: java_libjvm_path +# +# Purpose: get path to libjvm.so +# +# Resolution: +# Lists file in java default home and searches for the file +# +# Caveats: +# Needs to list files recursively. Returns the first match +# +# Notes: +# None +Facter.add(:java_libjvm_path) do + confine :kernel => "Linux" + setcode do + java_default_home = Facter.value(:java_default_home) + java_libjvm_file = Dir.glob("#{java_default_home}/jre/lib/**/libjvm.so") + if java_libjvm_file.nil? || java_libjvm_file.empty? + nil + else + File.dirname(java_libjvm_file[0]) + end + end +end + diff --git a/spec/unit/facter/java_default_home_spec.rb b/spec/unit/facter/java_default_home_spec.rb new file mode 100644 index 0000000..32ebed9 --- /dev/null +++ b/spec/unit/facter/java_default_home_spec.rb @@ -0,0 +1,29 @@ +require "spec_helper" + +describe Facter::Util::Fact do + before { + Facter.clear + Facter.fact(:kernel).stubs(:value).returns('Linux') + } + + describe "java_default_home" do + context 'returns java home path when readlink present' do + it do + java_path_output = <<-EOS +/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java + EOS + Facter::Util::Resolution.expects(:which).with("readlink").returns(true) + Facter::Util::Resolution.expects(:exec).with("readlink -e /usr/bin/java").returns(java_path_output) + Facter.value(:java_default_home).should == "/usr/lib/jvm/java-7-openjdk-amd64" + end + end + + context 'returns nil when readlink not present' do + it do + Facter::Util::Resolution.stubs(:exec) + Facter::Util::Resolution.expects(:which).with("readlink").at_least(1).returns(false) + Facter.value(:java_default_home).should be_nil + end + end + end +end diff --git a/spec/unit/facter/java_libjvm_path_spec.rb b/spec/unit/facter/java_libjvm_path_spec.rb new file mode 100644 index 0000000..ded1a4f --- /dev/null +++ b/spec/unit/facter/java_libjvm_path_spec.rb @@ -0,0 +1,21 @@ +require "spec_helper" + +describe Facter::Util::Fact do + before { + Facter.clear + Facter.fact(:kernel).stubs(:value).returns('Linux') + java_default_home = '/usr/lib/jvm/java-8-openjdk-amd64' + Facter.fact(:java_default_home).stubs(:value).returns(java_default_home) + Dir.stubs(:glob).with("#{java_default_home}/jre/lib/**/libjvm.so").returns( ['/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server/libjvm.so']) + } + + describe "java_libjvm_path" do + context 'returns libjvm path' do + context 'on Linux' do + it do + Facter.value(:java_libjvm_path).should == "/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server" + end + end + end + end +end