Skip to content

Commit 627ed86

Browse files
committed
Merge branch '427-ibm-jre' into 3.x
2 parents ebbbda4 + afed86e commit 627ed86

11 files changed

Lines changed: 536 additions & 1 deletion

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,10 @@ To learn how to configure various properties of the buildpack, follow the "Confi
9696
* [Spring Insight](docs/framework-spring_insight.md)
9797
* [YourKit Profiler](docs/framework-your_kit_profiler.md) ([Configuration](docs/framework-your_kit_profiler.md#configuration))
9898
* Standard JREs
99+
* [Azul Zulu](docs/jre-zulu_jre.md) ([Configuration](docs/jre-zulu_jre.md#configuration))
100+
* [IBM® SDK, Java™ Technology Edition](docs/jre-ibm_jre.md) ([Configuration](docs/jre-ibm_jre.md#configuration))
99101
* [OpenJDK](docs/jre-open_jdk_jre.md) ([Configuration](docs/jre-open_jdk_jre.md#configuration))
100102
* [Oracle](docs/jre-oracle_jre.md) ([Configuration](docs/jre-oracle_jre.md#configuration))
101-
* [Azul Zulu](docs/jre-zulu_jre.md) ([Configuration](docs/jre-zulu_jre.md#configuration))
102103
* [Extending](docs/extending.md)
103104
* [Application](docs/extending-application.md)
104105
* [Droplet](docs/extending-droplet.md)

config/components.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ containers:
2929
# In order to use Zulu JREs instead of OpenJDK, you must comment out the OpenJDK line and uncomment the Zulu line.
3030
# Please see the documentation for more detail.
3131
jres:
32+
# - "JavaBuildpack::Jre::IbmJRE"
3233
- "JavaBuildpack::Jre::OpenJdkJRE"
3334
# - "JavaBuildpack::Jre::OracleJRE"
3435
# - "JavaBuildpack::Jre::ZuluJRE"

config/ibm_jre.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Cloud Foundry Java Buildpack
2+
# Copyright 2017 the original author or authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
17+
# Configuration for JRE repository
18+
---
19+
jre:
20+
version: 1.8.+
21+
repository_root: "https://raw.githubusercontent.com/ibmruntimes/ci.docker/master/ibmjava/meta/jre/linux/x86_64/"
22+
heap_ratio: 0.75
23+
jvmkill_agent:
24+
version: 1.+
25+
repository_root: "https://raw.githubusercontent.com/ibmruntimes/jvmkill/jvmkill-ibmagent/jvmkill-agent/"

docs/jre-ibm_jre.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## IBM JRE
2+
IBM JRE provides IBM® SDK, Java™ Technology Edition, Version 8. Unless otherwise configured, the version of Java that will be used is specified in [`config/ibm_jre.yml`][]. See the license section for restrictions that relate to the use of this image. For more information about IBM® SDK, Java™ Technology Edition and API documentation, see the [IBM Knowledge Center][].
3+
4+
### License
5+
Licenses for the products installed within the buildpack:
6+
7+
IBM® SDK, Java™ Technology Edition, Version 8: [International License Agreement for Non-Warranted Programs][].
8+
9+
## Configuration
10+
For general information on configuring the buildpack, including how to specify configuration values through environment variables, refer to [Configuration and Extension][].
11+
12+
The JRE can be configured by modifying the [`config/ibm_jre.yml`][] file in the buildpack fork. The JRE uses the [`Repository` utility support][repositories] and so, it supports the [version syntax][] defined there.
13+
14+
To use IBM JRE instead of OpenJDK without forking java-buildpack, set environment variable:
15+
16+
`cf set-env <app_name> JBP_CONFIG_COMPONENTS '{jres: ["JavaBuildpack::Jre::IbmJRE"]}'`
17+
18+
`cf restage <app_name>`
19+
20+
| Name | Description
21+
| ---- | -----------
22+
| `repository_root` | The URL of the IBM JRE repository index ([details][repositories]).
23+
| `version` | The version of Java runtime to use.
24+
25+
### TLS Options
26+
It is recommended to use the following Transport Layer Security (TLS) options for IBM JRE version 8 and above:
27+
28+
`cf set-env <app_name> JAVA_OPTS '-Dcom.ibm.jsse2.overrideDefaultTLS=true'`
29+
30+
### Additional Resources
31+
The JRE can also be configured by overlaying a set of resources on the default distribution. To do this, add files to the `resources/ibm_jre` directory in the buildpack fork.
32+
33+
#### Custom CA Certificates
34+
To add custom SSL certificates, add your `cacerts` file to `resources/ibm_jre/jre/lib/security/cacerts`. This file will be overlayed onto the IBM JRE distribution.
35+
36+
### Memory
37+
The total available memory for the application's container is specified when an application is pushed.The Java buildpack uses this value to control the JRE's use of various regions of memory and logs the JRE memory settings when the application starts or restarts.
38+
39+
Note: If the total available memory is scaled up or down, the Java buildpack will re-calculate the JRE memory settings the next time the application is started.
40+
41+
#### Total Memory
42+
The user can change the container's total memory available to influence the JRE memory settings. Unless the user specifies the heap size Java option (`-Xmx`), increasing or decreasing the total memory available results in the heap size setting increasing or decreasing by a corresponding amount.
43+
44+
#### Memory Calculation
45+
The user can configure the desired heap ratio (`-Xmx`) by changing the `heap_ratio` attribute under `jre` in [`config/ibm_jre.yml`][] and the buildpack calculates the `-Xmx Memory Setting` based on the total memory available.
46+
47+
The container's total memory is logged during `cf push` and `cf scale`, for example:
48+
```
49+
state since cpu memory disk details
50+
#0 running 2017-04-10 02:20:03 PM 0.0% 896K of 1G 1.3M of 1G
51+
```
52+
53+
[`config/components.yml`]: ../config/components.yml
54+
[`config/ibm_jre.yml`]: ../config/ibm_jre.yml
55+
[Configuration and Extension]: ../README.md#configuration-and-extension
56+
[repositories]: extending-repositories.md
57+
[version syntax]: extending-repositories.md#version-syntax-and-ordering
58+
[IBM Knowledge Center]: http://www.ibm.com/support/knowledgecenter/SSYKE2/welcome_javasdk_family.html
59+
[International License Agreement for Non-Warranted Programs]: http://www14.software.ibm.com/cgi-bin/weblap/lap.pl?la_formnum=&li_formnum=L-PMAA-A3Z8P2&title=IBM%AE+SDK%2C+Java%99+Technology+Edition%2C+Version+8.0&l=en

lib/java_buildpack/jre/ibm_jre.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Cloud Foundry Java Buildpack
2+
# Copyright 2017 the original author or authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
require 'java_buildpack/component/modular_component'
17+
require 'java_buildpack/jre'
18+
require 'java_buildpack/jre/ibm_jre_initializer'
19+
require 'java_buildpack/jre/open_jdk_like_security_providers'
20+
21+
module JavaBuildpack
22+
module Jre
23+
24+
# Encapsulates the detect, compile, and release functionality for selecting a JRE.
25+
class IbmJRE < JavaBuildpack::Component::ModularComponent
26+
27+
protected
28+
29+
# (see JavaBuildpack::Component::ModularComponent#command)
30+
def command
31+
# no command need to be executed
32+
end
33+
34+
# (see JavaBuildpack::Component::ModularComponent#sub_components)
35+
def sub_components(context)
36+
[
37+
IbmJreInitializer.new(sub_configuration_context(context, 'jre')
38+
.merge(component_name: self.class.to_s.space_case)),
39+
OpenJDKLikeSecurityProviders.new(context)
40+
]
41+
end
42+
43+
# (see JavaBuildpack::Component::ModularComponent#supports?)
44+
def supports?
45+
true
46+
end
47+
48+
end
49+
50+
end
51+
end
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
# Cloud Foundry Java Buildpack
2+
# Copyright 2017 the original author or authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
require 'fileutils'
17+
require 'java_buildpack/component/versioned_dependency_component'
18+
require 'java_buildpack/jre'
19+
require 'java_buildpack/util/tokenized_version'
20+
21+
module JavaBuildpack
22+
module Jre
23+
24+
# Encapsulates the detect, compile, and release functionality for selecting a JRE.
25+
class IbmJreInitializer < JavaBuildpack::Component::VersionedDependencyComponent
26+
27+
# Creates an instance
28+
#
29+
# @param [Hash] context a collection of utilities used the component
30+
def initialize(context)
31+
@application = context[:application]
32+
@component_name = context[:component_name]
33+
@configuration = context[:configuration]
34+
@droplet = context[:droplet]
35+
36+
@droplet.java_home.root = @droplet.sandbox + 'jre/'
37+
end
38+
39+
# (see JavaBuildpack::Component::BaseComponent#detect)
40+
def detect
41+
@version, @uri = JavaBuildpack::Repository::ConfiguredItem.find_item(@component_name,
42+
@configuration)
43+
@droplet.java_home.version = @version
44+
super
45+
end
46+
47+
# (see JavaBuildpack::Component::BaseComponent#compile)
48+
def compile
49+
download(@version, @uri, @component_name) do |file|
50+
with_timing "Installing #{@component_name} to #{@droplet.sandbox.relative_path_from(@droplet.root)}" do
51+
install_bin(@droplet.sandbox, file)
52+
end
53+
end
54+
@droplet.copy_resources
55+
end
56+
57+
# (see JavaBuildpack::Component::BaseComponent#release)
58+
def release
59+
@droplet
60+
.java_opts
61+
.add_system_property('java.io.tmpdir', '$TMPDIR')
62+
.add_option('-XX:OnOutOfMemoryError', killjava)
63+
.add_preformatted_options('-Xtune:virtualized')
64+
.add_preformatted_options('-Xshareclasses:none')
65+
66+
@droplet.java_opts.concat mem_opts
67+
end
68+
69+
private
70+
71+
# constant HEAP_RATIO is the ratio of memory assigned to the heap
72+
# as against the container total and is set using -Xmx.
73+
HEAP_RATIO = 0.75
74+
75+
KILO = 1024
76+
77+
private_constant :HEAP_RATIO, :KILO
78+
79+
# Installs the Downloaded InstallAnywhere (tm) BIN file to the target directory
80+
#
81+
# @param [String] target_directory, Where the java needs to be installed
82+
# @param [File] file, InstallAnywhere (tm) BIN file
83+
# @return [Void]
84+
def install_bin(target_directory, file)
85+
FileUtils.mkdir_p target_directory
86+
response_file = Tempfile.new('response.properties')
87+
response_file.puts('INSTALLER_UI=silent')
88+
response_file.puts('LICENSE_ACCEPTED=TRUE')
89+
response_file.puts("USER_INSTALL_DIR=#{target_directory}")
90+
response_file.close
91+
92+
File.chmod(0o755, file.path) unless File.executable?(file.path)
93+
shell "#{file.path} -i silent -f #{response_file.path} 2>&1"
94+
end
95+
96+
# Returns the max heap size ('-Xmx') value
97+
def mem_opts
98+
mopts = []
99+
total_memory = memory_limit_finder
100+
if total_memory.nil?
101+
# if no memory option has been set by cloudfoundry, we just assume defaults
102+
else
103+
calculated_heap_ratio = heap_ratio_verification(heap_ratio)
104+
heap_size = heap_size_calculator(total_memory, calculated_heap_ratio)
105+
mopts.push "-Xmx#{heap_size}"
106+
end
107+
mopts
108+
end
109+
110+
# Returns the heap_ratio attribute in config file (if specified) or the HEAP_RATIO constant value
111+
def heap_ratio
112+
@configuration['heap_ratio'] || HEAP_RATIO
113+
end
114+
115+
def killjava
116+
@droplet.sandbox + 'jre/bin/killjava.sh'
117+
end
118+
119+
# Returns the container total memory limit in bytes
120+
def memory_limit_finder
121+
memory_limit = ENV['MEMORY_LIMIT']
122+
return nil unless memory_limit
123+
memory_limit_size = memory_size_bytes(memory_limit)
124+
raise "Invalid negative $MEMORY_LIMIT #{memory_limit}" if memory_limit_size < 0
125+
memory_limit_size
126+
end
127+
128+
# Returns the no. of bytes for a given string of minified size representation
129+
#
130+
# @param [String] size, A minified memory representation string
131+
# @return [Integer] bytes, value of size in bytes
132+
def memory_size_bytes(size)
133+
if size == '0'
134+
bytes = 0
135+
else
136+
raise "Invalid memory size '#{size}'" if !size || size.length < 2
137+
unit = size[-1]
138+
value = size[0..-2]
139+
raise "Invalid memory size '#{size}'" unless check_is_integer? value
140+
value = size.to_i
141+
# store the bytes
142+
bytes = calculate_bytes(unit, value)
143+
end
144+
bytes
145+
end
146+
147+
# Returns the no. of bytes for a given memory size unit
148+
#
149+
# @param [String] unit, Represents a Memory Size Unit
150+
# @param [Integer] value
151+
# @return [Integer] bytes, value of size in bytes
152+
def calculate_bytes(unit, value)
153+
if %w[b B].include?(unit)
154+
bytes = value
155+
elsif %w[k K].include?(unit)
156+
bytes = KILO * value
157+
elsif %w[m M].include?(unit)
158+
bytes = KILO * KILO * value
159+
elsif %w[g G].include?(unit)
160+
bytes = KILO * KILO * KILO * value
161+
else
162+
raise "Invalid unit '#{unit}' in memory size"
163+
end
164+
bytes
165+
end
166+
167+
# Checks whether the given value is an Integer
168+
#
169+
# @param [String] v, value as a string
170+
def check_is_integer?(v)
171+
v = Float(v)
172+
v && v.floor == v
173+
end
174+
175+
# Calculates the Heap size as per the Heap ratio
176+
#
177+
# @param [Integer] membytes, total memory in bytes
178+
# @param [Numeric] heapratio, Desired/Default Heap Ratio
179+
def heap_size_calculator(membytes, heapratio)
180+
memory_size_minified(membytes * heapratio)
181+
end
182+
183+
# Calculates the Memory Size in a Minified String Representation
184+
#
185+
# @param [Numeric] membytes, calculated heap size
186+
def memory_size_minified(membytes)
187+
giga = membytes / 2**(10 * 3)
188+
mega = membytes / 2**(10 * 2)
189+
kilo = (membytes / 2**(10 * 1)).round
190+
if check_is_integer?(giga)
191+
minified_size_calculator(giga, 'G')
192+
elsif check_is_integer?(mega)
193+
minified_size_calculator(mega, 'M')
194+
elsif check_is_integer?(kilo)
195+
minified_size_calculator(kilo, 'K')
196+
end
197+
end
198+
199+
# Returns the minified memory string
200+
#
201+
# @param [Integer] order, calculated memory value
202+
# @param [String] char, calculated memory unit
203+
# @return [String] minified memory string
204+
def minified_size_calculator(order, char)
205+
order.to_i.to_s + char
206+
end
207+
208+
# Verifies whether heap ratio is valid
209+
def heap_ratio_verification(ratio)
210+
raise 'Invalid heap ratio' unless ratio.is_a? Numeric
211+
raise 'heap ratio cannot be greater than 100%' unless ratio <= 1
212+
ratio
213+
end
214+
215+
end
216+
217+
end
218+
end

0 commit comments

Comments
 (0)