Skip to content

Commit 8162a00

Browse files
author
Glyn Normington
committed
Add a Repository module
Rework Details and TomcatDetails in terms of Repository::RepositoryIndex. Improve the interface to DownloadCache. Avoid the need to pass in a separate key which means that the cache is more easily reused without having to pass keys around. Improve the diagnostics for OpenJdk. Abstract common details into Repository::ConfiguredItem Delete some support for system.properties in the process. Normalise VersionResolver parameters. The version specified on resolve must be a TokenizedVersion or nil. Split out some VersionResolver tests into TokenizedVersion tests. [#51720657]
1 parent f689901 commit 8162a00

27 files changed

Lines changed: 475 additions & 521 deletions

lib/java_buildpack/container/tomcat.rb

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# limitations under the License.
1515

1616
require 'java_buildpack/container'
17-
require 'java_buildpack/container/tomcat_details'
17+
require 'java_buildpack/repository/configured_item'
1818
require 'java_buildpack/util/properties'
1919

2020
module JavaBuildpack::Container
@@ -40,8 +40,8 @@ def initialize(context)
4040
# returns +nil+
4141
def detect
4242
if web_inf?
43-
@tomcat_details = TomcatDetails.new(@configuration)
44-
id @tomcat_details
43+
tomcat_version, tomcat_uri = find_tomcat
44+
id tomcat_version
4545
else
4646
nil
4747
end
@@ -67,8 +67,22 @@ def web_inf?
6767
File.exists? File.join(@app_dir, WEB_INF_DIRECTORY)
6868
end
6969

70-
def id(tomcat_details)
71-
"tomcat-#{tomcat_details.configured_version}"
70+
def find_tomcat
71+
JavaBuildpack::Repository::ConfiguredItem.find_item(@configuration) do |version|
72+
check_version_format version
73+
end
74+
rescue => e
75+
raise RuntimeError, "Tomcat container error: #{e.message}", e.backtrace
76+
end
77+
78+
private
79+
80+
def check_version_format(version)
81+
raise "Malformed Tomcat version #{version}: too many version components" if version[3]
82+
end
83+
84+
def id(tomcat_version)
85+
"tomcat-#{tomcat_version}"
7286
end
7387

7488
end

lib/java_buildpack/container/tomcat_details.rb

Lines changed: 0 additions & 75 deletions
This file was deleted.

lib/java_buildpack/jre/openjdk.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
require 'java_buildpack/jre'
1717
require 'java_buildpack/jre/memory/memory_heuristics_openjdk_pre8'
1818
require 'java_buildpack/jre/memory/memory_heuristics_openjdk'
19-
require 'java_buildpack/util/details'
19+
require 'java_buildpack/repository/configured_item'
2020
require 'java_buildpack/util/application_cache'
2121
require 'java_buildpack/util/format_duration'
2222
require 'java_buildpack/util/tokenized_version'
@@ -36,7 +36,7 @@ def initialize(context)
3636
@app_dir = context[:app_dir]
3737
@java_opts = context[:java_opts]
3838
@configuration = context[:configuration]
39-
@details = JavaBuildpack::Util::Details.new(@configuration)
39+
@version, @uri = OpenJdk.find_openjdk(@configuration)
4040
end
4141

4242
# Detects which version of Java this application should use. *NOTE:* This method will always return _some_ value,
@@ -45,17 +45,17 @@ def initialize(context)
4545
# @return [String, nil] returns +openjdk-<version>+.
4646
def detect
4747
memory_sizes @configuration # drive out errors early
48-
id @details
48+
id @version
4949
end
5050

5151
# Downloads and unpacks a JRE
5252
#
5353
# @return [void]
5454
def compile
5555
download_start_time = Time.now
56-
print "-----> Downloading OpenJDK #{@details.version} JRE from #{@details.uri} "
56+
print "-----> Downloading OpenJDK #{@version} JRE from #{@uri} "
5757

58-
JavaBuildpack::Util::ApplicationCache.new.get(id(@details), @details.uri) do |file| # TODO Use global cache #50175265
58+
JavaBuildpack::Util::ApplicationCache.new.get(@uri) do |file| # TODO Use global cache #50175265
5959
puts "(#{(Time.now - download_start_time).duration})"
6060
expand file
6161
end
@@ -93,6 +93,12 @@ def release
9393
}
9494
}
9595

