Skip to content

Commit d7fc55a

Browse files
committed
Cache Factory
This change adds a CacheFactory that will automatically choose the appropriate type of cache based on whether an invocation has access to an application cache location.
1 parent c36c854 commit d7fc55a

5 files changed

Lines changed: 126 additions & 9 deletions

File tree

lib/java_buildpack/component/base_component.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
require 'fileutils'
1717
require 'java_buildpack/component'
18-
require 'java_buildpack/util/cache/application_cache'
18+
require 'java_buildpack/util/cache/cache_factory'
1919
require 'java_buildpack/util/colorize'
2020
require 'java_buildpack/util/format_duration'
2121
require 'java_buildpack/util/shell'
@@ -89,7 +89,7 @@ def download(version, uri, name = @component_name)
8989
download_start_time = Time.now
9090
print "#{'----->'.red.bold} Downloading #{name.blue.bold} #{version.to_s.blue} from #{uri.sanitize_uri} "
9191

92-
JavaBuildpack::Util::Cache::ApplicationCache.new.get(uri) do |file, downloaded|
92+
JavaBuildpack::Util::Cache::CacheFactory.create.get(uri) do |file, downloaded|
9393
if downloaded
9494
puts "(#{(Time.now - download_start_time).duration})".green.italic
9595
else

lib/java_buildpack/repository/repository_index.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
require 'java_buildpack/logging/logger_factory'
1717
require 'java_buildpack/repository'
1818
require 'java_buildpack/repository/version_resolver'
19-
require 'java_buildpack/util/cache'
20-
require 'java_buildpack/util/cache/download_cache'
19+
require 'java_buildpack/util/cache/cache_factory'
2120
require 'java_buildpack/util/configuration_utils'
2221
require 'rbconfig'
2322
require 'yaml'
@@ -66,8 +65,7 @@ def architecture
6665
end
6766

6867
def cache
69-
JavaBuildpack::Util::Cache::DownloadCache.new(Pathname.new(Dir.tmpdir),
70-
JavaBuildpack::Util::Cache::CACHED_RESOURCES_DIRECTORY)
68+
JavaBuildpack::Util::Cache::CacheFactory.create
7169
end
7270

7371
def canonical(raw)

lib/java_buildpack/util/cache/application_cache.rb

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
require 'java_buildpack/logging/logger_factory'
1617
require 'java_buildpack/util/cache'
1718
require 'java_buildpack/util/cache/download_cache'
1819

@@ -26,11 +27,32 @@ module Cache
2627
# <b>WARNING: This cache should only by used by code run by the +compile+ script</b>
2728
class ApplicationCache < DownloadCache
2829

30+
class << self
31+
32+
# Whether an +ApplicationCache+ can be created
33+
#
34+
# @return [Boolean] whether an +ApplicationCache+ can be created
35+
def available?
36+
!application_cache_directory.nil?
37+
end
38+
39+
# The path to the application cache directory if it exists
40+
#
41+
# @return [void, String] the path to the application cache directory if it exists
42+
def application_cache_directory
43+
ARGV[1]
44+
end
45+
46+
end
47+
2948
# Creates an instance of the cache that is backed by the the application cache
3049
def initialize
31-
application_cache_directory = ARGV[1]
32-
raise 'Application cache directory is undefined' if application_cache_directory.nil?
33-
super(Pathname.new(application_cache_directory), CACHED_RESOURCES_DIRECTORY)
50+
logger = Logging::LoggerFactory.instance.get_logger ApplicationCache
51+
52+
raise 'Application cache directory is undefined' unless self.class.available?
53+
logger.debug { "Application Cache Directory: #{self.class.application_cache_directory}" }
54+
55+
super(Pathname.new(self.class.application_cache_directory), CACHED_RESOURCES_DIRECTORY)
3456
end
3557

3658
end
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Cloud Foundry Java Buildpack
2+
# Copyright 2013-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/util/cache'
17+
require 'java_buildpack/util/cache/application_cache'
18+
require 'java_buildpack/util/cache/download_cache'
19+
20+
module JavaBuildpack
21+
module Util
22+
module Cache
23+
24+
# A factory for creating {DownloadCache}s. Will create an {ApplicationCache} if it can, otherwise a
25+
# {DownloadCache}.
26+
class CacheFactory
27+
28+
class << self
29+
30+
# Creates a new instance of an {ApplicationCache} if it can, otherwise a {DownloadCache}
31+
#
32+
# @return [ApplicationCache, DownloadCache] a new instance of an {ApplicationCache} if it can, otherwise a
33+
# {DownloadCache}
34+
def create
35+
if ApplicationCache.available?
36+
ApplicationCache.new
37+
else
38+
DownloadCache.new(Pathname.new(Dir.tmpdir), JavaBuildpack::Util::Cache::CACHED_RESOURCES_DIRECTORY)
39+
end
40+
end
41+
42+
end
43+
44+
end
45+
46+
end
47+
end
48+
end
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Cloud Foundry Java Buildpack
2+
# Copyright 2013-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 'spec_helper'
17+
require 'application_helper'
18+
require 'internet_availability_helper'
19+
require 'logging_helper'
20+
require 'java_buildpack/util/cache/application_cache'
21+
require 'java_buildpack/util/cache/cache_factory'
22+
require 'java_buildpack/util/cache/download_cache'
23+
24+
describe JavaBuildpack::Util::Cache::CacheFactory do
25+
include_context 'application_helper'
26+
include_context 'internet_availability_helper'
27+
include_context 'logging_helper'
28+
29+
previous_arg_value = ARGV[1]
30+
31+
before do
32+
ARGV[1] = nil
33+
end
34+
35+
after do
36+
ARGV[1] = previous_arg_value
37+
end
38+
39+
it 'returns an ApplicationCache if ARGV[1] is defined' do
40+
ARGV[1] = app_dir
41+
42+
expect(described_class.create).to be_instance_of JavaBuildpack::Util::Cache::ApplicationCache
43+
end
44+
45+
it 'returns a DownloadCache if ARGV[1] is not defined' do
46+
expect(described_class.create).to be_instance_of JavaBuildpack::Util::Cache::DownloadCache
47+
end
48+
49+
end

0 commit comments

Comments
 (0)