1 16 17 package org.springframework.jms.connection; 18 19 import javax.jms.Connection ; 20 import javax.jms.ConnectionFactory ; 21 import javax.jms.JMSException ; 22 import javax.jms.QueueConnection ; 23 import javax.jms.QueueConnectionFactory ; 24 import javax.jms.TopicConnection ; 25 import javax.jms.TopicConnectionFactory ; 26 27 import org.springframework.beans.factory.InitializingBean; 28 import org.springframework.util.Assert; 29 30 50 public class DelegatingConnectionFactory 51 implements SmartConnectionFactory, QueueConnectionFactory , TopicConnectionFactory , InitializingBean { 52 53 private ConnectionFactory targetConnectionFactory; 54 55 private boolean shouldStopConnections = false; 56 57 58 61 public void setTargetConnectionFactory(ConnectionFactory targetConnectionFactory) { 62 Assert.notNull(targetConnectionFactory, "'targetConnectionFactory' must not be null"); 63 this.targetConnectionFactory = targetConnectionFactory; 64 } 65 66 69 public ConnectionFactory getTargetConnectionFactory() { 70 return this.targetConnectionFactory; 71 } 72 73 81 public void setShouldStopConnections(boolean shouldStopConnections) { 82 this.shouldStopConnections = shouldStopConnections; 83 } 84 85 public void afterPropertiesSet() { 86 if (getTargetConnectionFactory() == null) { 87 throw new IllegalArgumentException ("'targetConnectionFactory' is required"); 88 } 89 } 90 91 92 public Connection createConnection() throws JMSException { 93 return getTargetConnectionFactory().createConnection(); 94 } 95 96 public Connection createConnection(String username, String password) throws JMSException { 97 return getTargetConnectionFactory().createConnection(username, password); 98 } 99 100 public QueueConnection createQueueConnection() throws JMSException { 101 ConnectionFactory cf = getTargetConnectionFactory(); 102 if (!(cf instanceof QueueConnectionFactory )) { 103 throw new javax.jms.IllegalStateException ("'targetConnectionFactory' is not a QueueConnectionFactory"); 104 } 105 return ((QueueConnectionFactory ) cf).createQueueConnection(); 106 } 107 108 public QueueConnection createQueueConnection(String username, String password) throws JMSException { 109 ConnectionFactory cf = getTargetConnectionFactory(); 110 if (!(cf instanceof QueueConnectionFactory )) { 111 throw new javax.jms.IllegalStateException ("'targetConnectionFactory' is not a QueueConnectionFactory"); 112 } 113 return ((QueueConnectionFactory ) cf).createQueueConnection(username, password); 114 } 115 116 public TopicConnection createTopicConnection() throws JMSException { 117 ConnectionFactory cf = getTargetConnectionFactory(); 118 if (!(cf instanceof TopicConnectionFactory )) { 119 throw new javax.jms.IllegalStateException ("'targetConnectionFactory' is not a TopicConnectionFactory"); 120 } 121 return ((TopicConnectionFactory ) cf).createTopicConnection(); 122 } 123 124 public TopicConnection createTopicConnection(String username, String password) throws JMSException { 125 ConnectionFactory cf = getTargetConnectionFactory(); 126 if (!(cf instanceof TopicConnectionFactory )) { 127 throw new javax.jms.IllegalStateException ("'targetConnectionFactory' is not a TopicConnectionFactory"); 128 } 129 return ((TopicConnectionFactory ) cf).createTopicConnection(username, password); 130 } 131 132 public boolean shouldStop(Connection con) { 133 return this.shouldStopConnections; 134 } 135 136 } 137 | Popular Tags |