trinidad 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ == 0.7.0 (2009-12-01)
2
+
3
+ * Support to run different applications within the same Tomcat container
4
+
1
5
  == 0.6.0 (2009-11-02)
2
6
 
3
7
  * Rackup compatibe frameworks support
@@ -33,6 +33,8 @@ The server can also be configured from a yaml file. If a file is not especified,
33
33
 
34
34
  You can also specify a default web.xml to config your web application. By default the server tries to load the file <em>config/web.xml</em> but you can modify this path adding the option <em>default_web_xml</em> within your configuration file.
35
35
 
36
+ Other <a href="http://wiki.github.com/calavera/trinidad/advanced-configuration">advanced options</a> can be found in the wiki.
37
+
36
38
  == Copyright
37
39
 
38
40
  Copyright (c) 2009 David Calavera<calavera@apache.org>. See LICENSE for details.
data/Rakefile CHANGED
@@ -16,6 +16,7 @@ begin
16
16
  gem.add_dependency 'rack', '>=1.0'
17
17
  end
18
18
 
19
+ Jeweler::GemcutterTasks.new
19
20
  rescue LoadError
20
21
  puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
21
22
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.0
1
+ 0.7.0
@@ -29,4 +29,19 @@ Hash.class_eval do
29
29
  end
30
30
  end
31
31
  end
32
- end
32
+
33
+ def deep_key?(key)
34
+ exist = false
35
+
36
+ keys.each do |k|
37
+ if self[k].is_a? Hash
38
+ exist = self[k].deep_key? key
39
+ break
40
+ end
41
+
42
+ exist = (k == key)
43
+ end
44
+
45
+ exist
46
+ end
47
+ end
@@ -11,7 +11,7 @@ module Trinidad
11
11
  end
12
12
 
13
13
  def rackup_script
14
- IO.read(File.join(@config[:web_app_dir], @config[:rackup]))
14
+ IO.read(File.join(@app[:web_app_dir], @app[:rackup]))
15
15
  end
16
16
  end
17
17
  end
@@ -3,7 +3,7 @@ module Trinidad
3
3
 
4
4
  def add_init_params
5
5
  super
6
- @context.addParameter('rails.env', @config[:environment].to_s) unless @context.findParameter('rails.env')
6
+ @context.addParameter('rails.env', environment.to_s) unless @context.findParameter('rails.env')
7
7
  @context.addParameter('rails.root', '/') unless @context.findParameter('rails.root')
8
8
  end
9
9
 
@@ -12,44 +12,52 @@ module Trinidad
12
12
  :default_web_xml => 'config/web.xml',
13
13
  :port => 3000,
14
14
  :jruby_min_runtimes => 1,
15
- :jruby_max_runtimes => 5,
16
- :ssl => {
17
- :keystore => 'ssl/keystore',
18
- :keystorePass => 'waduswadus'
19
- }
15
+ :jruby_max_runtimes => 5
20
16
  }
21
17
  end
22
18
 
23
19
  def initialize(config = {})
24
20
  load_config(config)
25
21
  load_tomcat_server
26
- create_web_app
22
+ create_web_apps
27
23
  end
28
24
 
29
25
  def load_config(config)
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])
26
+ @config = default_options.deep_merge(config)
27
+ add_default_web_app!(@config)
28
+
29
+ configure_ssl!(@config)
33
30
  end
34
31
 
35
32
  def load_tomcat_server
36
33
  @tomcat = Trinidad::Tomcat::Tomcat.new
37
34
  @tomcat.setPort(@config[:port].to_i)
35
+ @tomcat.setBaseDir(Dir.pwd)
38
36
 
39
37
  add_ssl_connector if ssl_enabled?
40
38
  add_ajp_connector if ajp_enabled?
41
39
  end
42
40
 
43
- def create_web_app
44
- web_app = WebApp.create(@tomcat.addWebapp(@config[:context_path].to_s, @config[:web_app_dir]), @config)
41
+ def create_web_apps
42
+ @config[:web_apps].each do |name, app|
43
+ unless app[:context_path]
44
+ app[:context_path] = name.to_s == 'default' ? '/' : "/#{name.to_s}"
45
+ end
46
+ app[:web_app_dir] = Dir.pwd unless app.has_key?(:web_app_dir)
47
+
48
+ tomcat_app = @tomcat.addWebapp(app[:context_path].to_s, app[:web_app_dir])
49
+ # tomcat_app.setDocBase(app[:web_app_dir])
50
+
51
+ web_app = WebApp.create(tomcat_app, @config, app)
45
52
 
