1515
1616require 'java_buildpack/container'
1717require 'java_buildpack/repository/configured_item'
18- require 'java_buildpack/util/properties'
18+ require 'java_buildpack/util/application_cache'
19+ require 'java_buildpack/util/format_duration'
1920
2021module JavaBuildpack ::Container
2122
@@ -26,63 +27,120 @@ class Tomcat
2627 #
2728 # @param [Hash] context the context that is provided to the instance
2829 # @option context [String] :app_dir the directory that the application exists in
30+ # @option context [String] :java_home the directory that acts as +JAVA_HOME+
2931 # @option context [Array<String>] :java_opts an array that Java options can be added to
30- # @option context [Hash] :configuration the properties
32+ # @option context [Hash] :configuration the properties provided by the user
3133 def initialize ( context )
3234 @app_dir = context [ :app_dir ]
35+ @java_home = context [ :java_home ]
3336 @java_opts = context [ :java_opts ]
34- @configuration = context [ :configuration ]
37+ @version , @uri = Tomcat . find_tomcat ( @app_dir , context [ :configuration ] )
38+ @tomcat_home = Tomcat . tomcat_home @app_dir
3539 end
3640
3741 # Detects whether this application is a Tomcat application.
3842 #
3943 # @return [String] returns +tomcat-<version>+ if and only if the application has a +WEB-INF+ directory, otherwise
4044 # returns +nil+
4145 def detect
42- if web_inf?
43- tomcat_version , tomcat_uri = find_tomcat
44- id tomcat_version
45- else
46- nil
47- end
46+ @version ? id ( @version ) : nil
4847 end
4948
50- # Does nothing as no transformations are currently performed for Tomcat applications.
49+ # Downlaods and unpacks a Tomcat instance
5150 #
5251 # @return [void]
5352 def compile
53+ download_start_time = Time . now
54+ print "-----> Downloading Tomcat #{ @version } from #{ @uri } "
55+
56+ JavaBuildpack ::Util ::ApplicationCache . new . get ( @uri ) do |file | # TODO Use global cache #50175265
57+ puts "(#{ ( Time . now - download_start_time ) . duration } )"
58+ expand file
59+ end
60+
61+ link_application
5462 end
5563
5664 # Creates the command to run the Tomcat application.
5765 #
5866 # @return [String] the command to run the application.
5967 def release
68+ @java_opts << "-D#{ KEY_HTTP_PORT } =$PORT"
69+
70+ "JAVA_HOME=#{ @java_home } JAVA_OPTS=\" #{ java_opts } \" #{ @tomcat_home } /bin/catalina.sh run"
6071 end
6172
6273 private
6374
75+ KEY_HTTP_PORT = 'http.port' . freeze
76+
77+ RESOURCES = '../../../resources/tomcat'
78+
79+ TOMCAT_HOME = '.tomcat' . freeze
80+
6481 WEB_INF_DIRECTORY = 'WEB-INF' . freeze
6582
66- def web_inf?
67- File . exists? File . join ( @app_dir , WEB_INF_DIRECTORY )
83+ def self . check_version_format ( version )
84+ raise "Malformed Tomcat version #{ version } : too many version components" if version [ 3 ]
6885 end
6986
70- def find_tomcat
71- JavaBuildpack ::Repository ::ConfiguredItem . find_item ( @configuration ) do |version |
72- check_version_format version
87+ def copy_resources ( tomcat_home )
88+ resources = File . expand_path ( RESOURCES , File . dirname ( __FILE__ ) )
89+ system "cp -r #{ resources } /* #{ tomcat_home } "
90+ end
91+
92+ def expand ( file )
93+ expand_start_time = Time . now
94+ print "-----> Expanding Tomcat to #{ TOMCAT_HOME } "
95+
96+ tomcat_home = File . join @app_dir , TOMCAT_HOME
97+ system "rm -rf #{ @tomcat_home } "
98+ system "mkdir -p #{ @tomcat_home } "
99+ system "tar xzf #{ file . path } -C #{ @tomcat_home } --strip 1 --exclude webapps --exclude conf/server.xml --exclude conf/context.xml 2>&1"
100+
101+ copy_resources @tomcat_home
102+
103+ puts "(#{ ( Time . now - expand_start_time ) . duration } )"
104+ end
105+
106+ def self . find_tomcat ( app_dir , configuration )
107+ if web_inf? app_dir
108+ version , uri = JavaBuildpack ::Repository ::ConfiguredItem . find_item ( configuration ) do |version |
109+ check_version_format version
110+ end
111+ else
112+ version = nil
113+ uri = nil
73114 end
115+
116+ return version , uri
74117 rescue => e
75118 raise RuntimeError , "Tomcat container error: #{ e . message } " , e . backtrace
76119 end
77120
78- private
121+ def id ( version )
122+ "tomcat-#{ version } "
123+ end
79124
80- def check_version_format ( version )
81- raise "Malformed Tomcat version #{ version } : too many version components" if version [ 3 ]
125+ def java_opts
126+ @java_opts . compact . sort . join ( ' ' )
127+ end
128+
129+ def link_application
130+ webapps = "#{ @tomcat_home } /webapps"
131+ root = "#{ webapps } /ROOT"
132+
133+ system "rm -rf #{ root } "
134+ system "mkdir -p #{ webapps } "
135+ system "ln -s #{ File . expand_path @app_dir } #{ root } "
136+ end
137+
138+ def self . tomcat_home ( app_dir )
139+ File . join app_dir , TOMCAT_HOME
82140 end
83141
84- def id ( tomcat_version )
85- "tomcat- #{ tomcat_version } "
142+ def self . web_inf? ( app_dir )
143+ File . exists? File . join ( app_dir , WEB_INF_DIRECTORY )
86144 end
87145
88146 end
0 commit comments