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.Session ; 22 import javax.jms.Topic ; 23 import javax.naming.Context ; 24 import javax.naming.NamingException ; 25 26 30 31 public class Jms11Support implements JmsSupport 32 { 33 protected Context context; 34 protected boolean jndiDestinations = false; 35 protected boolean forceJndiDestinations = false; 36 protected JmsConnector connector; 37 38 public Jms11Support(JmsConnector connector, 39 Context context, 40 boolean jndiDestinations, 41 boolean forceJndiDestinations) 42 { 43 this.connector = connector; 44 this.context = context; 45 this.jndiDestinations = jndiDestinations; 46 this.forceJndiDestinations = forceJndiDestinations; 47 } 48 49 public Connection createConnection(ConnectionFactory connectionFactory, String username, String password) 50 throws JMSException 51 { 52 if (connectionFactory == null) 53 { 54 throw new IllegalArgumentException ("connectionFactory cannot be null"); 55 } 56 return connectionFactory.createConnection(username, password); 57 } 58 59 public Connection createConnection(ConnectionFactory connectionFactory) throws JMSException 60 { 61 if (connectionFactory == null) 62 { 63 throw new IllegalArgumentException ("connectionFactory cannot be null"); 64 } 65 return connectionFactory.createConnection(); 66 } 67 68 public Session createSession(Connection connection, 69 boolean topic, 70 boolean transacted, 71 int ackMode, 72 boolean noLocal) throws JMSException 73 { 74 return connection.createSession(transacted, (transacted ? Session.SESSION_TRANSACTED : ackMode)); 75 } 76 77 public MessageProducer createProducer(Session session, Destination destination, boolean topic) 78 throws JMSException 79 { 80 return session.createProducer(destination); 81 } 82 83 public MessageConsumer createConsumer(Session session, Destination destination, boolean topic) 84 throws JMSException 85 { 86 return createConsumer(session, destination, null, false, null, topic); 87 } 88 89 public MessageConsumer createConsumer(Session session, 90 Destination destination, 91 String messageSelector, 92 boolean noLocal, 93 String durableName, 94 boolean topic) throws JMSException 95 { 96 if (durableName == null) 97 { 98 if (topic) 99 { 100 return session.createConsumer(destination, messageSelector, noLocal); 101 } 102 else 103 { 104 return session.createConsumer(destination, messageSelector); 105 } 106 } 107 else 108 { 109 if (topic) 110 { 111 return session.createDurableSubscriber((Topic ) destination, durableName, messageSelector, 112 noLocal); 113 } 114 else 115 { 116 throw new JMSException ( 117 "A durable subscriber name was set but the destination was not a Topic"); 118 } 119 } 120 } 121 122 public Destination createDestination(Session session, String name, boolean topic) throws JMSException 123 { 124 if (session == null) 125 { 126 throw new IllegalArgumentException ("Session cannot be null when creating a destination"); 127 } 128 if (name == null) 129 { 130 throw new IllegalArgumentException ("Destination name cannot be null when creating a destination"); 131 } 132 133 if (jndiDestinations) 134 { 135 if (context == null) 136 { 137 throw new IllegalArgumentException ( 138 "Jndi Context name cannot be null when looking up a destination"); 139 } 140 Destination dest = getJndiDestination(name); 141 if (dest != null) 142 { 143 return dest; 144 } 145 else if (forceJndiDestinations) 146 { 147 throw new JMSException ("JNDI destination not found with name: " + name); 148 } 149 } 150 151 if (topic) 152 { 153 return session.createTopic(name); 154 } 155 else 156 { 157 return session.createQueue(name); 158 } 159 } 160 161 protected Destination getJndiDestination(String name) throws JMSException 162 { 163 Object temp; 164 try 165 { 166 temp = context.lookup(name); 167 } 168 catch (NamingException e) 169 { 170 throw new JMSException ("Failed to look up destination: " + e.getMessage()); 171 } 172 if (temp != null) 173 { 174 if (temp instanceof Destination ) 175 { 176 return (Destination )temp; 177 } 178 else if (forceJndiDestinations) 179 { 180 throw new JMSException ("JNDI destination not found with name: " + name); 181 } 182 } 183 return null; 184 } 185 186 public Destination createTemporaryDestination(Session session, boolean topic) throws JMSException 187 { 188 if (session == null) 189 { 190 throw new IllegalArgumentException ("Session cannot be null when creating a destination"); 191 } 192 193 if (topic) 194 { 195 return session.createTemporaryTopic(); 196 } 197 else 198 { 199 return session.createTemporaryQueue(); 200 } 201 } 202 203 public void send(MessageProducer producer, Message message, boolean topic) throws JMSException 204 { 205 send(producer, message, connector.isPersistentDelivery(), Message.DEFAULT_PRIORITY, 206 Message.DEFAULT_TIME_TO_LIVE, topic); 207 } 208 209 public void send(MessageProducer producer, Message message, Destination dest, boolean topic) 210 throws JMSException 211 { 212 send(producer, message, dest, connector.isPersistentDelivery(), Message.DEFAULT_PRIORITY, 213 Message.DEFAULT_TIME_TO_LIVE, topic); 214 } 215 216 public void send(MessageProducer producer, 217 Message message, 218 boolean persistent, 219 int priority, 220 long ttl, 221 boolean topic) throws JMSException 222 { 223 producer.send(message, (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT), 224 priority, ttl); 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 producer.send(dest, message, (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT), 236 priority, ttl); 237 } 238 239 } 240 | Popular Tags |