about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorzedshaw <zedshaw@19e92222-5c0b-0410-8929-a290d50e31e9>2006-03-02 03:54:32 +0000
committerzedshaw <zedshaw@19e92222-5c0b-0410-8929-a290d50e31e9>2006-03-02 03:54:32 +0000
commit5c13c5dd9d92c60223829881b9835ee9fb02f400 (patch)
tree908a64c658caa2ceddac28ae692b5692d081b26a
parent58723a54f16e73b6d92a201200f1972283be9b74 (diff)
downloadunicorn-5c13c5dd9d92c60223829881b9835ee9fb02f400.tar.gz
git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@67 19e92222-5c0b-0410-8929-a290d50e31e9
-rw-r--r--bin/mongrel_rails33
-rw-r--r--bin/mongrel_rails_service8
-rw-r--r--lib/mongrel/command.rb9
-rw-r--r--lib/mongrel/plugins.rb16
-rw-r--r--test/test_plugins.rb14
5 files changed, 56 insertions, 24 deletions
diff --git a/bin/mongrel_rails b/bin/mongrel_rails
index c9051fe..c78895d 100644
--- a/bin/mongrel_rails
+++ b/bin/mongrel_rails
@@ -3,21 +3,22 @@ require 'mongrel/rails'
 
 
 class Start < Mongrel::Plugin "/commands"
