summaryrefslogtreecommitdiffstats
path: root/lib/puppet/server/servlet.rb
blob: 70f47a0691a6be8857e466be514abbf7250802a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
require 'xmlrpc/server'

module Puppet
class Server
    class ServletError < RuntimeError; end
    class Servlet < XMLRPC::WEBrickServlet
        attr_accessor :request

        # this is just a duplicate of the normal method; it's here for
        # debugging when i need it
        def self.get_instance(server, *options)
            self.new(server, *options)
        end

        def initialize(server, handlers)
            #Puppet.info server.inspect
            
            # the servlet base class does not consume any arguments
            # and its BasicServer base class only accepts a 'class_delim'
            # option which won't change in Puppet at all
            # thus, we don't need to pass any args to our base class,
            # and we can consume them all ourselves
            super()

            handlers.each { |handler|
                Puppet.debug "adding handler for %s" % handler.class
                self.add_handler(handler.class.interface, handler)
            }

            @request = nil
            self.set_service_hook { |obj, *args|
                #raise "crap!"
                if @request
                    args.push @request
                    #obj.call(args, @request)
                end
                begin
                    obj.call(*args)
                rescue => detail
                    Puppet.warning obj.inspect
                    Puppet.warning args.inspect
                    Puppet.err "Could not call: %s" % detail.to_s
                end
            }
        end

        def service(request, response)
            @request = request
            if @request.client_cert
                Puppet.info "client cert is %s" % @request.client_cert
            end
            if @request.server_cert
                #Puppet.info "server cert is %s" % @request.server_cert
            end
            #p @request
            begin
                super
            rescue => detail
                Puppet.err "Could not service request: %s: %s" %
                    [detail.class, detail]
            end
            @request = nil
        end

        private

        # this is pretty much just a copy of the original method but with more
        # feedback
        def dispatch(methodname, *args)
            #Puppet.warning "dispatch on %s called with %s" %
            #    [methodname, args.inspect]
            for name, obj in @handler
                if obj.kind_of? Proc
                    unless methodname == name
                        Puppet.debug "obj is proc but %s != %s" %
                            [methodname, name]
                        next
                    end
                else
                    unless methodname =~ /^#{name}(.+)$/
                        Puppet.debug "methodname did not match"
                        next
                    end
                    unless obj.respond_to? $1
                        Puppet.debug "methodname does not respond to %s" % $1
                        next
                    end
                    obj = obj.method($1)
                end

                if check_arity(obj, args.size)
                    if @service_hook.nil?
                        return obj.call(*args) 
                    else
                        return @service_hook.call(obj, *args)
                    end
                else
                    Puppet.debug "arity is incorrect"
                end
            end 

            if @default_handler.nil?
                raise XMLRPC::FaultException.new(
                    ERR_METHOD_MISSING,
                    "Method #{methodname} missing or wrong number of parameters!"
                )
            else
                @default_handler.call(methodname, *args) 
            end
        end
    end
end
end