96+
def self.find_openjdk(configuration)
97+
JavaBuildpack::Repository::ConfiguredItem.find_item(configuration)
98+
rescue => e
99+
raise RuntimeError, "OpenJDK JRE error: #{e.message}", e.backtrace
100+
end
101+
96102
def expand(file)
97103
expand_start_time = Time.now
98104
print "-----> Expanding JRE to #{JAVA_HOME} "
@@ -105,8 +111,8 @@ def expand(file)
105111
puts "(#{(Time.now - expand_start_time).duration})"
106112
end
107113

108-
def id(details)
109-
"openjdk-#{details.version}"
114+
def id(version)
115+
"openjdk-#{version}"
110116
end
111117

112118
def to_java_opts(memory_values)
@@ -129,7 +135,7 @@ def memory_sizes(configuration)
129135
end
130136

131137
def pre_8
132-
@details.version < JavaBuildpack::Util::TokenizedVersion.new("1.8.0")
138+
@version < JavaBuildpack::Util::TokenizedVersion.new("1.8.0")
133139
end
134140

135141
def specified_sizes(configuration)

lib/java_buildpack/repository.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Cloud Foundry Java Buildpack
2+
# Copyright (c) 2013 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'
17+
18+
# A module encapsulating versioned file repositories for the Java buildpack.
19+
module JavaBuildpack::Repository
20+
end
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Cloud Foundry Java Buildpack
2+
# Copyright (c) 2013 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/repository'
17+
require 'java_buildpack/repository/repository_index'
18+
require 'java_buildpack/util/tokenized_version'
19+
20+
module JavaBuildpack::Repository
21+
22+
# A class encapsulating details of a file stored in a versioned repository.
23+
class ConfiguredItem
24+
25+
# Finds an instance of the file based on the configuration.
26+
#
27+
# @param [Hash] configuration the configuration
28+
# @param [Block, nil] version_validator an optional version validation block
29+
# @return [TokenizedVersion] the chosen version of the file
30+
# @return [String] the download URI of the chosen version of the file
31+
def self.find_item(configuration, &version_validator)
32+
repository_root = ConfiguredItem.repository_root(configuration)
33+
version = ConfiguredItem.version(configuration)
34+
version_validator.call(version) if version_validator
35+
index = ConfiguredItem.index(repository_root)
36+
index.find_item version
37+
end
38+
39+
private
40+
41+
KEY_REPOSITORY_ROOT = 'repository_root'.freeze
42+
KEY_VERSION = 'version'.freeze
43+
44+
def self.index(repository_root)
45+
RepositoryIndex.new(repository_root)
46+
end
47+
48+
def self.repository_root(configuration)
49+
raise "A repository root must be specified as a key-value pair of '#{KEY_REPOSITORY_ROOT}'' to the URI of the repository." unless configuration.has_key? KEY_REPOSITORY_ROOT
50+
configuration[KEY_REPOSITORY_ROOT]
51+
end
52+
53+
def self.version(configuration)
54+
JavaBuildpack::Util::TokenizedVersion.new(configuration[KEY_VERSION])
55+
end
56+
57+
end
58+
59+
end
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Cloud Foundry Java Buildpack
2+
# Copyright (c) 2013 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/repository'
17+
require 'java_buildpack/util/download_cache'
18+
require 'java_buildpack/util/version_resolver'
19+
20+
module JavaBuildpack::Repository
21+
22+
# A repository index represents the index of repository containing various versions of a file.
23+
class RepositoryIndex
24+
25+
# Creates a new repository index, populating it with values from an index file.
26+
#
27+
# @param [String] repository_root the root of the repository to create the index for
28+
def initialize(repository_root)
29+
@index = {}
30+
JavaBuildpack::Util::DownloadCache.new().get("#{repository_root}#{INDEX_PATH}") do |file| # TODO Use global cache #50175265
31+
@index.merge! YAML.load_file(file)
32+
end
33+
end
34+
35+
# Finds a version of the file matching the given, possibly wildcarded, version.
36+
#
37+
# @param [String] version the possibly wildcarded version to find
38+
# @return [TokenizedVersion] the version of the file found
39+
# @return [String] the URI of the file found
40+
def find_item(version)
41+
version = JavaBuildpack::Util::VersionResolver.resolve(version, @index.keys)
42+
uri = @index[version.to_s]
43+
return version, uri
44+
end
45+
46+
private
47+
48+
INDEX_PATH = '/index.yml'
49+
50+
end
51+
52+
end

0 commit comments

Comments
 (0)