1 10 11 package org.mule.providers.jms; 12 13 import javax.jms.Connection ; 14 import javax.jms.ConnectionFactory ; 15 import javax.jms.DeliveryMode ; 16 import javax.jms.Destination ; 17 import javax.jms.JMSException ; 18 import javax.jms.Message ; 19 import javax.jms.MessageConsumer ; 20 import javax.jms.MessageProducer ; 21 import javax.jms.Queue ; 22 import javax.jms.QueueConnection ; 23 import javax.jms.QueueConnectionFactory ; 24 import javax.jms.QueueSender ; 25 import javax.jms.QueueSession ; 26 import javax.jms.Session ; 27 import javax.jms.Topic ; 28 import javax.jms.TopicConnection ; 29 import javax.jms.TopicConnectionFactory ; 30 import javax.jms.TopicPublisher ; 31 import javax.jms.TopicSession ; 32 import javax.naming.Context ; 33 34 38 39 public class Jms102bSupport extends Jms11Support 40 { 41 42 public Jms102bSupport(JmsConnector connector, 43 Context context, 44 boolean jndiDestinations, 45 boolean forceJndiDestinations) 46 { 47 super(connector, context, jndiDestinations, forceJndiDestinations); 48 } 49 50 public Connection createConnection(ConnectionFactory connectionFactory, String username, String password) 51 throws JMSException 52 { 53 if (connectionFactory == null) 54 { 55 throw new IllegalArgumentException ("connectionFactory cannot be null"); 56 } 57 if (connectionFactory instanceof QueueConnectionFactory ) 58 { 59 return ((QueueConnectionFactory ) connectionFactory).createQueueConnection(username, password); 60 } 61 else if (connectionFactory instanceof TopicConnectionFactory ) 62 { 63 return ((TopicConnectionFactory ) connectionFactory).createTopicConnection(username, password); 64 } 65 else 66 { 67 throw new IllegalArgumentException ("Unsupported ConnectionFactory type: " 68 + connectionFactory.getClass().getName()); 69 } 70 } 71 72 public Connection createConnection(ConnectionFactory connectionFactory) throws JMSException 73 { 74 if (connectionFactory == null) 75 { 76 throw new IllegalArgumentException ("connectionFactory cannot be null"); 77 } 78 if (connectionFactory instanceof QueueConnectionFactory ) 79 { 80 return ((QueueConnectionFactory ) connectionFactory).createQueueConnection(); 81 } 82 else if (connectionFactory instanceof TopicConnectionFactory ) 83 { 84 return ((TopicConnectionFactory ) connectionFactory).createTopicConnection(); 85 } 86 else 87 { 88 throw new IllegalArgumentException ("Unsupported ConnectionFactory type: " 89 + connectionFactory.getClass().getName()); 90 } 91 } 92 93 public Session createSession(Connection connection, 94 boolean topic, 95 boolean transacted, 96 int ackMode, 97 boolean noLocal) throws JMSException 98 { 99 if (topic && connection instanceof TopicConnection ) 100 { 101 return ((TopicConnection ) connection).createTopicSession(noLocal, ackMode); 102 } 103 else if (connection instanceof QueueConnection ) 104 { 105 return ((QueueConnection ) connection).createQueueSession(transacted, ackMode); 106 } 107 else 108 { 109 throw new IllegalArgumentException ("Connection and domain type do not match"); 110 } 111 } 112 113 public MessageConsumer createConsumer(Session session, 114 Destination destination, 115 String messageSelector, 116 boolean noLocal, 117 String durableName, 118 boolean topic) throws JMSException 119 { 120 if (topic && session instanceof TopicSession ) 121 { 122 if (durableName == null) 123 { 124 return ((TopicSession ) session).createSubscriber((Topic ) destination, messageSelector, noLocal); 125 } 126 else 127 { 128 return session.createDurableSubscriber((Topic ) destination, durableName, messageSelector, noLocal); 129 } 130 } 131 else if (session instanceof QueueSession ) 132 { 133 if (messageSelector != null) 134 { 135 return ((QueueSession ) session).createReceiver((Queue ) destination, messageSelector); 136 } 137 else 138 { 139 return ((QueueSession ) session).createReceiver((Queue ) destination); 140 } 141 } 142 else 143 { 144 throw new IllegalArgumentException ("Session and domain type do not match"); 145 } 146 } 147 148 public MessageProducer createProducer(Session session, Destination dest, boolean topic) 149 throws JMSException 150 { 151 if (topic && session instanceof TopicSession ) 152 { 153 return ((TopicSession ) session).createPublisher((Topic ) dest); 154 } 155 else if (session instanceof QueueSession ) 156 { 157 return ((QueueSession ) session).createSender((Queue ) dest); 158 } 159 else 160 { 161 throw new IllegalArgumentException ("Session and domain type do not match"); 162 } 163 } 164 165 public Destination createDestination(Session session, String name, boolean topic) throws JMSException 166 { 167 if (session == null) 168 { 169 throw new IllegalArgumentException ("Session cannot be null when creating a destination"); 170 } 171 if (name == null) 172 { 173 throw new IllegalArgumentException ("Destination name cannot be null when creating a destination"); 174 } 175 176 if (jndiDestinations) 177 { 178 if (context == null) 179 { 180 throw new IllegalArgumentException ( 181 "Jndi Context name cannot be null when looking up a destination"); 182 } 183 Destination dest = getJndiDestination(name); 184 if (dest != null) 185 { 186 return dest; 187 } 188 else if (forceJndiDestinations) 189 { 190 throw new JMSException ("JNDI destination not found with name: " + name); 191 } 192 } 193 194 if (topic) 195 { 196 return session.createTopic(name); 197 } 198 else 199 { 200 return session.createQueue(name); 201 } 202 } 203 204 public void send(MessageProducer producer, 205 Message message, 206 boolean persistent, 207 int priority, 208 long ttl, 209 boolean topic) throws JMSException 210 { 211 if (topic && producer instanceof TopicPublisher ) 212 { 213 ((TopicPublisher ) producer).publish(message, (persistent 214 ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT), priority, ttl); 215 } 216 else if (producer instanceof QueueSender ) 217 { 218 producer.send(message, (persistent 219 ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT), priority, ttl); 220 } 221 else 222 { 223 throw new IllegalArgumentException ("Producer and domain type do not match"); 224 } 225 } 226 227 public void send(MessageProducer producer, 228 Message message, 229 Destination dest, 230 boolean persistent, 231 int priority, 232 long ttl, 233 boolean topic) throws JMSException 234 { 235 if (topic && producer instanceof TopicPublisher ) 236 { 237 ((TopicPublisher ) producer).publish((Topic ) dest, message, (persistent 238 ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT), priority, ttl); 239 } 240 else if (producer instanceof QueueSender ) 241 { 242 ((QueueSender ) producer).send((Queue ) dest, message, (persistent 243 ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT), priority, ttl); 244 } 245 else 246 { 247 throw new IllegalArgumentException ("Producer and domain type do not match"); 248 } 249 } 250 251 } 252 | Popular Tags |