Browse files

Initial commit of Jetty Ruby binding with additional JI support

  • Loading branch information...
0 parents commit 5db5154c8df5a452b310b3542d9de3e903334a00 Yehuda Katz committed Nov 12, 2009
4 .gitignore
@@ -0,0 +1,4 @@
+bin/*
+vendor/gems/*
+!vendor/gems/cache/
+!vendor/gems/dirs/
3 Gemfile
@@ -0,0 +1,3 @@
+gem "rack-mount", :git => "git://github.com/rails/rack-mount.git"
+gem "multimap", :git => "git://github.com/wycats/multimap.git"
+disable_rubygems
92 lib/jetty.rb
@@ -0,0 +1,92 @@
+require "jetty/jruby_ext/packages"
+
+%w(ajp annotations client continuation deploy http
+io jmx jndi plus policy rewrite security server
+servlet servlets util webapp xml).each do |jar|
+ load_jar "jetty/jar/jetty-#{jar}-7.0.0.v20091005"
+end
+
+load_jar "jetty/jar/servlet-api-2.5"
+
+import org.eclipse.jetty.server.handler.AbstractHandler
+import javax.servlet.http.HttpServletResponse
+
+class LazyRackEnv
+ def initialize(request)
+ @request = request
+ end
+
+ def [](key)
+ if key =~ /^HTTP_(.*)/
+ @request.header($1)
+ else
+ send(key)
+ end
+ rescue NoMethodError
+ end
+
+ def REQUEST_METHOD
+ @request.method
+ end
+
+ def SCRIPT_NAME
+ ""
+ end
+
+ def PATH_INFO
+ @request.path_info
+ end
+
+ def QUERY_STRING
+ @request.query_string
+ end
+
+ def SERVER_NAME
+ @request.server_name
+ end
+
+ def SERVER_PORT
+ @request.server_port
+ end
+end
+
+class RackHandler < AbstractHandler
+ attr_accessor :app
+
+ def self.build(endpoint)
+ new.tap {|h| h.app = endpoint }
+ end
+
+ def handle(target, base_request, request, response)
+ lazy_env = LazyRackEnv.new(request)
+ env = Hash.new {|h,k| h[k] = lazy_env[k] }
+
+ status, headers, body = @app.call(env)
+ response.status = status.to_i
+ base_request.handled = true
+
+ headers.each do |header, value|
+ response.set_header header, value
+ end
+
+ writer = response.writer
+ body.each { |part| writer.print part }
+ end
+end
+
+using(org.eclipse.jetty => "Jetty") do
+ # org.eclipse.jetty.server.Server
+ class Server::Server
+ # org.eclipse.jetty.util.thread
+ include Util::Thread
+
+ def self.start_rack(endpoint, options = {})
+ new(options[:port] || 8080).tap do |server|
+ server.thread_pool = QueuedThreadPool.new(20)
+ server.handler = RackHandler.build(endpoint)
+ server.start
+ server.join
+ end
+ end
+ end
+end
BIN lib/jetty/jar/asm-3.1.jar
Binary file not shown.
BIN lib/jetty/jar/asm-commons-3.1.jar
Binary file not shown.
BIN lib/jetty/jar/asm-tree-3.1.jar
Binary file not shown.
BIN lib/jetty/jar/geronimo-annotation_1.0_spec-1.1.1.jar
Binary file not shown.
BIN lib/jetty/jar/jetty-ajp-7.0.0.v20091005.jar
Binary file not shown.
BIN lib/jetty/jar/jetty-annotations-7.0.0.v20091005.jar
Binary file not shown.
BIN lib/jetty/jar/jetty-client-7.0.0.v20091005.jar
Binary file not shown.
BIN lib/jetty/jar/jetty-continuation-7.0.0.v20091005.jar
Binary file not shown.
BIN lib/jetty/jar/jetty-deploy-7.0.0.v20091005.jar
Binary file not shown.
BIN lib/jetty/jar/jetty-http-7.0.0.v20091005.jar
Binary file not shown.
BIN lib/jetty/jar/jetty-io-7.0.0.v20091005.jar
Binary file not shown.
BIN lib/jetty/jar/jetty-jmx-7.0.0.v20091005.jar
Binary file not shown.
BIN lib/jetty/jar/jetty-jndi-7.0.0.v20091005.jar
Binary file not shown.
BIN lib/jetty/jar/jetty-plus-7.0.0.v20091005.jar
Binary file not shown.
BIN lib/jetty/jar/jetty-policy-7.0.0.v20091005.jar
Binary file not shown.
BIN lib/jetty/jar/jetty-rewrite-7.0.0.v20091005.jar
Binary file not shown.
BIN lib/jetty/jar/jetty-security-7.0.0.v20091005.jar
Binary file not shown.
BIN lib/jetty/jar/jetty-server-7.0.0.v20091005.jar
Binary file not shown.
BIN lib/jetty/jar/jetty-servlet-7.0.0.v20091005.jar
Binary file not shown.
BIN lib/jetty/jar/jetty-servlets-7.0.0.v20091005.jar
Binary file not shown.
BIN lib/jetty/jar/jetty-util-7.0.0.v20091005.jar
Binary file not shown.
BIN lib/jetty/jar/jetty-webapp-7.0.0.v20091005.jar
Binary file not shown.
BIN lib/jetty/jar/jetty-xml-7.0.0.v20091005.jar
Binary file not shown.
BIN lib/jetty/jar/servlet-api-2.5.jar
Binary file not shown.
96 lib/jetty/jruby_ext/packages.rb
@@ -0,0 +1,96 @@
+require "java"
+
+module Java
+ class Packages
+ class << self
+ @@all_packages = {}
+
+ def all
+ @@all_packages
+ end
+
+ def add_jar(jar)
+ next unless File.exists?(jar)
+
+ entries = java.util.jar.JarFile.new(java.io.File.new(jar)).entries
+ entries.each do |x|
+ next if x.name !~ /\.class$/ || x.name =~ /\$/
+ chunks = x.name.split(%r{[./\$]})[0..-2]
+ add_package(chunks)
+ end
+ end
+
+ def add_package(parts)
+ current = @@all_packages
+ parts.each do |part|
+ current = (current[part] ||= {})
+ end
+ end
+
+ def children(package)
+ package.split(".").inject(@@all_packages) do |package, part|
+ package[part]
+ end.keys
+ end
+ end
+
+ java.lang.System.getProperty("sun.boot.class.path").split(":").each {|jar| add_jar(jar) }
+ end
+end
+
+module JavaPackageModuleTemplate
+ class << self
+ def included(klass)
+ __import_children_into__(klass)
+ super
+ end
+
+ def __import_children_into__(mod)
+ children = Java::Packages.children(package_name)
+ package = self
+
+ mod.module_eval do
+ children.each do |child|
+ name = child.gsub(/^[a-z]/) {|m| m.upcase }
+ const_set name, package.__send__(child)
+ end
+ end
+ end
+
+ def const_missing(const)
+ children = Java::Packages.children(package_name)
+
+ if child = children.find {|c| c.gsub(/^[a-z]/) {|m| m.upcase } == const.to_s }
+ return __send__(child)
+ end
+ super
+ end
+ private :const_missing
+ end
+end
+
+module Kernel
+ def load_jar(path)
+ unless path =~ %r{^/}
+ $LOAD_PATH.each do |load_path|
+ searched = File.expand_path(File.join(load_path, path))
+ searched.gsub! /(.jar)?$/, '.jar'
+ path = searched if File.exist?(searched)
+ end
+ end
+
+ Java::Packages.add_jar(path)
+ require path
+ end
+
+ def using(hash, &block)
+ m = Module.new do
+ hash.keys.each do |package|
+ package.__import_children_into__(self)
+ end
+ end
+
+ JRuby.reference(block).block.body.static_scope.module = m
+ m.module_eval(&block)
+ end
+end
22 test.rb
@@ -0,0 +1,22 @@
+$:.push File.join(Dir.pwd, "lib")
+require "jetty"
+
+require "#{File.dirname(__FILE__)}/vendor/gems/environment"
+require "rack/mount"
+
+class FooApp
+ def call(env)
+ [200, {"Content-Type" => "text/html"},
+ ["<h1>Hello world from FooApp #{env["rack.routing_args"].inspect}</h1>"]]
+ end
+end
+
+routes = Rack::Mount::RouteSet.new do |set|
+ set.add_route(FooApp.new, :path_info => Rack::Mount::Strexp.new("/:foo"))
+ set.add_route(proc {|env| [200, {}, ["DEFAULT"]]}, :path => "/")
+end
+
+using(org.eclipse.jetty => "Jetty") do
+ # org.eclipse.jetty.server.Server
+ Server::Server.start_rack(routes)
+end
BIN vendor/gems/cache/rack-1.0.1.gem
Binary file not shown.
1 vendor/gems/dirs/multimap
@@ -0,0 +1 @@
+Subproject commit 648e874278ba8148fc7bf760dfafc19dbc35dd17
1 vendor/gems/dirs/rack-mount
@@ -0,0 +1 @@
+Subproject commit 3784e633b42f43a4131e02519be60080d179da21

0 comments on commit 5db5154

Please sign in to comment.