46
- web_app.load_default_web_xml
47
- web_app.add_rack_filter
48
- web_app.add_context_loader
49
- web_app.add_init_params
50
- web_app.add_web_dir_resources
53
+ web_app.load_default_web_xml
54
+ web_app.add_rack_filter
55
+ web_app.add_context_loader
56
+ web_app.add_init_params
57
+ web_app.add_web_dir_resources
51
58
 
52
- web_app.add_rack_context_listener
59
+ web_app.add_rack_context_listener
60
+ end
53
61
  end
54
62
 
55
63
  def add_service_connector(options, protocol = nil)
@@ -59,7 +67,7 @@ module Trinidad
59
67
 
60
68
  connector.scheme = opts.delete(:scheme) if opts[:scheme]
61
69
  connector.secure = opts.delete(:secure) || false
62
- connector.port = opts.delete(:port)
70
+ connector.port = opts.delete(:port).to_i
63
71
 
64
72
  options.each do |key, value|
65
73
  connector.setProperty(key.to_s, value.to_s)
@@ -73,26 +81,28 @@ module Trinidad
73
81
  end
74
82
 
75
83
  def add_ssl_connector
76
- options = @config[:ssl].merge({
77
- :scheme => 'https',
78
- :secure => true,
79
- :SSLEnabled => 'true',
80
- })
81
- add_service_connector(options)
84
+ @config[:web_apps].each do |name, app|
85
+ options = app[:ssl].merge({
86
+ :scheme => 'https',
87
+ :secure => true,
88
+ :SSLEnabled => 'true',
89
+ })
90
+ add_service_connector(options)
82
91
 
83
- create_default_keystore unless File.exist?(@config[:ssl][:keystore])
92
+ create_default_keystore(app) unless File.exist?(app[:ssl][:keystore])
93
+ end
84
94
  end
85
95
 
86
96
  def ssl_enabled?
87
- !@config[:ssl].nil? && !@config[:ssl][:port].nil? && @config[:ssl][:port].is_a?(Fixnum)
97
+ @config.has_key?(:ssl) || @config[:web_apps].deep_key?(:ssl)
88
98
  end
89
99
 
90
100
  def ajp_enabled?
91
- !@config[:ajp].nil? && !@config[:ajp][:port].nil? && @config[:ajp][:port].is_a?(Fixnum)
101
+ @config.has_key?(:ajp)
92
102
  end
93
103
 
94
- def create_default_keystore
95
- keystore_file = java.io.File.new(@config[:ssl][:keystore])
104
+ def create_default_keystore(config)
105
+ keystore_file = java.io.File.new(config[:ssl][:keystore])
96
106
 
97
107
  if (!keystore_file.parent_file.exists() &&
98
108
  !keystore_file.parent_file.mkdir())
@@ -105,9 +115,9 @@ module Trinidad
105
115
  "-keyalg", "RSA",
106
116
  "-validity", "365",
107
117
  "-storepass", "key",
108
- "-keystore", @config[:ssl][:keystore],
109
- "-storepass", @config[:ssl][:keystorePass],
110
- "-keypass", @config[:ssl][:keystorePass]]
118
+ "-keystore", config[:ssl][:keystore],
119
+ "-storepass", config[:ssl][:keystorePass],
120
+ "-keypass", config[:ssl][:keystorePass]]
111
121
 
112
122
  Trinidad::Tomcat::KeyTool.main(keytool_args.to_java(:string))
113
123
  end
@@ -116,5 +126,40 @@ module Trinidad
116
126
  @tomcat.start
117
127
  @tomcat.getServer().await
118
128
  end
129
+
130
+ private
131
+
132
+ def add_default_web_app!(config)
133
+ if (!config.has_key?(:web_apps))
134
+ default_app = if (config.has_key?(:rackup))
135
+ {:rackup => config[:rackup]}
136
+ else
137
+ {
138
+ :context_path => config[:context_path] || '/',
139
+ :web_app_dir => config[:web_app_dir] || Dir.pwd
140
+ }
141
+ end
142
+
143
+ config.merge!({
144
+ :web_apps => {
145
+ :default => default_app
146
+ }
147
+ })
148
+ end
149
+ end
150
+
151
+ def configure_ssl!(config)
152
+ if config.has_key?(:ssl) || config[:web_apps].deep_key?(:ssl)
153
+ if (config.has_key?(:ssl))
154
+ config[:ssl][:keystore] ||= 'ssl/keystore'
155
+ config[:ssl][:keystorePass] ||= 'waduswadus'
156
+ end
157
+
158
+ config[:web_apps].each do |name, app|
159
+ app[:ssl] = config[:ssl] unless app.has_key?(:ssl)
160
+ app[:ssl][:keystore] = File.join(app[:web_app_dir], app[:ssl][:keystore]) if app.has_key?(:ssl)
161
+ end
162
+ end
163
+ end
119
164
  end
