|
| 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/dash_case' |
| 20 | +require 'java_buildpack/util/find_single_directory' |
| 21 | +require 'java_buildpack/util/play/factory' |
| 22 | +require 'java_buildpack/util/qualify_path' |
| 23 | +require 'java_buildpack/util/start_script' |
| 24 | + |
| 25 | +module JavaBuildpack |
| 26 | + module Container |
| 27 | + |
| 28 | + # Encapsulates the detect, compile, and release functionality for +distZip+ style applications. |
| 29 | + class DistZip < JavaBuildpack::Component::BaseComponent |
| 30 | + include JavaBuildpack::Util |
| 31 | + |
| 32 | + # Creates an instance |
| 33 | + # |
| 34 | + # @param [Hash] context a collection of utilities used the component |
| 35 | + def initialize(context) |
| 36 | + super(context) |
| 37 | + end |
| 38 | + |
| 39 | + # (see JavaBuildpack::Component::BaseComponent#detect) |
| 40 | + def detect |
| 41 | + supports? ? DistZip.to_s.dash_case : nil |
| 42 | + end |
| 43 | + |
| 44 | + # (see JavaBuildpack::Component::BaseComponent#compile) |
| 45 | + def compile |
| 46 | + start_script.chmod 0755 |
| 47 | + augment_classpath |
| 48 | + end |
| 49 | + |
| 50 | + # (see JavaBuildpack::Component::BaseComponent#release) |
| 51 | + def release |
| 52 | + [ |
| 53 | + @droplet.java_home.as_env_var, |
| 54 | + @droplet.java_opts.as_env_var, |
| 55 | + 'SERVER_PORT=$PORT', |
| 56 | + qualify_path(start_script, @droplet.root) |
| 57 | + ].flatten.compact.join(' ') |
| 58 | + end |
| 59 | + |
| 60 | + private |
| 61 | + |
| 62 | + PATTERN_APP_CLASSPATH = /^declare -r app_classpath=\"(.*)\"$/ |
| 63 | + |
| 64 | + PATTERN_CLASSPATH = /^CLASSPATH=(.*)$/.freeze |
| 65 | + |
| 66 | + def augment_classpath |
| 67 | + content = start_script.read |
| 68 | + |
| 69 | + if content =~ PATTERN_CLASSPATH |
| 70 | + additional_classpath = @droplet.additional_libraries.sort.map do |additional_library| |
| 71 | + "$APP_HOME/#{additional_library.relative_path_from(root)}" |
| 72 | + end |
| 73 | + |
| 74 | + update_file start_script, content, |
| 75 | + PATTERN_CLASSPATH, "CLASSPATH=#{additional_classpath.join(':')}:\\1" |
| 76 | + elsif content =~ PATTERN_APP_CLASSPATH |
| 77 | + additional_classpath = @droplet.additional_libraries.sort.map do |additional_library| |
| 78 | + "$app_home/#{additional_library.relative_path_from(start_script.dirname)}" |
| 79 | + end |
| 80 | + |
| 81 | + update_file start_script, content, |
| 82 | + PATTERN_APP_CLASSPATH, "declare -r app_classpath=\"#{additional_classpath.join(':')}:\\1\"" |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + def jars? |
| 87 | + (lib_dir + '*.jar').glob.any? |
| 88 | + end |
| 89 | + |
| 90 | + def lib_dir |
| 91 | + root + 'lib' |
| 92 | + end |
| 93 | + |
| 94 | + def root |
| 95 | + find_single_directory || @droplet.root |
| 96 | + end |
| 97 | + |
| 98 | + def start_script |
| 99 | + JavaBuildpack::Util.start_script root |
| 100 | + end |
| 101 | + |
| 102 | + def supports? |
| 103 | + start_script && start_script.exist? && jars? && !JavaBuildpack::Util::Play::Factory.create(@droplet) |
| 104 | + end |
| 105 | + |
| 106 | + def update_file(path, content, pattern, replacement) |
| 107 | + path.open('w') do |f| |
| 108 | + f.write content.gsub pattern, replacement |
| 109 | + f.fsync |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | + end |
| 114 | + |
| 115 | + end |
| 116 | +end |
0 commit comments