trinidad 1.4.5.B1 → 1.4.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,23 +3,23 @@ require 'fileutils'
3
3
 
4
4
  module Trinidad
5
5
  module Logging
6
-
6
+
7
7
  JUL = Java::JavaUtilLogging
8
8
  LogFactory = Java::OrgApacheJuliLogging::LogFactory
9
-
9
+
10
10
  @@configured = nil
11
-
11
+
12
12
  # Configure the "global" Trinidad logging.
13
13
  def self.configure(log_level = nil)
14
14
  return false if @@configured
15
15
  @@configured = true
16
-
16
+
17
17
  root_logger = JUL::Logger.getLogger('')
18
18
  level = parse_log_level(log_level, :INFO)
19
-
19
+
20
20
  out_handler = new_console_handler JRuby.runtime.out
21
21
  out_handler.formatter = console_formatter
22
-
22
+
23
23
  root_logger.synchronized do
24
24
  root_logger.handlers.to_a.each do |handler|
25
25
  root_logger.remove_handler(handler) if handler.is_a?(JUL::ConsoleHandler)
@@ -33,26 +33,26 @@ module Trinidad
33
33
  err_handler.formatter = console_formatter
34
34
  err_handler.level = level.intValue > JUL::Level::WARNING.intValue ?
35
35
  level : JUL::Level::WARNING # only >= WARNING on STDERR
36
-
36
+
37
37
  root_logger.add_handler(err_handler)
38
38
  end
39
39
  set_log_level(root_logger, level)
40
40
  end
41
41
  silence_tomcat_loggers
42
-
42
+
43
43
  root_logger
44
44
  end
45
-
45
+
46
46
  # Force logging (re-)configuration.
47
47
  # @see #configure
48
48
  def self.configure!(log_level = nil)
49
49
  ( @@configured = false ) || configure(log_level)
50
50
  end
51
-
51
+
52
52
  def self.configure_web_app!(web_app, context)
53
53
  configure_web_app!(web_app, context, true)
54
54
  end
55
-
55
+
56
56
  # Configure logging for a web application.
57
57
  def self.configure_web_app(web_app, context, reset = nil)
58
58
  param_name, param_value = 'jruby.rack.logging', 'JUL'
@@ -70,14 +70,14 @@ module Trinidad
70
70
  context.add_parameter(param_name, logger_name = context.send(:logName))
71
71
  end
72
72
  configure # make sure 'global' logging is configured
73
-
73
+
74
74
  logger = JUL::Logger.getLogger(logger_name) # exclusive for web app
75
75
  logger.handlers.each { |h| logger.remove_handler(h); h.close } if reset
76
76
  # avoid duplicate calls - do not configure (e.g. FileHandler) twice :
77
77
  return false unless logger.handlers.empty?
78
-
78
+
79
79
  logging = web_app.logging
80
-
80
+
81
81
  logger.level = parse_log_level(logging[:level], nil)
82
82
  # delegate to root (console) output only in development mode :
83
83
  logger.use_parent_handlers = logging[:use_parent_handlers]
@@ -97,31 +97,31 @@ module Trinidad
97
97
  end
98
98
  logger
99
99
  end
100
-
100
+
101
101
  protected
102
-
102
+
103
103
  def self.console_formatter
104
104
  MessageFormatter.new
105
105
  end
106
-
106
+
107
107
  def self.web_app_formatter(format = nil)
108
108
  # format used by Rails "2012-06-13 16:42:21 +0200"
109
109
  DefaultFormatter.new(format.nil? ? 'yyyy-MM-dd HH:mm:ss Z' : format)
110
110
  end
111
-
111
+
112
112
  private
113
-
113
+
114
114
  def self.parse_log_level(log_level, default = nil)
115
115
  log_level = log_level && log_level.to_s.upcase
116
116
  unless JUL::Level.constants.find { |level| level.to_s == log_level }
117
117
  log_level = { # try mapping common level names to JUL names
118
- 'ERROR' => 'SEVERE', 'WARN' => 'WARNING', 'DEBUG' => 'FINE'
118
+ 'ERROR' => 'SEVERE', 'WARN' => 'WARNING', 'DEBUG' => 'FINE'
119
119
  }[log_level]
120
120
  log_level = default ? default.to_s.upcase : nil unless log_level
