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 = Trinidad.configuration) ⇒ Server

Returns a new instance of Server.



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

def initialize(config = Trinidad.configuration)
  load_config(config)
  load_tomcat_server
  apps = create_web_apps
  load_host_monitor(apps)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/trinidad/server.rb', line 4

def config
  @config
end

#tomcatObject (readonly)

Returns the value of attribute tomcat.



4
5
6
# File 'lib/trinidad/server.rb', line 4

def tomcat
  @tomcat
end

Instance Method Details

#add_ajp_connectorObject



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

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

#add_http_connectorObject



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/trinidad/server.rb', line 162

def add_http_connector
  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]

  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

#add_service_connector(options, protocol = nil) ⇒ Object



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

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.service.add_connector(connector)
  connector
end

#add_ssl_connectorObject



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

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

  options[:keystoreFile] ||= options.delete(:keystore)

  if !options[:keystoreFile] && !options[:SSLCertificateFile]
    options[:keystoreFile] = 'ssl/keystore'
    options[:keystorePass] = 'waduswadus'
    create_default_keystore(options)
  end

  add_service_connector(options)
end

#ajp_enabled?Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/trinidad/server.rb', line 180

def ajp_enabled?
  @config[:ajp] && !@config[:ajp].empty?
end

#create_default_keystore(config) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/trinidad/server.rb', line 188

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

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

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

#create_from_apps_baseObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/trinidad/server.rb', line 84

def create_from_apps_base
  if @config[:apps_base] || @config[:hosts]
    @tomcat.engine.find_children.map do |host|
      apps_base = host.app_base

      apps_path = Dir.glob(File.join(apps_base, '*')).
        select {|path| !(path =~ /tomcat\.\d+$/) }

      apps_path.reject! {|path| apps_path.include?(path + '.war') }

      apps_path.map do |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),
            :host         => host
          }

          create_web_app(app_config)
        end
      end
    end.flatten
  end
end

#create_from_web_appsObject



73
74
75
76
77
78
79
80
81
82
# File 'lib/trinidad/server.rb', line 73

def create_from_web_apps
  if @config[:web_apps]
    @config[:web_apps].map 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

#create_hostsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/trinidad/server.rb', line 34

def create_hosts
  if @config[:hosts]
    @config[:hosts].each do |apps_base, names|
      create_host(apps_base, names)
    end

    set_default_host
  elsif @config[:web_apps]
    # create the hosts when they are specified for each app into
    # web_apps. We must create them before creating the
    # applications.
    @config[:web_apps].each do |name, app_config|
      if host_names = app_config.delete(:hosts)
        dir = app_config[:web_app_dir] || Dir.pwd
        apps_base = File.dirname(dir) == '.' ? dir : File.dirname(dir)
        app_config[:host] = create_host(apps_base, host_names)
      end

      set_default_host
    end
  else
    @tomcat.host.app_base = @config[:apps_base] || Dir.pwd
  end
end

#create_web_app(app_config) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/trinidad/server.rb', line 110

def create_web_app(app_config)
  web_app = WebApp.create(@config, app_config)

  app_context = @tomcat.addWebapp(app_config[:host] || @tomcat.host, web_app.context_path, web_app.web_app_dir)

  Trinidad::Extensions.configure_webapp_extensions(web_app.extensions, @tomcat, app_context)

  app_context.add_lifecycle_listener(web_app.define_lifecycle)

  {:context => app_context, :app => web_app, :monitor => web_app.monitor}
end

#create_web_appsObject



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

def create_web_apps
  apps = []
  apps << create_from_web_apps
  apps << create_from_apps_base

  apps.flatten.compact
end

#http_configured?Boolean

Returns:

  • (Boolean)


184
185
186
# File 'lib/trinidad/server.rb', line 184

def http_configured?
  (@config[:http] && !@config[:http].empty?) || @config[:address] != 'localhost'
end

#load_config(config) ⇒ Object



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

def load_config(config)
  @config = config
  add_default_web_app!(config)
end

#load_host_monitor(apps) ⇒ Object



67
68
69
70
71
# File 'lib/trinidad/server.rb', line 67

def load_host_monitor(apps)
  @tomcat.engine.find_children.each do |host|
    host.add_lifecycle_listener(Trinidad::Lifecycle::Host.new(@tomcat, *apps))
  end
end

#load_tomcat_serverObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/trinidad/server.rb', line 18

def load_tomcat_server
  @tomcat = Trinidad::Tomcat::Tomcat.new
  @tomcat.base_dir = Dir.pwd
  @tomcat.hostname = @config[:address] || 'localhost'
  @tomcat.server.address = @config[:address]
  @tomcat.port = @config[:port].to_i
  create_hosts
  @tomcat.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)


176
177
178
# File 'lib/trinidad/server.rb', line 176

def ssl_enabled?
  @config[:ssl] && !@config[:ssl].empty?
end

#startObject



209
210
211
212
213
214
# File 'lib/trinidad/server.rb', line 209

def start
  trap_signals if @config[:trap]

  @tomcat.start
  @tomcat.server.await
end

#stopObject



216
217
218
219
# File 'lib/trinidad/server.rb', line 216

def stop
  @tomcat.stop
  @tomcat.destroy
end