| 1 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 2 |
# or more contributor license agreements. See the NOTICE file |
| 3 |
# distributed with this work for additional information |
| 4 |
# regarding copyright ownership. The ASF licenses this file |
| 5 |
# to you under the Apache License, Version 2.0 (the |
| 6 |
# "License"); you may not use this file except in compliance |
| 7 |
# with the License. You may obtain a copy of the License at |
| 8 |
# |
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
# |
| 11 |
# Unless required by applicable law or agreed to in writing, |
| 12 |
# software distributed under the License is distributed on an |
| 13 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 |
# KIND, either express or implied. See the License for the |
| 15 |
# specific language governing permissions and limitations |
| 16 |
# under the License. |
| 17 |
|
| 18 |
|
| 19 |
# @private |
| 20 |
module Qpid::Proton |
| 21 |
module Handler |
| 22 |
|
| 23 |
def self.handler_method?(method) /^on_/ =~ name; end |
| 24 |
|
| 25 |
# Handler for an array of handlers of uniform type, with non-conflicting options |
| 26 |
class ArrayHandler |
| 27 |
|
| 28 |
def initialize(handlers) |
| 29 |
raise "empty handler array" if handlers.empty? |
| 30 |
adapters = (handlers.map { |h| Adapter.adapter(h) }).uniq |
| 31 |
raise "handler array not uniform, adapters requested: #{adapters}" if adapters.size > 1 |
| 32 |
@proton_adapter_class = adapters[0] |
| 33 |
@methods = Set.new |
| 34 |
handlers.each do |h| |
| 35 |
@methods.merge(h.methods.select { |m| handler_method? m }) # Collect handler methods |
| 36 |
end |
| 37 |
end |
| 38 |
|
| 39 |
attr_reader :options, :proton_adapter_class |
| 40 |
|
| 41 |
def method_missing(name, *args) |
| 42 |
if respond_to_missing?(name) |
| 43 |
@adapters.each { |a| a.__send__(name, *args) if a.respond_to? name} |
| 44 |
else |
| 45 |
super |
| 46 |
end |
| 47 |
end |
| 48 |
|
| 49 |
def respond_to_missing?(name, private=false); @methods.include?(name); end |
| 50 |
def respond_to?(name, all=false) super || respond_to_missing?(name); end # For ruby < 1.9.2 |
| 51 |
end |
| 52 |
|
| 53 |
# Base adapter for raw proton events |
| 54 |
class Adapter |
| 55 |
def initialize(h) @handler = h; end |
| 56 |
|
| 57 |
# Create and return an adapter for handler, or return h if it does not need adapting. |
| 58 |
def self.adapt(handler) |
| 59 |
return unless handler |
| 60 |
a = Array(handler) |
| 61 |
h = (a.size == 1) ? a[0] : ArrayHandler.new(a) |
| 62 |
ac = adapter(h) |
| 63 |
ac ? ac.new(h) : h |
| 64 |
end |
| 65 |
|
| 66 |
# Adapter class if requested by handler, else default to MessagingHandler |
| 67 |
def self.adapter(handler) |
| 68 |
handler.respond_to?(:proton_adapter_class) ? handler.proton_adapter_class : MessagingAdapter |
| 69 |
end |
| 70 |
|
| 71 |
def proton_adapter_class() nil; end # Adapters don't need adapting |
| 72 |
|
| 73 |
def forward(method, *args) |
| 74 |
(@handler.__send__(method, *args); true) if @handler.respond_to? method |
| 75 |
end |
| 76 |
end |
| 77 |
end |
| 78 |
end |