trinidad 1.4.6 → 1.5.0.B1

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,10 +6,11 @@ module Trinidad
6
6
 
7
7
  include Trinidad::Tomcat::LifecycleListener
8
8
 
9
- EVENTS = Trinidad::Tomcat::Lifecycle # :nodoc:
9
+ # @private
10
+ EVENTS = Trinidad::Tomcat::Lifecycle
10
11
 
11
12
  # The base implementation simply routes events to correspondig methods.
12
- #
13
+ #
13
14
  # http://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/Lifecycle.html
14
15
  # http://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/LifecycleListener.html
15
16
  def lifecycleEvent(event)
@@ -44,27 +45,27 @@ module Trinidad
44
45
  raise "unsupported event.type = #{event.type}"
45
46
  end
46
47
  end
47
-
48
+
48
49
  # Event hook methods for a more Ruby-ish API :
49
-
50
+
50
51
  def before_init(event); end
51
52
  def after_init(event); end
52
-
53
+
53
54
  def before_start(event); end
54
55
  def configure_start(event); end
55
56
  def start(event); end
56
57
  def after_start(event); end
57
-
58
+
58
59
  def before_stop(event); end
59
60
  def stop(event); end
60
61
  def configure_stop(event); end
61
62
  def after_stop(event); end
62
-
63
+
63
64
  def before_destroy(event); end
64
65
  def after_destroy(event); end
65
-
66
+
66
67
  def periodic(event); end
67
-
68
+
68
69
  end
69
70
  end
70
71
  end
@@ -2,17 +2,15 @@ require 'trinidad/lifecycle/base'
2
2
 
3
3
  module Trinidad
4
4
  module Lifecycle
5
- # Host listener - monitors deployed applications
5
+ # Host listener - monitors deployed applications
6
6
  # (re-invented HostConfig with Ruby/Rack semantics).
7
7
  class Host # TODO < Tomcat::HostConfig !
8
8
 
9
9
  include Trinidad::Tomcat::LifecycleListener
10
10
 
11
- EVENTS = Trinidad::Tomcat::Lifecycle # :nodoc:
12
-
11
+ EVENTS = Trinidad::Tomcat::Lifecycle
12
+
13
13
  attr_reader :server, :app_holders
14
- # @deprecated (<= 1.3.5)
15
- alias_method :contexts, :app_holders
16
14
 
17
15
  # #server current server instance
18
16
  # #app_holders deployed web application holders
@@ -27,7 +25,9 @@ module Trinidad
27
25
  @server, @app_holders = server, app_holders
28
26
  end
29
27
 
30
- def lifecycleEvent(event) # :nodoc:
28
+ def contexts; @app_holders.map(&:context) end
29
+
30
+ def lifecycleEvent(event)
31
31
  case event.type
32
32
  when EVENTS::BEFORE_START_EVENT then
33
33
  before_start(event)
@@ -44,26 +44,23 @@ module Trinidad
44
44
  init_monitors
45
45
  end
46
46
 
47
- def start(event); end # :nodoc:
47
+ def start(event); end
48
48
 
49
49
  def periodic(event)
50
50
  check_changes event.lifecycle
51
51
  end
52
52
 
53
- def stop(event); end # :nodoc:
53
+ def stop(event); end
54
54
 
55
- def tomcat; @server.tomcat; end # :nodoc: for backwards compatibility
56
-
57
55
  protected
58
-
56
+
59
57
  def check_changes(host)
60
58
  check_monitors
61
59
  end
62
60
 
63
61
  def init_monitors
64
- app_holders.each do |app_holder|
65
- monitor = app_holder.monitor
66
- opts = 'w+'
62
+ for app_holder in app_holders
63
+ monitor = app_holder.monitor; opts = 'w+'
67
64
  if ! File.exist?(dir = File.dirname(monitor))
68
65
  Dir.mkdir dir
69
66
  elsif File.exist?(monitor)
@@ -76,13 +73,13 @@ module Trinidad
76
73
  end
77
74
 
78
75
  def check_monitors
79
- app_holders.each do |app_holder|
76
+ for app_holder in app_holders
80
77
  # double check monitor, capistrano removes it temporarily
81
78
  unless File.exist?(monitor = app_holder.monitor)
82
79
  sleep(0.5)
83
80
  next unless File.exist?(monitor)
84
81
  end
85
-
82
+
86
83
  mtime = File.mtime(monitor)
87
84
  if mtime > app_holder.monitor_mtime && app_holder.try_lock
