trinidad 1.4.6 → 1.5.0.B1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env jruby
2
2
 
3
- require "trinidad"
3
+ require 'trinidad'
4
4
 
5
- Trinidad::CommandLineParser.parse(ARGV)
6
- Trinidad::Server.new(Trinidad.configuration).start
5
+ Trinidad::CLI.parse(ARGV)
6
+ Trinidad::Server.new.start
@@ -3,17 +3,31 @@ require 'java'
3
3
  require 'jruby-rack'
4
4
  require 'trinidad/jars'
5
5
 
6
- require 'trinidad/version'
7
-
8
- require 'trinidad/helpers'
9
- require 'trinidad/configuration'
10
- require 'trinidad/command_line_parser'
11
- require 'trinidad/extensions'
12
- require 'trinidad/logging'
13
- require 'trinidad/server'
14
- require 'trinidad/web_app'
15
- require 'trinidad/lifecycle/base'
16
- require 'trinidad/lifecycle/host'
17
- require 'trinidad/lifecycle/web_app/shared'
18
- require 'trinidad/lifecycle/web_app/default'
19
- require 'trinidad/lifecycle/web_app/war'
6
+ module Trinidad
7
+
8
+ require 'trinidad/version'
9
+ require 'trinidad/helpers'
10
+ require 'trinidad/configuration'
11
+ require 'trinidad/server'
12
+ require 'trinidad/web_app'
13
+ require 'trinidad/extensions'
14
+ require 'trinidad/logging'
15
+
16
+ autoload :CLI, 'trinidad/cli'
17
+
18
+ module Lifecycle
19
+
20
+ autoload :Base, 'trinidad/lifecycle/base'
21
+ autoload :Host, 'trinidad/lifecycle/host'
22
+
23
+ module WebApp
24
+
25
+ require 'trinidad/lifecycle/web_app/shared'
26
+
27
+ autoload :Default, 'trinidad/lifecycle/web_app/default'
28
+ autoload :War, 'trinidad/lifecycle/web_app/war'
29
+
30
+ end
31
+
32
+ end
33
+ end
@@ -1,12 +1,12 @@
1
1
  module Trinidad
2
- class CommandLineParser
2
+ class CLI
3
3
 
4
- def self.parse(argv)
5
- CommandLineParser.new.parse!(argv)
4
+ def self.parse(argv = ARGV)
5
+ new.parse!(argv)
6
6
  end
7
7
 
8
8
  def self.load(options = {})
9
- CommandLineParser.new.load!(options)
9
+ new.load!(options)
10
10
  end
11
11
 
12
12
  attr_reader :default_options
@@ -94,7 +94,7 @@ module Trinidad
94
94
  default_options[:monitor] = monitor
95
95
  end
96
96
 
97
- opts.on('-t', '--threadsafe', 'force thread-safe mode (use single runtime)') do
97
+ opts.on('-t', '--threadsafe', 'force thread-safe mode (use shared runtime)') do
98
98
  default_options[:jruby_min_runtimes] = 1
99
99
  default_options[:jruby_max_runtimes] = 1
100
100
  end
@@ -112,12 +112,12 @@ module Trinidad
112
112
  end
113
113
 
114
114
  opts.on('--address', '--address ADDRESS', 'host address',
115
- "default: #{default(:address)}") do |address|
115
+ "default: #{default(:address) || '*'}") do |address|
116
116
  default_options[:address] = address
117
117
  end
118
118
 
119
119
  opts.on('-p', '--port PORT', 'port to bind to',
120
- "default: #{default(:port)}") do |port|
120
+ "default: #{default(:port) || 3000}") do |port|
121
121
  default_options[:port] = port
122
122
  end
123
123
 
@@ -126,11 +126,6 @@ module Trinidad
126
126
  default_options[:ssl] = { :port => (port || 3443).to_i }
127
127
  end
128
128
 
