trinidad 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.txt CHANGED
@@ -1,3 +1,8 @@
1
+ == 0.4.0 (2009-07-16)
2
+
3
+ * support for ssl connections
4
+ * support for ajp connections
5
+
1
6
  == 0.3.0 (2009-07-07)
2
7
 
3
8
  * project renamed due to tomcat legal issues
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ begin
6
6
  Jeweler::Tasks.new do |gem|
7
7
  gem.name = "trinidad"
8
8
  gem.summary = %Q{Simple library to run rails applications into an embedded Tomcat}
9
- gem.email = "david.calavera@gmail.com"
9
+ gem.email = "calavera@apache.org"
10
10
  gem.homepage = "http://calavera.github.com/trinidad"
11
11
  gem.authors = ["David Calavera"]
12
12
  gem.rubyforge_project = 'trinidad'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
data/lib/trinidad.rb CHANGED
@@ -4,6 +4,8 @@ $:.unshift(File.dirname(__FILE__)) unless
4
4
  require "java"
5
5
  require 'rubygems'
6
6
 
7
+ require 'trinidad/core_ext'
8
+
7
9
  require 'trinidad/command_line_parser'
8
10
  require 'trinidad/jars'
9
11
  require 'trinidad/server'
@@ -9,7 +9,9 @@ module Trinidad
9
9
  :context_path => '/',
10
10
  :libs_dir => 'lib',
11
11
  :classes_dir => 'classes',
12
- :config => 'config/tomcat.yml'
12
+ :config => 'config/tomcat.yml',
13
+ :ssl_port => 8443,
14
+ :ajp_port => 8009
13
15
  }
14
16
 
15
17
  parser = OptionParser.new do |opts|
@@ -41,10 +43,22 @@ module Trinidad
41
43
  default_options[:classes_dir] = v
42
44
  end
43
45
 
46
+ opts.on('-s', '--ssl [SSL_PORT]', 'Enable secure socket layout',
47
+ "default port: #{default_options[:ssl_port]}") do |v|
48
+ ssl_port = v.nil? ? default_options.delete(:ssl_port) : v.to_i
49
+ default_options[:ssl] = {:port => ssl_port}
50
+ end
51
+
52
+ opts.on('-a', '--ajp [AJP_PORT]', 'Enable ajp connections',
53
+ "default port: #{default_options[:ajp_port]}") do |v|
54
+ ajp_port = v.nil? ? default_options.delete(:ajp_port) : v.to_i
55
+ default_options[:ajp] = {:port => ajp_port}
56
+ end
57
+
44
58
  opts.on('-f', '--config [CONFIG_FILE]', 'Configuration file',
45
59
  "default: #{default_options[:config]}") do |v|
46
60
  default_options[:config] = v if v
47
- default_options.merge! YAML.load_file(default_options[:config])
61
+ default_options.deep_merge! YAML.load_file(default_options[:config])
48
62
  end
49
63
 
50
64
  opts.on('-v', '--version', 'display the current version') do
@@ -0,0 +1,32 @@
1
+ Hash.class_eval do
2
+ # Merges self with another hash, recursively.
3
+ #
4
+ # This code was lovingly stolen from some random gem:
5
+ # http://gemjack.com/gems/tartan-0.1.1/classes/Hash.html
6
+ #
7
+ # Thanks to whoever made it.
8
+ def deep_merge(hash)
9
+ target = dup
10
+
11
+ hash.keys.each do |key|
12
+ if hash[key].is_a? Hash and self[key].is_a? Hash
13
+ target[key] = target[key].deep_merge(hash[key])
14
+ next
15
+ end
16
+
17
+ target[key] = hash[key]
18
+ end
19
+
20
+ target
21
+ end
22
+
23
+ def deep_merge!(second)
24
+ second.each_pair do |k,v|
25
+ if self[k].is_a?(Hash) and second[k].is_a?(Hash)
26
+ self[k].deep_merge!(second[k])
27
+ else
28
+ self[k] = second[k]
29
+ end
30
+ end
31
+ end
32
+ end
data/lib/trinidad/jars.rb CHANGED
@@ -17,6 +17,9 @@ module Trinidad
17
17
  include_package 'org.apache.catalina.loader'
