View Javadoc

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      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      private Map messengers = new Hashtable();
37  
38      
39      public MessengerManager() {
40      }
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          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              MessengerDigester digester = new MessengerDigester();
57              return (MessengerManager) digester.parse( xmlURL );
58          }
59          catch (Exception e) {
60              JMSException newException = new JMSException( 
61                  "Could not load the Messenger XML config file from: " + xmlURL 
62                  + ". Underlying reason: " + e 
63              );
64              newException.setLinkedException(e);
65              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          setInstance( load( xmlURL ) );
74      }
75      
76      /** Returns the messenger for the given name */
77      public Messenger getMessenger(String name) {
78          return (Messenger) messengers.get(name);
79      }
80  
81      public void addMessenger(Messenger messenger) {
82          messengers.put(messenger.getName(), messenger);
83      }
84      
85      public void removeMessenger(Messenger messenger) {
86          messengers.remove(messenger.getName());
87      }
88      
89      /** Returns an iterator over the names of the available Messenger instances */
90      public Iterator getMessengerNames() {
91          return messengers.keySet().iterator();
92      }
93      
94      /** Returns the singleton MessengerManager */
95      public synchronized static MessengerManager getInstance() throws JMSException {
96          if ( singleton == null ) {
97              singleton = createInstance();
98          }
99          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