Skip to content

Commit 1b6aec3

Browse files
committed
Improvements to *JavaHome types
Previously the ImmutableJavaHome.do_with() method used the $PWD qualified version of JAVA_HOME. This caused problem during some executions, so both of the *JavaHome types have been changed such that the fully qualified path is used in the do_with() method. [#63086152]
1 parent 1607ecb commit 1b6aec3

15 files changed

Lines changed: 62 additions & 62 deletions

lib/java_buildpack/buildpack.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def release
9393

9494
private
9595

96-
DEFAULT_BUILDPACK_MESSAGE = ' Java Buildpack source: system'.freeze
96+
DEFAULT_BUILDPACK_MESSAGE = '-----> Java Buildpack source: system'.freeze
9797

9898
GIT_DIR = Pathname.new(__FILE__).dirname + '../../.git'
9999

@@ -105,8 +105,8 @@ def initialize(app_dir, application)
105105
log_environment_variables
106106

107107
additional_libraries = Component::AdditionalLibraries.new app_dir
108-
mutable_java_home = Component::MutableJavaHome.new app_dir
109-
immutable_java_home = Component::ImmutableJavaHome.new mutable_java_home
108+
mutable_java_home = Component::MutableJavaHome.new
109+
immutable_java_home = Component::ImmutableJavaHome.new mutable_java_home, app_dir
110110
java_opts = Component::JavaOpts.new app_dir
111111

112112
components = JavaBuildpack::Util::ConfigurationUtils.load 'components'
@@ -127,7 +127,7 @@ def diagnose_git_info(print)
127127
if system("git --git-dir=#{GIT_DIR} status 2>/dev/null 1>/dev/null")
128128
remote_url = diagnose_remotes
129129
head_commit_sha = diagnose_head_commit
130-
puts " Java Buildpack source: #{remote_url}##{head_commit_sha}" if print
130+
puts "-----> Java Buildpack source: #{remote_url}##{head_commit_sha}" if print
131131
else
132132
@logger.debug { DEFAULT_BUILDPACK_MESSAGE }
133133
puts DEFAULT_BUILDPACK_MESSAGE if print

lib/java_buildpack/component/additional_libraries.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
# limitations under the License.
1616

1717
require 'java_buildpack/component'
18-
require 'java_buildpack/component/qualify_path'
18+
require 'java_buildpack/util/qualify_path'
1919

2020
module JavaBuildpack::Component
2121

2222
# An abstraction around the additional libraries provided to a droplet by components.
2323
#
2424
# A new instance of this type should be created once for the application.
2525
class AdditionalLibraries < Array
26-
include JavaBuildpack::Component
26+
include JavaBuildpack::Util
2727

2828
# Creates an instance of the +JAVA_OPTS+ abstraction.
2929
#

lib/java_buildpack/component/immutable_java_home.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# limitations under the License.
1616

1717
require 'java_buildpack/component'
18+
require 'java_buildpack/util/qualify_path'
1819

1920
module JavaBuildpack::Component
2021

@@ -23,15 +24,17 @@ module JavaBuildpack::Component
2324
#
2425
# A new instance of this type should be created once for the application.
2526
class ImmutableJavaHome
27+
include JavaBuildpack::Util
2628

2729
# Creates a new instance of the java home abstraction
2830
#
2931
# @param [MutableJavaHome] delegate the instance of +MutableJavaHome+ to use as a delegate for +root+ calls
30-
def initialize(delegate)
32+
def initialize(delegate, droplet_root)
3133
@delegate = delegate
34+
@droplet_root = droplet_root
3235
end
3336

34-
# Returns the path of +JAVA_HOME+ as an environment variable formatted as +JAVA_HOME="$PWD/<value>"+
37+
# Returns the path of +JAVA_HOME+ as an environment variable formatted as +JAVA_HOME=$PWD/<value>+
3538
#
3639
# @return [String] the path of +JAVA_HOME+ as an environment variable
3740
def as_env_var
@@ -44,16 +47,16 @@ def as_env_var
4447
def do_with
4548
previous_value = ENV['JAVA_HOME']
4649
begin
47-
ENV['JAVA_HOME'] = root
50+
ENV['JAVA_HOME'] = @delegate.root.cleanpath.to_s
4851
yield
4952
ensure
5053
ENV['JAVA_HOME'] = previous_value
5154
end
5255
end
5356

54-
# @return [String] the root of the droplet's +JAVA_HOME+
57+
# @return [String] the root of the droplet's +JAVA_HOME+ formatted as +$PWD/<value>+
5558
def root
56-
@delegate.root
59+
qualify_path @delegate.root
5760
end
5861

5962
end

lib/java_buildpack/component/java_opts.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
# limitations under the License.
1616

1717
require 'java_buildpack/component'
18-
require 'java_buildpack/component/qualify_path'
18+
require 'java_buildpack/util/qualify_path'
1919

2020
module JavaBuildpack::Component
2121

2222
# An abstraction encapsulating the +JAVA_OPTS+ of an application.
2323
#
2424
# A new instance of this type should be created once for the application.
2525
class JavaOpts < Array
26-
include JavaBuildpack::Component
26+
include JavaBuildpack::Util
2727

2828
# Creates an instance of the +JAVA_OPTS+ abstraction.
2929
#

lib/java_buildpack/component/mutable_java_home.rb

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,18 @@
1515
# limitations under the License.
1616

1717
require 'java_buildpack/component'
18-
require 'java_buildpack/component/qualify_path'
19-
require 'java_buildpack/component/immutable_java_home'
2018

2119
module JavaBuildpack::Component
2220

23-
# An abstraction around the +JAVA_HOME+ path used by the droplet. This implementation is immutable and should be
24-
# passed to any component that is not a jre.
21+
# An abstraction around the +JAVA_HOME+ path used by the droplet. This implementation is mutable and should be
22+
# passed to any component that is a jre.
2523
#
2624
# A new instance of this type should be created once for the application.
27-
class MutableJavaHome < ImmutableJavaHome
28-
include JavaBuildpack::Component
25+
class MutableJavaHome
2926

30-
# @!attribute [r] root
27+
# @!attribute [rw] root
3128
# @return [String] the root of the droplet's +JAVA_HOME+
32-
attr_reader :root
33-
34-
# Creates a new instance of the java home abstraction
35-
#
36-
# @param [Pathname] droplet_root the root directory of the droplet
37-
def initialize(droplet_root)
38-
@droplet_root = droplet_root
39-
end
40-
41-
# Sets the root of the droplet's +JAVA_HOME+
42-
#
43-
# @param [Pathname] value the root of the droplet's +JAVA_HOME+
44-
def root=(value)
45-
@root = qualify_path value
46-
end
29+
attr_accessor :root
4730

4831
end
4932

lib/java_buildpack/container/groovy.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
require 'java_buildpack/container'
1919
require 'java_buildpack/util/class_file_utils'
2020
require 'java_buildpack/util/groovy_utils'
21+
require 'java_buildpack/util/qualify_path'
2122
require 'pathname'
2223
require 'set'
2324
require 'tmpdir'
@@ -27,6 +28,7 @@ module JavaBuildpack::Container
2728
# Encapsulates the detect, compile, and release functionality for applications running non-compiled Groovy
2829
# applications.
2930
class Groovy < JavaBuildpack::Component::VersionedDependencyComponent
31+
include JavaBuildpack::Util
3032

3133
def initialize(context)
3234
super(context) { |candidate_version| candidate_version.check_size(3) }
@@ -40,7 +42,7 @@ def release
4042
[
4143
@droplet.java_home.as_env_var,
4244
@droplet.java_opts.as_env_var,
43-
"$PWD/#{(@droplet.sandbox + 'bin/groovy').relative_path_from(@droplet.root)}",
45+
qualify_path(@droplet.sandbox + 'bin/groovy', @droplet.root),
4446
@droplet.additional_libraries.as_classpath,
4547
relative_main_groovy,
4648
relative_other_groovy

lib/java_buildpack/container/spring_boot_cli.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
require 'java_buildpack/component/versioned_dependency_component'
1919
require 'java_buildpack/container'
2020
require 'java_buildpack/util/groovy_utils'
21+
require 'java_buildpack/util/qualify_path'
2122

2223
module JavaBuildpack::Container
2324

2425
# Encapsulates the detect, compile, and release functionality for applications running Spring Boot CLI
2526
# applications.
2627
class SpringBootCLI < JavaBuildpack::Component::VersionedDependencyComponent
28+
include JavaBuildpack::Util
2729

2830
def compile
2931
download_tar
@@ -34,7 +36,7 @@ def release
3436
[
3537
@droplet.java_home.as_env_var,
3638
@droplet.java_opts.as_env_var,
37-
"$PWD/#{(@droplet.sandbox + 'bin/spring').relative_path_from(@droplet.root)}",
39+
qualify_path(@droplet.sandbox + 'bin/spring', @droplet.root),
3840
'run',
3941
'--local',
4042
relative_groovy_files,

lib/java_buildpack/util/play/base.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
# limitations under the License.
1616

1717
require 'java_buildpack/util/play'
18+
require 'java_buildpack/util/qualify_path'
1819

1920
module JavaBuildpack::Util::Play
2021

2122
# Base class for Play application classes.
2223
class Base
24+
include JavaBuildpack::Util
2325

2426
def initialize(droplet)
2527
@droplet = droplet
@@ -47,7 +49,7 @@ def release
4749
[
4850
"PATH=#{@droplet.java_home.root}/bin:$PATH",
4951
@droplet.java_home.as_env_var,
50-
"$PWD/#{start_script.relative_path_from(@droplet.root)}",
52+
qualify_path(start_script, @droplet.root),
5153
java_opts
5254
].compact.join(' ')
5355
end

lib/java_buildpack/component/qualify_path.rb renamed to lib/java_buildpack/util/qualify_path.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
require 'java_buildpack/component'
18-
require 'java_buildpack/component/immutable_java_home'
17+
require 'java_buildpack/util'
1918

20-
module JavaBuildpack::Component
19+
module JavaBuildpack::Util
2120

22-
# Qualifies the path such that is is formatted as +$PWD/<path>+. Also ensures that the path is relative to the
23-
# +@droplet_root+ of the class.
24-
def qualify_path(path)
25-
"$PWD/#{path.relative_path_from(@droplet_root)}"
21+
# Qualifies the path such that is is formatted as +$PWD/<path>+. Also ensures that the path is relative to a root,
22+
# which defaults to the +@droplet_root+ of the class.
23+
#
24+
# @param [Pathname] path the path to qualify
25+
# @param [Pathname] root the root to make relative to
26+
# @return [String] the qualified path
27+
def qualify_path(path, root = @droplet_root)
28+
"$PWD/#{path.relative_path_from(root)}"
2629
end
2730

2831
end

lib/java_buildpack/util/shell.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module JavaBuildpack::Util::Shell
2626
def shell(command)
2727
Open3.popen3(command) do |stdin, stdout, stderr, wait_thr|
2828
if wait_thr.value != 0
29-
puts "Command '#{command}' has failed"
29+
puts "\nCommand '#{command}' has failed"
3030
puts "STDOUT: #{stdout.gets}"
3131
puts "STDERR: #{stderr.gets}"
3232

0 commit comments

Comments
 (0)