1 /*
2 * @(#)ConnectionImpl.java
3 *
4 * JFoxMQ the open source JMS MOM.
5 *
6 * Corpyright 2002-2003 Huihoo Power, Inc. All Rights Reserved. This software
7 * is licensed under LGPL license.
8 *
9 * For more information, please visit: http://www.huihoo.org
10 */
11
12 package org.huihoo.jfox.ms.jms.base;
13
14 import java.util.Iterator;
15 import java.util.Vector;
16
17 import javax.jms.Connection;
18 import javax.jms.ConnectionMetaData;
19 import javax.jms.ExceptionListener;
20 import javax.jms.JMSException;
21
22 /***
23 * <p>
24 * Description: This class implements javax.jms.Connection. The connection
25 * object is received from a connection factory.
26 * </p>
27 *
28 * <p>
29 * Connections support concurrent use.
30 *
31 * <ul>
32 * <li>It encapsulates an open connection with a JMS provider. It typically
33 * represents an open TCP/IP socket between a client and a provider service
34 * daemon.
35 * <li>Its creation is where client authentication takes place.
36 * <li>It can specify a unique client identifier.
37 * <li>It provides a <CODE>ConnectionMetaData</CODE> object.
38 * <li>It supports an optional <CODE>ExceptionListener</CODE> object.
39 * </ul>
40 *
41 * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter.Cheng</a>
42 * @version Revision: 1.1 Date: 2003-05-19 22:36:31
43 */
44
45 public abstract class ConnectionImpl implements Connection {
46
47 private String userName;
48 private String password;
49 private boolean stopped = true;
50 private boolean closed = false;
51 private ConnectionFactoryImpl connFactory;
52
53 /***
54 * The list of registered client sessions.
55 */
56 private Vector sessions = new Vector();
57
58 public ConnectionImpl(
59 ConnectionFactoryImpl connFactory,
60 String userName,
61 String password) {
62 this.connFactory = connFactory;
63 this.userName = userName;
64 this.password = password;
65
66 }
67
68 public String getClientID() throws JMSException {
69 // TODO
70 return null;
71 }
72
73 public void setClientID(String clientID) throws JMSException {
74 // TODO
75 }
76
77 public ConnectionMetaData getMetaData() throws JMSException {
78 // TODO
79 return null;
80 }
81
82 public ExceptionListener getExceptionListener() throws JMSException {
83 // TODO
84 return null;
85 }
86
87 public void setExceptionListener(ExceptionListener listener)
88 throws JMSException {
89 // TODO
90 }
91
92 /***
93 * Starts (or restarts) a connection's delivery of incoming messages. A
94 * call to <CODE>start</CODE> on a connection that has already been
95 * started is ignored.
96 *
97 * @exception javax.jms.IllegalStateException
98 * if the connection is closed.
99 * @exception JMSException
100 * if the JMS provider fails to start message delivery due
101 * to some internal error.
102 *
103 */
104 public void start() throws JMSException {
105 if (closed) {
106 throw new IllegalStateException("The connection is closed");
107 }
108
109 try {
110 if (stopped) {
111 Iterator sessionsIter = sessions.iterator();
112 while (sessionsIter.hasNext()) {
113 SessionImpl session = (SessionImpl)sessionsIter.next();
114 session.start();
115 }
116 stopped = false;
117 }
118 } catch (JMSException e) {
119 throw new JMSException("Start message delivery failed");
120 }
121 }
122
123 /***
124 * Stops the asynchronous deliveries in the session. Temporarily stops a
125 * connection's delivery of incoming messages. Delivery can be restarted
126 * using the connection's <CODE>start</CODE> method. When the connection
127 * is stopped, delivery to all the connection's message consumers is
128 * inhibited: synchronous receives block, and messages are not delivered to
129 * message listeners.
130 *
131 * @exception javax.jms.IllegalStateException
132 * if the connection is closed.
133 * @exception JMSException
134 * if the JMS provider fails to start message delivery due
135 * to some internal error.
136 */
137 public void stop() throws JMSException {
138 if (closed) {
139 throw new IllegalStateException("The connection is closed");
140 }
141
142 try {
143 if (!stopped) {
144 Iterator sessionsIter = sessions.iterator();
145 while (sessionsIter.hasNext()) {
146 SessionImpl session = (SessionImpl)sessionsIter.next();
147 session.stop();
148 }
149 stopped = false;
150 }
151 } catch (JMSException e) {
152 throw new JMSException("Start message delivery failed");
153 }
154 }
155
156 /***
157 * Closes the connection.
158 *
159 * <P>
160 * Since a provider typically allocates significant resources outside the
161 * JVM on behalf of a connection, clients should close these resources when
162 * they are not needed. Relying on garbage collection to eventually reclaim
163 * these resources may not be timely enough.
164 *
165 * @exception JMSException
166 * if the JMS provider fails to start message delivery due
167 * to some internal error.
168 */
169 public void close() throws JMSException {
170 if (closed) {
171 return;
172 }
173 Iterator sessionsIter = sessions.iterator();
174 while (sessionsIter.hasNext()) {
175 SessionImpl session = (SessionImpl)sessionsIter.next();
176 session.close();
177 }
178 closed = true;
179 }
180
181 /***
182 * Identify the connection state.
183 *
184 * @return boolean true the connection is closed
185 */
186 public boolean isClosed() {
187 return closed;
188 }
189
190 /***
191 * Identify the connection state.
192 *
193 * @return boolean ture the connection is stopped
194 */
195 public boolean isStopped() {
196 return stopped;
197 }
198
199 /***
200 * Add the specified session to the list of managed sessions
201 *
202 * @param session
203 * session to register
204 */
205 protected void addSession(SessionImpl session) {
206 sessions.addElement(session);
207 }
208
209 /***
210 * Remove the specified session to the list of managed sessions
211 *
212 * @param session
213 * session to deregister
214 */
215 protected void removeSession(SessionImpl session) {
216 sessions.removeElement(session);
217 }
218
219 }
This page was automatically generated by Maven