Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .idea/dictionaries/cgfrost.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions config/open_jdk_jre.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@
# limitations under the License.

# Configuration for JRE repositories keyed by vendor
# To go back to Java 7, permgen should be used instead of metaspace. Please see the documentation for more detail.
# Pre Java 1.8, permgen was used instead of metaspace, the buildpack will consume the configuration as appropriate.
# Please see the documentation for more detail.
---
repository_root: "{default.repository.root}/openjdk/{platform}/{architecture}"
version: 1.8.0_+
version:
detect_compiled: enabled
8: 1.8.0_+
7: 1.7.0_+
6: 1.6.0_+
memory_sizes:
metaspace: 64m..
# permgen: 64m..
permgen: 64m..
memory_heuristics:
heap: 75
metaspace: 10
# permgen: 10
permgen: 10
stack: 5
native: 10
13 changes: 9 additions & 4 deletions config/oracle_jre.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,24 @@
# limitations under the License.

# Configuration for JRE repositories keyed by vendor
# Pre Java 1.8, permgen was used instead of metaspace. Please see the documentation for more detail.
# Pre Java 1.8, permgen was used instead of metaspace, the buildpack will consume the configuration as appropriate.
# Please see the documentation for more detail.
---
# You must specify a the repository root of an Oracle JRE repository. Please see the documentation for more detail.
# e.g. repository_root: "http://example.com/oracle-jre/{platform}/{architecture}"

repository_root: ""
version: 1.8.0_+
version:
detect_compiled: enabled
8: 1.8.0_+
7: 1.7.0_+
6: 1.6.0_+
memory_sizes:
metaspace: 64m..
# permgen: 64m..
permgen: 64m..
memory_heuristics:
heap: 75
metaspace: 10
# permgen: 10
permgen: 10
stack: 5
native: 10
6 changes: 3 additions & 3 deletions java-buildpack.iml
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@
<orderEntry type="library" scope="PROVIDED" name="addressable (v2.3.6, rbenv: 1.9.3-p551) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="ast (v2.0.0, rbenv: 1.9.3-p551) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="astrolabe (v1.3.0, rbenv: 1.9.3-p551) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="bundler (v1.7.8, rbenv: 1.9.3-p551) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="bundler (v1.7.7, rbenv: 1.9.3-p551) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="codeclimate-test-reporter (v0.4.3, rbenv: 1.9.3-p551) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="crack (v0.4.2, rbenv: 1.9.3-p551) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="diff-lcs (v1.2.5, rbenv: 1.9.3-p551) [gem]" level="application" />
Expand All @@ -281,13 +281,13 @@
<orderEntry type="library" scope="PROVIDED" name="powerpack (v0.0.9, rbenv: 1.9.3-p551) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="rainbow (v2.0.0, rbenv: 1.9.3-p551) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="rake (v10.4.2, rbenv: 1.9.3-p551) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="redcarpet (v3.2.1, rbenv: 1.9.3-p551) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="redcarpet (v3.2.2, rbenv: 1.9.3-p551) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="rspec (v3.1.0, rbenv: 1.9.3-p551) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="rspec-core (v3.1.7, rbenv: 1.9.3-p551) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="rspec-expectations (v3.1.2, rbenv: 1.9.3-p551) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="rspec-mocks (v3.1.3, rbenv: 1.9.3-p551) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="rspec-support (v3.1.2, rbenv: 1.9.3-p551) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="rubocop (v0.27.1, rbenv: 1.9.3-p551) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="rubocop (v0.28.0, rbenv: 1.9.3-p551) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="rubocop-rspec (v1.2.1, rbenv: 1.9.3-p551) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="ruby-progressbar (v1.7.0, rbenv: 1.9.3-p551) [gem]" level="application" />
<orderEntry type="library" scope="PROVIDED" name="rubyzip (v1.1.6, rbenv: 1.9.3-p551) [gem]" level="application" />
Expand Down
89 changes: 85 additions & 4 deletions lib/java_buildpack/jre/open_jdk_like.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# limitations under the License.

require 'fileutils'
require 'zip'
require 'find'
require 'java_buildpack/component/versioned_dependency_component'
require 'java_buildpack/jre'
require 'java_buildpack/jre/memory/openjdk_memory_heuristic_factory'
Expand All @@ -39,8 +41,12 @@ def initialize(context)

# (see JavaBuildpack::Component::BaseComponent#detect)
def detect
version = required_java_version
version_specific_configuration = { KEY_REPOSITORY_ROOT => @configuration[KEY_REPOSITORY_ROOT],
KEY_VERSION => version }

@version, @uri = JavaBuildpack::Repository::ConfiguredItem.find_item(@component_name,
@configuration)
version_specific_configuration)
@droplet.java_home.version = @version
super
end
Expand All @@ -65,15 +71,90 @@ def release

KEY_MEMORY_SIZES = 'memory_sizes'.freeze

private_constant :KEY_MEMORY_HEURISTICS, :KEY_MEMORY_SIZES
KEY_DETECT_COMPILED_VERSION = 'detect_compiled'.freeze

KEY_JAVA_EIGHT_VERSION = 8.freeze

KEY_JAVA_SEVEN_VERSION = 7.freeze

KEY_JAVA_SIX_VERSION = 6.freeze

KEY_REPOSITORY_ROOT = 'repository_root'.freeze

KEY_VERSION = 'version'.freeze

CAFEBABE = 'cafebabe'.freeze

private_constant :KEY_MEMORY_HEURISTICS, :KEY_MEMORY_SIZES, :KEY_DETECT_COMPILED_VERSION,
:KEY_JAVA_SEVEN_VERSION, :KEY_JAVA_SIX_VERSION, :KEY_VERSION, :CAFEBABE

