lib/trinidad/server.rb in trinidad-1.4.5 vs lib/trinidad/server.rb in trinidad-1.4.6
- old
+ new
@@ -8,11 +8,11 @@
def initialize(config = Trinidad.configuration)
configure(config)
end
def configure(config = Trinidad.configuration)
- configure_logging config[:log]
+ configure_logging config[:logging] || config[:log]
@config = config.freeze
end
# @deprecated replaced with {#configure}
def load_config(config); configure(config); end
@@ -37,15 +37,18 @@
end
attr_writer :trap
def ssl_enabled?
if ! defined?(@ssl_enabled) || @ssl_enabled.nil?
- @ssl_enabled = ( !! @config[:ssl] && ! @config[:ssl].empty? )
+ ssl = @config.key?(:https) ? @config[:https] : @config[:ssl]
+ @ssl_enabled = ( !! ssl && ( ! ssl.respond_to?(:empty?) || ! ssl.empty? ) )
end
@ssl_enabled
end
attr_writer :ssl_enabled
+ alias_method :https_enabled?, :ssl_enabled?
+ alias_method :https_enabled=, :ssl_enabled=
def ajp_enabled?
if ! defined?(@ajp_enabled) || @ajp_enabled.nil?
ajp = @config[:ajp]
@ajp_enabled = ( !! ajp && ( ! ajp.respond_to?(:empty?) || ! ajp.empty? ) )
@@ -59,107 +62,153 @@
http = @config[:http]
@http_configured = ( !! http && ( ! http.respond_to?(:empty?) || ! http.empty? ) )
end
@http_configured
end
- attr_writer :http_configured
+ # @deprecated
+ def http_configured=(flag); @http_configured = flag end
def tomcat; @tomcat ||= initialize_tomcat; end
- LOCALHOST = 'localhost'.freeze # :nodoc:
-
def initialize_tomcat
set_system_properties
- tomcat = Trinidad::Tomcat::Tomcat.new
+ tomcat = Tomcat.new # @see Trinidad::Tomcat
tomcat.base_dir = config[:base_dir] || Dir.pwd
- tomcat.hostname = config[:address] || LOCALHOST
- tomcat.server.address = config[:address]
- tomcat.port = config[:port].to_i
+ address = config[:address] if config.key?(:address)
+ tomcat.hostname = address || 'localhost'
+ tomcat.server.address = address || nil unless address.nil?
+ tomcat.port = config[:port].to_i if config.key?(:port)
default_host(tomcat)
create_hosts(tomcat)
tomcat.enable_naming
- http_connector = http_configured? ||
- ( ! ajp_enabled? && config[:address] && config[:address] != LOCALHOST )
+ http_connector = http_configured? || ( ! ajp_enabled? && ! ssl_enabled? )
- if http_connector
- tomcat.connector = add_http_connector(tomcat)
+ tomcat.connector = add_http_connector(config[:http], tomcat) if http_connector
+
+ if ssl_enabled?
+ options = config.key?(:https) ? config[:https] : config[:ssl]
+ options = {} if options == true
+ unless options.key?(:port)
+ options[:port] = http_connector ? 3443 : config[:port] || 3443
+ end
+ connector = add_ssl_connector(options, tomcat)
+ tomcat.connector = connector unless http_connector
+ http_connector = true # tomcat.connector http: or https: or ajp:
end
+
if ajp_enabled?
- connector = add_ajp_connector(tomcat)
+ options = config[:ajp]; options = {} if options == true
+ unless options.key?(:port)
+ options[:port] = config[:port] || 8009 unless http_connector
+ end
+ connector = add_ajp_connector(options, tomcat)
tomcat.connector = connector unless http_connector
end
- add_ssl_connector(tomcat) if ssl_enabled?
- Trinidad::Extensions.configure_server_extensions(config[:extensions], tomcat)
+ Extensions.configure_server_extensions(config[:extensions], tomcat)
end
protected :initialize_tomcat
# #deprecated renamed to {#initialize_tomcat}
def load_tomcat_server; initialize_tomcat; end
def add_host_monitor(app_holders)
for host in tomcat.engine.find_children
- host_apps = select_host_apps(app_holders, host)
- host.add_lifecycle_listener(Trinidad::Lifecycle::Host.new(self, *host_apps))
+ host_apps = select_host_apps(app_holders, host, tomcat)
+ host.add_lifecycle_listener(Lifecycle::Host.new(self, *host_apps))
end
end
protected :add_host_monitor
# @deprecated replaced with {#setup_host_monitor}
def load_host_monitor(web_apps); add_host_monitor(web_apps); end
- def add_ajp_connector(tomcat = @tomcat)
- options = config[:ajp]
- options = {
- :address => @config[:address], :port => @config[:port]
- }.merge!( options.respond_to?(:[]) ? options : {} )
+ def add_ajp_connector(options = config[:ajp], tomcat = nil)
+ # backwards compatibility - single argument (tomcat = @tomcat)
+ if options && ! options.respond_to?(:[])
+ tomcat = options; options = config[:ajp]
+ else
+ tomcat = @tomcat
+ end if tomcat.nil?
- add_service_connector(options, options[:protocol_handler] || 'AJP/1.3', tomcat)
+ options = options.respond_to?(:[]) ? options.dup : {}
+ options[:address] = config[:address] unless options.key?(:address)
+
+ add_service_connector(options, 'AJP/1.3', tomcat)
end
- def add_http_connector(tomcat = @tomcat)
- options = config[:http]
- options = {
- :address => @config[:address], :port => @config[:port]
- }.merge!( options.respond_to?(:[]) ? options : {} )
+ def add_http_connector(options = config[:http], tomcat = nil)
+ # backwards compatibility - single argument (tomcat = @tomcat)
+ if options && ! options.respond_to?(:[])
+ tomcat = options; options = config[:http]
+ else
+ tomcat = @tomcat
+ end if tomcat.nil?
+ options = options.respond_to?(:[]) ? options.dup : {}
+ options[:port] = config[:port] || 3000 unless options.key?(:port)
+ options[:address] = config[:address] unless options.key?(:address)
+
if options.delete(:nio)
options[:protocol_handler] ||= 'org.apache.coyote.http11.Http11NioProtocol'
end
if options.delete(:apr)
- tomcat.server.add_lifecycle_listener(Trinidad::Tomcat::AprLifecycleListener.new)
+ tomcat.server.add_lifecycle_listener(Tomcat::AprLifecycleListener.new)
end
- add_service_connector(options, options[:protocol_handler] || 'HTTP/1.1', tomcat)
+ add_service_connector(options, 'HTTP/1.1', tomcat)
end
- def add_ssl_connector(tomcat = @tomcat)
- options = config[:ssl]
- options = {
- :scheme => 'https', :secure => true, :SSLEnabled => 'true'
- }.merge!( options.respond_to?(:[]) ? options : {} )
+ # @private
+ DEFAULT_KEYSTORE_FILE = 'ssl/keystore' # TODO review default location
- options[:keystoreFile] ||= options.delete(:keystore)
+ def add_ssl_connector(options = config[:ssl], tomcat = nil)
+ # backwards compatibility - single argument (tomcat = @tomcat)
+ if options && ! options.respond_to?(:[])
+ tomcat = options; options = config[:ssl]
+ else
+ tomcat = @tomcat
+ end if tomcat.nil?
+ options = { :scheme => 'https', :secure => true }.merge!( options.respond_to?(:[]) ? options : {} )
+ options[:address] = config[:address] unless options.key?(:address)
+
+ if keystore_file = options.delete(:keystore) || options.delete(:keystore_file)
+ options[:keystoreFile] ||= keystore_file
+ end
+ options[:keystorePass] ||= options.delete(:keystore_pass) if options.key?(:keystore_pass)
+ # handle "custom" alternative SSL (casing) options :
+ options[:SSLEnabled] = options.delete(:ssl_enabled) || true # always true
+ options[:SSLCertificateFile] ||= options.delete(:ssl_certificate_file) if options.key?(:ssl_certificate_file)
+ options[:SSLCertificateKeyFile] ||= options.delete(:ssl_certificate_key_file) if options.key?(:ssl_certificate_key_file)
+ options[:SSLVerifyClient] ||= options.delete(:ssl_verify_client) if options.key?(:ssl_verify_client)
+ options[:SSLProtocol] ||= options.delete(:ssl_protocol) if options.key?(:ssl_protocol)
+ # NOTE: there's quite more SSL prefixed options with APR ...
+
if ! options[:keystoreFile] && ! options[:SSLCertificateFile]
- options[:keystoreFile] ||= 'ssl/keystore'
- options[:keystorePass] ||= 'waduswadus42'
- generate_default_keystore(options)
+ # generate one for development/testing SSL :
+ options[:keystoreFile] = DEFAULT_KEYSTORE_FILE
+ options[:keystorePass] ||= 'waduswadus42' # NOTE change/ask for default
+ if File.exist?(DEFAULT_KEYSTORE_FILE)
+ logger.info "Using (default) keystore at #{DEFAULT_KEYSTORE_FILE.inspect}"
+ else
+ generate_default_keystore(options)
+ end
end
- add_service_connector(options, nil, tomcat)
+ add_service_connector(options, 'HTTP/1.1', tomcat)
end
# NOTE: make sure to pass an options Hash that might be changed !
def add_service_connector(options, protocol = nil, tomcat = @tomcat)
- connector = Trinidad::Tomcat::Connector.new(protocol)
- connector.scheme = options.delete(:scheme) if options[:scheme]
- connector.secure = options.delete(:secure) || false
+ connector = Tomcat::Connector.new(options.delete(:protocol) || protocol)
+ connector.scheme = options.delete(:scheme) if options.key?(:scheme)
+ connector.secure = options.key?(:secure) ? options.delete(:secure) : false
connector.port = options.delete(:port).to_i if options[:port]
- if handler = options.delete(:protocol_handler)
+ if handler = options.delete(:protocol_handler) || options.delete(:protocol_handler_class_name)
connector.protocol_handler_class_name = handler
end
options.each { |key, value| connector.setProperty(key.to_s, value.to_s) }
@@ -176,30 +225,31 @@
prev_start = host.start_children
context = begin
host.start_children = start unless start.nil?
# public Context addWebapp(Host host, String url, String name, String docBase)
tomcat.addWebapp(host, web_app.context_path, web_app.context_name, web_app.root_dir)
- rescue java.lang.IllegalArgumentException => e
+ rescue Java::JavaLang::IllegalArgumentException => e
if e.message =~ /addChild\:/
context_name = web_app.context_name
logger.error "could not add application #{context_name.inspect} from #{web_app.root_dir}\n" <<
" (same context name is used for #{host.find_child(context_name).doc_base})"
raise "there's already an application named #{context_name.inspect} for host #{host.name.inspect}"
end
raise e
ensure
host.start_children = prev_start unless start.nil?
end
- Trinidad::Extensions.configure_webapp_extensions(web_app.extensions, tomcat, context)
+ Extensions.configure_webapp_extensions(web_app.extensions, tomcat, context)
if lifecycle = web_app.define_lifecycle
context.add_lifecycle_listener(lifecycle)
end
context
end
def deploy_web_apps(tomcat = self.tomcat)
- add_host_monitor web_apps = create_web_apps
+ web_apps = create_web_apps
+ add_host_monitor web_apps
web_apps
end
def start
deploy_web_apps(tomcat)
@@ -272,11 +322,11 @@
app_root = File.expand_path(path, host.app_base)
if File.directory?(app_root) || ( app_root[-4..-1] == '.war' )
app_base_name = File.basename(app_root)
deployed = apps.find do |app_holder|; web_app = app_holder.web_app
web_app.root_dir == app_root ||
- web_app.context_path == Trinidad::Tomcat::ContextName.new(app_base_name).path
+ web_app.context_path == Tomcat::ContextName.new(app_base_name).path
end
if deployed
logger.debug "Skipping auto-deploy from #{app_root} (already deployed)"
else
apps << ( app_holder = create_web_app({
@@ -290,11 +340,11 @@
apps
end
def create_web_app(app_config)
- host_name = app_config[:host_name] || 'localhost'
+ host_name = app_config[:host_name] || tomcat.host.name
host = tomcat.engine.find_child(host_name)
app_config[:root_dir] = web_app_root_dir(app_config, host)
web_app = WebApp.create(app_config, config)
WebApp::Holder.new(web_app, add_web_app(web_app))
@@ -338,11 +388,11 @@
end
end if web_apps
end
def create_host(app_base, host_config, tomcat = @tomcat)
- host = Trinidad::Tomcat::StandardHost.new
+ host = Tomcat::StandardHost.new
host.app_base = nil # reset default app_base
host.deployXML = false # disabled by default
setup_host(app_base, host_config, host)
tomcat.engine.add_child host if tomcat
host
@@ -380,12 +430,12 @@
system.set_property("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE", 'true')
end
# @deprecated renamed to {#set_system_properties}
def load_default_system_properties; set_system_properties; end
- def configure_logging(log_level)
- Trinidad::Logging.configure(log_level)
+ def configure_logging(logging)
+ Trinidad::Logging.configure(logging)
end
def logger; @logger ||= self.class.logger; end
def self.logger
@@ -404,11 +454,12 @@
host_config.each { |name, value| host.send("#{name}=", value) }
end
host
end
- DEFAULT_HOST_APP_BASE = 'webapps' # :nodoc:
+ # @private
+ DEFAULT_HOST_APP_BASE = 'webapps'
def default_host_base?(host)
host.app_base.nil? || ( host.app_base == DEFAULT_HOST_APP_BASE && host.name == 'localhost' )
end
@@ -450,14 +501,14 @@
else
host.app_base = app_path.parent.realpath.to_s
end
end
- def select_host_apps(app_holders, host)
+ def select_host_apps(app_holders, host, tomcat = self.tomcat)
app_holders.select do |app_holder|
host_name = app_holder.web_app.host_name
- ( host_name || 'localhost' ) == host.name
+ host_name ? host_name == host.name : host == tomcat.host # default host
end
end
def find_host(name, host_config, tomcat = nil)
if tomcat.nil? # assume 2 args (host_config, tomcat)
@@ -504,28 +555,31 @@
else
path
end
end
- def generate_default_keystore(config)
- keystore_file = java.io.File.new(config[:keystoreFile])
-
- if ! keystore_file.parent_file.exists && ! keystore_file.parent_file.mkdir
- raise "Unable to create keystore folder: #{keystore_file.parent_file.canonical_path}"
+ def generate_default_keystore(file, pass = nil) # or (config)
+ file, pass = file[:keystoreFile], file[:keystorePass] if pass.nil?
+ file = Java::JavaIo::File.new(file)
+ keystore_dir = file.parent_file
+ if ! keystore_dir.exists && ! keystore_dir.mkdir
+ raise "Unable to create keystore folder: #{keystore_dir.canonical_path}"
end
- key_tool_args = ["-genkey",
+ key_tool_args = [ "-genkey",
"-alias", "localhost",
- "-dname", "CN=localhost, OU=Trinidad, O=Trinidad, C=ES",
+ "-dname", dname = "CN=localhost, OU=Trinidad, O=Trinidad, C=ES",
"-keyalg", "RSA",
"-validity", "365",
"-storepass", "key",
- "-keystore", config[:keystoreFile],
- "-storepass", config[:keystorePass],
- "-keypass", config[:keystorePass]]
+ "-keystore", file.absolute_path,
+ "-storepass", pass,
+ "-keypass", pass ]
- key_tool = Java::SunSecurityTools::KeyTool
- key_tool.main key_tool_args.to_java(:string)
+ logger.info "Generating a (default) keystore for localhost #{dname.inspect} at " <<
+ "#{file.canonical_path} (password: '#{pass}')"
+
+ Java::SunSecurityTools::KeyTool.main key_tool_args.to_java(:string)
end
def trap_signals
trap('INT') { stop! }
trap('TERM') { stop! }