121
121
  end
122
122
  JUL::Level.parse(log_level) if log_level
123
123
  end
124
-
124
+
125
125
  def self.set_log_level(logger, level)
126
126
  logger.level = level; LogFactory.getLog(logger.name)
127
127
  end
@@ -143,16 +143,16 @@ module Trinidad
143
143
  set_log_level(logger, level) if logger
144
144
  end
145
145
  end
146
-
146
+
147
147
  def self.web_app_context_param(web_app, context, name)
148
148
  context.find_parameter(name) || web_app.web_xml_context_param(name)
149
149
  end
150
-
150
+
151
151
  JUL::ConsoleHandler.class_eval do
152
152
  field_accessor :sealed rescue nil
153
153
  field_writer :writer rescue nil
154
154
  end
155
-
155
+
156
156
  def self.new_console_handler(stream)
157
157
  handler = JUL::ConsoleHandler.new # sets output stream to System.err
158
158
  handler.writer = nil if handler.respond_to?(:writer=) # avoid writer.close
@@ -165,90 +165,14 @@ module Trinidad
165
165
  end
166
166
  handler
167
167
  end
168
-
169
- if ( Java::JavaClass.for_name('rb.trinidad.logging.FileHandler') rescue nil )
170
- FileHandler = Java::RbTrinidadLogging::FileHandler # recent trinidad_jars
171
- else
172
- # we'd achieve logging to a production.log file while rotating it (daily)
173
- class FileHandler < Java::OrgApacheJuli::FileHandler # :nodoc
174
-
175
- field_reader :directory, :prefix, :suffix
176
- field_accessor :rotatable, :bufferSize => :buffer_size
177
-
178
- # JULI::FileHandler internals :
179
- field_accessor :date => :_date # current date string e.g. 2012-06-26
180
-
181
- def initialize(directory, prefix, suffix)
182
- super(directory, prefix, suffix)
183
- self._date = nil # to openWriter on first #publish(record)
184
- end
185
-
186
- def openWriter
187
- # NOTE: following code is heavily based on super's internals !
188
- synchronized do
189
- # we're normally in the lock here (from #publish)
190
- # thus we do not perform any more synchronization
191
- prev_rotatable = self.rotatable
192
- begin
193
- self.rotatable = false
194
- # thus current file name will be always {prefix}{suffix} :
195
- # due super's `prefix + (rotatable ? _date : "") + suffix`
196
- super
197
- ensure
198
- self.rotatable = prev_rotatable
199
- end
200
- end
201
- end
202
168
 
203
- def close
204
- @_close = true
205
- super
206
- @_close = nil
207
- end
208
-
209
- def closeWriter
210
- date = _date
211
- super # sets `date = null`
212
- # the additional trick here is to rotate the closed file
213
- synchronized do
214
- # we're normally in the lock here (from #publish)
215
- # thus we do not perform any more synchronization
216
- dir = java.io.File.new(directory).getAbsoluteFile
217
- log = java.io.File.new(dir, prefix + "" + suffix)
218
- if log.exists
219
- if ! date || date.empty?
220
- date = log.lastModified
221
- # we abuse Timestamp to get a date formatted !
222
- # just like super does internally (just in case)
223
- date = java.sql.Timestamp.new(date).toString[0, 10]
224
- end
225
- today = java.lang.System.currentTimeMillis
226
- today = java.sql.Timestamp.new(today).toString[0, 10]
227
- return if date == today # no need to rotate just yet
228
- to_file = java.io.File.new(dir, prefix + date + suffix)
229
- if to_file.exists
230
- file = java.io.RandomAccessFile.new(to_file, 'rw')
231
- file.seek(file.length)
232
- log_channel = java.io.FileInputStream.new(log).getChannel
233
- log_channel.transferTo(0, log_channel.size, file.getChannel)
234
- file.close
235
- log_channel.close
236
- log.delete
237
- else
238
- log.renameTo(to_file)
239
- end
240
- end
241
- end if rotatable && ! @_close
242
- end
169
+ FileHandler = Java::RbTrinidadLogging::FileHandler
243
170
 
244
- end
245
- end
246
-
247
171
  # We're truly missing a #formatThrown exception helper method.
