mirror of
https://github.com/puma/puma.git
synced 2022-11-09 13:48:40 -05:00
Implemented the basic plugin system for Mongrel to replace pluginfactory with something more manageable (and cool).
git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@64 19e92222-5c0b-0410-8929-a290d50e31e9
This commit is contained in:
parent
658f730c92
commit
4e5132f63a
5 changed files with 149 additions and 2 deletions
|
@ -4,12 +4,29 @@ require 'thread'
|
|||
require 'stringio'
|
||||
require 'mongrel/cgi'
|
||||
require 'mongrel/handlers'
|
||||
require 'mongrel/plugins'
|
||||
|
||||
|
||||
# Mongrel module containing all of the classes (include C extensions) for running
|
||||
# a Mongrel web server. It contains a minimalist HTTP server with just enough
|
||||
# functionality to service web application requests fast as possible.
|
||||
module Mongrel
|
||||
|
||||
class URIClassifier
|
||||
# Returns the URIs that have been registered with this classifier so far.
|
||||
# The URIs returned should not be modified as this will cause a memory leak.
|
||||
# You can use this to inspect the contents of the URIClassifier.
|
||||
def uris
|
||||
@handler_map.keys
|
||||
end
|
||||
|
||||
# Simply does an inspect that looks like a Hash inspect.
|
||||
def inspect
|
||||
@handler_map.inspect
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# Used to stop the HttpServer via Thread.raise.
|
||||
class StopServer < Exception
|
||||
end
|
||||
|
@ -441,7 +458,8 @@ module Mongrel
|
|||
# off the request queue before finally exiting.
|
||||
def stop
|
||||
@acceptor[:stopped] = true
|
||||
@acceptor.raise(StopServer.new)
|
||||
exc = StopServer.new
|
||||
@acceptor.raise(exc)
|
||||
end
|
||||
|
||||
end
|
||||
|
|
74
lib/mongrel/plugins.rb
Normal file
74
lib/mongrel/plugins.rb
Normal file
|
@ -0,0 +1,74 @@
|
|||
require 'singleton'
|
||||
|
||||
module Mongrel
|
||||
class PluginManager
|
||||
include Singleton
|
||||
|
||||
def initialize
|
||||
@plugins = URIClassifier.new
|
||||
end
|
||||
|
||||
def load(path)
|
||||
Dir.chdir(path) do
|
||||
Dir["**/*.rb"].each do |rbfile|
|
||||
require rbfile
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def register(category, name, klass)
|
||||
cat, ignored, map = @plugins.resolve(category)
|
||||
if not cat
|
||||
map = {name => klass}
|
||||
@plugins.register(category, map)
|
||||
else
|
||||
map[name] = klass
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def create(name, options = {})
|
||||
category, plugin, map = @plugins.resolve(name)
|
||||
if category and plugin and plugin.length > 0
|
||||
STDERR.puts "found: #{category} #{plugin} for #{name}"
|
||||
map[plugin].new(options)
|
||||
else
|
||||
raise "Plugin #{name} does not exist"
|
||||
end
|
||||
end
|
||||
|
||||
def available
|
||||
map = {}
|
||||
@plugins.uris.each do |u|
|
||||
cat, name, plugins = @plugins.resolve(u)
|
||||
map[cat] ||= []
|
||||
map[cat] += plugins.keys
|
||||
end
|
||||
|
||||
return map
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
class PluginBase
|
||||
|
||||
def PluginBase.inherited(klass)
|
||||
|
||||
PluginManager.instance.register(@@category, klass.to_s.downcase, klass)
|
||||
end
|
||||
|
||||
def PluginBase.category=(category)
|
||||
@@category = category
|
||||
end
|
||||
end
|
||||
|
||||
def Plugin(c)
|
||||
PluginBase.category = c
|
||||
PluginBase
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
21
test/plugins/commands/test1.rb
Normal file
21
test/plugins/commands/test1.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
|
||||
include Mongrel
|
||||
|
||||
class First < Plugin "/commands"
|
||||
def initialize(options = {})
|
||||
puts "First with options: #{options.inspect}"
|
||||
end
|
||||
end
|
||||
|
||||
class Second < Plugin "/commands"
|
||||
def initialize(options = {})
|
||||
puts "Second with options: #{options.inspect}"
|
||||
end
|
||||
end
|
||||
|
||||
class Last < Plugin "/commands"
|
||||
def initialize(options = {})
|
||||
puts "Last with options: #{options.inspect}"
|
||||
end
|
||||
end
|
||||
|
30
test/test_plugins.rb
Normal file
30
test/test_plugins.rb
Normal file
|
@ -0,0 +1,30 @@
|
|||
require 'test/unit'
|
||||
require 'mongrel'
|
||||
|
||||
|
||||
include Mongrel
|
||||
|
||||
class PluginTest < Test::Unit::TestCase
|
||||
|
||||
def setup
|
||||
@pmgr = PluginManager.instance
|
||||
@categories = ["/commands"]
|
||||
@names = ["FirstCommand", "SecondCommand", "LastCommands"]
|
||||
end
|
||||
|
||||
def test_load_plugins
|
||||
@pmgr.load(File.join(File.dirname(__FILE__),"plugins"))
|
||||
puts "#{@pmgr.available.inspect}"
|
||||
@pmgr.available.each {|cat,plugins|
|
||||
plugins.each do |p|
|
||||
puts "TEST: #{cat}/#{p}"
|
||||
assert @names.include?(p)
|
||||
end
|
||||
}
|
||||
|
||||
@pmgr.available.each do |name|
|
||||
plugin = @pmgr.create(name, options={"name" => name})
|
||||
end
|
||||
end
|
||||
|
||||
end
|
|
@ -30,7 +30,9 @@ class URIClassifierTest < Test::Unit::TestCase
|
|||
assert val != nil, "didn't resolve"
|
||||
assert_equal prefix,sn, "wrong script name"
|
||||
assert_equal test[sn.length .. -1],pi, "wrong path info"
|
||||
|
||||
|
||||
assert u.inspect != nil, "No inspect for classifier"
|
||||
assert u.uris[0] == prefix, "URI list didn't match"
|
||||
end
|
||||
|
||||
def test_not_finding
|
||||
|
@ -172,5 +174,7 @@ class URIClassifierTest < Test::Unit::TestCase
|
|||
assert_equal root,sn, "didn't get right script name"
|
||||
assert_equal 1,h, "didn't find handler"
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue