|
| 1 | +""" |
| 2 | + Copyright 2006-2008 SpringSource (http://springsource.com), All Rights Reserved |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +""" |
| 16 | + |
| 17 | +# stdlib |
| 18 | +import locale |
| 19 | +from string import Template |
| 20 | +from cStringIO import StringIO |
| 21 | + |
| 22 | +# Spring Python |
| 23 | +from springpython.jms import JMSException |
| 24 | +from springpython.jms import DEFAULT_DELIVERY_MODE |
| 25 | + |
| 26 | +__all__ = ["MessageConverter", "JmsTemplate", "TextMessage"] |
| 27 | + |
| 28 | +# These attributes have special meaning to connection factories. |
| 29 | +reserved_attributes = set(("text", "jms_correlation_id", "jms_delivery_mode", |
| 30 | + "jms_destination", "jms_expiration", "jms_message_id", "jms_priority", |
| 31 | + "jms_redelivered", "jms_reply_to", "jms_timestamp", "max_chars_printed", |
| 32 | + "JMS_IBM_Report_Exception", "JMS_IBM_Report_Expiration", "JMS_IBM_Report_COA", |
| 33 | + "JMS_IBM_Report_COD", "JMS_IBM_Report_PAN", "JMS_IBM_Report_NAN", |
| 34 | + "JMS_IBM_Report_Pass_Msg_ID", "JMS_IBM_Report_Pass_Correl_ID", |
| 35 | + "JMS_IBM_Report_Discard_Msg", "JMSXGroupID", "JMSXGroupSeq", "JMS_IBM_Feedback", |
| 36 | + "JMS_IBM_Last_Msg_In_Group", "JMSXUserID", "JMS_IBM_PutTime", "JMS_IBM_PutDate", |
| 37 | + "JMSXAppID")) |
| 38 | + |
| 39 | +# Magic methods are also forbidden. |
| 40 | +reserved_attributes.update(set(dir(object) + ["__weakref__", "__dict__", "__module__"])) |
| 41 | + |
| 42 | + |
| 43 | +text_message_template = """ |
| 44 | +JMS message class: jms_text |
| 45 | + jms_delivery_mode: $jms_delivery_mode |
| 46 | + jms_expiration: $jms_expiration |
| 47 | + jms_priority: $jms_priority |
| 48 | + jms_message_id: $jms_message_id |
| 49 | + jms_timestamp: $jms_timestamp |
| 50 | + jms_correlation_id: $jms_correlation_id |
| 51 | + jms_destination: $jms_destination |
| 52 | + jms_reply_to: $jms_reply_to |
| 53 | + jms_redelivered: $jms_redelivered |
| 54 | +""".lstrip() |
| 55 | + |
| 56 | +class MessageConverter(object): |
| 57 | + def to_message(self, object_to_be_converted_to_a_message): |
| 58 | + raise NotImplementedError("Should be implemented by subclasses") |
| 59 | + |
| 60 | + def from_message(self, message_to_be_converted_to_an_object): |
| 61 | + raise NotImplementedError("Should be implemented by subclasses") |
| 62 | + |
| 63 | +class JmsTemplate(object): |
| 64 | + def __init__(self, factory=None, delivery_persistent=None, |
| 65 | + priority=None, time_to_live=None, message_converter=None, |
| 66 | + default_destination=None): |
| 67 | + |
| 68 | + self.factory = factory |
| 69 | + |
| 70 | + # QoS |
| 71 | + self.delivery_persistent = delivery_persistent |
| 72 | + self.priority = priority |
| 73 | + self.time_to_live = time_to_live |
| 74 | + |
| 75 | + self.message_converter = message_converter |
| 76 | + self.default_destination = default_destination |
| 77 | + |
| 78 | + def convert_and_send(self, object_, destination=None): |
| 79 | + if not self.message_converter: |
| 80 | + raise JMSException("Couldn't send the message, no message converter set") |
| 81 | + |
| 82 | + self.send(self.message_converter.to_message(object_), destination) |
| 83 | + |
| 84 | + def send(self, message, destination=None): |
| 85 | + if isinstance(message, basestring): |
| 86 | + message = TextMessage(message) |
| 87 | + |
| 88 | + if destination: |
| 89 | + dest = destination |
| 90 | + elif self.default_destination: |
| 91 | + dest = self.default_destination |
| 92 | + else: |
| 93 | + raise JMSException("No destination given and no default destination set") |
| 94 | + |
| 95 | + message.jms_destination = dest |
| 96 | + |
| 97 | + self.factory.send(message, dest) |
| 98 | + |
| 99 | + def receive(self, destination=None, timeout=1000): |
| 100 | + |
| 101 | + if destination: |
| 102 | + dest = destination |
| 103 | + elif self.default_destination: |
| 104 | + dest = self.default_destination |
| 105 | + else: |
| 106 | + raise JMSException("No destination given and no default destination set") |
| 107 | + |
| 108 | + return self.factory.receive(dest, timeout) |
| 109 | + |
| 110 | + def receive_and_convert(self, destination=None, timeout=1000): |
| 111 | + if not self.message_converter: |
| 112 | + raise JMSException("Couldn't receive a message, no message converter set") |
| 113 | + |
| 114 | + return self.message_converter.from_message(self.receive(destination, timeout)) |
| 115 | + |
| 116 | + def open_dynamic_queue(self): |
| 117 | + return self.factory.open_dynamic_queue() |
| 118 | + |
| 119 | + def close_dynamic_queue(self, dynamic_queue_name): |
| 120 | + self.factory.close_dynamic_queue(dynamic_queue_name) |
| 121 | + |
| 122 | +class TextMessage(object): |
| 123 | + def __init__(self, text=None, jms_correlation_id=None, jms_delivery_mode=None, |
| 124 | + jms_destination=None, jms_expiration=None, jms_message_id=None, |
| 125 | + jms_priority=None, jms_redelivered=None, jms_reply_to=None, |
| 126 | + jms_timestamp=None, max_chars_printed=100): |
| 127 | + |
| 128 | + self.text = text |
| 129 | + self.jms_correlation_id = jms_correlation_id |
| 130 | + |
| 131 | + self.jms_delivery_mode = jms_delivery_mode or DEFAULT_DELIVERY_MODE |
| 132 | + |
| 133 | + self.jms_destination = jms_destination |
| 134 | + self.jms_expiration = jms_expiration |
| 135 | + self.jms_message_id = jms_message_id |
| 136 | + self.jms_priority = jms_priority |
| 137 | + self.jms_redelivered = jms_redelivered |
| 138 | + self.jms_reply_to = jms_reply_to |
| 139 | + self.jms_timestamp = jms_timestamp |
| 140 | + self.max_chars_printed = max_chars_printed |
| 141 | + |
| 142 | + def __str__(self): |
| 143 | + basic_data = { |
| 144 | + "jms_delivery_mode": self.jms_delivery_mode, |
| 145 | + "jms_expiration":self.jms_expiration, |
| 146 | + "jms_priority":self.jms_priority, |
| 147 | + "jms_message_id":self.jms_message_id, |
| 148 | + "jms_timestamp":self.jms_timestamp, |
| 149 | + "jms_correlation_id":self.jms_correlation_id, |
| 150 | + "jms_destination":self.jms_destination, |
| 151 | + "jms_reply_to":self.jms_reply_to, |
| 152 | + "jms_redelivered":self.jms_redelivered, |
| 153 | + } |
| 154 | + |
| 155 | + buff = StringIO() |
| 156 | + buff.write(Template(text_message_template).safe_substitute(basic_data)) |
| 157 | + |
| 158 | + user_attrs = set(dir(self)) - reserved_attributes |
| 159 | + user_attrs = list(user_attrs) |
| 160 | + user_attrs.sort() |
| 161 | + |
| 162 | + if user_attrs: |
| 163 | + for user_attr in user_attrs: |
| 164 | + user_attr_value = getattr(self, user_attr) |
| 165 | + |
| 166 | + if isinstance(user_attr_value, unicode): |
| 167 | + user_attr_value = user_attr_value.encode("utf-8") |
| 168 | + |
| 169 | + buff.write(" %s:%s\n" % (user_attr, user_attr_value)) |
| 170 | + |
| 171 | + if self.text != None: |
| 172 | + text_to_show = self.text[:self.max_chars_printed] |
| 173 | + |
| 174 | + if isinstance(text_to_show, unicode): |
| 175 | + text_to_show = text_to_show.encode("utf-8") |
| 176 | + |
| 177 | + buff.write(text_to_show) |
| 178 | + |
| 179 | + if len(text_to_show) < len(self.text): |
| 180 | + omitted = locale.format("%d", (len(self.text) - len(text_to_show)), True) |
| 181 | + buff.write("\nAnother ") |
| 182 | + buff.write(omitted) |
| 183 | + buff.write(" character(s) omitted") |
| 184 | + else: |
| 185 | + buff.write("<None>") |
| 186 | + |
| 187 | + value = buff.getvalue() |
| 188 | + buff.close() |
| 189 | + |
| 190 | + return value |
0 commit comments