kirk 0.1.8-java → 0.2.0.beta.1-java

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.
Files changed (37) hide show
  1. data/lib/kirk.rb +24 -26
  2. data/lib/kirk/cli.rb +1 -1
  3. data/lib/kirk/client.rb +57 -18
  4. data/lib/kirk/client/exchange.rb +146 -26
  5. data/lib/kirk/client/group.rb +94 -0
  6. data/lib/kirk/client/request.rb +42 -6
  7. data/lib/kirk/client/response.rb +20 -4
  8. data/lib/kirk/jetty.rb +22 -14
  9. data/lib/kirk/jetty/{jetty-client-7.2.2.v20101205.jar → jetty-client-7.3.0.v20110203.jar} +0 -0
  10. data/lib/kirk/jetty/jetty-continuation-7.3.0.v20110203.jar +0 -0
  11. data/lib/kirk/jetty/jetty-http-7.3.0.v20110203.jar +0 -0
  12. data/lib/kirk/jetty/jetty-io-7.3.0.v20110203.jar +0 -0
  13. data/lib/kirk/jetty/jetty-server-7.3.0.v20110203.jar +0 -0
  14. data/lib/kirk/jetty/jetty-util-7.3.0.v20110203.jar +0 -0
  15. data/lib/kirk/native.jar +0 -0
  16. data/lib/kirk/native.rb +12 -0
  17. data/lib/kirk/server.rb +11 -1
  18. data/lib/kirk/server/application_config.rb +56 -0
  19. data/lib/kirk/server/bootstrap.rb +59 -0
  20. data/lib/kirk/server/builder.rb +145 -0
  21. data/lib/kirk/{applications → server}/deploy_watcher.rb +1 -1
  22. data/lib/kirk/server/handler.rb +140 -0
  23. data/lib/kirk/server/hot_deployable.rb +63 -0
  24. data/lib/kirk/server/input_stream.rb +114 -0
  25. data/lib/kirk/{applications → server}/redeploy_client.rb +1 -1
  26. data/lib/kirk/version.rb +1 -1
  27. metadata +19 -16
  28. data/lib/kirk/applications/config.rb +0 -50
  29. data/lib/kirk/applications/hot_deployable.rb +0 -65
  30. data/lib/kirk/applications/rack.rb +0 -21
  31. data/lib/kirk/bootstrap.rb +0 -59
  32. data/lib/kirk/builder.rb +0 -143
  33. data/lib/kirk/client/connection.rb +0 -18
  34. data/lib/kirk/client/session.rb +0 -40
  35. data/lib/kirk/handler.rb +0 -129
  36. data/lib/kirk/input_stream.rb +0 -264
  37. data/lib/kirk/jetty/jetty-server-7.2.2.v20101205.jar +0 -0