def killjava
@droplet.sandbox + 'bin/killjava.sh'
end

def java_6?(version_code)
version_code == 32
end

def java_7?(version_code)
version_code == 33
end

def java_6_or_java_7?(version_code)
java_6?(version_code) || java_7?(version_code)
end

def required_java_version
version_configuration = @configuration[KEY_VERSION]
detected_version = resolved_version_code version_configuration

if java_6? detected_version
version = version_configuration[KEY_JAVA_SIX_VERSION]
elsif java_7? detected_version
version = version_configuration[KEY_JAVA_SEVEN_VERSION]
else
version = version_configuration[KEY_JAVA_EIGHT_VERSION]
end
version
end

def resolved_version_code(configuration)
configuration[KEY_DETECT_COMPILED_VERSION] == 'enabled' ? detect_compiled_version(@application.root) : 34
end

# If no valid files are found with 'cafebabe' and an expected version code then
# the code '34' for Java 8 will be returned.
def detect_compiled_version(application_root)
result = 0
Find.find(application_root.to_path) do |sub_entry|
result = check_file(sub_entry, result) if sub_entry.end_with? '.class' unless FileTest.directory?(sub_entry)
end
result == 0 ? 34 : result
end

def check_file(entry, result)
bits = File.open(entry).read.unpack('H*')[0]
logger = JavaBuildpack::Logging::LoggerFactory.instance.get_logger OpenJDKLike
logger.debug "Scanning '#{entry}', first 8 bits are '#{bits[0, 8]}' and the version code is '#{bits[14, 2]}'."
result = [result, Integer(bits[14, 2])].max if bits[0, 8] == CAFEBABE
result
end

def memory
sizes = @configuration[KEY_MEMORY_SIZES] || {}
heuristics = @configuration[KEY_MEMORY_HEURISTICS] || {}
sizes = @configuration[KEY_MEMORY_SIZES] ? @configuration[KEY_MEMORY_SIZES].clone : {}
heuristics = @configuration[KEY_MEMORY_HEURISTICS] ? @configuration[KEY_MEMORY_HEURISTICS].clone : {}

version_configuration = @configuration[KEY_VERSION]
detected_version = resolved_version_code version_configuration

if java_6_or_java_7? detected_version
heuristics.delete 'metaspace'
sizes.delete 'metaspace'
else
heuristics.delete 'permgen'
sizes.delete 'permgen'
end

OpenJDKMemoryHeuristicFactory.create_memory_heuristic(sizes, heuristics, @version).resolve
end

Expand Down
2 changes: 2 additions & 0 deletions spec/fixtures/jre_java6_application/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Manifest-Version: 1.0

Binary file not shown.
Binary file not shown.
2 changes: 2 additions & 0 deletions spec/fixtures/jre_java7_application/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Manifest-Version: 1.0

Binary file not shown.
Binary file not shown.
91 changes: 91 additions & 0 deletions spec/java_buildpack/jre/open_jdk_jre_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@

let(:memory_heuristic) { double('MemoryHeuristic', resolve: %w(opt-1 opt-2)) }

let(:configuration) do
{ 'version' => { 'detect_compile' => 'disabled',
8 => '1.8.0_+',
7 => '1.7.0_+',
6 => '1.6.0_+' },
'memory_sizes' => { 'metaspace' => '64m..',
'permgen' => '64m..' },
'memory_heuristics' => { 'heap' => '75',
'metaspace' => '10',
'permgen' => '10',
'stack' => '5',
'native' => '10' } }
end

before do
allow(JavaBuildpack::Jre::WeightBalancingMemoryHeuristic).to receive(:new).and_return(memory_heuristic)
end
Expand Down Expand Up @@ -81,4 +95,81 @@
expect(java_opts).to include('-Djava.io.tmpdir=$TMPDIR')
end

context do

let(:configuration) { super().merge 'version' => { 'detect_compile' => 'enabled' } }

context do

before do
allow_any_instance_of(described_class).to receive(:java_6?).and_return(true)
end

it 'detects with id of openjdk_jre-<version> for a java 6 app',
cache_fixture: 'stub-java.tar.gz',
app_fixture: 'jre_java6_application' do

expect(component.detect).to eq("open-jdk-jre=#{version}")
end

it 'adds memory options to java_opts for a java 6 app',
cache_fixture: 'stub-java.tar.gz',
app_fixture: 'jre_java6_application' do

component.detect
component.release

expect(java_opts).to include('opt-1')
expect(java_opts).to include('opt-2')
end

end

context do

before do
allow_any_instance_of(described_class).to receive(:java_7?).and_return(true)
end

it 'detects with id of openjdk_jre-<version> for a java 7 app',
cache_fixture: 'stub-java.tar.gz',
app_fixture: 'jre_java7_application' do

expect(component.detect).to eq("open-jdk-jre=#{version}")
end

end

context do

let(:component) { StubOpenJdkJRE.new context }

before do
allow_any_instance_of(StubOpenJdkJRE).to receive(:supports?).and_return(true)
end

it 'detects the correct version for a java 7 app',
cache_fixture: 'stub-java.tar.gz',
app_fixture: 'jre_java7_application' do

expect(component.detect_compiled_version Pathname.new('spec/fixtures/jre_java7_application')).to eq(33)
end

it 'detects the correct version for a java 6 app',
cache_fixture: 'stub-java.tar.gz',
app_fixture: 'jre_java6_application' do

expect(component.detect_compiled_version Pathname.new('spec/fixtures/jre_java6_application')).to eq(32)
end

class StubOpenJdkJRE < JavaBuildpack::Jre::OpenJdkJRE

public :detect_compiled_version

end

end

end

end
Loading