|
| 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/component/base_component' |
| 18 | +require 'java_buildpack/container' |
| 19 | +require 'java_buildpack/util/find_single_directory' |
| 20 | +require 'java_buildpack/util/qualify_path' |
| 21 | +require 'java_buildpack/util/start_script' |
| 22 | + |
| 23 | +module JavaBuildpack |
| 24 | + module Container |
| 25 | + |
| 26 | + # Encapsulates the detect, compile, and release functionality for selecting a `distZip`-like container. |
| 27 | + class DistZipLike < JavaBuildpack::Component::BaseComponent |
| 28 | + include JavaBuildpack::Util |
| 29 | + |
| 30 | + # (see JavaBuildpack::Component::BaseComponent#detect) |
| 31 | + def detect |
| 32 | + supports? ? id : nil |
| 33 | + end |
| 34 | + |
| 35 | + # (see JavaBuildpack::Component::BaseComponent#compile) |
| 36 | + def compile |
| 37 | + start_script(root).chmod 0755 |
| 38 | + augment_classpath_content |
| 39 | + end |
| 40 | + |
| 41 | + # (see JavaBuildpack::Component::BaseComponent#release) |
| 42 | + def release |
| 43 | + [ |
| 44 | + @droplet.java_home.as_env_var, |
| 45 | + @droplet.java_opts.as_env_var, |
| 46 | + qualify_path(start_script(root), @droplet.root) |
| 47 | + ].flatten.compact.join(' ') |
| 48 | + end |
| 49 | + |
| 50 | + protected |
| 51 | + |
| 52 | + # The id of this container |
| 53 | + # |
| 54 | + # @return [String] the id of this container |
| 55 | + def id |
| 56 | + fail "Method 'id' must be defined" |
| 57 | + end |
| 58 | + |
| 59 | + # The root directory of the application |
| 60 | + # |
| 61 | + # @return [Pathname] the root directory of the application |
| 62 | + def root |
| 63 | + find_single_directory || @droplet.root |
| 64 | + end |
| 65 | + |
| 66 | + # Whether or not this component supports this application |
| 67 | + # |
| 68 | + # @return [Boolean] whether or not this component supports this application |
| 69 | + def supports? |
| 70 | + fail "Method 'supports?' must be defined" |
| 71 | + end |
| 72 | + |
| 73 | + private |
| 74 | + |
| 75 | + PATTERN_APP_CLASSPATH = /^declare -r app_classpath=\"(.*)\"$/ |
| 76 | + |
| 77 | + PATTERN_CLASSPATH = /^CLASSPATH=(.*)$/.freeze |
| 78 | + |
| 79 | + private_constant :PATTERN_APP_CLASSPATH, :PATTERN_CLASSPATH |
| 80 | + |
| 81 | + def augment_app_classpath(content) |
| 82 | + additional_classpath = @droplet.additional_libraries.sort.map do |additional_library| |
| 83 | + "$app_home/#{additional_library.relative_path_from(start_script(root).dirname)}" |
| 84 | + end |
| 85 | + |
| 86 | + update_file start_script(root), content, |
| 87 | + PATTERN_APP_CLASSPATH, "declare -r app_classpath=\"#{additional_classpath.join(':')}:\\1\"" |
| 88 | + end |
| 89 | + |
| 90 | + def augment_classpath(content) |
| 91 | + additional_classpath = @droplet.additional_libraries.sort.map do |additional_library| |
| 92 | + "$APP_HOME/#{additional_library.relative_path_from(root)}" |
| 93 | + end |
| 94 | + |
| 95 | + update_file start_script(root), content, |
| 96 | + PATTERN_CLASSPATH, "CLASSPATH=#{additional_classpath.join(':')}:\\1" |
| 97 | + end |
| 98 | + |
| 99 | + def augment_classpath_content |
| 100 | + content = start_script(root).read |
| 101 | + |
| 102 | + if content =~ PATTERN_CLASSPATH |
| 103 | + augment_classpath content |
| 104 | + elsif content =~ PATTERN_APP_CLASSPATH |
| 105 | + augment_app_classpath content |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + def update_file(path, content, pattern, replacement) |
| 110 | + path.open('w') do |f| |
| 111 | + f.write content.gsub pattern, replacement |
| 112 | + f.fsync |
| 113 | + end |
| 114 | + end |
| 115 | + |
| 116 | + end |
| 117 | + |
| 118 | + end |
| 119 | +end |
0 commit comments