129
- opts.on('-a', '--ajp [AJP_PORT]', 'enable the AJP web protocol',
130
- "default port: 8009") do |port|
131
- default_options[:ajp] = { :port => (port || 8009).to_i }
132
- end
133
-
134
129
  opts.on('--java_lib LIB_DIR', '--lib LIB_DIR (deprecated use --java_lib)',
135
130
  'contains .jar files used by the app',
136
131
  "default: #{default(:java_lib)}") do |lib|
@@ -154,19 +149,28 @@ module Trinidad
154
149
  default_options[:apps_base] = apps_base
155
150
  end
156
151
 
157
- opts.on('-g', '--log LEVEL', 'set logging level') do |log|
158
- default_options[:log] = log
159
- end
160
-
161
152
  opts.on('-v', '--version', 'show server version') do
162
153
  puts "Trinidad #{Trinidad::VERSION} (Tomcat #{Trinidad::TOMCAT_VERSION})"
163
154
  exit
164
155
  end
165
156
 
166
- opts.on('-h', '--help', 'display this help') do
167
- puts opts
168
- exit
157
+ opts.on('-h', '--help', 'display this help') { puts opts; exit }
158
+
159
+ opts.on_tail; opts.on_tail 'DEPRECATED options:'
160
+
161
+ opts.on_tail('-a', '--ajp [AJP_PORT]', 'enable the AJP web protocol',
162
+ "default port: 8009") do |port|
163
+ Helpers.deprecated "option ( -a / --ajp )"
164
+ default_options[:ajp] = { :port => (port || 8009).to_i }
165
+ end
166
+
167
+
168
+ opts.on_tail('-g', '--log LEVEL', 'set logging level') do |log|
169
+ Helpers.deprecated "option ( -g / --log )"
170
+ default_options[:log] = log
169
171
  end
172
+
173
+ opts.on_tail
170
174
  end
171
175
  end
172
176
 
@@ -182,4 +186,6 @@ module Trinidad
182
186
  end
183
187
 
184
188
  end
185
- end
189
+ # @private
190
+ CommandLineParser = CLI
191
+ end
@@ -33,18 +33,11 @@ module Trinidad
33
33
  class Configuration
34
34
 
35
35
  DEFAULTS = {
36
- # :port => 3000, HTTP (depends on connector used)
37
- :address => 'localhost',
38
36
  :environment => 'development',
39
37
  :context_path => '/',
40
38
  :public => 'public',
41
39
  :java_lib => 'lib/java',
42
40
  :default_web_xml => 'config/web.xml',
43
- :jruby_min_runtimes =>
44
- java.lang.System.getProperty('jruby.min.runtimes') || 1,
45
- :jruby_max_runtimes =>
46
- java.lang.System.getProperty('jruby.max.runtimes') || 5,
47
- :log => 'INFO',
48
41
  :trap => true
49
42
  }
50
43
 
@@ -81,7 +74,7 @@ module Trinidad
81
74
  end
82
75
 
