KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jms > connection > SingleConnectionFactory102


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jms.connection;
18
19 import javax.jms.Connection JavaDoc;
20 import javax.jms.ConnectionFactory JavaDoc;
21 import javax.jms.JMSException JavaDoc;
22 import javax.jms.QueueConnectionFactory JavaDoc;
23 import javax.jms.TopicConnectionFactory JavaDoc;
24
25 /**
26  * A subclass of SingleConnectionFactory that uses the JMS 1.0.2 specification,
27  * rather than the JMS 1.1 methods used by SingleConnectionFactory itself.
28  * This class can be used for JMS 1.0.2 providers, offering the same API as
29  * SingleConnectionFactory does for JMS 1.1 providers.
30  *
31  * <p>You need to set the "pubSubDomain" property accordingly, as this class
32  * will always create either a QueueConnection or a TopicConnection.
33  *
34  * @author Juergen Hoeller
35  * @since 1.1
36  * @see #setTargetConnectionFactory
37  * @see #setPubSubDomain
38  */

39 public class SingleConnectionFactory102 extends SingleConnectionFactory {
40
41     private boolean pubSubDomain = false;
42
43
44     /**
45      * Create a new SingleConnectionFactory102 for bean-style usage.
46      */

47     public SingleConnectionFactory102() {
48         super();
49     }
50
51     /**
52      * Create a new SingleConnectionFactory102 that always returns a single
53      * Connection that it will lazily create via the given target
54      * ConnectionFactory.
55      * @param connectionFactory the target ConnectionFactory
56      * @param pubSubDomain whether the Publish/Subscribe domain (Topics) or
57      * Point-to-Point domain (Queues) should be used
58      */

59     public SingleConnectionFactory102(ConnectionFactory JavaDoc connectionFactory, boolean pubSubDomain) {
60         setTargetConnectionFactory(connectionFactory);
61         this.pubSubDomain = pubSubDomain;
62         afterPropertiesSet();
63     }
64
65
66     /**
67      * Configure the factory with knowledge of the JMS domain used.
68      * This tells the JMS 1.0.2 provider which class hierarchy to use
69      * for creating Connections. Default is Point-to-Point (Queues).
70      * @param pubSubDomain true for Publish/Subscribe domain (Topics),
71      * false for Point-to-Point domain (Queues)
72      */

73     public void setPubSubDomain(boolean pubSubDomain) {
74         this.pubSubDomain = pubSubDomain;
75     }
76
77     /**
78      * Return whether the Publish/Subscribe domain (Topics) is used.
79      * Otherwise, the Point-to-Point domain (Queues) is used.
80      */

81     public boolean isPubSubDomain() {
82         return this.pubSubDomain;
83     }
84
85
86     /**
87      * In addition to checking whether the target ConnectionFactory is set,
88      * make sure that the supplied factory is of the appropriate type for
89      * the specified destination type: QueueConnectionFactory for queues,
90      * TopicConnectionFactory for topics.
91      */

92     public void afterPropertiesSet() {
93         super.afterPropertiesSet();
94
95         // Make sure that the ConnectionFactory passed is consistent.
96
// Some provider implementations of the ConnectionFactory interface
97
// implement both domain interfaces under the cover, so just check if
98
// the selected domain is consistent with the type of connection factory.
99
if (isPubSubDomain()) {
100             if (!(getTargetConnectionFactory() instanceof TopicConnectionFactory JavaDoc)) {
101                 throw new IllegalArgumentException JavaDoc(
102                         "Specified a Spring JMS 1.0.2 SingleConnectionFactory for topics " +
103                         "but did not supply an instance of TopicConnectionFactory");
104             }
105         }
106         else {
107             if (!(getTargetConnectionFactory() instanceof QueueConnectionFactory JavaDoc)) {
108                 throw new IllegalArgumentException JavaDoc(
109                         "Specified a Spring JMS 1.0.2 SingleConnectionFactory for queues " +
110                         "but did not supply an instance of QueueConnectionFactory");
111             }
112         }
113     }
114
115     /**
116      * This implementation overrides the superclass method to use JMS 1.0.2 API.
117      */

118     protected Connection JavaDoc doCreateConnection() throws JMSException JavaDoc {
119         if (isPubSubDomain()) {
120             return ((TopicConnectionFactory JavaDoc) getTargetConnectionFactory()).createTopicConnection();
121         }
122         else {
123             return ((QueueConnectionFactory JavaDoc) getTargetConnectionFactory()).createQueueConnection();
124         }
125     }
126
127 }
128
Popular Tags