Skip to content

Instantly share code, notes, and snippets.

@ribrdb
Created May 3, 2010 17:43
Show Gist options
  • Select an option

  • Save ribrdb/388356 to your computer and use it in GitHub Desktop.

Select an option

Save ribrdb/388356 to your computer and use it in GitHub Desktop.
import org.jruby.rack.DefaultRackApplicationFactory
import org.jruby.rack.RackApplicationFactory
import org.jruby.rack.RackInitializationException
import org.jruby.rack.RackServletContextListener
import org.jruby.rack.SharedRackApplicationFactory
class AppEngineRackApplicationFactory < DefaultRackApplicationFactory
def initialize
end
def createApplicationObject(runtime)
createRackServletWrapper(
runtime, "eval(IO.read('config.ru'), nil, 'config.ru', 1)")
end
def createRackServletWrapper(runtime, rackup)
runtime.evalScriptlet(<<-EOF)
require 'appengine-rack/boot'
load 'jruby/rack/boot/rack.rb'
Rack::Handler::Servlet.new(Rack::Builder.new {( #{rackup}
)}.to_app)
EOF
end
end
class LazyApplicationFactory; implements RackApplicationFactory
def initialize
end
def init(context)
@context = context
end
def real_factory
throws RackInitializationException
if @factory.nil?
@factory = SharedRackApplicationFactory.new(
AppEngineRackApplicationFactory.new)
@factory.init(@context)
end
@factory
end
def newApplication
throws RackInitializationException
real_factory.newApplication
end
def getApplication
throws RackInitializationException
real_factory.getApplication
end
def finishedWithApplication(app)
@factory.finishedWithApplication(app) unless @factory.nil?
end
def getErrorApplication
real_factory.getErrorApplication rescue nil
end
def destroy
@factory.destroy unless @factory.nil?
end
end
class LazyContextListener < RackServletContextListener
def newApplicationFactory(context)
if context.getServerInfo.contains("Development")
RackApplicationFactory(SharedRackApplicationFactory.new(
RackApplicationFactory(AppEngineRackApplicationFactory.new)))
else
RackApplicationFactory(LazyApplicationFactory.new)
end
end
end
require 'jruby/rack/booter'
# try to require the bundled environment
begin
require 'bundler_gems/environment'
rescue LoadError
# continue
end
module JRuby
module Rack
class AppEngineLayout < WebInfLayout
def app_uri
@app_uri ||= '/'
end
def public_uri
@public_uri ||= begin
path = @rack_context.getInitParameter('public.root') || '/public'
path = "/#{path}" unless path =~ %r{^/}
path.chomp("/")
end
end
def change_working_directory
if @rack_context.server_info.include?('Development')
ENV['RACK_ENV'] = 'development'
else
ENV['RACK_ENV'] = 'production'
end
super
end
end
class Booter
def default_layout_class
AppEngineLayout
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment