~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/actionpack/lib/action_controller/reloader.rb

  • Committer: Richard Lee (Canonical)
  • Date: 2010-10-15 15:17:58 UTC
  • mfrom: (190.1.3 use-case-mapper)
  • Revision ID: richard.lee@canonical.com-20101015151758-wcvmfxrexsongf9d
Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
require 'thread'
2
 
 
3
 
module ActionController
4
 
  class Reloader
5
 
    @@default_lock = Mutex.new
6
 
    cattr_accessor :default_lock
7
 
 
8
 
    class BodyWrapper
9
 
      def initialize(body, lock)
10
 
        @body = body
11
 
        @lock = lock
12
 
      end
13
 
 
14
 
      def close
15
 
        @body.close if @body.respond_to?(:close)
16
 
      ensure
17
 
        Dispatcher.cleanup_application
18
 
        @lock.unlock
19
 
      end
20
 
 
21
 
      def method_missing(*args, &block)
22
 
        @body.send(*args, &block)
23
 
      end
24
 
 
25
 
      def respond_to?(symbol, include_private = false)
26
 
        symbol == :close || @body.respond_to?(symbol, include_private)
27
 
      end
28
 
    end
29
 
 
30
 
    def self.run(lock = @@default_lock)
31
 
      lock.lock
32
 
      begin
33
 
        Dispatcher.reload_application
34
 
        status, headers, body = yield
35
 
        # We do not want to call 'cleanup_application' in an ensure block
36
 
        # because the returned Rack response body may lazily generate its data. This
37
 
        # is for example the case if one calls
38
 
        #
39
 
        #   render :text => lambda { ... code here which refers to application models ... }
40
 
        #
41
 
        # in an ActionController.
42
 
        #
43
 
        # Instead, we will want to cleanup the application code after the request is
44
 
        # completely finished. So we wrap the body in a BodyWrapper class so that
45
 
        # when the Rack handler calls #close during the end of the request, we get to
46
 
        # run our cleanup code.
47
 
        [status, headers, BodyWrapper.new(body, lock)]
48
 
      rescue Exception
49
 
        lock.unlock
50
 
        raise
51
 
      end
52
 
    end
53
 
  end
54
 
end