120
165
  end
@@ -2,13 +2,14 @@ module Trinidad
2
2
  class WebApp
3
3
  attr_reader :context, :config
4
4
 
5
- def self.create(context, config)
6
- config.has_key?(:rackup) ? RackupWebApp.new(context, config) : RailsWebApp.new(context, config)
5
+ def self.create(context, config, app)
6
+ app.has_key?(:rackup) ? RackupWebApp.new(context, config, app) : RailsWebApp.new(context, config, app)
7
7
  end
8
8
 
9
- def initialize(context, config)
9
+ def initialize(context, config, app)
10
10
  @context = context
11
11
  @config = config
12
+ @app = app
12
13
  end
13
14
 
14
15
  def add_rack_filter
@@ -17,10 +18,9 @@ module Trinidad
17
18
  filter_def.setFilterName('RackFilter')
18
19
  filter_def.setFilterClass('org.jruby.rack.RackFilter')
19
20
 
20
- pattern = @config[:context_path][-1..-1] != '/' ? @config[:context_path] : @config[:context_path][0..-2]
21
21
  filter_map = Trinidad::Tomcat::FilterMap.new
22
22
  filter_map.setFilterName('RackFilter')
23
- filter_map.addURLPattern("#{pattern}/*")
23
+ filter_map.addURLPattern('/*')
24
24
 
25
25
  @context.addFilterDef(filter_def)
26
26
  @context.addFilterMap(filter_map)
@@ -49,7 +49,7 @@ module Trinidad
49
49
  end
50
50
 
51
51
  def add_web_dir_resources
52
- @context.setDocBase(File.join(@config[:web_app_dir], public_root)) if File.exist?(File.join(@config[:web_app_dir], public_root))
52
+ @context.setDocBase(File.join(@app[:web_app_dir], public_root)) if File.exist?(File.join(@app[:web_app_dir], public_root))
53
53
  end
54
54
 
55
55
  def add_rack_context_listener
@@ -59,7 +59,7 @@ module Trinidad
59
59
  end
60
60
 
61
61
  def add_application_libs(class_loader)
62
- resources_dir = File.join(@config[:web_app_dir], @config[:libs_dir], '**', '*.jar')
62
+ resources_dir = File.join(@app[:web_app_dir], libs_dir, '**', '*.jar')
63
63
 
64
64
  Dir[resources_dir].each do |resource|
65
65
  class_loader.addURL(java.io.File.new(resource).to_url)
@@ -67,12 +67,12 @@ module Trinidad
67
67
  end
68
68
 
69
69
  def add_application_classes(class_loader)
70
- resources_dir = File.join(@config[:web_app_dir], @config[:classes_dir])
70
+ resources_dir = File.join(@app[:web_app_dir], classes_dir)
71
71
  class_loader.addURL(java.io.File.new(resources_dir).to_url)
72
72
  end
73
73
 
74
74
  def load_default_web_xml
75
- default_web_xml = File.expand_path(File.join(@config[:web_app_dir], @config[:default_web_xml]))
75
+ default_web_xml = File.expand_path(File.join(@app[:web_app_dir], default_web_xml_file))
76
76
 
77
77
  if File.exist?(default_web_xml)
78
78
  @context.setDefaultWebXml(default_web_xml)
@@ -103,7 +103,23 @@ module Trinidad
103
103
  end
104
104
 
105
105
  def public_root
106
- @context.findParameter('public.root') || @config[:public] || 'public'
106
+ @context.findParameter('public.root') || @app[:public] || @config[:public] || 'public'
107
+ end
108
+
109
+ def libs_dir
110
+ @app[:libs_dir] || @config[:libs_dir]
111
+ end
112
+
113
+ def classes_dir
114
+ @app[:classes_dir] || @config[:classes_dir]
115
+ end
116
+
117
+ def default_web_xml_file
118
+ @app[:default_web_xml] || @config[:default_web_xml]
119
+ end
120
+
121
+ def environment
122
+ @app[:environment] || @config[:environment]
107
123
  end
