Class: Trinidad::Server

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

Constant Summary collapse

DEFAULT_KEYSTORE_FILE =
'ssl/keystore'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = Trinidad.configuration) ⇒ Server

Returns a new instance of Server.



8
9
10
# File 'lib/trinidad/server.rb', line 8

def initialize(config = Trinidad.configuration)
  configure(config)
end

Instance Attribute Details

#ajp_enabled=(value) ⇒ Object (writeonly)

Sets the attribute ajp_enabled

Parameters:

  • value

    the value to set the attribute ajp_enabled to.



58
59
60
# File 'lib/trinidad/server.rb', line 58

def ajp_enabled=(value)
  @ajp_enabled = value
end

#app_baseObject



24
25
26
# File 'lib/trinidad/server.rb', line 24

def app_base
  @app_base ||= @config[:app_base] || @config[:apps_base]
end

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#hostsObject



19
20
21
# File 'lib/trinidad/server.rb', line 19

def hosts
  @hosts ||= @config[:hosts]
end

#ssl_enabled=(value) ⇒ Object (writeonly) Also known as: https_enabled=

Sets the attribute ssl_enabled

Parameters:

  • value

    the value to set the attribute ssl_enabled to.



47
48
49
# File 'lib/trinidad/server.rb', line 47

def ssl_enabled=(value)
  @ssl_enabled = value
end

#trap=(value) ⇒ Object (writeonly)

Sets the attribute trap

Parameters:

  • value

    the value to set the attribute trap to.



38
39
40
# File 'lib/trinidad/server.rb', line 38

def trap=(value)
  @trap = value
end

#web_appsObject



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

def web_apps
  @web_apps ||= @config[:web_apps] || @config[:webapps]
end

Instance Method Details

#add_ajp_connector(options = , tomcat = nil) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/trinidad/server.rb', line 125

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?

  options = options.respond_to?(:[]) ? options.dup : {}
  options[:address] = config[:address] unless options.key?(:address)

  add_service_connector(options, 'AJP/1.3', tomcat)
end

#add_http_connector(options = , tomcat = nil) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/trinidad/server.rb', line 139

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(Tomcat::AprLifecycleListener.new)
  end

  add_service_connector(options, 'HTTP/1.1', tomcat)
end

#add_ssl_connector(options = , tomcat = nil) ⇒ Object

TODO review default location



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/trinidad/server.rb', line 165

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]
    # 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, 'HTTP/1.1', tomcat)
end

#add_web_app(web_app, host = nil, start = nil) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/trinidad/server.rb', line 220

def add_web_app(web_app, host = nil, start = nil)
  host ||= begin
    name = web_app.host_name
    name ? find_host(name, tomcat) : tomcat.host
  end
  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::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
  Extensions.configure_webapp_extensions(web_app.extensions, tomcat, context)
  if lifecycle = web_app.define_lifecycle
    context.add_lifecycle_listener(lifecycle)
  end
  context
end

#ajp_enabled?Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
# File 'lib/trinidad/server.rb', line 51

def ajp_enabled?
  if ! defined?(@ajp_enabled) || @ajp_enabled.nil?
    ajp = @config[:ajp]
    @ajp_enabled = ( !! ajp && ( ! ajp.respond_to?(:empty?) || ! ajp.empty? ) )
  end
  @ajp_enabled
end

#configure(config = Trinidad.configuration) ⇒ Object



12
13
14
15
# File 'lib/trinidad/server.rb', line 12

def configure(config = Trinidad.configuration)
  configure_logging config[:logging] || config[:log]
  @config = config.freeze
end

#deploy_web_apps(tomcat = self.tomcat) ⇒ Object



248
249
250
251
252
# File 'lib/trinidad/server.rb', line 248

def deploy_web_apps(tomcat = self.tomcat)
  web_apps = create_web_apps
  add_host_monitor web_apps
  web_apps
end

#http_configured=(flag) ⇒ Object

Deprecated.


68
# File 'lib/trinidad/server.rb', line 68

def http_configured=(flag); @http_configured = flag end

#http_configured?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
# File 'lib/trinidad/server.rb', line 60

def http_configured?
  if ! defined?(@http_configured) || @http_configured.nil?
    http = @config[:http]
    @http_configured = ( !! http && ( ! http.respond_to?(:empty?) || ! http.empty? ) )
  end
  @http_configured
end

#load_config(config) ⇒ Object

Deprecated.

replaced with #configure



17
# File 'lib/trinidad/server.rb', line 17

def load_config(config); configure(config); end

#load_host_monitor(web_apps) ⇒ Object

Deprecated.

replaced with #setup_host_monitor



123
# File 'lib/trinidad/server.rb', line 123

def load_host_monitor(web_apps); add_host_monitor(web_apps); end

#load_tomcat_serverObject

#deprecated renamed to #initialize_tomcat



113
# File 'lib/trinidad/server.rb', line 113

def load_tomcat_server; initialize_tomcat; end

#ssl_enabled?Boolean Also known as: https_enabled?

Returns:

  • (Boolean)


40
41
42
43
44
45
46
# File 'lib/trinidad/server.rb', line 40

def ssl_enabled?
  if ! defined?(@ssl_enabled) || @ssl_enabled.nil?
    ssl = @config.key?(:https) ? @config[:https] : @config[:ssl]
    @ssl_enabled = ( !! ssl && ( ! ssl.respond_to?(:empty?) || ! ssl.empty? ) )
  end
  @ssl_enabled
end

#startObject



254
255
256
257
258
259
260
261
# File 'lib/trinidad/server.rb', line 254

def start
  deploy_web_apps(tomcat)

  trap_signals if trap?

  tomcat.start
  tomcat.server.await
end

#start!Object



263
264
265
266
267
268
# File 'lib/trinidad/server.rb', line 263

def start!
  if defined?(@tomcat) && @tomcat
    @tomcat.destroy; @tomcat = nil
  end
  start
end

#stopObject



270
271
272
273
274
# File 'lib/trinidad/server.rb', line 270

def stop
  if defined?(@tomcat) && @tomcat
    @tomcat.stop; true
  end
end

#stop!Object



276
277
278
# File 'lib/trinidad/server.rb', line 276

def stop!
  (@tomcat.destroy; true) if stop
end

#tomcatObject



70
# File 'lib/trinidad/server.rb', line 70

def tomcat; @tomcat ||= initialize_tomcat; end

#trap?Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/trinidad/server.rb', line 34

def trap?
  @trap = !! @config[:trap] if ! defined?(@trap) || @trap.nil?
  @trap
end