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
26
# File 'lib/trinidad/server.rb', line 21

def initialize(config = {})
  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



127
128
129
# File 'lib/trinidad/server.rb', line 127

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

#add_http_connectorObject



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/trinidad/server.rb', line 149

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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/trinidad/server.rb', line 109

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



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/trinidad/server.rb', line 131

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)


167
168
169
# File 'lib/trinidad/server.rb', line 167

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

#create_default_keystore(config) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/trinidad/server.rb', line 175

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/trinidad/server.rb', line 72

def create_from_apps_base
  if @config[:apps_base]
    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.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)
        }
        if File.directory?(path) && File.exist?(File.expand_path('config.ru', path))
          app_config[:rackup] = 'config.ru'
        end

        create_web_app(app_config)
      end
    end
  end
end

#create_from_web_appsObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/trinidad/server.rb', line 61

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_web_app(app_config) ⇒ Object



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

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

  app_context = @tomcat.addWebapp(web_app.context_path, web_app.web_app_dir)

  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)

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

#create_web_appsObject



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

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

  apps.flatten.compact
end

#default_optionsObject



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

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,
    :address => 'localhost',
    :log => 'INFO'
  }
end

#http_configured?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/trinidad/server.rb', line 171

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

#load_config(config) ⇒ Object



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

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

#load_host_monitor(apps) ⇒ Object



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

def load_host_monitor(apps)
  @tomcat.host.add_lifecycle_listener(Trinidad::Lifecycle::Host.new(*apps))
end

#load_tomcat_serverObject



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

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.host.app_base = @config[:apps_base] || Dir.pwd
  @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)


163
164
165
# File 'lib/trinidad/server.rb', line 163

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

#startObject



196
197
198
199
# File 'lib/trinidad/server.rb', line 196

def start
  @tomcat.start
  @tomcat.server.await
end