001    /*
002     * Copyright (C) The Apache Software Foundation. All rights reserved.
003     *
004     * This software is published under the terms of the Apache Software License
005     * version 1.1, a copy of which has been included with this distribution in
006     * the LICENSE file.
007     * 
008     * $Id: MessengerManager.java 155459 2005-02-26 13:24:44Z dirkv $
009     */
010    package org.apache.commons.messenger;
011    
012    import java.net.URL;
013    import java.util.Hashtable;
014    import java.util.Iterator;
015    import java.util.Map;
016    
017    import javax.jms.JMSException;
018    
019    import org.apache.commons.logging.Log;
020    import org.apache.commons.logging.LogFactory;
021    
022    /** <p><code>MessengerManager</code> is a manager of {@link Messenger} instances.</p>
023      *
024      * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
025      * @version $Revision: 155459 $
026      */
027    public class MessengerManager {
028        
029        /** Logger */
030        private static final Log log = LogFactory.getLog( MessengerManager.class );
031        
032        /** The singleton MessengerManager used by the static get() method */
033        private static MessengerManager singleton;
034        
035        /** A map where the key = name and value = messenger */
036        private Map messengers = new Hashtable();
037    
038        
039        public MessengerManager() {
040        }
041        
042        
043        /** Uses the default configuration mechanism this method will return 
044          * the Messenger for the given name. The Messenger.xml file will be searched
045          * on the classpath  
046          */
047        public static Messenger get( String name ) throws JMSException {
048            return getInstance().getMessenger( name );
049        }
050        
051        /** A helper method to load a MessengerManager 
052          * from a given XML deployment configuration document
053          */
054        public static MessengerManager load( String xmlURL ) throws JMSException {
055            try {
056                MessengerDigester digester = new MessengerDigester();
057                return (MessengerManager) digester.parse( xmlURL );
058            }
059            catch (Exception e) {
060                JMSException newException = new JMSException( 
061                    "Could not load the Messenger XML config file from: " + xmlURL 
062                    + ". Underlying reason: " + e 
063                );
064                newException.setLinkedException(e);
065                throw newException;
066            }
067        }
068        
069        /** A helper method to explicitly configure the MessengerManager singleton
070          * from a given XML deployment configuration document
071          */
072        public static void configure( String xmlURL ) throws JMSException {
073            setInstance( load( xmlURL ) );
074        }
075        
076        /** Returns the messenger for the given name */
077        public Messenger getMessenger(String name) {
078            return (Messenger) messengers.get(name);
079        }
080    
081        public void addMessenger(Messenger messenger) {
082            messengers.put(messenger.getName(), messenger);
083        }
084        
085        public void removeMessenger(Messenger messenger) {
086            messengers.remove(messenger.getName());
087        }
088        
089        /** Returns an iterator over the names of the available Messenger instances */
090        public Iterator getMessengerNames() {
091            return messengers.keySet().iterator();
092        }
093        
094        /** Returns the singleton MessengerManager */
095        public synchronized static MessengerManager getInstance() throws JMSException {
096            if ( singleton == null ) {
097                singleton = createInstance();
098            }
099            return singleton;
100        }
101    
102        /** Installs a new singleton MessengerManager instance */
103        public static void setInstance(MessengerManager messengerManager) {
104            singleton = messengerManager;
105        }
106    
107        public void close() {
108            synchronized (messengers) {
109                for ( Iterator iter = messengers.entrySet().iterator(); iter.hasNext(); ) {
110                    Map.Entry entry = (Map.Entry) iter.next();
111                    String name = entry.getKey().toString();
112                    Messenger messenger = (Messenger) entry.getValue();
113                    iter.remove();
114                    try {
115                        messenger.close();
116                    }
117                    catch (Exception e) {
118                        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                    }
125                }
126            }
127        }
128        
129        // Implementation methods
130        //-------------------------------------------------------------------------    
131        
132        /** Factory method to create the singleton MessengerManager instance */
133        protected static MessengerManager createInstance() throws JMSException {
134            String config = null;
135            try {
136                config = System.getProperty( "org.apache.commons.messenger" );
137            }
138            catch (Exception e) {
139            }
140            if ( config != null ) {
141                return load( config );
142            }
143            URL url = MessengerManager.class.getClassLoader().getResource( "Messenger.xml" );
144            if ( url == null ) {
145                // lets try the threads class loader
146                ClassLoader loader = Thread.currentThread().getContextClassLoader();
147                url = loader.getResource( "Messenger.xml" );
148            }
149            if ( url != null ) {             
150                return load( url.toString() );                
151            }
152            else {
153                throw new JMSException( "No Messenger.xml configuration document found on the CLASSPATH. Could not initialise the default MessengerManager!!" );
154            }        
155        }
156        
157    }
158