18
18
 
19
19
  include_package 'org.apache.naming.resources'
20
+
21
+ import 'org.apache.catalina.connector.Connector'
22
+ import 'sun.security.tools.KeyTool'
20
23
  end
21
24
 
22
25
  module Rack
@@ -1,16 +1,24 @@
1
1
  module Trinidad
2
2
  class Server
3
3
 
4
- @@defaults = {
5
- :environment => 'development',
6
- :context_path => '/',
7
- :libs_dir => 'lib',
8
- :classes_dir => 'classes',
9
- :default_web_xml => 'config/web.xml',
10
- :port => 3000,
11
- :jruby_min_runtimes => 1,
12
- :jruby_max_runtimes => 5
13
- }
4
+ attr_reader :tomcat
5
+
6
+ def default_options
7
+ {
8
+ :environment => 'development',
9
+ :context_path => '/',
10
+ :libs_dir => 'lib',
11
+ :classes_dir => 'classes',
12
+ :default_web_xml => 'config/web.xml',
13
+ :port => 3000,
14
+ :jruby_min_runtimes => 1,
15
+ :jruby_max_runtimes => 5,
16
+ :ssl => {
17
+ :keystore => 'ssl/keystore',
18
+ :keystorePass => 'waduswadus'
19
+ }
20
+ }
21
+ end
14
22
 
15
23
  def initialize(config = {})
16
24
  load_config(config)
@@ -19,13 +27,17 @@ module Trinidad
19
27
  end
20
28
 
21
29
  def load_config(config)
22
- @config = @@defaults.merge!(config)
23
- @config[:web_app_dir] = Dir.pwd
30
+ @config = {:web_app_dir => Dir.pwd}.merge(default_options).deep_merge(config)
31
+
32
+ @config[:ssl][:keystore] = File.join(@config[:web_app_dir], @config[:ssl][:keystore])
24
33
  end
25
34
 
26
35
  def load_tomcat_server
27
36
  @tomcat = Trinidad::Tomcat::Tomcat.new
28
37
  @tomcat.setPort(@config[:port].to_i)
38
+
39
+ add_ssl_connector if ssl_enabled?
40
+ add_ajp_connector if ajp_enabled?
29
41
  end
30
42
 
31
43
  def create_web_app
@@ -39,10 +51,70 @@ module Trinidad
39
51
 
40
52
  web_app.add_rack_context_listener
41
53
  end
54
+
55
+ def add_service_connector(options, protocol = nil)
56
+ connector = Trinidad::Tomcat::Connector.new(protocol)
57
+
58
+ opts = options.dup
59
+
60
+ connector.scheme = opts.delete(:scheme) if opts[:scheme]
61
+ connector.secure = opts.delete(:secure) || false
62
+ connector.port = opts.delete(:port)
63
+
64
+ options.each do |key, value|
65
+ connector.setProperty(key.to_s, value.to_s)
66
+ end
67
+
68
+ @tomcat.getService().addConnector(connector)
69
+ end
70
+
71
+ def add_ajp_connector
72
+ add_service_connector(@config[:ajp], 'AJP/1.3')
73
+ end
74
+
75
+ def add_ssl_connector
76
+ options = @config[:ssl].merge({
77
+ :scheme => 'https',
78
+ :secure => true,
79
+ :SSLEnabled => 'true',
80
+ })
81
+ add_service_connector(options)
82
+
83
+ create_default_keystore unless File.exist?(@config[:ssl][:keystore])
84
+ end
85
+
86
+ def ssl_enabled?
87
+ !@config[:ssl].nil? && !@config[:ssl][:port].nil? && @config[:ssl][:port].is_a?(Fixnum)
88
+ end
89
+
90
+ def ajp_enabled?
91
+ !@config[:ajp].nil? && !@config[:ajp][:port].nil? && @config[:ajp][:port].is_a?(Fixnum)
92
+ end
93
+
94
+ def create_default_keystore
95
+ keystore_file = java.io.File.new(@config[:ssl][:keystore])
96
+
97
+ if (!keystore_file.parent_file.exists() &&
98
+ !keystore_file.parent_file.mkdir())
99
+ raise "Unable to create keystore folder: " + keystore_file.parent_file.canonical_path
100
+ end
101
+
102
+ keytool_args = ["-genkey",
103
+ "-alias", "localhost",
104
+ "-dname", "CN=localhost, OU=Trinidad, O=Trinidad, C=ES",
105
+ "-keyalg", "RSA",
106
+ "-validity", "365",
107
+ "-storepass", "key",
108
+ "-keystore", @config[:ssl][:keystore],
109
+ "-storepass", @config[:ssl][:keystorePass],
110
+ "-keypass", @config[:ssl][:keystorePass]]
111
+
112
+ Trinidad::Tomcat::KeyTool.main(keytool_args.to_java(:string))
113
+ end
42
114
 