88
85
  app_holder.monitor_mtime = mtime
@@ -93,13 +90,13 @@ module Trinidad
93
90
 
94
91
  autoload :RestartReload, 'trinidad/lifecycle/host/restart_reload'
95
92
  autoload :RollingReload, 'trinidad/lifecycle/host/rolling_reload'
96
-
93
+
97
94
  RELOAD_STRATEGIES = {
98
95
  :default => :RestartReload,
99
96
  :restart => :RestartReload,
100
97
  :rolling => :RollingReload,
101
98
  }
102
-
99
+
103
100
  def reload_application!(app_holder)
104
101
  strategy = (app_holder.web_app.reload_strategy || :default).to_sym
105
102
  strategy = RELOAD_STRATEGIES[ strategy ]
@@ -108,7 +105,7 @@ module Trinidad
108
105
  new_args << server if strategy.instance_method(:initialize).arity != 0
109
106
  strategy.new(*new_args).reload!(app_holder)
110
107
  end
111
-
108
+
112
109
  end
113
110
  end
114
111
  end
@@ -9,52 +9,64 @@ module Trinidad
9
9
  @server = server
10
10
  end
11
11
 
12
- def reload!(app_holder)
12
+ def reload!(app_holder, wait = false)
13
13
  web_app, old_context = app_holder.web_app, app_holder.context
14
+
15
+ millis = java.lang.System.currentTimeMillis
16
+ context_name = old_context.name.split('-')
17
+ context_name.pop if context_name.last.to_i.to_s.size == millis.to_s.size
18
+ context_name = context_name.join('-')
19
+
14
20
  logger = self.class.logger
15
- logger.info "Context with name [#{old_context.name}] has started rolling"
21
+ logger.info "Context with name [#{context_name}] has started rolling"
16
22
 
17
23
  web_app.reset! # force a new class loader + re-read state (from config)
18
24
  no_host = org.apache.catalina.Host.impl {} # do not add to parent yet
19
25
  new_context = @server.add_web_app(web_app, no_host, false)
20
26
  # Tomcat requires us to have unique names for its containers :
21
- new_context.name = "#{old_context.name}-#{java.lang.System.currentTimeMillis}"
27
+ new_context.name = "#{context_name}-#{millis}"
22
28
  new_context.add_lifecycle_listener(takeover = Takeover.new(old_context))
23
29
  app_holder.context = new_context
24
30
 
25
- Thread.new do
31
+ thread = Thread.new do
32
+ if ( thread = thread.to_java ).respond_to?(:native_thread)
33
+ thread.native_thread.name = "Trinidad::Lifecycle::Host::RollingReload #{context_name}"
34
+ end
26
35
  begin
27
36
  logger.debug "Starting a new Context for [#{new_context.path}]"
28
37
  old_context.parent.add_child new_context # NOTE: likely starts!
29
-
38
+
30
39
  new_context.start unless new_context.state_name =~ /START|STOP|FAILED/i
31
-
40
+
32
41
  if new_context.state_name =~ /STOP|FAILED/i
33
- logger.error("Context with name [#{old_context.name}] failed rolling")
42
+ logger.error("Context with name [#{context_name}] failed rolling")
34
43
  takeover.failed!(new_context)
35
44
  else
36
- logger.info "Context with name [#{old_context.name}] has completed rolling"
45
+ logger.info "Context with name [#{context_name}] has completed rolling"
37
46
  end
38
- rescue => error
39
- e = org.jruby.exceptions.RaiseException.new(error)
40
- logger.error("Context with name [#{old_context.name}] failed rolling", e)
41
- takeover.failed!(new_context)
42
47
  rescue java.lang.Exception => e
43
- logger.error("Context with name [#{old_context.name}] failed rolling", e)
48
+ logger.error("Context with name [#{context_name}] failed rolling", e)
49
+ takeover.failed!(new_context)
50
+ rescue => error
51
+ e = org.jruby.exceptions.RaiseException.new(error, false)
52
+ logger.error("Context with name [#{context_name}] failed rolling", e)
44
53
  takeover.failed!(new_context)
45
54
  ensure
46
55
  app_holder.unlock
47
56
  end
48
57
  end
58
+ thread.join if wait
49
59
  false # not yet reloaded do not release lock
50
60
  end
51
61
 
62
+ # @private
52
63
  def self.logger # log into the same location as context.reload does :
53
64
  Trinidad::Logging::LogFactory.getLog('org.apache.catalina.core.StandardContext')
54
65
  end
