Coverage Report - org.apache.commons.messenger.MessengerManager
 
Classes in this File Line Coverage Branch Coverage Complexity
MessengerManager
0%
0/50
0%
0/12
2.083
 
 1  
 /*
 2  
  * Copyright (C) The Apache Software Foundation. All rights reserved.
 3  
  *
 4  
  * This software is published under the terms of the Apache Software License
 5  
  * version 1.1, a copy of which has been included with this distribution in
 6  
  * the LICENSE file.
 7  
  * 
 8  
  * $Id: MessengerManager.java 155459 2005-02-26 13:24:44Z dirkv $
 9  
  */
 10  
 package org.apache.commons.messenger;
 11  
 
 12  
 import java.net.URL;
 13  
 import java.util.Hashtable;
 14  
 import java.util.Iterator;
 15  
 import java.util.Map;
 16  
 
 17  
 import javax.jms.JMSException;
 18  
 
 19  
 import org.apache.commons.logging.Log;
 20  
 import org.apache.commons.logging.LogFactory;
 21  
 
 22  
 /** <p><code>MessengerManager</code> is a manager of {@link Messenger} instances.</p>
 23  
   *
 24  
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
 25  
   * @version $Revision: 155459 $
 26  
   */
 27  
 public class MessengerManager {
 28  
     
 29  
     /** Logger */
 30  0
     private static final Log log = LogFactory.getLog( MessengerManager.class );
 31  
     
 32  
     /** The singleton MessengerManager used by the static get() method */
 33  
     private static MessengerManager singleton;
 34  
     
 35  
     /** A map where the key = name and value = messenger */
 36  0
     private Map messengers = new Hashtable();
 37  
 
 38  
     
 39  0
     public MessengerManager() {
 40  0
     }
 41  
     
 42  
     
 43  
     /** Uses the default configuration mechanism this method will return 
 44  
       * the Messenger for the given name. The Messenger.xml file will be searched
 45  
       * on the classpath  
 46  
       */
 47  
     public static Messenger get( String name ) throws JMSException {
 48  0
         return getInstance().getMessenger( name );
 49  
     }
 50  
     
 51  
     /** A helper method to load a MessengerManager 
 52  
       * from a given XML deployment configuration document
 53  
       */
 54  
     public static MessengerManager load( String xmlURL ) throws JMSException {
 55  
         try {
 56  0
             MessengerDigester digester = new MessengerDigester();
 57  0
             return (MessengerManager) digester.parse( xmlURL );
 58  
         }
 59  0
         catch (Exception e) {
 60  0
             JMSException newException = new JMSException( 
 61  
                 "Could not load the Messenger XML config file from: " + xmlURL 
 62  
                 + ". Underlying reason: " + e 
 63  
             );
 64  0
             newException.setLinkedException(e);
 65  0
             throw newException;
 66  
         }
 67  
     }
 68  
     
 69  
     /** A helper method to explicitly configure the MessengerManager singleton
 70  
       * from a given XML deployment configuration document
 71  
       */
 72  
     public static void configure( String xmlURL ) throws JMSException {
 73  0
         setInstance( load( xmlURL ) );
 74  0
     }
 75  
     
 76  
     /** Returns the messenger for the given name */
 77  
     public Messenger getMessenger(String name) {
 78  0
         return (Messenger) messengers.get(name);
 79  
     }
 80  
 
 81  
     public void addMessenger(Messenger messenger) {
 82  0
         messengers.put(messenger.getName(), messenger);
 83  0
     }
 84  
     
 85  
     public void removeMessenger(Messenger messenger) {
 86  0
         messengers.remove(messenger.getName());
 87  0
     }
 88  
     
 89  
     /** Returns an iterator over the names of the available Messenger instances */
 90  
     public Iterator getMessengerNames() {
 91  0
         return messengers.keySet().iterator();
 92  
     }
 93  
     
 94  
     /** Returns the singleton MessengerManager */
 95  
     public synchronized static MessengerManager getInstance() throws JMSException {
 96  0
         if ( singleton == null ) {
 97  0
             singleton = createInstance();
 98  
         }
 99  0
         return singleton;
 100  
     }
 101  
 
 102  
     /** Installs a new singleton MessengerManager instance */
 103  
     public static void setInstance(MessengerManager messengerManager) {
 104  0
         singleton = messengerManager;
 105  0
     }
 106  
 
 107  
     public void close() {
 108  0
         synchronized (messengers) {
 109  0
             for ( Iterator iter = messengers.entrySet().iterator(); iter.hasNext(); ) {
 110  0
                 Map.Entry entry = (Map.Entry) iter.next();
 111  0
                 String name = entry.getKey().toString();
 112  0
                 Messenger messenger = (Messenger) entry.getValue();
 113  0
                 iter.remove();
 114  
                 try {
 115  0
                     messenger.close();
 116  
                 }
 117  0
                 catch (Exception e) {
 118  0
                     log.error( 
 119  
                         "Caught exception trying to close messenger: " + name 
 120  
                         + " Exception: " + e,
 121  
                         e
 122  
                     );
 123  
                     // continue to close all connections even if an error occurs
 124  0
                 }
 125  0
             }
 126  0
         }
 127  0
     }
 128  
     
 129  
     // Implementation methods
 130  
     //-------------------------------------------------------------------------    
 131  
     
 132  
     /** Factory method to create the singleton MessengerManager instance */
 133  
     protected static MessengerManager createInstance() throws JMSException {
 134  0
         String config = null;
 135  
         try {
 136  0
             config = System.getProperty( "org.apache.commons.messenger" );
 137  
         }
 138  0
         catch (Exception e) {
 139  0
         }
 140  0
         if ( config != null ) {
 141  0
             return load( config );
 142  
         }
 143  0
         URL url = MessengerManager.class.getClassLoader().getResource( "Messenger.xml" );
 144  0
         if ( url == null ) {
 145  
             // lets try the threads class loader
 146  0
             ClassLoader loader = Thread.currentThread().getContextClassLoader();
 147  0
             url = loader.getResource( "Messenger.xml" );
 148  
         }
 149  0
         if ( url != null ) {             
 150  0
             return load( url.toString() );                
 151  
         }
 152  
         else {
 153  0
             throw new JMSException( "No Messenger.xml configuration document found on the CLASSPATH. Could not initialise the default MessengerManager!!" );
 154  
         }        
 155  
     }
 156  
     
 157  
 }
 158