001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.jms.connection;
018:
019: import javax.jms.Connection;
020: import javax.jms.ConnectionFactory;
021: import javax.jms.JMSException;
022: import javax.jms.QueueConnection;
023: import javax.jms.QueueConnectionFactory;
024: import javax.jms.TopicConnection;
025: import javax.jms.TopicConnectionFactory;
026:
027: import org.springframework.beans.factory.InitializingBean;
028: import org.springframework.util.Assert;
029:
030: /**
031: * {@link javax.jms.ConnectionFactory} implementation that delegates all calls
032: * to a given target {@link javax.jms.ConnectionFactory}.
033: *
034: * <p>This class allows for being subclassed, with subclasses overriding only
035: * those methods (such as {@link #createConnection()}) that should not simply
036: * delegate to the target ConnectionFactory.
037: *
038: * <p>Can also be defined as-is, wrapping a specific target ConnectionFactory,
039: * using the "shouldStopConnections" flag to indicate whether Connections
040: * obtained from the target factory are supposed to be stopped before closed.
041: * The latter may be necessary for some connection pools that simply return
042: * released connections to the pool, not stopping them while they sit in the pool.
043: *
044: * @author Juergen Hoeller
045: * @since 2.0.2
046: * @see #createConnection()
047: * @see #setShouldStopConnections
048: * @see ConnectionFactoryUtils#releaseConnection
049: */
050: public class DelegatingConnectionFactory implements
051: SmartConnectionFactory, QueueConnectionFactory,
052: TopicConnectionFactory, InitializingBean {
053:
054: private ConnectionFactory targetConnectionFactory;
055:
056: private boolean shouldStopConnections = false;
057:
058: /**
059: * Set the target ConnectionFactory that this ConnectionFactory should delegate to.
060: */
061: public void setTargetConnectionFactory(
062: ConnectionFactory targetConnectionFactory) {
063: Assert.notNull(targetConnectionFactory,
064: "'targetConnectionFactory' must not be null");
065: this .targetConnectionFactory = targetConnectionFactory;
066: }
067:
068: /**
069: * Return the target ConnectionFactory that this ConnectionFactory delegates to.
070: */
071: public ConnectionFactory getTargetConnectionFactory() {
072: return this .targetConnectionFactory;
073: }
074:
075: /**
076: * Indicate whether Connections obtained from the target factory are supposed
077: * to be stopped before closed ("true") or simply closed ("false").
078: * The latter may be necessary for some connection pools that simply return
079: * released connections to the pool, not stopping them while they sit in the pool.
080: * <p>Default is "false", simply closing Connections.
081: * @see ConnectionFactoryUtils#releaseConnection
082: */
083: public void setShouldStopConnections(boolean shouldStopConnections) {
084: this .shouldStopConnections = shouldStopConnections;
085: }
086:
087: public void afterPropertiesSet() {
088: if (getTargetConnectionFactory() == null) {
089: throw new IllegalArgumentException(
090: "'targetConnectionFactory' is required");
091: }
092: }
093:
094: public Connection createConnection() throws JMSException {
095: return getTargetConnectionFactory().createConnection();
096: }
097:
098: public Connection createConnection(String username, String password)
099: throws JMSException {
100: return getTargetConnectionFactory().createConnection(username,
101: password);
102: }
103:
104: public QueueConnection createQueueConnection() throws JMSException {
105: ConnectionFactory cf = getTargetConnectionFactory();
106: if (!(cf instanceof QueueConnectionFactory)) {
107: throw new javax.jms.IllegalStateException(
108: "'targetConnectionFactory' is not a QueueConnectionFactory");
109: }
110: return ((QueueConnectionFactory) cf).createQueueConnection();
111: }
112:
113: public QueueConnection createQueueConnection(String username,
114: String password) throws JMSException {
115: ConnectionFactory cf = getTargetConnectionFactory();
116: if (!(cf instanceof QueueConnectionFactory)) {
117: throw new javax.jms.IllegalStateException(
118: "'targetConnectionFactory' is not a QueueConnectionFactory");
119: }
120: return ((QueueConnectionFactory) cf).createQueueConnection(
121: username, password);
122: }
123:
124: public TopicConnection createTopicConnection() throws JMSException {
125: ConnectionFactory cf = getTargetConnectionFactory();
126: if (!(cf instanceof TopicConnectionFactory)) {
127: throw new javax.jms.IllegalStateException(
128: "'targetConnectionFactory' is not a TopicConnectionFactory");
129: }
130: return ((TopicConnectionFactory) cf).createTopicConnection();
131: }
132:
133: public TopicConnection createTopicConnection(String username,
134: String password) throws JMSException {
135: ConnectionFactory cf = getTargetConnectionFactory();
136: if (!(cf instanceof TopicConnectionFactory)) {
137: throw new javax.jms.IllegalStateException(
138: "'targetConnectionFactory' is not a TopicConnectionFactory");
139: }
140: return ((TopicConnectionFactory) cf).createTopicConnection(
141: username, password);
142: }
143:
144: public boolean shouldStop(Connection con) {
145: return this.shouldStopConnections;
146: }
147:
148: }
|