55
-
56
- class Takeover < Trinidad::Lifecycle::Base # :nodoc
57
-
66
+
67
+ # @private
68
+ class Takeover < Trinidad::Lifecycle::Base
69
+
58
70
  def initialize(context)
59
71
  @old_context = context
60
72
  end
@@ -76,19 +88,19 @@ module Trinidad
76
88
  def failed!(new_context)
77
89
  # NOTE: this will also likely destroy() the child - new context :
78
90
  @old_context.parent.remove_child new_context
79
- logger.info "Failed to start new Context for [#{@old_context.path}] " +
91
+ logger.info "Failed to start new Context for [#{@old_context.path}] " <<
80
92
  "(check application logs) keeping the old one running ..."
81
93
  new_context.remove_lifecycle_listener(self)
82
94
  end
83
-
95
+
84
96
  private
85
-
97
+
86
98
  def logger
87
99
  Trinidad::Lifecycle::Host::RollingReload.logger
88
100
  end
89
-
101
+
90
102
  end
91
-
103
+
92
104
  end
93
105
  end
94
106
  end
@@ -6,7 +6,7 @@ module Trinidad
6
6
  module WebApp
7
7
  class Default < Lifecycle::Base
8
8
  include Shared
9
-
9
+
10
10
  def configure(context)
11
11
  super
12
12
  deployment_descriptor = configure_deployment_descriptor(context)
@@ -14,20 +14,26 @@ module Trinidad
14
14
  configure_rack_servlet(context)
15
15
  configure_rack_listener(context)
16
16
  end
17
- configure_context_params(context)
18
17
  configure_context_loader(context)
18
+ configure_context_params(context)
19
19
  end
20
-
20
+
21
21
  def before_init(event)
22
22
  super
23
23
  set_context_xml event.lifecycle
24
24
  # AFTER_INIT_EVENT ContextConfig#init() will pick this up
25
25
  end
26
-
26
+
27
+ def before_start(event)
28
+ super
29
+ # on CONFIGURE_START context.jar_scanner is used
30
+ set_jar_scanner event.lifecycle
31
+ end
32
+
27
33
  protected
28
-
34
+
29
35
  @@_add_context_config = true # due backward compatibility
30
-
36
+
31
37
  def configure_deployment_descriptor(context)
32
38
  descriptor = web_app.deployment_descriptor
33
39
  if descriptor && File.exist?(descriptor)
@@ -39,7 +45,7 @@ module Trinidad
39
45
  if context_config.nil?
40
46
  if @@_add_context_config
41
47
  context_config = Trinidad::Tomcat::ContextConfig.new
42
- context.addLifecycleListener(context_config)
48
+ context.add_lifecycle_listener(context_config)
43
49
  else
44
50
  raise "initialized context is missing a ContextConfig listener"
45
51
  end
@@ -55,6 +61,7 @@ module Trinidad
55
61
  rack_servlet = web_app.rack_servlet
56
62
  if rack_servlet[:instance]
57
63
  wrapper.servlet = rack_servlet[:instance]
64
+ web_app[:add_jruby_rack_jar] = false
58
65
  else
59
66
  wrapper.servlet_class = rack_servlet[:class]
60
67
  wrapper.async_supported = rack_servlet[:async_supported]
@@ -69,7 +76,9 @@ module Trinidad
69
76
 
70
77
  def configure_rack_listener(context)
71
78
  unless web_app.rack_servlet[:instance]
72
- context.add_application_listener(web_app.rack_listener)
79
+ if rack_listener = web_app.rack_listener
80
+ context.add_application_listener(rack_listener)
81
+ end
73
82
  end
74
83
  end
75
84
 
@@ -82,28 +91,41 @@ module Trinidad
82
91
  alias_method :configure_init_params, :configure_context_params
83
92
 
84
93
  def configure_context_loader(context)
85
- class_loader = web_app.class_loader
86
-
87
- add_application_java_classes(class_loader)
88
- add_application_jars(class_loader) # classes takes precedence !
94
+ loader = new_context_loader
95
+ add_jruby_rack_jar(loader)
96
+ add_application_java_classes(loader)
97
+ add_application_jars(loader) # classes takes precedence !
89
98
 
90
- loader = Trinidad::Tomcat::WebappLoader.new(class_loader)
91
99
  context.loader = loader # does loader.container = context
92
100
  end
93
101
 
