lib/trinidad/server.rb in trinidad-0.9.12 vs lib/trinidad/server.rb in trinidad-1.0.0
- old
+ new
@@ -29,14 +29,14 @@
add_default_web_app!(@config)
end
def load_tomcat_server
@tomcat = Trinidad::Tomcat::Tomcat.new
+ @tomcat.base_dir = Dir.pwd
@tomcat.hostname = @config[:address]
@tomcat.server.address = @config[:address]
@tomcat.port = @config[:port].to_i
- @tomcat.base_dir = Dir.pwd
@tomcat.host.app_base = @config[:apps_base] || Dir.pwd
@tomcat.enable_naming
add_http_connector if http_configured?
add_ssl_connector if ssl_enabled?
@@ -44,23 +44,34 @@
@tomcat = Trinidad::Extensions.configure_server_extensions(@config[:extensions], @tomcat)
end
def create_web_apps
+ create_from_web_apps
+ create_from_apps_base
+ end
+
+ def create_from_web_apps
if @config[:web_apps]
@config[:web_apps].each do |name, app_config|
app_config[:context_path] ||= (name.to_s == 'default' ? '/' : "/#{name.to_s}")
app_config[:web_app_dir] ||= Dir.pwd
create_web_app(app_config)
end
end
+ end
+
+ def create_from_apps_base
if @config[:apps_base]
- apps_path = Dir.glob(File.join(@config[:apps_base], '*')).select {|path| !(path =~ /tomcat\.8080$/) }
+ apps_path = Dir.glob(File.join(@config[:apps_base], '*')).
+ select {|path| !(path =~ /tomcat\.\d+$/) }
+ apps_path.reject! {|path| apps_path.include?(path + '.war') }
+
apps_path.each do |path|
- if File.directory?(path)
+ if (File.directory?(path) || path =~ /\.war$/)
name = File.basename(path)
app_config = {
:context_path => (name == 'default' ? '/' : "/#{name.to_s}"),
:web_app_dir => File.expand_path(path)
}
@@ -70,23 +81,23 @@
end
end
end
def create_web_app(app_config)
- app_context = @tomcat.addWebapp(app_config[:context_path], app_config[:web_app_dir])
- remove_defaults(app_context)
+ web_app = WebApp.create(@config, app_config)
- web_app = WebApp.create(@config, app_config)
- configure_logging(web_app)
+ app_context = @tomcat.addWebapp(web_app.context_path, web_app.web_app_dir)
+ app_context.work_dir = web_app.work_dir
- Trinidad::Extensions.configure_webapp_extensions(web_app.extensions, @tomcat, app_context)
- app_context.add_lifecycle_listener(WebAppLifecycleListener.new(web_app))
+ Trinidad::Extensions.configure_webapp_extensions(web_app.extensions, @tomcat, app_context)
+
+ lifecycle = web_app.war? ? Lifecycle::War.new(web_app) : Lifecycle::Default.new(web_app)
+ app_context.add_lifecycle_listener(lifecycle)
end
def add_service_connector(options, protocol = nil)
connector = Trinidad::Tomcat::Connector.new(protocol)
-
opts = options.dup
connector.scheme = opts.delete(:scheme) if opts[:scheme]
connector.secure = opts.delete(:secure) || false
connector.port = opts.delete(:port).to_i
@@ -122,15 +133,20 @@
add_service_connector(options)
end
def add_http_connector
- options = @config[:http]
+ options = @config[:http] || {}
+ options[:address] ||= @config[:address] if @config[:address] != 'localhost'
options[:port] = @config[:port]
options[:protocol_handler] = 'org.apache.coyote.http11.Http11NioProtocol' if options[:nio]
- connector = add_service_connector(options, options[:protocol_handler])
+ if options[:apr]
+ @tomcat.server.add_lifecycle_listener(Trinidad::Tomcat::AprLifecycleListener.new)
+ end
+
+ connector = add_service_connector(options, options[:protocol_handler] || 'HTTP/1.1')
@tomcat.connector = connector
end
def ssl_enabled?
@config.has_key?(:ssl)
@@ -139,11 +155,11 @@
def ajp_enabled?
@config.has_key?(:ajp)
end
def http_configured?
- @config.has_key?(:http)
+ @config.has_key?(:http) || @config[:address] != 'localhost'
end
def create_default_keystore(config)
keystore_file = java.io.File.new(config[:keystoreFile])
@@ -181,54 +197,8 @@
}
default_app[:rackup] = config[:rackup] if (config.has_key?(:rackup))
config[:web_apps] = { :default => default_app }
end
- end
-
- def remove_defaults(app_context)
- default_servlet = app_context.find_child('default')
- app_context.remove_child(default_servlet) if default_servlet
-
- jsp_servlet = app_context.find_child('jsp')
- app_context.remove_child(jsp_servlet) if jsp_servlet
-
- app_context.remove_servlet_mapping('/')
- app_context.remove_servlet_mapping('*.jspx')
- app_context.remove_servlet_mapping('*.jsp')
-
- app_context.process_tlds = false
- end
-
- def configure_logging(web_app)
- log_path = File.join(web_app.web_app_dir, 'log', "#{web_app.environment}.log")
- log_file = java.io.File.new(log_path)
-
- unless log_file.exists
- log_file.parent_file.mkdirs
- log_file.create_new_file
- end
-
- jlogging = java.util.logging
-
- log_handler = jlogging.FileHandler.new(log_path, true)
- logger = jlogging.Logger.get_logger("")
-
- log_level = web_app.log
- unless %w{ALL CONFIG FINE FINER FINEST INFO OFF SEVERE WARNING}.include?(log_level)
- puts "Invalid log level #{log_level}, using default: INFO"
- log_level = 'INFO'
- end
-
- level = jlogging.Level.parse(log_level)
-
- logger.handlers.each do |handler|
- handler.level = level
- end
-
- logger.level = level
-
- log_handler.formatter = jlogging.SimpleFormatter.new
- logger.add_handler(log_handler)
end
end
end