248
- JUL::Formatter.class_eval do
249
-
172
+ JUL::Formatter.class_eval do # :nodoc:
173
+
250
174
  LINE_SEP = java.lang.System.getProperty("line.separator")
251
-
175
+
252
176
  protected
253
177
  def formatThrown(record)
254
178
  if record.thrown
@@ -260,12 +184,12 @@ module Trinidad
260
184
  return writer.toString
261
185
  end
262
186
  end
263
-
187
+
264
188
  end
265
-
189
+
266
190
  # A message formatter only prints the log message (and the thrown value).
267
- class MessageFormatter < JUL::Formatter # :nodoc
268
-
191
+ class MessageFormatter < JUL::Formatter # :nodoc:
192
+
269
193
  def format(record)
270
194
  msg = formatMessage(record)
271
195
  msg << formatThrown(record).to_s
@@ -277,64 +201,20 @@ module Trinidad
277
201
  msg << LINE_SEP
278
202
  end
279
203
  end
280
-
204
+
281
205
  # e.g. org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/foo]
282
206
  # or org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[default]
283
207
  WEB_APP_LOGGER_NAME = /^org\.apache\.catalina\.core\.ContainerBase.*?\[(.*?)\]$/
284
-
208
+
285
209
  private
286
210
  def context_name(name)
287
211
  ( match = (name || '').match(WEB_APP_LOGGER_NAME) ) && match[1]
288
212
  end
289
-
290
- end
291
-
292
- if ( Java::JavaClass.for_name('rb.trinidad.logging.DefaultFormatter') rescue nil )
293
- DefaultFormatter = Java::RbTrinidadLogging::DefaultFormatter # recent trinidad_jars
294
- else
295
- # A formatter that formats application file logs (e.g. production.log).
296
- class DefaultFormatter < JUL::Formatter # :nodoc
297
-
298
- # Allows customizing the date format + the time zone to be used.
299
- def initialize(format = nil, time_zone = nil)
300
- super()
301
- @format = format ?
302
- Java::JavaText::SimpleDateFormat.new(format) :
303
- Java::JavaText::SimpleDateFormat.new
304
- case time_zone
305
- when Java::JavaUtil::Calendar then
306
- @format.calendar = time_zone
307
- when Java::JavaUtil::TimeZone then
308
- @format.time_zone = time_zone
309
- when String then
310
- time_zone = Java::JavaUtil::TimeZone.getTimeZone(time_zone)
311
- @format.time_zone = time_zone
312
- when Numeric then
313
- time_zones = Java::JavaUtil::TimeZone.getAvailableIDs(time_zone)
314
- if time_zones.length > 0
315
- time_zone = Java::JavaUtil::TimeZone.getTimeZone(time_zones[0])
316
- @format.time_zone = time_zone
317
- end
318
- end if time_zone
319
- end
320
213
 
321
- JDate = Java::JavaUtil::Date
322
-
323
- def format(record)
324
- timestamp = @format.synchronized do
325
- @format.format JDate.new(record.millis)
326
- end
327
- level = record.level.name
328
- message = formatMessage(record)
214
+ end
329
215
 
330
- out = "#{timestamp} #{level}: #{message}"
331
- out << formatThrown(record).to_s
332
- (lns = "\n") == out[-1, 1] ? out : out << lns
333
- end
216
+ DefaultFormatter = Java::RbTrinidadLogging::DefaultFormatter
334
217
 
335
- end
336
- end
337
-
338
218
  end
339
219
  LogFormatter = Logging::DefaultFormatter # backwards compatibility
340
220
  end
@@ -20,7 +20,7 @@ module Trinidad
20
20
  @hosts ||= @config[:hosts]
21
21
  end
22
22
  attr_writer :hosts
23
-
23
+
24
24
  def app_base
25
25
  @app_base ||= @config[:app_base] || @config[:apps_base]
26
26
  end
@@ -30,16 +30,16 @@ module Trinidad
30
30
  @web_apps ||= @config[:web_apps] || @config[:webapps]
31
31
  end
32
32
  attr_writer :web_apps
33
-
33
+
34
34
  def trap?
35
- @trap ||= @config[:trap] if ! defined?(@trap) || @trap.nil?
35
+ @trap = !! @config[:trap] if ! defined?(@trap) || @trap.nil?
36
36
  @trap
