1 package net.walend.somnifugi; 2 3 import java.util.Set ; 4 import java.util.HashSet ; 5 import java.util.Map ; 6 import java.util.HashMap ; 7 import java.util.Iterator ; 8 import java.util.Enumeration ; 9 import java.util.NoSuchElementException ; 10 11 import javax.naming.Context ; 12 13 import javax.jms.Connection ; 14 import javax.jms.ConnectionFactory ; 15 import javax.jms.JMSException ; 16 import javax.jms.ConnectionMetaData ; 17 import javax.jms.ExceptionListener ; 18 import javax.jms.Session ; 19 import javax.jms.Destination ; 20 import javax.jms.ServerSessionPool ; 21 import javax.jms.ConnectionConsumer ; 22 import javax.jms.Topic ; 23 24 25 30 31 public abstract class SomniConnection 32 implements Connection 33 { 34 protected final Object guard = new Object (); 35 private ConnectionFactory factory; 36 private String clientID; 37 private Set <SomniSession> sessionSet = new HashSet <SomniSession>(); 38 private boolean started = false; 39 private boolean closed = false; 40 private int sessionCounter = 0; 41 42 private Context context; 43 44 private ExceptionListener exceptionListener = new SomniDefaultExceptionListener(); 45 46 protected SomniConnection(ConnectionFactory factory,String clientID,Context context) 47 { 48 this.factory = factory; 49 this.clientID = clientID; 50 this.context = context; 51 } 52 53 60 public Map <String ,SomniConsumerReport> createSessionReport() 61 { 62 Map <String ,SomniConsumerReport> report = new HashMap <String ,SomniConsumerReport>(); 63 64 synchronized(guard) 65 { 66 for(SomniSession session : sessionSet) 67 { 68 report.putAll(session.createConsumerReports()); 69 } 70 } 71 72 return report; 73 } 74 75 protected Context getContext() 76 { 77 return context; 78 } 79 80 protected String createSessionName() 81 throws JMSException 82 { 83 StringBuffer buffy = new StringBuffer (); 84 buffy.append(getClientID()); 85 buffy.append(":"+sessionCounter); 86 sessionCounter++; 87 88 return buffy.toString(); 89 } 90 91 113 114 public abstract Session createSession(boolean transacted,int acknowledgeMode) 115 throws JMSException ; 116 117 133 public String getClientID() 134 throws JMSException 135 { 136 return clientID; 137 } 138 139 181 public void setClientID(String clientID) 182 throws JMSException 183 { 184 checkClosed(); 185 if(!started) 186 { 187 SomniLogger.IT.config(this.clientID+" renamed "+clientID); 188 this.clientID=clientID; 189 } 190 else 191 { 192 throw new IllegalStateException ("Do not attempt to change the clientID after the connection has started."); 193 } 194 } 195 196 private static class SomniConnectionMetaData 197 implements ConnectionMetaData 198 { 199 public String getJMSVersion() 200 throws JMSException 201 { 202 return new String ("1.0.2b"); 203 } 204 205 public int getJMSMajorVersion() 206 throws JMSException 207 { 208 return 1; 209 } 210 211 public int getJMSMinorVersion() 212 throws JMSException 213 { 214 return 0; 215 } 216 217 public String getJMSProviderName() 218 throws JMSException 219 { 220 return "SomnifugiJMS from http://walend.net"; 221 } 222 223 public String getProviderVersion() 224 throws JMSException 225 { 226 return "0.2.0"; 227 } 228 229 public int getProviderMajorVersion() 230 throws JMSException 231 { 232 return 0; 233 } 234 235 public int getProviderMinorVersion() 236 throws JMSException 237 { 238 return 2; 239 } 240 241 private class EmptyEnumeration 242 implements Enumeration 243 { 244 public EmptyEnumeration() 245 {} 246 247 public boolean hasMoreElements() 248 { 249 return false; 250 } 251 252 public Object nextElement() 253 { 254 throw new NoSuchElementException ("Use hasMoreElements() before calling nextElement()."); 255 } 256 } 257 258 public Enumeration getJMSXPropertyNames() 259 throws JMSException 260 { 261 return new EmptyEnumeration(); 262 } 263 264 } 265 266 267 277 public ConnectionMetaData getMetaData() 278 throws JMSException 279 { 280 return new SomniConnectionMetaData(); 281 } 282 283 292 public ExceptionListener getExceptionListener() 293 throws JMSException 294 { 295 synchronized(guard) 296 { 297 return exceptionListener; 298 } 299 } 300 301 326 public void setExceptionListener(ExceptionListener listener) 327 throws JMSException 328 { 329 if(!(listener instanceof SomniExceptionListener)) 330 { 331 throw new ClassCastException ("listener, a "+listener.getClass().getName()+", must implement "+SomniExceptionListener.class.getName()); 332 } 333 synchronized(guard) 334 { 335 checkClosed(); 336 exceptionListener = listener; 337 } 338 SomniLogger.IT.config("exceptionListener in "+getClientID()+" set to a "+exceptionListener.getClass().getName()); 339 } 340 341 protected void addSession(SomniSession session) 342 { 343 synchronized(guard) 344 { 345 checkClosed(); 346 sessionSet.add(session); 347 if(started) 348 { 349 session.start(); 350 } 351 } 352 try 353 { 354 SomniLogger.IT.fine("Session "+session.getName()+" added to "+getClientID()); 355 } 356 catch(JMSException jmse) 357 { 358 exceptionListener.onException(jmse); 359 } 360 } 361 362 protected void removeSession(SomniSession session) 363 { 364 synchronized(guard) 365 { 366 checkClosed(); 367 session.stop(); 368 sessionSet.remove(session); 369 } 370 try 371 { 372 SomniLogger.IT.fine(session.getName()+" removed from "+getClientID()); 373 } 374 catch(JMSException jmse) 375 { 376 exceptionListener.onException(jmse); 377 } 378 } 379 380 381 391 public void start() 392 throws JMSException 393 { 394 synchronized(guard) 395 { 396 checkClosed(); 397 started = true; 398 Iterator it = sessionSet.iterator(); 399 while(it.hasNext()) 400 { 401 SomniSession session = (SomniSession)it.next(); 402 session.start(); 403 } 404 } 405 SomniLogger.IT.fine(getClientID()+" started successfully."); 406 } 407 408 protected void checkClosed() 409 { 410 synchronized(guard) 411 { 412 if(closed) 413 { 414 throw new IllegalStateException ("This Connection is closed."); 415 } 416 } 417 } 418 419 protected boolean isStarted() 420 { 421 synchronized(guard) 422 { 423 return started; 424 } 425 } 426 427 462 public void stop() 463 throws JMSException 464 { 465 synchronized(guard) 466 { 467 started = false; 468 Iterator it = sessionSet.iterator(); 469 while(it.hasNext()) 470 { 471 SomniSession session = (SomniSession)it.next(); 472 session.stop(); 473 } 474 } 475 SomniLogger.IT.fine(getClientID()+" stopped successfully."); 476 } 477 478 527 public void close() 528 throws JMSException 529 { 530 synchronized(guard) 531 { 532 if(closed) 533 { 534 SomniLogger.IT.info("Attempting to close "+getClientID()+" which is already closed."); 535 } 536 Iterator it = sessionSet.iterator(); 537 while(it.hasNext()) 538 { 539 SomniSession session = (SomniSession)it.next(); 540 session.close(); 541 } 542 started = false; 543 closed = true; 544 } 545 SomniLogger.IT.fine(getClientID()+" closed successfully."); 546 } 547 548 574 575 public abstract ConnectionConsumer createConnectionConsumer(Destination destination, 576 String messageSelector, 577 ServerSessionPool sessionPool, 578 int maxMessages) 579 throws JMSException ; 580 581 582 609 610 public abstract ConnectionConsumer createDurableConnectionConsumer(Topic topic, 611 String subscriptionName, 612 String messageSelector, 613 ServerSessionPool sessionPool, 614 int maxMessages) 615 throws JMSException ; 616 617 } 618 619 639 | Popular Tags |