|
| 1 | +# Encoding: utf-8 |
| 2 | +# Cloud Foundry Java Buildpack |
| 3 | +# Copyright 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' |
| 18 | +require 'java_buildpack/util/application_cache' |
| 19 | + |
| 20 | +module JavaBuildpack |
| 21 | + |
| 22 | + # A convenience base class for all components in the buildpack. This base class ensures that the contents of the |
| 23 | + # +context+ are assigned to instance variables matching their keys. It also ensures that all contract methods are |
| 24 | + # implemented. |
| 25 | + class BaseComponent |
| 26 | + |
| 27 | + # Creates an instance. The contents of +context+ are assigned to instance variables matching their keys. |
| 28 | + # +component_name+ and +context+ are exposed via +@component_name+ and +@context+ respectively for any component |
| 29 | + # that wishes to use them. |
| 30 | + # |
| 31 | + # @param [Hash] context A shared context provided to all components |
| 32 | + def initialize(component_name, context) |
| 33 | + @component_name = component_name |
| 34 | + @context = context |
| 35 | + @context.each { |key, value| instance_variable_set("@#{key}", value) } |
| 36 | + end |
| 37 | + |
| 38 | + # If the component should be used when staging an application |
| 39 | + # |
| 40 | + # @return [Array<String>, String, nil] If the component should be used when staging the application, a +String+ or |
| 41 | + # an +Array<String>+ that uniquely identifies the component (e.g. |
| 42 | + # +openjdk-1.7.0_40+). Otherwise, +nil+. |
| 43 | + def detect |
| 44 | + fail "Method 'detect' must be defined" |
| 45 | + end |
| 46 | + |
| 47 | + # Modifies the application's file system. The component is expected to transform the application's file system in |
| 48 | + # whatever way is necessary (e.g. downloading files or creating symbolic links) to support the function of the |
| 49 | + # component. Status output written to +STDOUT+ is expected as part of this invocation. |
| 50 | + # |
| 51 | + # @return [void] |
| 52 | + def compile |
| 53 | + fail "Method 'compile' must be defined" |
| 54 | + end |
| 55 | + |
| 56 | + # Modifies the application's runtime configuration. The component is expected to transform members of the +context+ |
| 57 | + # (e.g. +@java_home+, +@java_opts+, etc.) in whatever way is necessary to support the function of the component. |
| 58 | + # |
| 59 | + # Container components are also expected to create the command required to run the application. These components |
| 60 | + # are expected to read the +context+ values and take them into account when creating the command. |
| 61 | + # |
| 62 | + # @return [void, String] components other than containers are not expected to return any value. Container |
| 63 | + # compoonents are expected to return the command required to run the application. |
| 64 | + def release |
| 65 | + fail "Method 'release' must be defined" |
| 66 | + end |
| 67 | + |
| 68 | + protected |
| 69 | + |
| 70 | + # Downloads an item with the given name and version from the given URI, then yields the resultant file to the given |
| 71 | + # block. |
| 72 | + # |
| 73 | + # @param [JavaBuildpack::Util::TokenizedVersion] version |
| 74 | + # @param [String] uri |
| 75 | + # @param [String] description an optional description for the download. Defaults to +@component_name+. |
| 76 | + # @return [void] |
| 77 | + def download(version, uri, description = @component_name, &block) |
| 78 | + download_start_time = Time.now |
| 79 | + print "-----> Downloading #{description} #{version} from #{uri} " |
| 80 | + |
| 81 | + JavaBuildpack::Util::ApplicationCache.new.get(uri) do |file| # TODO: Use global cache #50175265 |
| 82 | + puts "(#{(Time.now - download_start_time).duration})" |
| 83 | + yield file |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + # Downloads a given JAR and copies it to a given destination. |
| 88 | + # |
| 89 | + # @param [JavaBuildpack::Util::TokenizedVersion] version the version of the item |
| 90 | + # @param [String] uri the URI of the item |
| 91 | + # @param [String] jar_name the filename of the item |
| 92 | + # @param [String] target_directory the path of the directory into which to download the item. Defaults to |
| 93 | + # +@lib_directory+ |
| 94 | + # @param [String] description an optional description for the download. Defaults to +@component_name+. |
| 95 | + def download_jar(version, uri, jar_name, target_directory = @lib_directory, description = @component_name) |
| 96 | + download(version, uri, description) { |file| system "cp #{file.path} #{File.join(target_directory, jar_name)}" } |
| 97 | + end |
| 98 | + |
| 99 | + end |
| 100 | + |
| 101 | +end |
0 commit comments