108
124
  end
109
125
  end
@@ -39,4 +39,30 @@ describe Trinidad::Server do
39
39
  server.tomcat.service.findConnectors().should have(1).connectors
40
40
  server.tomcat.service.findConnectors()[0].protocol.should == 'AJP/1.3'
41
41
  end
42
+
43
+ it "loads one application for each option present into :web_apps" do
44
+ server = Trinidad::Server.new({
45
+ :web_apps => {
46
+ :mock1 => {
47
+ :context_path => '/mock1',
48
+ :web_app_dir => File.join(File.dirname(__FILE__), '..', 'web_app_mock')
49
+ },
50
+ :mock2 => {
51
+ :context_path => '/mock2',
52
+ :web_app_dir => File.join(File.dirname(__FILE__), '..', 'web_app_mock')
53
+ }
54
+ }
55
+ })
56
+
57
+ server.tomcat.host.findChildren().should have(2).web_apps
58
+ server.tomcat.host.findChildren().each do |child|
59
+ puts child.getPath()
60
+ end
61
+ end
62
+
63
+ it "loads the default application from the current directory if :web_apps is not present" do
64
+ server = Trinidad::Server.new({:web_app_dir => File.join(File.dirname(__FILE__), '..', 'web_app_mock')})
65
+
66
+ server.tomcat.host.findChildren().should have(1).web_apps
67
+ end
42
68
  end
@@ -4,24 +4,31 @@ describe Trinidad::WebApp do
4
4
  before do
5
5
  @tomcat = Trinidad::Tomcat::Tomcat.new
6
6
  @tomcat_web_app = @tomcat.addWebapp('/', File.dirname(__FILE__) + '/../../')
7
+ @app = {
8
+ :web_app_dir => File.join(File.dirname(__FILE__), '..', 'web_app_mock'),
9
+ :context_path => '/'
10
+ }
7
11
  @config = {
8
12
  :libs_dir => 'lib',
9
13
  :classes_dir => 'classes',
10
14
  :default_web_xml => 'config/web.xml',
11
- :web_app_dir => File.join(File.dirname(__FILE__), '..', 'web_app_mock'),
12
15
  :jruby_min_runtimes => 2,
13
16
  :jruby_max_runtimes => 6,
14
- :context_path => '/'
17
+ :web_apps => {
18
+ :default => @app
19
+ }
15
20
  }
16
- @web_app = Trinidad::RailsWebApp.new(@tomcat_web_app, @config)
21
+ @web_app = Trinidad::RailsWebApp.new(@tomcat_web_app, @config, @app)
17
22
  end
18
23
 
19
24
  it "creates a RailsWebApp if rackup option is not present" do
20
- Trinidad::WebApp.create(@tomcat_web_app, @config).is_a?(Trinidad::RailsWebApp).should be_true
25
+ Trinidad::WebApp.create(@tomcat_web_app, @config, @app).is_a?(Trinidad::RailsWebApp).should be_true
21
26
  end
22
27
 
23
28
  it "creates a RackupWebApp if rackup option is present" do
24
- Trinidad::WebApp.create(@tomcat_web_app, @config.merge(:rackup => 'config.ru')).is_a?(Trinidad::RackupWebApp).should be_true
29
+ rackup_app = {:rackup => 'config.ru'}
30
+ @config.deep_merge({:web_apps => {:default => rackup_app}})
31
+ Trinidad::WebApp.create(@tomcat_web_app, @config, rackup_app).is_a?(Trinidad::RackupWebApp).should be_true
25
32
  end
26
33
 
27
34
  it "should load custom jars" do
metadata CHANGED
@@ -67,15 +67,15 @@ requirements: []
67
67
 
68
68
  authors:
69
69
  - David Calavera
70
- date: 2009-11-01 23:00:00 +00:00
70
+ date: 2009-11-30 23:00:00 +00:00
71
71
  platform: ruby
72
72
  test_files:
73
73
  - spec/spec_helper.rb
74
- - spec/trinidad/command_line_parser_spec.rb
75
74
  - spec/trinidad/server_spec.rb
75
+ - spec/trinidad/command_line_parser_spec.rb
76
76
  - spec/trinidad/web_app_spec.rb
77
77
  version: !ruby/object:Gem::Version
78
- version: 0.6.0
78
+ version: 0.7.0
79
79
  require_paths:
80
80
  - lib
81
81
  dependencies: