Skip to content

Commit da8110b

Browse files
author
Glyn Normington
committed
Move duplicated code to Resources utility class
[#56447064]
1 parent 649023d commit da8110b

6 files changed

Lines changed: 57 additions & 44 deletions

File tree

lib/java_buildpack/container/tomcat.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
require 'java_buildpack/repository/configured_item'
2121
require 'java_buildpack/util/application_cache'
2222
require 'java_buildpack/util/format_duration'
23+
require 'java_buildpack/util/resource_utils'
2324

2425
module JavaBuildpack::Container
2526

@@ -86,17 +87,10 @@ def release
8687

8788
KEY_SUPPORT = 'support'.freeze
8889

89-
RESOURCES = File.join('..', '..', '..', 'resources', 'tomcat').freeze
90-
9190
TOMCAT_HOME = '.tomcat'.freeze
9291

9392
WEB_INF_DIRECTORY = 'WEB-INF'.freeze
9493

95-
def copy_resources(tomcat_home)
96-
resources = File.expand_path(RESOURCES, File.dirname(__FILE__))
97-
system "cp -r #{File.join resources, '*'} #{tomcat_home}"
98-
end
99-
10094
def download_tomcat
10195
JavaBuildpack::Util::ApplicationCache.download('Tomcat', @tomcat_version, @tomcat_uri) do |file|
10296
expand(file, @configuration)
@@ -115,7 +109,7 @@ def expand(file, configuration)
115109
system "mkdir -p #{tomcat_home}"
116110
system "tar xzf #{file.path} -C #{tomcat_home} --strip 1 --exclude webapps --exclude #{File.join 'conf', 'server.xml'} --exclude #{File.join 'conf', 'context.xml'} 2>&1"
117111

118-
copy_resources tomcat_home
112+
JavaBuildpack::Util::ResourceUtils.copy_resources('tomcat', tomcat_home)
119113
puts "(#{(Time.now - expand_start_time).duration})"
120114
end
121115

lib/java_buildpack/framework/new_relic.rb

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
require 'java_buildpack/framework'
1818
require 'java_buildpack/repository/configured_item'
1919
require 'java_buildpack/util/application_cache'
20+
require 'java_buildpack/util/resource_utils'
2021

2122
module JavaBuildpack::Framework
2223

@@ -57,7 +58,7 @@ def compile
5758
system "mkdir -p #{File.join new_relic_home, 'logs'}"
5859

5960
JavaBuildpack::Util::ApplicationCache.download_jar(@version, @uri, 'New Relic Agent', jar_name(@version), new_relic_home)
60-
copy_resources new_relic_home
61+
JavaBuildpack::Util::ResourceUtils.copy_resources('new-relic', new_relic_home)
6162
end
6263

6364
# Adds configuration information to +JAVA_OPTS+
@@ -75,24 +76,10 @@ def release
7576

7677
NAME_KEY = 'application_name'
7778

78-
RESOURCES = File.join('..', '..', '..', 'resources', 'new-relic').freeze
79-
8079
NEW_RELIC_HOME = '.new-relic'.freeze
8180

82-
def copy_resources(new_relic_home)
83-
resources = File.expand_path(RESOURCES, File.dirname(__FILE__))
84-
system "cp -r #{File.join resources, '*'} #{new_relic_home}"
85-
end
86-
8781
def self.find_new_relic_agent(vcap_services, configuration)
88-
if license_key(vcap_services)
89-
version, uri = JavaBuildpack::Repository::ConfiguredItem.find_item(configuration)
90-
else
91-
version = nil
92-
uri = nil
93-
end
94-
95-
return version, uri # rubocop:disable RedundantReturn
82+
license_key(vcap_services) ? JavaBuildpack::Repository::ConfiguredItem.find_item(configuration) : [nil, nil]
9683
end
9784

9885
def id(version)
@@ -114,7 +101,7 @@ def self.license_key(vcap_services)
114101
license_key = services[0]['credentials']['licenseKey']
115102
end
116103

117-
return license_key
104+
license_key
118105
end
119106

120107
def new_relic_home

lib/java_buildpack/framework/play_jpa_plugin.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,7 @@ def self.candidate?(app_dir)
7575
end
7676

7777
def self.find_play_jpa_plugin(app_dir, configuration)
78-
if candidate? app_dir
79-
version, uri = JavaBuildpack::Repository::ConfiguredItem.find_item(configuration)
80-
else
81-
version = nil
82-
uri = nil
83-
end
84-
85-
return version, uri # rubocop:disable RedundantReturn
78+
candidate?(app_dir) ? JavaBuildpack::Repository::ConfiguredItem.find_item(configuration) : [nil, nil]
8679
end
8780

8881
def id(version)

lib/java_buildpack/framework/spring_auto_reconfiguration.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,7 @@ def release
6969
WEB_XML = File.join 'WEB-INF', 'web.xml'
7070

7171
def self.find_auto_reconfiguration(app_dir, configuration)
72-
if spring_application? app_dir
73-
version, uri = JavaBuildpack::Repository::ConfiguredItem.find_item(configuration)
74-
else
75-
version = nil
76-
uri = nil
77-
end
78-
79-
return version, uri # rubocop:disable RedundantReturn
72+
spring_application?(app_dir) ? JavaBuildpack::Repository::ConfiguredItem.find_item(configuration) : [nil, nil]
8073
end
8174

8275
def id(version)

lib/java_buildpack/jre/openjdk.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
require 'java_buildpack/repository/configured_item'
2323
require 'java_buildpack/util/application_cache'
2424
require 'java_buildpack/util/format_duration'
25+
require 'java_buildpack/util/resource_utils'
2526
require 'java_buildpack/util/tokenized_version'
2627

2728
module JavaBuildpack::Jre
@@ -77,8 +78,6 @@ def release
7778

7879
private
7980

80-
RESOURCES = '../../../resources/openjdk/diagnostics'.freeze
81-
8281
JAVA_HOME = '.java'.freeze
8382

8483
KEY_MEMORY_HEURISTICS = 'memory_heuristics'
@@ -121,7 +120,7 @@ def pre_8
121120
end
122121

123122
def copy_killjava_script
124-
resources = File.expand_path(RESOURCES, File.dirname(__FILE__))
123+
resources = JavaBuildpack::Util::ResourceUtils.get_resources(File.join('openjdk', 'diagnostics'))
125124
killjava_file_content = File.read(File.join resources, KILLJAVA_FILE_NAME)
126125
updated_content = killjava_file_content.gsub(/@@LOG_FILE_NAME@@/, JavaBuildpack::Diagnostics::LOG_FILE_NAME)
127126
diagnostic_dir = JavaBuildpack::Diagnostics.get_diagnostic_directory @app_dir
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Encoding: utf-8
2+
# Cloud Foundry Java Buildpack
3+
# Copyright (c) 2013 the original author or authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
require 'java_buildpack/util'
18+
19+
module JavaBuildpack::Util
20+
21+
# Utilities for dealing with buildpack resources
22+
class ResourceUtils
23+
24+
# Copies the contents of the given subdirectory of the buildpack resources directory to the given target directory.
25+
#
26+
# @param [String] subdirectory the subdirectory of the resources directory
27+
# @param [String] target the target directory
28+
def self.copy_resources(subdirectory, target)
29+
system "cp -r #{File.join(get_resources(subdirectory), '*')} #{target}"
30+
end
31+
32+
# Returns the path of the given subdirectory of the buildpack resources directory.
33+
#
34+
# @param [String] subdirectory the subdirectory of the resources directory
35+
# @return [String] the path of the subdirectory
36+
def self.get_resources(subdirectory)
37+
File.join(File.expand_path(RESOURCES, File.dirname(__FILE__)), subdirectory)
38+
end
39+
40+
private
41+
42+
RESOURCES = File.join('..', '..', '..', 'resources').freeze
43+
44+
private_class_method :new
45+
end
46+
47+
end

0 commit comments

Comments
 (0)