83
76
  %w{ port address environment context_path
84
- java_lib libs_dir java_classes classes_dir default_web_xml
77
+ java_lib java_classes default_web_xml
85
78
  jruby_min_runtimes jruby_max_runtimes jruby_compat_version
86
79
  rackup servlet rack_servlet default_servlet public hosts
87
80
  http ajp ssl https extensions
@@ -90,46 +83,20 @@ module Trinidad
90
83
  class_eval "def #{method}; self[:'#{method}']; end"
91
84
  class_eval "def #{method}=(value); self[:'#{method}'] = value; end"
92
85
  end
93
- # TODO #deprecate libs_dir classes_dir servlet
86
+ # TODO deprecate servlet
94
87
 
95
- # a Hash like #symbolize helper
96
- def self.symbolize_options(options, deep = true)
97
- new_options = options.class.new
98
- options.each do |key, value|
99
- if deep && value.is_a?(Array) # YAML::Omap is an Array
100
- array = new_options[key.to_sym] = value.class.new
101
- value.each do |v|
102
- array << ( options_like?(v) ? symbolize_options(v, deep) : v )
103
- end
104
- elsif deep && options_like?(value)
105
- new_options[key.to_sym] = symbolize_options(value, deep)
106
- else
107
- new_options[key.to_sym] = value
108
- end
109
- end
110
- new_options
88
+ # @private
89
+ def self.symbolize_options(options)
90
+ Helpers.symbolize(options, true)
111
91
  end
112
92
 
113
- # a Hash like deep_merge helper
114
- def self.merge_options(target, current, deep = true)
115
- return target unless current
116
- target_dup = target.dup
117
- current.keys.each do |key|
118
- target_dup[key] =
119
- if deep && options_like?(target[key]) && options_like?(current[key])
120
- merge_options(target[key], current[key], deep)
121
- else
122
- current[key]
123
- end
124
- end
125
- target_dup
93
+ # @private
94
+ def self.merge_options(target, current)
95
+ Helpers.merge(target, current, true)
126
96
  end
127
97
 
128
- private
129
- def self.options_like?(object)
130
- object.is_a?(Hash) ||
131
- ( object.respond_to?(:keys) && object.respond_to?(:'[]') )
132
- end
98
+ # @private
99
+ def self.options_like?(object); Helpers.hash_like?(object) end
133
100
 
134
101
  end
135
102
  end
@@ -8,7 +8,7 @@ module Trinidad
8
8
  end
9
9
  end if extensions
10
10
  end
11
-
11
+
12
12
  def self.configure_server_extensions(extensions, tomcat)
13
13
  extensions.each do |name, options|
14
14
  if extension = extension(name, 'ServerExtension', options)
@@ -22,20 +22,22 @@ module Trinidad
22
22
  end
23
23
 
24
24
  def self.configure_webapp_extensions(extensions, tomcat, context)
25
- extensions.each do |name, options|
25
+ extensions.map do |name, options|
26
+ next if options == false # allow to explicitly disable
26
27
  if extension = extension(name, 'WebAppExtension', options)
27
28
  extension.tomcat = tomcat
28
29
  if extension.method(:configure).arity == 2
29
- extension.configure(tomcat, context) # #deprecated old way
30
+ extension.configure(tomcat, context) # @deprecated old way
30
31
  else
31
32
  extension.configure(context)
32
33
  end
34
+ extension
33
35
  end
34
36
  end if extensions
35
37
  end
36
-
38
+
37
39
  protected
38
-
40
+
39
41
  def self.extension(name, type, options)
40
42
  class_name = Helpers.camelize(name) << type; clazz = nil
41
43
  if ( const_defined?(class_name) rescue nil )
@@ -57,63 +59,63 @@ module Trinidad
57
59
  end
58
60
 
59
61
  private
60
-
62
+
61
63
  def self.tomcat_like?(tomcat)
62
64
  tomcat.respond_to?(:server) && tomcat.respond_to?(:start) && tomcat.respond_to?(:stop)
63
65
  end
64
-
66
+
65
67
  def self.camelize(string)
66
- Helpers.deprecate("Trinidad::Extensions.camelize use the camelize helper " <<
68
+ Helpers.deprecate("Trinidad::Extensions.camelize use the camelize helper " <<
67
69
  "available in your Extension")
68
70
  Helpers.camelize(string)
69
71
  end
70
-
72
+
71
73
  class Extension
72
74
  include Helpers; extend Helpers
73
-
75
+
74
76
  attr_reader :options
75
-
77
+
76
78
  def initialize(options = {})
77
79
  @options = options ? options.dup : {}
78
80
  end
79
-
81
+
80
82
  private
81
-
83
+
82
84
  # Hash#symbolize
83
85
  def symbolize(options, deep = false)
84
86
  Trinidad::Configuration.symbolize_options(options, deep)
85
87
  end
86
-
88
+
87
89
  end
88
90
 
89
91
  class WebAppExtension < Extension
90
-
92
+
91
93
  attr_accessor :tomcat
92
-
94
+
93
95
  def configure(context)
94
96
  raise NotImplementedError, "#{self.class.name}#configure(context) not implemented"
95
97
  end
96
-
98
+
97
99
  end
98
100
 
99
101
  class ServerExtension < Extension
100
-
102
+
101
103
  def configure(tomcat)
102
104
  raise NotImplementedError, "#{self.class.name}#configure(tomcat) not implemented"
103
105
  end
104
-
106
+
105
107
  # #deprecated override tomcat by returning it from #configure
106
108
  def override_tomcat?; false; end
107
-
109
+
108
110
  end
109
111
 
110
112
  class OptionsExtension < Extension
111
-
113
+
112
114
  def configure(parser, default_options)
113
115
  raise NotImplementedError, "#{self.class.name}#configure(parser, default_options) not implemented"
114
116
  end
115
-
117
+
116
118
  end
117
-
119
+
118
120
  end
119
121
  end
@@ -1,34 +1,89 @@
1
1
  module Trinidad
2
2
  module Helpers
3
3
 
4
- @@silence = nil # :nodoc
4
+ # @private
5
+ @@silence = nil
5
6
  # Should we be silent - no warnings will be printed.
6
7
  def self.silence?; @@silence; end
7
8
  # Silence ! (... or I kill you)
8
9
  def self.silence!; @@silence = true; end
9
10
 
10
- # Print a warning (Kernel.warn).
11
+ # Print a warning (using `Kernel.warn`).
11
12
  def self.warn(msg)
12
13
  super unless silence? # Kernel.warn
13
14
  end
14
-
15
+
15
16
  module_function
16
17
 
17
- @@deprecated = {} # :nodoc
18
-
18
+ # @private
19
+ @@deprecated = {}
20
+
19
21
  # Print a deprecated message (once - no matter how many times it's called).
20
- def deprecate(msg, prefix = '[DEPRECATED] ')
22
+ def deprecated(msg, prefix = '[DEPRECATED] ')
21
23
  return nil if @@deprecated[msg]
22
24
  @@deprecated[msg] = true
23
25
  Helpers.warn "#{prefix}#{msg}" # Kernel.warn
24
26
  end
25
-
27
+ # @private
28
+ def deprecate(msg); deprecated(msg) end
29
+
30
+ # Camelizes the passed (string) parameter.
31
+ # @return a new string
26
32
  def camelize(string)
27
33
  string = string.to_s.sub(/^[a-z\d]*/) { $&.capitalize }
28
34
  string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }
29
35
  string.gsub!('/', '::')
30
36
  string
31
37
  end
32
-
38
+
39
+ # a Hash like `symbolize` helper
40
+ def symbolize(hash, deep = true)
41
+ new_options = hash.class.new
42
+ hash.each do |key, value|
43
+ if deep && value.is_a?(Array) # YAML::Omap is an Array
44
+ array = new_options[key.to_sym] = value.class.new
45
+ value.each do |v|
46
+ array << ( hash_like?(v) ? symbolize(v, deep) : v )
47
+ end
48
+ elsif deep && hash_like?(value)
49
+ new_options[key.to_sym] = symbolize(value, deep)
50
+ else
51
+ new_options[key.to_sym] = value
52
+ end
53
+ end
54
+ new_options
55
+ end
56
+
57
+ # a Hash like `deep_merge` helper
58
+ def merge(target, current, deep = true)
59
+ return target unless current
60
+ target_dup = target.dup
61
+ current.keys.each do |key|
62
+ target_dup[key] =
63
+ if deep && hash_like?(target[key]) && hash_like?(current[key])
64
+ merge(target[key], current[key], deep)
65
+ else
66
+ current[key]
67
+ end
68
+ end
69
+ target_dup
70
+ end
71
+
72
+ def hash_like?(object)
73
+ object.is_a?(Hash) || ( object.respond_to?(:keys) && object.respond_to?(:'[]') )
74
+ end
75
+
76
+ def to_url(path)
77
+ path = java.net.URLDecoder.decode(path)
78
+ if path =~ /^\w*\:/ # might be a valid url: (or C:/windows/yay ... )
79
+ return java.net.URL.new("file:/#{path}") if path[1, 1] == ':'
80
+ begin
81
+ return java.net.URL.new(path)
82
+ rescue java.net.MalformedURLException
83
+ end
84
+ end
85
+ java.io.File.new(path).toURL
86
+ end
87
+
33
88
  end
34
89
  end