94
- def add_application_jars(class_loader)
102
+ def add_jruby_rack_jar(loader)
103
+ return if web_app[:add_jruby_rack_jar] == false
104
+ if jruby_rack_jar = JRUBY_RACK_JAR_PATH
105
+ logger.debug "[#{web_app.context_path}] adding jar: #{jruby_rack_jar}"
106
+ loader.addRepository to_url_path(jruby_rack_jar)
107
+ end
108
+ end
109
+
110
+ def add_application_jars(loader)
95
111
  return unless lib_dir = web_app.java_lib_dir
112
+ # loader.setJarPath(lib_dir) no point since startInternal re-sets it
96
113
  Dir[ File.join(lib_dir, "**/*.jar") ].each do |jar|
97
114
  logger.debug "[#{web_app.context_path}] adding jar: #{jar}"
98
- class_loader.addURL java.io.File.new(jar).to_url
115
+ loader.addRepository to_url_path(jar)
99
116
  end
100
117
  end
101
118
 
102
- def add_application_java_classes(class_loader)
119
+ def add_application_java_classes(loader)
103
120
  return unless classes_dir = web_app.java_classes_dir
104
- class_loader.addURL java.io.File.new(classes_dir).to_url
121
+ logger.debug "[#{web_app.context_path}] adding dir: #{classes_dir}"
122
+ loader.addRepository to_url_path(classes_dir)
105
123
  end
106
-
124
+
125
+ def set_jar_scanner(context)
126
+ context.setJarScanner Java::RbTrinidadContext::DefaultJarScanner.new(context)
127
+ end
128
+
107
129
  def set_context_xml(context)
108
130
  # behave similar to a .war - checking /META-INF/context.xml on CP
109
131
  context_xml = web_app.context_xml
@@ -120,7 +142,16 @@ module Trinidad
120
142
  context.setDefaultContextXml(context_xml)
121
143
  end
122
144
  end
123
-
145
+
146
+ private
147
+
148
+ def new_context_loader
149
+ class_loader = JRuby.runtime.jruby_class_loader
150
+ Java::RbTrinidadContext::DefaultLoader.new(class_loader)
151
+ end
152
+
153
+ def to_url_path(path); Helpers.to_url(path).to_s end
154
+
124
155
  end
125
156
  end
126
157
  Default = Trinidad::Lifecycle::WebApp::Default # backwards compatibility
@@ -1,6 +1,14 @@
1
1
  module Trinidad
2
2
  module Lifecycle
3
3
  module WebApp
4
+
5
+ # @private
6
+ JRUBY_RACK_JAR_PATH = if defined?(JRuby::Rack::JAR_PATH)
7
+ JRuby::Rack::JAR_PATH
8
+ else
9
+ JRubyJars.jruby_rack_jar_path
10
+ end
11
+
4
12
  # Shared web application lifecycle hook,
5
13
  # does #configure before the context starts.
6
14
  module Shared
@@ -18,7 +18,7 @@ module Trinidad
18
18
 
19
19
  def configure(context)
20
20
  super # Shared#configure
21
- configure_class_loader(context)
21
+ # configure_class_loader(context)
22
22
  end
23
23
 
24
24
  protected
@@ -38,7 +38,7 @@ module Trinidad
38
38
  end
39
39
 
40
40
  def configure_class_loader(context)
41
- class_loader = web_app.class_loader || JRuby.runtime.jruby_class_loader
41
+ class_loader = JRuby.runtime.jruby_class_loader
42
42
  loader = Trinidad::Tomcat::WebappLoader.new(class_loader)
43
43
  loader.container = context
44
44
  context.loader = loader
@@ -137,11 +137,19 @@ module Trinidad
137
137
  'org.apache.catalina.core.StandardService',
138
138
  'org.apache.catalina.core.StandardEngine',
139
139
  'org.apache.catalina.startup.ContextConfig',
140
+ #'org.apache.coyote.http11.Http11Protocol',
141
+ 'org.apache.catalina.core.ApplicationContext',
142
+ #'org.apache.catalina.core.AprLifecycleListener',
143
+ 'org.apache.catalina.loader.WebappClassLoader',
144
+ 'org.apache.tomcat.websocket.server.WsSci',
140
145
  ]
141
146
  for name in logger_names
142
147
  logger = JUL::Logger.getLogger(name)
143
148
  set_log_level(logger, level) if logger
144
149
  end
150
+
151
+ #set_log_level(JUL::Logger.getLogger('rb.trinidad.context.DefaultLoader'), JUL::Level::FINEST)
152
+
145
153
  end
146
154
 
147
155
  def self.web_app_context_param(web_app, context, name)