37
37
  end
38
38
  attr_writer :trap
39
39
 
40
40
  def ssl_enabled?
41
41
  if ! defined?(@ssl_enabled) || @ssl_enabled.nil?
42
- @ssl_enabled ||= ( !! @config[:ssl] && ! @config[:ssl].empty? )
42
+ @ssl_enabled = ( !! @config[:ssl] && ! @config[:ssl].empty? )
43
43
  end
44
44
  @ssl_enabled
45
45
  end
@@ -47,7 +47,8 @@ module Trinidad
47
47
 
48
48
  def ajp_enabled?
49
49
  if ! defined?(@ajp_enabled) || @ajp_enabled.nil?
50
- @ajp_enabled ||= ( !! @config[:ajp] && ! @config[:ajp].empty? )
50
+ ajp = @config[:ajp]
51
+ @ajp_enabled = ( !! ajp && ( ! ajp.respond_to?(:empty?) || ! ajp.empty? ) )
51
52
  end
52
53
  @ajp_enabled
53
54
  end
@@ -55,30 +56,40 @@ module Trinidad
55
56
 
56
57
  def http_configured?
57
58
  if ! defined?(@http_configured) || @http_configured.nil?
58
- @http_configured ||=
59
- ( ( !! @config[:http] && ! @config[:http].empty? ) || @config[:address] != 'localhost' )
59
+ http = @config[:http]
60
+ @http_configured = ( !! http && ( ! http.respond_to?(:empty?) || ! http.empty? ) )
60
61
  end
61
62
  @http_configured
62
63
  end
63
64
  attr_writer :http_configured
64
-
65
+
65
66
  def tomcat; @tomcat ||= initialize_tomcat; end
66
67
 
68
+ LOCALHOST = 'localhost'.freeze # :nodoc:
69
+
67
70
  def initialize_tomcat
68
71
  set_system_properties
69
72
 
70
73
  tomcat = Trinidad::Tomcat::Tomcat.new
71
74
  tomcat.base_dir = config[:base_dir] || Dir.pwd
72
- tomcat.hostname = config[:address] || 'localhost'
75
+ tomcat.hostname = config[:address] || LOCALHOST
73
76
  tomcat.server.address = config[:address]
74
77
  tomcat.port = config[:port].to_i
75
78
  default_host(tomcat)
76
79
  create_hosts(tomcat)
77
80
  tomcat.enable_naming
78
81
 
79
- add_http_connector(tomcat) if http_configured?
80
- add_ssl_connector(tomcat) if ssl_enabled?
81
- add_ajp_connector(tomcat) if ajp_enabled?
82
+ http_connector = http_configured? ||
83
+ ( ! ajp_enabled? && config[:address] && config[:address] != LOCALHOST )
84
+
85
+ if http_connector
86
+ tomcat.connector = add_http_connector(tomcat)
87
+ end
88
+ if ajp_enabled?
89
+ connector = add_ajp_connector(tomcat)
90
+ tomcat.connector = connector unless http_connector
91
+ end
92
+ add_ssl_connector(tomcat) if ssl_enabled?
82
93
 
83
94
  Trinidad::Extensions.configure_server_extensions(config[:extensions], tomcat)
84
95
  end
@@ -97,52 +108,60 @@ module Trinidad
97
108
  def load_host_monitor(web_apps); add_host_monitor(web_apps); end
98
109
 
99
110
  def add_ajp_connector(tomcat = @tomcat)
100
- add_service_connector(@config[:ajp], 'AJP/1.3', tomcat)
111
+ options = config[:ajp]
112
+ options = {
113
+ :address => @config[:address], :port => @config[:port]
114
+ }.merge!( options.respond_to?(:[]) ? options : {} )
115
+
116
+ add_service_connector(options, options[:protocol_handler] || 'AJP/1.3', tomcat)
101
117
  end
102
118
 
103
119
  def add_http_connector(tomcat = @tomcat)
104
- options = config[:http] || {}
105
- options[:address] ||= @config[:address] if @config[:address] != 'localhost'
106
- options[:port] = @config[:port]
107
- options[:protocol_handler] = 'org.apache.coyote.http11.Http11NioProtocol' if options[:nio]
120
+ options = config[:http]
121
+ options = {
122
+ :address => @config[:address], :port => @config[:port]
123
+ }.merge!( options.respond_to?(:[]) ? options : {} )
124
+
125
+ if options.delete(:nio)
126
+ options[:protocol_handler] ||= 'org.apache.coyote.http11.Http11NioProtocol'
127
+ end
108
128
 