43
115
  def start
44
116
  @tomcat.start
45
117
  @tomcat.getServer().await
46
118
  end
47
119
  end
48
- end
120
+ end
data/spec/spec_helper.rb CHANGED
@@ -10,4 +10,9 @@ $:.unshift(File.dirname(__FILE__) + '/../lib')
10
10
  $:.unshift(File.dirname(__FILE__) + '/../trinidad-libs')
11
11
 
12
12
  require 'java'
13
- require 'trinidad'
13
+ require 'trinidad'
14
+ require 'mocha'
15
+
16
+ Spec::Runner.configure do |config|
17
+ config.mock_with :mocha
18
+ end
@@ -29,4 +29,39 @@ describe Trinidad::CommandLineParser do
29
29
  options[:environment].should == 'production'
30
30
  end
31
31
 
32
+ it "should add default ssl port to options" do
33
+ ARGV = '--ssl'.split
34
+
35
+ options = Trinidad::CommandLineParser.parse
36
+ options[:ssl].should == {:port => 8443}
37
+ end
38
+
39
+ it "should add custom ssl port to options" do
40
+ ARGV = '--ssl 8843'.split
41
+
42
+ options = Trinidad::CommandLineParser.parse
43
+ options[:ssl].should == {:port => 8843}
44
+ end
45
+
46
+ it "should add ajp connection with default port to options" do
47
+ ARGV = '--ajp'.split
48
+
49
+ options = Trinidad::CommandLineParser.parse
50
+ options[:ajp].should == {:port => 8009}
51
+ end
52
+
53
+ it "should add ajp connection with coustom port to options" do
54
+ ARGV = '--ajp 8099'.split
55
+
56
+ options = Trinidad::CommandLineParser.parse
57
+ options[:ajp].should == {:port => 8099}
58
+ end
59
+
60
+ it "should merge ajp options from the config file" do
61
+ ARGV = "--ajp 8099 -f #{File.join(File.dirname(__FILE__), '..', 'web_app_mock', 'tomcat.yml')}".split
62
+
63
+ options = Trinidad::CommandLineParser.parse
64
+ options[:ajp][:port].should == 8099
65
+ options[:ajp][:secure].should == true
66
+ end
32
67
  end