-  include Mongrel::Command::Command
+  include Mongrel::Command::Base
 
   def configure
     options [
-             ["-e", "--environment ENV", "Rails environment to run as", :@environment, ENV['RAILS_ENV'] || "development"],
-             ["-d", "--daemonize", "Whether to run in the background or not", :@daemon, false],
-             ['-p', '--port PORT', "Which port to bind to", :@port, 3000],
-             ['-a', '--address ADDR', "Address to bind to", :@address, "0.0.0.0"],
-             ['-l', '--log FILE', "Where to write log messages", :@log_file, "log/mongrel.log"],
-             ['-P', '--pid FILE', "Where to write the PID", :@pid_file, "log/mongrel.pid"],
-             ['-n', '--num-procs INT', "Number of processor threads to use", :@num_procs, 20],
-             ['-t', '--timeout SECONDS', "Timeout all requests after SECONDS time", :@timeout, 120],
-             ['-m', '--mime PATH', "A YAML file that lists additional MIME types", :@mime_map, nil],
-             ['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, Dir.pwd],
-             ['-r', '--root PATH', "Set the document root (default 'public')", :@docroot, "public"],
+      ["-e", "--environment ENV", "Rails environment to run as", :@environment, ENV['RAILS_ENV'] || "development"],
+      ["-d", "--daemonize", "Whether to run in the background or not", :@daemon, false],
+      ['-p', '--port PORT', "Which port to bind to", :@port, 3000],
+      ['-a', '--address ADDR', "Address to bind to", :@address, "0.0.0.0"],
+      ['-l', '--log FILE', "Where to write log messages", :@log_file, "log/mongrel.log"],
+      ['-P', '--pid FILE', "Where to write the PID", :@pid_file, "log/mongrel.pid"],
+      ['-n', '--num-procs INT', "Number of processor threads to use", :@num_procs, 20],
+      ['-t', '--timeout SECONDS', "Timeout all requests after SECONDS time", :@timeout, 120],
+      ['-m', '--mime PATH', "A YAML file that lists additional MIME types", :@mime_map, nil],
+      ['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, Dir.pwd],
+      ['-r', '--root PATH', "Set the document root (default 'public')", :@docroot, "public"],
+      ['-L', '--load PATH', "Loads plugins from the given directory", :@load_from, nil]
     ]
   end
   
@@ -33,9 +34,12 @@ class Start < Mongrel::Plugin "/commands"
     valid_dir? @docroot, "Path to docroot not valid: #@docroot"
     valid_exists? @mime_map, "MIME mapping file does not exist: #@mime_map" if @mime_map
 
+    valid_dir? @load_from, "Plugin directory path does not exist" if @load_from
+
     return @valid
   end
 
+
   def daemonize
     # save this for later since daemonize will hose it
     if @daemon and RUBY_PLATFORM !~ /mswin/
@@ -120,6 +124,7 @@ class Start < Mongrel::Plugin "/commands"
   end
 
   def run
+    Mongrel::PluginManager.instance.load(@load_from) if @load_from
     daemonize
     rails = configure_rails
     start_mongrel(rails)
@@ -141,7 +146,7 @@ end
 
 
 class Stop < Mongrel::Plugin "/commands"
-  include Mongrel::Command::Command
+  include Mongrel::Command::Base
 
   def configure
     options [
@@ -176,7 +181,7 @@ end
 
 
 class Restart < Mongrel::Plugin "/commands"
-    include Mongrel::Command::Command
+  include Mongrel::Command::Base
 
   def configure
     options [
diff --git a/bin/mongrel_rails_service b/bin/mongrel_rails_service
index 8609890..a82546e 100644
--- a/bin/mongrel_rails_service
+++ b/bin/mongrel_rails_service
@@ -31,7 +31,7 @@ module GenericCommand
 end
 
 class Install < Mongrel::Plugin "/commands"
-  include Mongrel::Command::Command
+  include Mongrel::Command::Base
 
   # Default every option to nil so only the defined ones get passed to service
   # (which will override ServiceCommand defaults).
@@ -175,7 +175,7 @@ class Install < Mongrel::Plugin "/commands"
 end
 
 class Delete < Mongrel::Plugin "/commands"
-  include Mongrel::Command::Command
+  include Mongrel::Command::Base
   include GenericCommand
 
   def run
@@ -194,7 +194,7 @@ class Delete < Mongrel::Plugin "/commands"
 end
 
 class Start < Mongrel::Plugin "/commands"
-  include Mongrel::Command::Command
+  include Mongrel::Command::Base
   include GenericCommand
   
   def run
@@ -219,7 +219,7 @@ class Start < Mongrel::Plugin "/commands"
 end
 
 class Stop < Mongrel::Plugin "/commands"
-  include Mongrel::Command::Command
+  include Mongrel::Command::Base
   include GenericCommand
   
   def run
diff --git a/lib/mongrel/command.rb b/lib/mongrel/command.rb
index 04af386..db54e1d 100644
--- a/lib/mongrel/command.rb
+++ b/lib/mongrel/command.rb
@@ -12,7 +12,7 @@ module Mongrel
     # A Command pattern implementation used to create the set of command available to the user
     # from Mongrel.  The script uses objects which implement this interface to do the
     # user's bidding.
-    module Command
+    module Base
       
       attr_reader :valid, :done_validating
 
@@ -31,7 +31,8 @@ module Mongrel
 
       # Called by the subclass to setup the command and parse the argv arguments.
       # The call is destructive on argv since it uses the OptionParser#parse! function.
-      def initialize(argv)
+      def initialize(options={})
+        argv = options[:argv]
         @opt = OptionParser.new
         @valid = true
         # this is retarded, but it has to be done this way because -h and -v exit
@@ -54,7 +55,7 @@ module Mongrel
           end
         end
         
-        @opt.parse! argv
+        @opt.parse! options[:argv]
       end
       
       # Returns true/false depending on whether the command is configured properly.
@@ -143,7 +144,7 @@ module Mongrel
         
         # command exists, set it up and validate it
         begin
-          command = PluginManager.instance.create("/commands/#{cmd_name}", args)
+          command = PluginManager.instance.create("/commands/#{cmd_name}", :argv => args)
         rescue
           STDERR.puts "INVALID COMMAND: #$!"
           print_command_list
diff --git a/lib/mongrel/plugins.rb b/lib/mongrel/plugins.rb
index f312e36..208b5e3 100644
--- a/lib/mongrel/plugins.rb
+++ b/lib/mongrel/plugins.rb
@@ -55,6 +55,7 @@ module Mongrel
     def load(path)
       Dir.chdir(path) do
         Dir["**/*.rb"].each do |rbfile|
+          STDERR.puts "Loading plugins from #{rbfile}"
           require rbfile
         end
       end
@@ -65,9 +66,12 @@ module Mongrel
     # plugin to a category.
     def register(category, name, klass)
       cat, ignored, map = @plugins.resolve(category)
-      if not cat
+      
+      if not cat or ignored.length > 0
         map = {name => klass}
         @plugins.register(category, map)
+      elsif not map
+        raise "Unknown category #{category}"
       else
         map[name] = klass
       end
@@ -79,6 +83,7 @@ module Mongrel
     # is fast.
     def create(name, options = {})
       category, plugin, map = @plugins.resolve(name)
+      STDERR.puts "found: #{category} #{plugin} #{map.inspect} #{map[plugin].inspect}"
       if category and plugin and plugin.length > 0
         map[plugin].new(options)
       else
@@ -106,16 +111,25 @@ module Mongrel
   # It is not thread-safe yet but will be soon.
   class PluginBase
     
+    attr_reader :options
+
+
     # See Mongrel::Plugin for an explanation.
     def PluginBase.inherited(klass)
       name = "/" + klass.to_s.downcase
       PluginManager.instance.register(@@category, name, klass)
+      @@category = nil
     end
     
     # See Mongrel::Plugin for an explanation.
     def PluginBase.category=(category)
       @@category = category
     end
+
+    def initialize(options = {})
+      @options = options
+    end
+
   end
   
   # This nifty function works with the PluginBase to give you
diff --git a/test/test_plugins.rb b/test/test_plugins.rb
index 0700e50..fc54202 100644
--- a/test/test_plugins.rb
+++ b/test/test_plugins.rb
@@ -1,6 +1,9 @@
 require 'test/unit'
 require 'mongrel'
 
+class ATestPlugin < Mongrel::Plugin "/stuff"
+end
+
 
 include Mongrel
 
@@ -29,5 +32,14 @@ class PluginTest < Test::Unit::TestCase
       end
     end
   end
-  
+
+  def test_similar_uris
+
+    @pmgr.register("/test", "/testme", ATestPlugin)
+    @pmgr.register("/test2", "/testme", ATestPlugin)
+
+    assert_equal @pmgr.create("/test/testme").class, ATestPlugin
+    assert_equal @pmgr.create("/test2/testme").class, ATestPlugin
+
+  end
 end