109
- if options[:apr]
129
+ if options.delete(:apr)
110
130
  tomcat.server.add_lifecycle_listener(Trinidad::Tomcat::AprLifecycleListener.new)
111
131
  end
112
132
 
113
- connector = add_service_connector(options, options[:protocol_handler] || 'HTTP/1.1', tomcat)
114
- tomcat.connector = connector
133
+ add_service_connector(options, options[:protocol_handler] || 'HTTP/1.1', tomcat)
115
134
  end
116
-
135
+
117
136
  def add_ssl_connector(tomcat = @tomcat)
118
- options = config[:ssl].merge({
119
- :scheme => 'https',
120
- :secure => true,
121
- :SSLEnabled => 'true'
122
- })
137
+ options = config[:ssl]
138
+ options = {
139
+ :scheme => 'https', :secure => true, :SSLEnabled => 'true'
140
+ }.merge!( options.respond_to?(:[]) ? options : {} )
123
141
 
124
142
  options[:keystoreFile] ||= options.delete(:keystore)
125
143
 
126
144
  if ! options[:keystoreFile] && ! options[:SSLCertificateFile]
127
- options[:keystoreFile] = 'ssl/keystore'
128
- options[:keystorePass] = 'waduswadus42'
145
+ options[:keystoreFile] ||= 'ssl/keystore'
146
+ options[:keystorePass] ||= 'waduswadus42'
129
147
  generate_default_keystore(options)
130
148
  end
131
149
 
132
150
  add_service_connector(options, nil, tomcat)
133
151
  end
134
-
135
- def add_service_connector(options, protocol = nil, tomcat = @tomcat)
136
- opts = options.dup
137
152
 
153
+ # NOTE: make sure to pass an options Hash that might be changed !
154
+ def add_service_connector(options, protocol = nil, tomcat = @tomcat)
138
155
  connector = Trinidad::Tomcat::Connector.new(protocol)
139
- connector.scheme = opts.delete(:scheme) if opts[:scheme]
140
- connector.secure = opts.delete(:secure) || false
141
- connector.port = opts.delete(:port).to_i
156
+ connector.scheme = options.delete(:scheme) if options[:scheme]
157
+ connector.secure = options.delete(:secure) || false
158
+ connector.port = options.delete(:port).to_i if options[:port]
142
159
 
143
- connector.protocol_handler_class_name = opts.delete(:protocol_handler) if opts[:protocol_handler]
160
+ if handler = options.delete(:protocol_handler)
161
+ connector.protocol_handler_class_name = handler
162
+ end
144
163
 
145
- opts.each { |key, value| connector.setProperty(key.to_s, value.to_s) }
164
+ options.each { |key, value| connector.setProperty(key.to_s, value.to_s) }
146
165
 
147
166
  tomcat.service.add_connector(connector)
148
167
  connector
@@ -150,7 +169,7 @@ module Trinidad
150
169
  private :add_service_connector
151
170
 
152
171
  def add_web_app(web_app, host = nil, start = nil)
153
- host ||= begin
172
+ host ||= begin
154
173
  name = web_app.host_name
155
174
  name ? find_host(name, tomcat) : tomcat.host
156
175
  end
@@ -176,7 +195,7 @@ module Trinidad
176
195
  end
177
196
  context
178
197
  end
179
-
198
+
180
199
  def deploy_web_apps(tomcat = self.tomcat)
181
200
  add_host_monitor web_apps = create_web_apps
182
201
  web_apps
@@ -207,7 +226,7 @@ module Trinidad
207
226
  def stop!
208
227
  (@tomcat.destroy; true) if stop
209
228
  end
210
-
229
+
211
230
  protected
212
231
 
213
232
  def create_web_apps
@@ -254,13 +273,13 @@ module Trinidad
254
273
  if File.directory?(app_root) || ( app_root[-4..-1] == '.war' )
255
274
  app_base_name = File.basename(app_root)
256
275
  deployed = apps.find do |app_holder|; web_app = app_holder.web_app
