KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.LinkedList JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.jms.Connection JavaDoc;
26 import javax.jms.ConnectionFactory JavaDoc;
27 import javax.jms.JMSException JavaDoc;
28 import javax.jms.Session JavaDoc;
29 import javax.jms.TransactionInProgressException JavaDoc;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34 import org.springframework.transaction.support.ResourceHolderSupport;
35 import org.springframework.util.Assert;
36 import org.springframework.util.CollectionUtils;
37
38 /**
39  * Connection holder, wrapping a JMS Connection and a JMS Session.
40  * JmsTransactionManager binds instances of this class to the thread,
41  * for a given JMS ConnectionFactory.
42  *
43  * <p>Note: This is an SPI class, not intended to be used by applications.
44  *
45  * @author Juergen Hoeller
46  * @since 1.1
47  * @see JmsTransactionManager
48  * @see org.springframework.jms.core.JmsTemplate
49  */

50 public class JmsResourceHolder extends ResourceHolderSupport {
51
52     private static final Log logger = LogFactory.getLog(JmsResourceHolder.class);
53
54     private boolean frozen;
55
56     private final ConnectionFactory JavaDoc connectionFactory;
57
58     private final List JavaDoc connections = new LinkedList JavaDoc();
59
60     private final List JavaDoc sessions = new LinkedList JavaDoc();
61
62     private final Map JavaDoc sessionsPerConnection = new HashMap JavaDoc();
63
64
65     /**
66      * Create a new JmsResourceHolder that is open for resources to be added.
67      * @see #addConnection
68      * @see #addSession
69      */

70     public JmsResourceHolder() {
71         this(null);
72     }
73
74     /**
75      * Create a new JmsResourceHolder that is open for resources to be added.
76      * @param connectionFactory the JMS ConnectionFactory that this
77      * resource holder is associated with (may be <code>null</code>)
78      */

79     public JmsResourceHolder(ConnectionFactory JavaDoc connectionFactory) {
80         this.connectionFactory = connectionFactory;
81         this.frozen = false;
82     }
83
84     /**
85      * Create a new JmsResourceHolder for the given JMS resources.
86      * @param connection the JMS Connection
87      * @param session the JMS Session
88      */

89     public JmsResourceHolder(Connection JavaDoc connection, Session JavaDoc session) {
90         this(null, connection, session);
91     }
92
93     /**
94      * Create a new JmsResourceHolder for the given JMS resources.
95      * @param connectionFactory the JMS ConnectionFactory that this
96      * resource holder is associated with (may be <code>null</code>)
97      * @param connection the JMS Connection
98      * @param session the JMS Session
99      */

100     public JmsResourceHolder(ConnectionFactory JavaDoc connectionFactory, Connection JavaDoc connection, Session JavaDoc session) {
101         this.connectionFactory = connectionFactory;
102         addConnection(connection);
103         addSession(session, connection);
104         this.frozen = true;
105     }
106
107
108     public final boolean isFrozen() {
109         return this.frozen;
110     }
111
112     public final void addConnection(Connection JavaDoc connection) {
113         Assert.isTrue(!this.frozen, "Cannot add Connection because JmsResourceHolder is frozen");
114         Assert.notNull(connection, "Connection must not be null");
115         if (!this.connections.contains(connection)) {
116             this.connections.add(connection);
117         }
118     }
119
120     public final void addSession(Session JavaDoc session) {
121         addSession(session, null);
122     }
123
124     public final void addSession(Session JavaDoc session, Connection JavaDoc connection) {
125         Assert.isTrue(!this.frozen, "Cannot add Session because JmsResourceHolder is frozen");
126         Assert.notNull(session, "Session must not be null");
127         if (!this.sessions.contains(session)) {
128             this.sessions.add(session);
129             if (connection != null) {
130                 List JavaDoc sessions = (List JavaDoc) this.sessionsPerConnection.get(connection);
131                 if (sessions == null) {
132                     sessions = new LinkedList JavaDoc();
133                     this.sessionsPerConnection.put(connection, sessions);
134                 }
135                 sessions.add(session);
136             }
137         }
138     }
139
140     public boolean containsSession(Session JavaDoc session) {
141         return this.sessions.contains(session);
142     }
143
144
145     public Connection JavaDoc getConnection() {
146         return (!this.connections.isEmpty() ? (Connection JavaDoc) this.connections.get(0) : null);
147     }
148
149     public Connection JavaDoc getConnection(Class JavaDoc connectionType) {
150         return (Connection JavaDoc) CollectionUtils.findValueOfType(this.connections, connectionType);
151     }
152
153     public Session JavaDoc getSession() {
154         return (!this.sessions.isEmpty() ? (Session JavaDoc) this.sessions.get(0) : null);
155     }
156
157     public Session JavaDoc getSession(Class JavaDoc sessionType) {
158         return getSession(sessionType, null);
159     }
160
161     public Session JavaDoc getSession(Class JavaDoc sessionType, Connection JavaDoc connection) {
162         List JavaDoc sessions = this.sessions;
163         if (connection != null) {
164             sessions = (List JavaDoc) this.sessionsPerConnection.get(connection);
165         }
166         return (Session JavaDoc) CollectionUtils.findValueOfType(sessions, sessionType);
167     }
168
169
170     public void commitAll() throws JMSException JavaDoc {
171         for (Iterator JavaDoc it = this.sessions.iterator(); it.hasNext();) {
172             try {
173                 ((Session JavaDoc) it.next()).commit();
174             }
175             catch (TransactionInProgressException JavaDoc ex) {
176                 // Ignore -> can only happen in case of a JTA transaction.
177
}
178             catch (javax.jms.IllegalStateException JavaDoc ex) {
179                 // Ignore -> can only happen in case of a JTA transaction.
180
}
181         }
182     }
183
184     public void closeAll() {
185         for (Iterator JavaDoc it = this.sessions.iterator(); it.hasNext();) {
186             try {
187                 ((Session JavaDoc) it.next()).close();
188             }
189             catch (Throwable JavaDoc ex) {
190                 logger.debug("Could not close synchronized JMS Session after transaction", ex);
191             }
192         }
193         for (Iterator JavaDoc it = this.connections.iterator(); it.hasNext();) {
194             Connection JavaDoc con = (Connection JavaDoc) it.next();
195             ConnectionFactoryUtils.releaseConnection(con, this.connectionFactory, true);
196         }
197     }
198
199 }
200
Popular Tags