data/lib/kirk/builder.rb DELETED
@@ -1,143 +0,0 @@
1
- module Kirk
2
- class MissingConfigFile < RuntimeError ; end
3
- class MissingRackupFile < RuntimeError ; end
4
-
5
- class Builder
6
-
7
- VALID_LOG_LEVELS = %w(severe warning info config fine finer finest all)
8
-
9
- attr_reader :options
10
-
11
- def initialize(root = nil)
12
- @root = root || Dir.pwd
13
- @current = nil
14
- @configs = []
15
- @options = {
16
- :watcher => Applications::DeployWatcher.new("/tmp/kirk.sock")
17
- }
18
- end
19
-
20
- def load(glob)
21
- glob = expand_path(glob)
22
-
23
- files = Dir[glob].select { |f| File.file?(f) }
24
-
25
- if files.empty?
26
- raise MissingConfigFile, "glob `#{glob}` did not match any files"
27
- end
28
-
29
- files.each do |file|
30
- with_root File.dirname(file) do
31
- instance_eval(File.read(file), file)
32
- end
33
- end
34
- end
35
-
36
- def log(opts = {})
37
- level = opts[:level]
38
- raise "Invalid log level" unless VALID_LOG_LEVELS.include?(level.to_s)
39
- @options[:log_level] = level.to_s
40
- end
41
-
42
- def rack(rackup)
43
- rackup = expand_path(rackup)
44
-
45
- unless File.exist?(rackup)
46
- raise MissingRackupFile, "rackup file `#{rackup}` does not exist"
47
- end
48
-
49
- @current = new_config
50
- @current.rackup = rackup
51
-
52
- yield if block_given?
53
-
54
- ensure
55
- @configs << @current
56
- @current = nil
57
- end
58
-
59
- def env(env)
60
- @current.env.merge!(env)
61
- end
62
-
63
- def hosts(*hosts)
64
- @current.hosts.concat hosts
65
- end
66
-
67
- def listen(listen)
68
- listen = listen.to_s
69
- listen = ":#{listen}" unless listen.index(':')
70
- listen = "0.0.0.0#{listen}" if listen.index(':') == 0
71
-
72
- @current.listen = listen
73
- end
74
-
75
- def watch(*watch)
76
- @current.watch = watch
77
- end
78
-
79
- def to_handler
80
- handlers = @configs.map do |c|
81
- Jetty::ContextHandler.new.tap do |ctx|
82
- # Set the virtual hosts
83
- unless c.hosts.empty?
84
- ctx.set_virtual_hosts(c.hosts)
85
- end
86
-
87
- application = Applications::HotDeployable.new(c)
88
- application.add_watcher(watcher)
89
-
90
- ctx.set_connector_names [c.listen]
91
- ctx.set_handler application
92
- end
93
- end
94
-
95
- Jetty::ContextHandlerCollection.new.tap do |collection|
96
- collection.set_handlers(handlers)
97
- end
98
- end
99
-
100
- def to_connectors
101
- @connectors = {}
102
-
103
- @configs.each do |config|
104
- next if @connectors.key?(config.listen)
105
-
106
- host, port = config.listen.split(':')
107
-
108
- connector = Jetty::SelectChannelConnector.new
109
- connector.set_host(host)
110
- connector.set_port(port.to_i)
111
-
112
- @connectors[config.listen] = connector
113
- end
114
-
115
- @connectors.values
116
- end
117
-
118
- private
119
-
120
- def watcher
121
- @options[:watcher]
122
- end
123
-
124
- def with_root(root)
125
- old, @root = @root, root
126
- yield
127
- ensure
128
- @root = old
129
- end
130
-
131
- def expand_path(path)
132
- File.expand_path(path, @root)
133
- end
134
-
135
- def new_config
136
- Applications::Config.new.tap do |config|
137
- config.listen = '0.0.0.0:9090'
138
- config.watch = [ ]
139
- config.bootstrap_path = File.expand_path('../bootstrap.rb', __FILE__)
140
- end
141
- end
142
- end
143
- end
@@ -1,18 +0,0 @@
1
- class Kirk::Client
2
- class Connection
3
- attr_reader :request
4
-
5
- def initialize
6
- @request = nil
7
- @writing = false
8
- @client = HttpClient.new
9
- @client.set_connector_type(HttpClient::CONNECTOR_SELECT_CHANNEL);
10
- @client.start
11
- end
12
-
13
- def process(request)
14
- exchange = Exchange.from_request(request)
15
- @client.send(exchange)
16
- end
17
- end
18
- end
@@ -1,40 +0,0 @@
1
- class Kirk::Client
2
- class Session
3
-
4
- attr_reader :responses, :queue
5
-
6
- def initialize
7
- @queue = LinkedBlockingQueue.new
8
- @executor = ThreadPoolExecutor.new(thread_count, thread_count, 0, TimeUnit::SECONDS, @queue)
9
- @connection = Connection.new
10
- @requests_count = 0
11
- @responses = []
12
- yield(self)
13
-
14
- get_responses
15
- end
16
-
17
- def request(method, url, handler = nil, headers = nil)
18
- request = Request.new(self, method, url, handler, headers)
19
- yield request if block_given?
20
- queue_request(request)
21
- request
22
- end
23
-
24
- def thread_count
25
- 3
26
- end
27
-
28
- def queue_request(request)
29
- @connection.process(request)
30
- @requests_count += 1
31
- end
32
-
33
- def get_responses
34
- while @requests_count > 0
35
- @responses << @queue.take
36
- @requests_count -= 1
37
- end
38
- end
39
- end
40
- end
data/lib/kirk/handler.rb DELETED
@@ -1,129 +0,0 @@
1
- module Kirk
2
- class Handler
3
- import "java.util.zip.GZIPInputStream"
4
- import "java.util.zip.InflaterInputStream"
5
-
6
- # Required environment variable keys
7
- REQUEST_METHOD = 'REQUEST_METHOD'.freeze
8
- SCRIPT_NAME = 'SCRIPT_NAME'.freeze
9
- PATH_INFO = 'PATH_INFO'.freeze
10
- QUERY_STRING = 'QUERY_STRING'.freeze
11
- SERVER_NAME = 'SERVER_NAME'.freeze
12
- SERVER_PORT = 'SERVER_PORT'.freeze
13
- CONTENT_TYPE = 'CONTENT_TYPE'.freeze
14
- CONTENT_LENGTH = 'CONTENT_LENGTH'.freeze
15
- REQUEST_URI = 'REQUEST_URI'.freeze
16
- REMOTE_HOST = 'REMOTE_HOST'.freeze
17
- REMOTE_ADDR = 'REMOTE_ADDR'.freeze
18
- REMOTE_USER = 'REMOTE_USER'.freeze
19
-
20
- # Rack specific variable keys
21
- RACK_VERSION = 'rack.version'.freeze
22
- RACK_URL_SCHEME = 'rack.url_scheme'.freeze
23
- RACK_INPUT = 'rack.input'.freeze
24
- RACK_ERRORS = 'rack.errors'.freeze
25
- RACK_MULTITHREAD = 'rack.multithread'.freeze
26
- RACK_MULTIPROCESS = 'rack.multiprocess'.freeze
27
- RACK_RUN_ONCE = 'rack.run_once'.freeze
28
- HTTP_PREFIX = 'HTTP_'.freeze
29
-
30
- # Rack response header names
31
- CONTENT_TYPE_RESP = 'Content-Type'
32
- CONTENT_LENGTH_RESP = 'Content-Length'
33
-
34
- # Kirk information
35
- SERVER = "#{NAME} #{VERSION}".freeze
36
- SERVER_SOFTWARE = 'SERVER_SOFTWARE'.freeze
37
-
38
- DEFAULT_RACK_ENV = {
39
- SERVER_SOFTWARE => SERVER,
40
-
41
- # Rack stuff
42
- RACK_ERRORS => STDERR,
43
- RACK_MULTITHREAD => true,
44
- RACK_MULTIPROCESS => false,
45
- RACK_RUN_ONCE => false,
46
- }
47
-
48
- CONTENT_LENGTH_TYPE_REGEXP = /^Content-(?:Type|Length)$/i
49
-
50
- def initialize(app)
51
- @app = app
52
- end
53
-
54
- def handle(target, base_request, request, response)
55
- begin
56
- env = DEFAULT_RACK_ENV.merge(
57
- REQUEST_URI => request.getRequestURI,
58
- PATH_INFO => request.get_path_info,
59
- REQUEST_METHOD => request.get_method || "GET",
60
- RACK_URL_SCHEME => request.get_scheme || "http",
61
- QUERY_STRING => request.get_query_string || "",
62
- SERVER_NAME => request.get_server_name || "",
63
- REMOTE_HOST => request.get_remote_host || "",
64
- REMOTE_ADDR => request.get_remote_addr || "",
65
- REMOTE_USER => request.get_remote_user || "",
66
- SERVER_PORT => request.get_server_port.to_s,
67
- RACK_VERSION => ::Rack::VERSION)
68
-
69
- # Process the content length
70
- if (content_length = request.get_content_length) >= 0
71
- env[CONTENT_LENGTH] = content_length.to_s
72
- else
73
- env[CONTENT_LENGTH] = "0"
74
- end
75
-
76
- if (content_type = request.get_content_type)
77
- env[CONTENT_TYPE] = content_type unless content_type == ''
78
- end
79
-
80
- request.get_header_names.each do |header|
81
- next if header =~ CONTENT_LENGTH_TYPE_REGEXP
82
- value = request.get_header(header)
83
-
84
- header.tr! '-', '_'
85
- header.upcase!
86
-
87
- header = "#{HTTP_PREFIX}#{header}"
88
- env[header] = value unless env.key?(header) || value == ''
89
- end
90
-
91
- input = request.get_input_stream
92
-
93
- case env['HTTP_CONTENT_ENCODING']
94
- when 'deflate' then input = InflaterInputStream.new(input)
95
- when 'gzip' then input = GZIPInputStream.new(input)
96
- end
97
-
98
- input = InputStream.new(input)
99
- env[RACK_INPUT] = input
100
-
101
- # Dispatch the request
102
- status, headers, body = @app.call(env)
103
-
104
- response.set_status(status.to_i)
105
-
106
- headers.each do |header, value|
107
- case header
108
- when CONTENT_TYPE_RESP
109
- response.set_content_type(value)
110
- when CONTENT_LENGTH_RESP
111
- response.set_content_length(value.to_i)
112
- else
113
- response.set_header(header, value)
114
- end
115
- end
116
-
117
- buffer = response.get_output_stream
118
- body.each do |s|
119
- buffer.write(s.to_java_bytes)
120
- end
121
-
122
- body.close if body.respond_to?(:close)
123
- ensure
124
- input.recycle if input
125
- request.set_handled(true)
126
- end
127
- end
128
- end
129
- end
@@ -1,264 +0,0 @@
1
- require 'java'
2
-
3
- module Kirk
4
- class InputStream
5
- READL_SIZE = 1_024
6
- CHUNK_SIZE = 8_192
7
- BUFFER_SIZE = 1_024 * 50
8
-
9
- import "java.io.File"
10
- import "java.io.RandomAccessFile"
11
- import "java.nio.ByteBuffer"
12
- import "java.nio.channels.Channels"
13
-
14
- # For our ROFL scale needs
15
- import "java.util.concurrent.LinkedBlockingQueue"
16
-
17
- BUFFER_POOL = LinkedBlockingQueue.new
18
- TMPFILE_PREFIX = "rackinput".to_java_string
19
- TMPFILE_SUFFIX = "".to_java_string
20
-
21
- def initialize(io)
22
- @io = channelize(io)
23
- @buffer = grab_buffer
24
- @filebuf = nil
25
- @read = 0
26
- @position = 0
27
- @eof = false
28
- end
29
-
30
- def read(size = nil, buffer = nil)
31
- one_loop = nil
32
- read_all = size.nil?
33
-
34
- buffer ? buffer.slice!(0..-1) : buffer = ''
35
-
36
- raise ArgumentError, "negative length #{size} given" if size && size < 0
37
-
38
- loop do
39
- limit = size && size < CHUNK_SIZE ? size : CHUNK_SIZE
40
-
41
- len = read_chunk(limit, buffer)
42
-
43
- break unless len
44
-
45
- one_loop = true
46
-
47
- break if size && ( size -= len ) <= 0
48
- end
49
-
50
- return "" if read_all && !one_loop
51
-
52
- one_loop && buffer
53
- end
54
-
55
- def gets(sep = $/)
56
- return read unless sep
57
-
58
- sep = "#{$/}#{$/}" if sep == ""
59
- buffer = ''
60
- curpos = pos
61
-
62
- while chunk = read(READL_SIZE)
63
- buffer << chunk
64
-
65
- if i = buffer.index(sep, 0)
66
- i += sep.bytesize
67
- buffer.slice!(i..-1)
68
- seek(curpos + buffer.bytesize)
69
- break
70
- end
71
- end
72
-
73
- buffer
74
- end
75
-
76
- def each
77
- while chunk = read(CHUNK_SIZE)
78
- yield chunk
79
- end
80
-
81
- self
82
- end
83
-
84
- def pos
85
- @position
86
- end
87
-
88
- def seek(pos)
89
- raise Errno::EINVAL, "Invalid argument - invalid seek value" if pos < 0
90
- @position = pos
91
- end
92
-
93
- def rewind
94
- seek(0)
95
- end
96
-
97
- def close
98
- nil
99
- end
100
-
101
- def recycle
102
- BUFFER_POOL.put(@buffer)
103
- @buffer = nil
104
- nil
105
- end
106
-
107
- private
108
-
109
- def channelize(stream)
110
- Channels.new_channel(stream)
111
- end
112
-
113
- def grab_buffer
114
- BUFFER_POOL.poll || ByteBuffer.allocate(BUFFER_SIZE)
115
- end
116
-
117
- def read_chunk(size, string)
118
- missing = size - ( @read - @position )
119
-
120
- if @filebuf || @read + missing > BUFFER_SIZE
121
-
122
- rotate_to_tmp_file unless @filebuf
123
- read_chunk_from_tmp_file(size, string)
124
-
125
- else
126
-
127
- read_chunk_from_mem(size, string, missing)
128
-
129
- end
130
- end
131
-
132
- def read_chunk_from_mem(size, string, missing)
133
- # We gonna have to read from the input stream
134
- if missing > 0 && !@eof
135
-
136
- # Set the new buffer limit to the amount that is going
137
- # to be read
138
- @buffer.limit(@read + missing).position(@read)
139
-
140
- # Read into the buffer
141
- len = @io.read(@buffer)
142
-
143
- # Bail if we're at the EOF
144
- if len == -1
145
- @eof = true
146
- return
147
- end
148
-
149
- @read += len
150
-
151
- # We're at the end
152
- elsif @position == @read
153
-
154
- return
155
-
156
- end
157
-
158
- limit = @position + size
159
- limit = @read if @read < limit
160
-
161
- # Now move the amount read into the string
162
- @buffer.position(@position).limit(limit)
163
-
164
- append_buffer_to_string(@buffer, string)
165
- end
166
-
167
- def read_chunk_from_tmp_file(size, string)
168
-
169
- if @read < @position && !@eof
170
-
171
- return unless buffer_to(@position)
172
-
173
- end
174
-
175
- @buffer.clear.limit(size)
176
-
177
- if @read > @position
178
-
179
- @filebuf.position(@position)
180
- @filebuf.read(@buffer)
181
-
182
- elsif @eof
183
-
184
- return
185
-
186
- end
187
-
188
- unless @eof
189
-
190
- offset = @buffer.position
191
- len = @io.read(@buffer)
192
-
193
- if len == -1
194
-
195
- @eof = true
196
- return if offset == 0
197
-
198
- else
199
-
200
- @filebuf.position(@read)
201
- @filebuf.write(@buffer.flip.position(offset))
202
-
203
- @read += len
204
-
205
- end
206
-
207
- @buffer.position(0)
208
-
209
- end
210
-
211
- append_buffer_to_string(@buffer, string)
212
- end
213
-
214
- def buffer_to(position)
215
- until @read == position
216
- limit = position - @read
217
- limit = BUFFER_SIZE if limit > BUFFER_SIZE
218
-
219
- @buffer.clear.limit(limit)
220
-
221
- len = @io.read(@buffer)
222
-
223
- if len == -1
224
- @eof = true
225
- return
226
- end
227
-
228
- @buffer.flip
229
-
230
- @filebuf.position(@read)
231
- @filebuf.write(@buffer)
232
-
233
- @read += len
234
- end
235
-
236
- true
237
- end
238
-
239
- def append_buffer_to_string(buffer, string)
240
- len = buffer.limit - buffer.position
241
- bytes = Java::byte[len].new
242
- buffer.get(bytes)
243
- string << String.from_java_bytes(bytes)
244
- @position += len
245
- len
246
- end
247
-
248
- def rotate_to_tmp_file
249
- @buffer.position(0).limit(@read)
250
-
251
- @filebuf = create_tmp_file
252
- @filebuf.write @buffer
253
-
254
- @buffer.clear
255
- end
256
-
257
- def create_tmp_file
258
- file = File.create_temp_file(TMPFILE_PREFIX, TMPFILE_SUFFIX)
259
- file.delete_on_exit
260
-
261
- RandomAccessFile.new(file, "rw").channel
262
- end
263
- end
264
- end