abstract.rb 4.7 KB
Newer Older
L
Leon Breedt 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
require 'benchmark'

module ActionWebService # :nodoc:
  module Dispatcher # :nodoc:
    class DispatcherError < ActionWebService::ActionWebServiceError # :nodoc:
    end

    def self.append_features(base) # :nodoc:
      super
      base.class_inheritable_option(:web_service_dispatching_mode, :direct)
      base.class_inheritable_option(:web_service_exception_reporting, true)
      base.send(:include, ActionWebService::Dispatcher::InstanceMethods)
    end

15 16 17 18 19 20 21 22
    def self.layered_service_name(public_method_name) # :nodoc:
      if public_method_name =~ /^([^\.]+)\.(.*)$/
        $1
      else
        nil
      end
    end

L
Leon Breedt 已提交
23 24
    module InstanceMethods # :nodoc:
      private
25 26
        def invoke_web_service_request(protocol_request)
          invocation = web_service_invocation(protocol_request)
L
Leon Breedt 已提交
27 28
          case web_service_dispatching_mode
          when :direct
29
            web_service_direct_invoke(invocation)
30
          when :delegated, :layered
31
            web_service_delegated_invoke(invocation)
L
Leon Breedt 已提交
32 33
          end
        end
34 35 36
      
        def web_service_direct_invoke(invocation)
          @method_params = invocation.method_ordered_params
37
          arity = method(invocation.api_method.name).arity rescue 0
38
          if arity < 0 || arity > 0
39
            return_value = self.__send__(invocation.api_method.name, *@method_params)
40
          else
41
            return_value = self.__send__(invocation.api_method.name)
42
          end
43 44
          if invocation.api.has_api_method?(invocation.api_method.name)
            api_method = invocation.api_method
45
          else
46 47
            api_method = invocation.api_method.dup
            api_method.instance_eval{ @returns = [ return_value.class ] }
48
          end
49
          invocation.protocol.marshal_response(api_method, return_value)
L
Leon Breedt 已提交
50 51
        end

52
        def web_service_delegated_invoke(invocation)
L
Leon Breedt 已提交
53
          cancellation_reason = nil
54
          return_value = invocation.service.perform_invocation(invocation.api_method.name, invocation.method_ordered_params) do |x|
L
Leon Breedt 已提交
55 56 57 58 59
            cancellation_reason = x
          end
          if cancellation_reason
            raise(DispatcherError, "request canceled: #{cancellation_reason}")
          end
60
          invocation.protocol.marshal_response(invocation.api_method, return_value)
L
Leon Breedt 已提交
61 62
        end

63
        def web_service_invocation(request)
64
          public_method_name = request.method_name
65 66 67
          invocation = Invocation.new
          invocation.protocol = request.protocol
          invocation.service_name = request.service_name
68 69 70 71 72 73
          if web_service_dispatching_mode == :layered
            if request.method_name =~ /^([^\.]+)\.(.*)$/
              public_method_name = $2
              invocation.service_name = $1
            end
          end
L
Leon Breedt 已提交
74 75
          case web_service_dispatching_mode
          when :direct
76 77
            invocation.api = self.class.web_service_api
            invocation.service = self
78
          when :delegated, :layered
79
            invocation.service = web_service_object(invocation.service_name)
80
            invocation.api = invocation.service.class.web_service_api
L
Leon Breedt 已提交
81
          end
82
          request.api = invocation.api
83
          if invocation.api.has_public_api_method?(public_method_name)
84
            invocation.api_method = invocation.api.public_api_method_instance(public_method_name)
85 86 87 88
          else
            if invocation.api.default_api_method.nil?
              raise(DispatcherError, "no such method '#{public_method_name}' on API #{invocation.api}")
            else
89
              invocation.api_method = invocation.api.default_api_method_instance
90 91
            end
          end
92 93
          unless invocation.service.respond_to?(invocation.api_method.name)
              raise(DispatcherError, "no such method '#{public_method_name}' on API #{invocation.api} (#{invocation.api_method.name})")
94
          end
95 96 97 98 99
          request.api_method = invocation.api_method
          begin
            invocation.method_ordered_params = invocation.api_method.cast_expects_ws2ruby(request.protocol.marshaler, request.method_params)
          rescue
            invocation.method_ordered_params = request.method_params.map{ |x| x.value }
L
Leon Breedt 已提交
100
          end
101 102 103 104
          invocation.method_named_params = {}
          invocation.api_method.param_names.inject(0) do |m, n|
            invocation.method_named_params[n] = invocation.method_ordered_params[m]
            m + 1
105
          end
106
          invocation
L
Leon Breedt 已提交
107 108
        end

109
        class Invocation # :nodoc:
110 111 112
          attr_accessor :protocol
          attr_accessor :service_name
          attr_accessor :api
113
          attr_accessor :api_method
114 115 116
          attr_accessor :method_ordered_params
          attr_accessor :method_named_params
          attr_accessor :service
L
Leon Breedt 已提交
117 118 119 120
        end
    end
  end
end