@@ -0,0 +1,42 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Trinidad::Server do
4
+
5
+ it "should have ssl disabled when config param is nil" do
6
+ server = Trinidad::Server.new
7
+ server.ssl_enabled?.should == false
8
+ end
9
+
10
+ it "should have ajp disabled when config param is nil" do
11
+ server = Trinidad::Server.new
12
+ server.ajp_enabled?.should == false
13
+ end
14
+
15
+ it "should have ssl enabled when config param is a number" do
16
+ server = Trinidad::Server.new({:ssl => {:port => 8443},
17
+ :web_app_dir => File.join(File.dirname(__FILE__), '..', 'web_app_mock')})
18
+
19
+ server.ssl_enabled?.should == true
20
+ end
21
+
22
+ it "should have ajp enabled when config param is a number" do
23
+ server = Trinidad::Server.new({:ajp => {:port => 8009}})
24
+
25
+ server.ajp_enabled?.should == true
26
+ end
27
+
28
+ it "should have a connector with https scheme" do
29
+ server = Trinidad::Server.new({:ssl => {:port => 8443},
30
+ :web_app_dir => File.join(File.dirname(__FILE__), '..', 'web_app_mock')})
31
+
32
+ server.tomcat.service.findConnectors().should have(1).connectors
33
+ server.tomcat.service.findConnectors()[0].scheme.should == 'https'
34
+ end
35
+
36
+ it "should have an ajp connector enabled" do
37
+ server = Trinidad::Server.new({:ajp => {:port => 8009}})
38
+
39
+ server.tomcat.service.findConnectors().should have(1).connectors
40
+ server.tomcat.service.findConnectors()[0].protocol.should == 'AJP/1.3'
41
+ end
42
+ end
@@ -38,7 +38,7 @@ describe Trinidad::WebApp do
38
38
 
39
39
  it "should add a filter from the default web.xml" do
40
40
  start_context_with_web_xml
41
- @web_app.context.findFilterDefs().size().should == 1
41
+ @web_app.context.findFilterDefs().should have(1).filters
42
42
  end
43
43
 
44
44
  it "shouldn't duplicate init params" do
@@ -55,12 +55,12 @@ describe Trinidad::WebApp do
55
55
 
56
56
  it "should configure rack filter" do
57
57
  @web_app.add_rack_filter
58
- @web_app.context.findFilterDefs().size().should == 1
58
+ @web_app.context.findFilterDefs().should have(1).filters
59
59
  end
60
60
 
61
61
  it "should configure rack listener" do
62
62
  @web_app.add_rack_context_listener
63
- @web_app.context.findApplicationListeners().size().should == 1
63
+ @web_app.context.findApplicationListeners().should have(1).listeners
64
64
  end
65
65
 
66
66
  it "should have rack filter already configured" do
@@ -68,7 +68,7 @@ describe Trinidad::WebApp do
68
68
  @web_app.rack_filter_configured?().should == true
69
69
 
70
70
  @web_app.add_rack_filter
71
- @web_app.context.findFilterDefs().size().should == 0
71
+ @web_app.context.findFilterDefs().should have(0).filters
72
72
  end
73
73
 
74
74
  it "should have rack listener already configured" do
@@ -76,7 +76,7 @@ describe Trinidad::WebApp do
76
76
  @web_app.rack_listener_configured?().should == true
77
77
 
78
78
  @web_app.add_rack_context_listener
79
- @web_app.context.findApplicationListeners().size().should == 0
79
+ @web_app.context.findApplicationListeners().should have(0).listeners
80
80
  end
81
81
 
82
82
  def start_context_with_web_xml
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trinidad
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Calavera
@@ -9,12 +9,12 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-07-07 00:00:00 +02:00
12
+ date: 2009-07-16 00:00:00 +02:00
13
13
  default_executable: trinidad
14
14
  dependencies: []
15
15
 
16
16
  description:
17
- email: david.calavera@gmail.com
17
+ email: calavera@apache.org
18
18
  executables:
19
19
  - trinidad
20
20
  extensions: []
@@ -31,6 +31,7 @@ files:
31
31
  - bin/trinidad
32
32
  - lib/trinidad.rb
33
33
  - lib/trinidad/command_line_parser.rb
34
+ - lib/trinidad/core_ext.rb
34
35
  - lib/trinidad/jars.rb
35
36
  - lib/trinidad/server.rb
36
37
  - lib/trinidad/web_app.rb
@@ -76,5 +77,6 @@ specification_version: 3
76
77
  summary: Simple library to run rails applications into an embedded Tomcat
77
78
  test_files:
78
79
  - spec/spec_helper.rb
80
+ - spec/trinidad/server_spec.rb
79
81
  - spec/trinidad/command_line_parser_spec.rb
80
82
  - spec/trinidad/web_app_spec.rb