1 45 package org.exolab.jms.client; 46 47 import java.util.ArrayList ; 48 import java.util.Iterator ; 49 import java.util.List ; 50 import javax.jms.Connection ; 51 import javax.jms.ConnectionConsumer ; 52 import javax.jms.ConnectionMetaData ; 53 import javax.jms.Destination ; 54 import javax.jms.ExceptionListener ; 55 import javax.jms.IllegalStateException ; 56 import javax.jms.InvalidDestinationException ; 57 import javax.jms.InvalidSelectorException ; 58 import javax.jms.JMSException ; 59 import javax.jms.ServerSessionPool ; 60 import javax.jms.Session ; 61 import javax.jms.Topic ; 62 import javax.jms.InvalidClientIDException ; 63 64 import org.apache.commons.logging.Log; 65 import org.apache.commons.logging.LogFactory; 66 67 import org.exolab.jms.server.ServerConnection; 68 69 70 78 class JmsConnection implements Connection { 79 80 83 private JmsConnectionFactory _factory; 84 85 88 private ServerConnection _connection; 89 90 93 private final long _connectionId; 94 95 98 private boolean _closed = false; 99 100 104 private boolean _stopped = true; 105 106 111 private boolean _modified = false; 112 113 117 private String _clientId; 118 119 122 private boolean _clientIdSet = false; 123 124 127 private ExceptionListener _exceptionListener; 128 129 132 private List _sessions = new ArrayList (); 133 134 138 private static final JmsConnectionMetaData _metaData = 139 new JmsConnectionMetaData(); 140 141 144 private static final Log _log = LogFactory.getLog(JmsConnection.class); 145 146 147 159 protected JmsConnection(JmsConnectionFactory factory, String clientID, 160 String username, String password) 161 throws JMSException { 162 163 if (factory == null) { 164 throw new IllegalArgumentException ("Argument 'factory' is null"); 165 } 166 _factory = factory; 167 _clientId = clientID; 168 169 _stopped = true; 170 171 _connection = factory.getProxy().createConnection(_clientId, username, 175 password); 176 _connectionId = _connection.getConnectionId(); 177 } 178 179 184 public long getConnectionId() { 185 return _connectionId; 186 } 187 188 195 public String getClientID() throws JMSException { 196 ensureOpen(); 197 setModified(); 198 199 return _clientId; 200 } 201 202 216 public void setClientID(String clientID) throws JMSException { 217 ensureOpen(); 218 219 if (_clientIdSet) { 221 throw new IllegalStateException ( 222 "The client id has already been set"); 223 } 224 225 if (_modified) { 226 throw new IllegalStateException ( 227 "The client identifier must be set before any other " 228 + "operation is performed"); 229 } 230 231 _connection.setClientID(clientID); 232 _clientId = clientID; 233 _clientIdSet = true; 235 } 236 237 238 245 public ConnectionMetaData getMetaData() throws JMSException { 246 ensureOpen(); 247 setModified(); 248 return _metaData; 249 } 250 251 259 public ExceptionListener getExceptionListener() throws JMSException { 260 ensureOpen(); 261 setModified(); 262 return _exceptionListener; 263 } 264 265 272 public void setExceptionListener(ExceptionListener listener) 273 throws JMSException { 274 ensureOpen(); 275 setModified(); 276 _exceptionListener = listener; 277 } 278 279 285 public void notifyExceptionListener(JMSException message) { 286 if (message.getErrorCode() != null && 288 message.getErrorCode().equals( 289 JmsErrorCodes.CONNECTION_TO_SERVER_DROPPED)) { 290 try { 293 close(); 294 } catch (JMSException exception) { 295 _log.error(exception.getMessage(), exception); 296 } 297 } 298 299 if (_exceptionListener != null) { 301 _exceptionListener.onException(message); 302 } 303 } 304 305 313 public synchronized void start() throws JMSException { 314 ensureOpen(); 315 setModified(); 316 317 try { 318 if (_stopped) { 319 Iterator iterator = _sessions.iterator(); 321 while (iterator.hasNext()) { 322 JmsSession session = (JmsSession) iterator.next(); 323 session.start(); 324 } 325 _stopped = false; 327 } 328 } catch (JMSException exception) { 329 throw exception; 332 } 333 } 334 335 352 public synchronized void stop() throws JMSException { 353 ensureOpen(); 354 setModified(); 355 356 if (!_stopped) { 357 synchronized (_sessions) { 359 Iterator iterator = _sessions.iterator(); 360 while (iterator.hasNext()) { 361 JmsSession session = (JmsSession) iterator.next(); 362 session.stop(); 363 } 364 } 365 _stopped = true; 367 } 368 } 369 370 402 public synchronized void close() throws JMSException { 403 if (!_closed) { 404 stop(); 407 408 JmsSession[] sessions = null; 410 synchronized (_sessions) { 411 sessions = (JmsSession[]) _sessions.toArray(new JmsSession[0]); 412 } 413 for (int i = 0; i < sessions.length; ++i) { 414 sessions[i].close(); 415 } 418 419 getServerConnection().close(); 421 _connection = null; 422 423 _factory.removeConnection(this); 426 _factory = null; 427 428 _closed = true; 431 } 432 } 433 434 453 public Session createSession(boolean transacted, int acknowledgeMode) 454 throws JMSException { 455 ensureOpen(); 456 setModified(); 457 458 JmsSession session = new JmsSession(this, transacted, acknowledgeMode); 459 460 if (!isStopped()) { 462 session.start(); 463 } 464 465 addSession(session); 467 468 return session; 469 } 470 471 494 public ConnectionConsumer createConnectionConsumer( 495 Destination destination, String messageSelector, 496 ServerSessionPool sessionPool, int maxMessages) 497 throws JMSException { 498 ensureOpen(); 499 setModified(); 500 return new JmsConnectionConsumer(this, destination, sessionPool, 501 messageSelector, maxMessages); 502 } 503 504 528 public ConnectionConsumer createDurableConnectionConsumer( 529 Topic topic, String subscriptionName, String messageSelector, 530 ServerSessionPool sessionPool, int maxMessages) 531 throws JMSException { 532 ensureOpen(); 533 setModified(); 534 return new JmsConnectionConsumer(this, topic, subscriptionName, 535 sessionPool, messageSelector, 536 maxMessages); 537 } 538 539 545 protected ServerConnection getServerConnection() throws JMSException { 546 if (_connection == null) { 547 throw new JMSException ("Connection closed"); 548 } 549 550 return _connection; 551 } 552 553 558 protected void addSession(JmsSession session) { 559 synchronized (_sessions) { 560 _sessions.add(session); 561 } 562 } 563 564 570 protected void removeSession(JmsSession session) { 571 synchronized (_sessions) { 572 _sessions.remove(session); 573 } 574 } 575 576 581 protected boolean isStopped() { 582 return _stopped; 583 } 584 585 590 protected void setModified() { 591 _modified = true; 592 } 593 594 606 protected synchronized void deleteTemporaryDestination( 607 JmsDestination destination) 608 throws JMSException { 609 if ((destination != null) && 610 (destination instanceof JmsTemporaryDestination)) { 611 JmsTemporaryDestination temp_dest = 612 (JmsTemporaryDestination) destination; 613 614 if (temp_dest.getOwningConnection() == this) { 617 } else { 620 throw new JMSException ( 621 "The temp destination cannot be used outside the scope " 622 + "of the connection creating it"); 623 } 624 } else { 625 throw new JMSException ("The destination is not temporary"); 626 } 627 } 628 629 634 protected void ensureOpen() throws IllegalStateException { 635 if (_closed) { 636 throw new IllegalStateException ( 637 "Cannot perform operation - session has been closed"); 638 } 639 } 640 641 } 642 643 | Popular Tags |