1 28 29 package com.caucho.jms.session; 30 31 import com.caucho.jms.ConnectionFactoryImpl; 32 import com.caucho.log.Log; 33 import com.caucho.util.L10N; 34 35 import javax.jms.*; 36 import javax.jms.IllegalStateException ; 37 import java.util.ArrayList ; 38 import java.util.HashMap ; 39 import java.util.logging.Level ; 40 import java.util.logging.Logger ; 41 42 45 public class ConnectionImpl implements Connection { 46 static final Logger log = Log.open(ConnectionImpl.class); 47 static final L10N L = new L10N(ConnectionImpl.class); 48 49 private static int _clientIdGenerator; 50 51 private ConnectionFactoryImpl _factory; 52 53 private String _clientId; 54 private boolean _isClientIdSet; 55 56 private ExceptionListener _exceptionListener; 57 58 private ArrayList <SessionImpl> _sessions = new ArrayList <SessionImpl>(); 59 60 private HashMap <String ,TopicSubscriber> _durableSubscriberMap = 61 new HashMap <String ,TopicSubscriber>(); 62 63 private volatile boolean _isActive; 64 private volatile boolean _isStopping; 65 protected volatile boolean _isClosed; 66 67 public ConnectionImpl(ConnectionFactoryImpl factory) 68 { 69 _factory = factory; 70 } 71 72 75 public String getClientID() 76 throws JMSException 77 { 78 if (_isClosed) 79 throw new IllegalStateException (L.l("connection is closed")); 80 81 return _clientId; 82 } 83 84 89 public void setClientID(String clientId) 90 throws JMSException 91 { 92 if (_isClosed) 93 throw new IllegalStateException (L.l("connection is closed")); 94 95 if (_isClientIdSet) 96 throw new IllegalStateException (L.l("Can't set client id '{0}' after the connection has been used.", 97 clientId)); 98 99 ConnectionImpl oldConn = _factory.findByClientID(clientId); 100 101 if (oldConn != null) 102 throw new InvalidClientIDException(L.l("'{0}' is a duplicate client id.", 103 clientId)); 104 105 _clientId = clientId; 106 _isClientIdSet = true; 107 } 108 109 112 public ConnectionFactoryImpl getConnectionFactory() 113 { 114 return _factory; 115 } 116 117 120 public ExceptionListener getExceptionListener() 121 throws JMSException 122 { 123 if (_isClosed) 124 throw new IllegalStateException (L.l("connection is closed")); 125 126 return _exceptionListener; 127 } 128 129 132 public void setExceptionListener(ExceptionListener listener) 133 throws JMSException 134 { 135 if (_isClosed) 136 throw new IllegalStateException (L.l("connection is closed")); 137 138 assignClientID(); 139 140 _exceptionListener = listener; 141 } 142 143 146 public ConnectionMetaData getMetaData() 147 throws JMSException 148 { 149 if (_isClosed) 150 throw new IllegalStateException (L.l("connection is closed")); 151 152 return new ConnectionMetaDataImpl(); 153 } 154 155 158 public void start() 159 throws JMSException 160 { 161 if (_isClosed) 162 throw new IllegalStateException (L.l("connection is closed")); 163 164 assignClientID(); 165 166 if (_isActive || _isStopping) 167 return; 168 169 _isActive = true; 170 171 for (int i = 0; i < _sessions.size(); i++) { 172 _sessions.get(i).start(); 173 } 174 } 175 176 179 public void stop() 180 throws JMSException 181 { 182 if (_isClosed) 183 throw new IllegalStateException (L.l("connection is closed")); 184 185 if (_isStopping || ! _isActive) 186 return; 187 188 assignClientID(); 189 190 _isStopping = true; 191 192 try { 193 for (int i = 0; i < _sessions.size(); i++) { 194 _sessions.get(i).stop(); 195 } 196 } finally { 197 _isActive = false; 198 _isStopping = false; 199 } 200 } 201 202 205 boolean isActive() 206 { 207 return _isActive; 208 } 209 210 213 boolean isStopping() 214 { 215 return _isStopping; 216 } 217 218 221 public Session createSession(boolean transacted, int acknowledgeMode) 222 throws JMSException 223 { 224 checkOpen(); 225 226 assignClientID(); 227 228 return new SessionImpl(this, transacted, acknowledgeMode); 229 } 230 231 234 protected void addSession(SessionImpl session) 235 { 236 _sessions.add(session); 237 238 if (_isActive) 239 session.start(); 240 } 241 242 245 void removeSession(SessionImpl session) 246 { 247 _sessions.remove(session); 248 } 249 250 253 TopicSubscriber getDurableSubscriber(String name) 254 { 255 return _durableSubscriberMap.get(name); 256 } 257 258 261 TopicSubscriber putDurableSubscriber(String name, TopicSubscriber subscriber) 262 { 263 return _durableSubscriberMap.put(name, subscriber); 264 } 265 266 269 TopicSubscriber removeDurableSubscriber(String name) 270 { 271 return _durableSubscriberMap.remove(name); 272 } 273 274 277 public ConnectionConsumer 278 createConnectionConsumer(Destination destination, 279 String messageSelector, 280 ServerSessionPool sessionPool, 281 int maxMessages) 282 throws JMSException 283 { 284 throw new UnsupportedOperationException (); 285 } 286 287 290 public ConnectionConsumer 291 createDurableConnectionConsumer(Topic topic, String name, 292 String messageSelector, 293 ServerSessionPool sessionPool, 294 int maxMessages) 295 throws JMSException 296 { 297 if (_isClosed) 298 throw new IllegalStateException (L.l("connection is closed")); 299 300 throw new UnsupportedOperationException (); 301 } 302 303 306 public void close() 307 throws JMSException 308 { 309 if (_isClosed) 310 return; 311 312 stop(); 313 314 _isClosed = true; 315 316 _factory.removeConnection(this); 317 318 ArrayList <SessionImpl> sessions = new ArrayList <SessionImpl>(_sessions); 319 _sessions.clear(); 320 321 for (int i = 0; i < sessions.size(); i++) { 322 try { 323 sessions.get(i).close(); 324 } catch (Throwable e) { 325 log.log(Level.WARNING, e.toString(), e); 326 } 327 } 328 } 329 330 333 protected void checkOpen() 334 throws IllegalStateException  335 { 336 if (_isClosed) 337 throw new IllegalStateException (L.l("connection is closed")); 338 } 339 340 345 protected void assignClientID() 346 { 347 if (_clientId == null) 348 _clientId = "resin-temp-" + _clientIdGenerator++; 349 _isClientIdSet = true; 350 } 351 352 355 public void finalize() 356 { 357 365 } 366 } 367 | Popular Tags |