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.QueueConnectionFactory;
023: import javax.jms.TopicConnectionFactory;
024:
025: /**
026: * A subclass of SingleConnectionFactory that uses the JMS 1.0.2 specification,
027: * rather than the JMS 1.1 methods used by SingleConnectionFactory itself.
028: * This class can be used for JMS 1.0.2 providers, offering the same API as
029: * SingleConnectionFactory does for JMS 1.1 providers.
030: *
031: * <p>You need to set the "pubSubDomain" property accordingly, as this class
032: * will always create either a QueueConnection or a TopicConnection.
033: *
034: * @author Juergen Hoeller
035: * @since 1.1
036: * @see #setTargetConnectionFactory
037: * @see #setPubSubDomain
038: */
039: public class SingleConnectionFactory102 extends SingleConnectionFactory {
040:
041: private boolean pubSubDomain = false;
042:
043: /**
044: * Create a new SingleConnectionFactory102 for bean-style usage.
045: */
046: public SingleConnectionFactory102() {
047: super ();
048: }
049:
050: /**
051: * Create a new SingleConnectionFactory102 that always returns a single
052: * Connection that it will lazily create via the given target
053: * ConnectionFactory.
054: * @param connectionFactory the target ConnectionFactory
055: * @param pubSubDomain whether the Publish/Subscribe domain (Topics) or
056: * Point-to-Point domain (Queues) should be used
057: */
058: public SingleConnectionFactory102(
059: ConnectionFactory connectionFactory, boolean pubSubDomain) {
060: setTargetConnectionFactory(connectionFactory);
061: this .pubSubDomain = pubSubDomain;
062: afterPropertiesSet();
063: }
064:
065: /**
066: * Configure the factory with knowledge of the JMS domain used.
067: * This tells the JMS 1.0.2 provider which class hierarchy to use
068: * for creating Connections. Default is Point-to-Point (Queues).
069: * @param pubSubDomain true for Publish/Subscribe domain (Topics),
070: * false for Point-to-Point domain (Queues)
071: */
072: public void setPubSubDomain(boolean pubSubDomain) {
073: this .pubSubDomain = pubSubDomain;
074: }
075:
076: /**
077: * Return whether the Publish/Subscribe domain (Topics) is used.
078: * Otherwise, the Point-to-Point domain (Queues) is used.
079: */
080: public boolean isPubSubDomain() {
081: return this .pubSubDomain;
082: }
083:
084: /**
085: * In addition to checking whether the target ConnectionFactory is set,
086: * make sure that the supplied factory is of the appropriate type for
087: * the specified destination type: QueueConnectionFactory for queues,
088: * TopicConnectionFactory for topics.
089: */
090: public void afterPropertiesSet() {
091: super .afterPropertiesSet();
092:
093: // Make sure that the ConnectionFactory passed is consistent.
094: // Some provider implementations of the ConnectionFactory interface
095: // implement both domain interfaces under the cover, so just check if
096: // the selected domain is consistent with the type of connection factory.
097: if (isPubSubDomain()) {
098: if (!(getTargetConnectionFactory() instanceof TopicConnectionFactory)) {
099: throw new IllegalArgumentException(
100: "Specified a Spring JMS 1.0.2 SingleConnectionFactory for topics "
101: + "but did not supply an instance of TopicConnectionFactory");
102: }
103: } else {
104: if (!(getTargetConnectionFactory() instanceof QueueConnectionFactory)) {
105: throw new IllegalArgumentException(
106: "Specified a Spring JMS 1.0.2 SingleConnectionFactory for queues "
107: + "but did not supply an instance of QueueConnectionFactory");
108: }
109: }
110: }
111:
112: /**
113: * This implementation overrides the superclass method to use JMS 1.0.2 API.
114: */
115: protected Connection doCreateConnection() throws JMSException {
116: if (isPubSubDomain()) {
117: return ((TopicConnectionFactory) getTargetConnectionFactory())
118: .createTopicConnection();
119: } else {
120: return ((QueueConnectionFactory) getTargetConnectionFactory())
121: .createQueueConnection();
122: }
123: }
124:
125: }
|