Class: Trinidad::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/trinidad/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Server

Returns a new instance of Server.



21
22
23
24
25
# File 'lib/trinidad/server.rb', line 21

def initialize(config = {})
  load_config(config)
  load_tomcat_server
  create_web_apps
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/trinidad/server.rb', line 6

def config
  @config
end

#tomcatObject (readonly)

Returns the value of attribute tomcat.



6
7
8
# File 'lib/trinidad/server.rb', line 6

def tomcat
  @tomcat
end

Instance Method Details

#add_ajp_connectorObject



80
81
82
# File 'lib/trinidad/server.rb', line 80

def add_ajp_connector
  add_service_connector(@config[:ajp], 'AJP/1.3')
end

#add_http_connectorObject



97
98
99
100
101
102
103
104
# File 'lib/trinidad/server.rb', line 97

def add_http_connector
  options = @config[:http]
  options[:port] = @config[:port]
  options[:protocol_handler] = 'org.apache.coyote.http11.Http11NioProtocol' if options[:nio]

  connector = add_service_connector(options)
  @tomcat.connector = connector
end

#add_service_connector(options, protocol = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/trinidad/server.rb', line 61

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

  connector.protocol_handler_class_name = opts.delete(:protocol_handler) if opts[:protocol_handler]

  opts.each do |key, value|
    connector.setProperty(key.to_s, value.to_s)
  end

  @tomcat.getService().addConnector(connector)
  connector
end

#add_ssl_connectorObject



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/trinidad/server.rb', line 84

def add_ssl_connector
  options = @config[:ssl].merge({
    :scheme => 'https', 
    :secure => true,
    :SSLEnabled => 'true'
  })
  options[:keystore] ||= 'ssl/keystore'
  options[:keystorePass] ||= 'waduswadus'

  add_service_connector(options)
  create_default_keystore(options) unless File.exist?(options[:keystore])
end

#ajp_enabled?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/trinidad/server.rb', line 110

def ajp_enabled?
  @config.has_key?(:ajp)
end

#create_default_keystore(config) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/trinidad/server.rb', line 118

def create_default_keystore(config)
  keystore_file = java.io.File.new(config[:keystore])

  if (!keystore_file.parent_file.exists() &&
          !keystore_file.parent_file.mkdir())
      raise "Unable to create keystore folder: " + keystore_file.parent_file.canonical_path
  end

  keytool_args = ["-genkey", 
    "-alias", "localhost", 
    "-dname", "CN=localhost, OU=Trinidad, O=Trinidad, C=ES", 
    "-keyalg", "RSA",
    "-validity", "365", 
    "-storepass", "key", 
    "-keystore", config[:keystore], 
    "-storepass", config[:keystorePass],
    "-keypass", config[:keystorePass]]

  Trinidad::Tomcat::KeyTool.main(keytool_args.to_java(:string))
end

#create_web_appsObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/trinidad/server.rb', line 46

def create_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

    app_context = @tomcat.addWebapp(app_config[:context_path], app_config[:web_app_dir])
    remove_defaults(app_context)

    web_app = WebApp.create(@config, app_config)

    Trinidad::Extensions.configure_webapp_extensions(web_app.extensions, @tomcat, app_context)
    app_context.add_lifecycle_listener(WebAppLifecycleListener.new(web_app))
  end
end

#default_optionsObject



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/trinidad/server.rb', line 8

def default_options
  {
    :environment => 'development',
    :context_path => '/',
    :libs_dir => 'lib',
    :classes_dir => 'classes',
    :default_web_xml => 'config/web.xml',
    :port => 3000,
    :jruby_min_runtimes => 1,
    :jruby_max_runtimes => 5
  }
end

#http_configured?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/trinidad/server.rb', line 114

def http_configured?
  @config.has_key?(:http)
end

#load_config(config) ⇒ Object



27
28
29
30
# File 'lib/trinidad/server.rb', line 27

def load_config(config)
  @config = default_options.deep_merge(config).symbolize!
  add_default_web_app!(@config)
end

#load_tomcat_serverObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/trinidad/server.rb', line 32

def load_tomcat_server
  @tomcat = Trinidad::Tomcat::Tomcat.new
  @tomcat.port = @config[:port].to_i
  @tomcat.base_dir = Dir.pwd
  @tomcat.host.app_base = Dir.pwd
  enable_naming

  add_http_connector if http_configured?
  add_ssl_connector if ssl_enabled?
  add_ajp_connector if ajp_enabled?

  @tomcat = Trinidad::Extensions.configure_server_extensions(@config[:extensions], @tomcat)
end

#ssl_enabled?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/trinidad/server.rb', line 106

def ssl_enabled?
  @config.has_key?(:ssl)
end

#startObject



139
140
141
142
# File 'lib/trinidad/server.rb', line 139

def start
  @tomcat.start
  @tomcat.getServer().await
end