257
- web_app.root_dir == app_root ||
276
+ web_app.root_dir == app_root ||
258
277
  web_app.context_path == Trinidad::Tomcat::ContextName.new(app_base_name).path
259
278
  end
260
279
  if deployed
261
280
  logger.debug "Skipping auto-deploy from #{app_root} (already deployed)"
262
281
  else
263
- apps << ( app_holder = create_web_app({
282
+ apps << ( app_holder = create_web_app({
264
283
  :context_name => path, :root_dir => app_root, :host_name => host.name
265
284
  }) ); app = app_holder.web_app
266
285
  logger.info "Auto-Deploying from #{app.root_dir} as #{app.context_path}"
@@ -293,7 +312,7 @@ module Trinidad
293
312
 
294
313
  default_host = tomcat.host
295
314
  default_app_base = ( default_host.app_base == DEFAULT_HOST_APP_BASE )
296
- if self.app_base ||
315
+ if self.app_base ||
297
316
  ( default_app_base && ! File.exists?(DEFAULT_HOST_APP_BASE) )
298
317
  tomcat.host.app_base = self.app_base || Dir.pwd
299
318
  end
@@ -328,7 +347,7 @@ module Trinidad
328
347
  tomcat.engine.add_child host if tomcat
329
348
  host
330
349
  end
331
-
350
+
332
351
  def setup_host(app_base, host_config, host)
333
352
  if host_config.is_a?(Array)
334
353
  name = host_config.shift
@@ -346,9 +365,9 @@ module Trinidad
346
365
  host.app_base = value if default_host_base?(host)
347
366
  when :aliases
348
367
  aliases = host.find_aliases || []
349
- value.each do |name|
350
- next if (name = name.to_s) == host.name
351
- host.add_alias(name) unless aliases.include?(name)
368
+ value.each do |aliaz|
369
+ next if (aliaz = aliaz.to_s) == host.name
370
+ host.add_alias(aliaz) unless aliases.include?(aliaz)
352
371
  end if host_config[:aliases]
353
372
  else
354
373
  value = value.to_s if value.is_a?(Symbol)
@@ -362,7 +381,7 @@ module Trinidad
362
381
  end
363
382
  # @deprecated renamed to {#set_system_properties}
364
383
  def load_default_system_properties; set_system_properties; end
365
-
384
+
366
385
  def configure_logging(log_level)
367
386
  Trinidad::Logging.configure(log_level)
368
387
  end
@@ -379,7 +398,11 @@ module Trinidad
379
398
  host = tomcat.host # make sure we initialize default host
380
399
  host.deployXML = false
381
400
  host_config = @config[:host] || ( @config[:hosts] && @config[:hosts][:default] )
382
- host_config.each { |name, value| host.send("#{name}=", value) } if host_config
401
+ if host_config.is_a?(String)
402
+ host.name = host_config
403
+ elsif host_config
404
+ host_config.each { |name, value| host.send("#{name}=", value) }
405
+ end
383
406
  host
384
407
  end
385
408
 
@@ -403,7 +426,9 @@ module Trinidad
403
426
  base_parent = false
404
427
  2.times do
405
428
  begin
406
- break if base_parent = ( app_real_path.index(base_path.realpath.to_s) == 0 )
429
+ if app_real_path.index(base_path.realpath.to_s) == 0
430
+ base_parent = true; break
431
+ end
407
432
  rescue => e
408
433
  logger.warn "Host #{host.name.inspect} app_base does not exist," <<
409
434
  " try configuring an absolute path or create it\n (#{e.message})"
@@ -480,7 +505,7 @@ module Trinidad
480
505
  path
481
506
  end
482
507
  end
483
-
508
+
484
509
  def generate_default_keystore(config)
485
510
  keystore_file = java.io.File.new(config[:keystoreFile])
486
511
 
@@ -501,11 +526,11 @@ module Trinidad
501
526
  key_tool = Java::SunSecurityTools::KeyTool
502
527
  key_tool.main key_tool_args.to_java(:string)
503
528
  end
504
-
529
+
505
530
  def trap_signals
506
531
  trap('INT') { stop! }
507
532
  trap('TERM') { stop! }
508
533
  end
509
-
534
+
510
535
  end
511
536
  end