@@ -13,8 +13,6 @@ module Trinidad
13
13
  configure_logging config[:logging] || config[:log]
14
14
  @config = config.freeze
15
15
  end
16
- # @deprecated replaced with {#configure}
17
- def load_config(config); configure(config); end
18
16
 
19
17
  def hosts
20
18
  @hosts ||= @config[:hosts]
@@ -74,10 +72,15 @@ module Trinidad
74
72
 
75
73
  tomcat = Tomcat.new # @see Trinidad::Tomcat
76
74
  tomcat.base_dir = config[:base_dir] || Dir.pwd
77
- address = config[:address] if config.key?(:address)
75
+ if config.key?(:address)
76
+ address = config[:address]; address = '0.0.0.0' if address == '*'
77
+ end
78
78
  tomcat.hostname = address || 'localhost'
79
79
  tomcat.server.address = address || nil unless address.nil?
80
80
  tomcat.port = config[:port].to_i if config.key?(:port)
81
+
82
+ set_tomcat_class_loader(tomcat)
83
+
81
84
  default_host(tomcat)
82
85
  create_hosts(tomcat)
83
86
  tomcat.enable_naming
@@ -109,8 +112,6 @@ module Trinidad
109
112
  Extensions.configure_server_extensions(config[:extensions], tomcat)
110
113
  end
111
114
  protected :initialize_tomcat
112
- # #deprecated renamed to {#initialize_tomcat}
113
- def load_tomcat_server; initialize_tomcat; end
114
115
 
115
116
  def add_host_monitor(app_holders)
116
117
  for host in tomcat.engine.find_children
@@ -119,8 +120,6 @@ module Trinidad
119
120
  end
120
121
  end
121
122
  protected :add_host_monitor
122
- # @deprecated replaced with {#setup_host_monitor}
123
- def load_host_monitor(web_apps); add_host_monitor(web_apps); end
124
123
 
125
124
  def add_ajp_connector(options = config[:ajp], tomcat = nil)
126
125
  # backwards compatibility - single argument (tomcat = @tomcat)
@@ -131,7 +130,7 @@ module Trinidad
131
130
  end if tomcat.nil?
132
131
 
133
132
  options = options.respond_to?(:[]) ? options.dup : {}
134
- options[:address] = config[:address] unless options.key?(:address)
133
+ options[:address] = config[:address] if ! options.key?(:address) && config.key?(:address)
135
134
 
136
135
  add_service_connector(options, 'AJP/1.3', tomcat)
137
136
  end
@@ -146,7 +145,7 @@ module Trinidad
146
145
 
147
146
  options = options.respond_to?(:[]) ? options.dup : {}
148
147
  options[:port] = config[:port] || 3000 unless options.key?(:port)
149
- options[:address] = config[:address] unless options.key?(:address)
148
+ options[:address] = config[:address] if ! options.key?(:address) && config.key?(:address)
150
149
 
151
150
  if options.delete(:nio)
152
151
  options[:protocol_handler] ||= 'org.apache.coyote.http11.Http11NioProtocol'
@@ -171,7 +170,7 @@ module Trinidad
171
170
  end if tomcat.nil?
172
171
 
173
172
  options = { :scheme => 'https', :secure => true }.merge!( options.respond_to?(:[]) ? options : {} )
174
- options[:address] = config[:address] unless options.key?(:address)
173
+ options[:address] = config[:address] if ! options.key?(:address) && config.key?(:address)
175
174
 
176
175
  if keystore_file = options.delete(:keystore) || options.delete(:keystore_file)
177
176
  options[:keystoreFile] ||= keystore_file
@@ -429,8 +428,14 @@ module Trinidad
429
428
  def set_system_properties(system = Java::JavaLang::System)
430
429
  system.set_property("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE", 'true')
431
430
  end
432
- # @deprecated renamed to {#set_system_properties}
433
- def load_default_system_properties; set_system_properties; end
431
+
432
+ def set_tomcat_class_loader(tomcat)
433
+ # NOTE: allows for Java class-resolution to work from within Tomcat :
434
+ tomcat_loader = tomcat.server.getParentClassLoader
435
+ if tomcat_loader.nil? || tomcat_loader == Java::JavaLang::ClassLoader.getSystemClassLoader
436
+ tomcat.server.setParentClassLoader JRuby.runtime.jruby_class_loader
437
+ end
438
+ end
434
439
 
435
440
  def configure_logging(logging)
436
441
  Trinidad::Logging.configure(logging)