Coverage Report - org.apache.commons.messagelet.ManagerServlet
 
Classes in this File Line Coverage Branch Coverage Complexity
ManagerServlet
0%
0/72
0%
0/18
2.909
 
 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: ManagerServlet.java 155459 2005-02-26 13:24:44Z dirkv $
 9  
  */
 10  
 package org.apache.commons.messagelet;
 11  
 
 12  
 import java.net.URL;
 13  
 import java.util.Iterator;
 14  
 
 15  
 import javax.jms.JMSException;
 16  
 import javax.servlet.GenericServlet;
 17  
 import javax.servlet.ServletException;
 18  
 import javax.servlet.ServletRequest;
 19  
 import javax.servlet.ServletResponse;
 20  
 
 21  
 import org.apache.commons.messagelet.model.SubscriptionDigester;
 22  
 import org.apache.commons.messagelet.model.SubscriptionList;
 23  
 import org.apache.commons.messenger.Messenger;
 24  
 import org.apache.commons.messenger.MessengerManager;
 25  
 
 26  
 /** <p><code>ManagerServlet</code> manages the 
 27  
   * initialisation and destruction of the Messenger connections
 28  
   * and use of MessageListener beans for a given ServletContext.</p>
 29  
   *
 30  
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
 31  
   * @version $Revision: 155459 $
 32  
   */
 33  
 public class ManagerServlet extends GenericServlet {
 34  
 
 35  
     /** Should HTTP servlets be used or generic servlets. If true then JSP can be dispatched to easily */
 36  
     private static final boolean USE_HTTP_SERVLETS = true;
 37  
 
 38  
     private static final String KEY_CONNECTIONS = "connections";
 39  
     private static final String KEY_SUBSCRIPTIONS = "subscriptions";
 40  
 
 41  
     /** 
 42  
      * Whether exceptions occurring during subscriptions on startup should 
 43  
      * terminate the initialization
 44  
      */
 45  
     private boolean continueOnSubscribeException;
 46  
     
 47  0
     public ManagerServlet() {
 48  0
     }
 49  
     
 50  
     public SubscriptionManager getSubscriptionManager() {
 51  0
         SubscriptionManager answer = (SubscriptionManager) getServletContext().getAttribute( "subscriptionManager" );
 52  0
         if (answer == null) {
 53  0
             answer = new SubscriptionManager();
 54  0
             getServletContext().setAttribute( "subscriptionManager", answer );
 55  
         }
 56  0
         return answer;
 57  
     }
 58  
 
 59  
     // Servlet methods
 60  
     //-------------------------------------------------------------------------    
 61  
     
 62  
     public synchronized void init() throws ServletException {        
 63  0
         String text = getServletContext().getInitParameter( "continueOnSubscribeException" );
 64  0
         if ( text != null && text.equals( "true" ) ) {
 65  0
             continueOnSubscribeException = true;
 66  
         }
 67  
         
 68  
         // ensure Messenger is initialised
 69  
         try {
 70  0
             SubscriptionManager subscriber = getSubscriptionManager();
 71  0
             MessengerManager manager = subscriber.getMessengerManager();
 72  0
             if ( manager == null ) {
 73  0
                 manager = createMessengerManager();
 74  
     
 75  0
                 subscriber.setMessengerManager( manager );
 76  0
                 subscriber.setSubscriptionList( createSubscriptionList() );
 77  0
                 subscriber.setServletContext( getServletContext() );
 78  
                 
 79  
                 // load the subscriptions....
 80  0
                 subscriber.subscribe();
 81  
                 
 82  
                 // now lets start all the connections...
 83  0
                 for (Iterator iter = manager.getMessengerNames(); iter.hasNext(); ) {
 84  0
                     String name = (String) iter.next();
 85  0
                     Messenger messenger = manager.getMessenger( name );
 86  
                     try {
 87  0
                         messenger.getConnection().start();
 88  
                     }
 89  0
                     catch (JMSException e) {
 90  0
                         log( "Caught exception trying to start messenger: " + name + ". Exception: " + e, e );
 91  0
                     }
 92  0
                 }
 93  
             }
 94  
         }
 95  0
         catch (JMSException e) {
 96  0
             throw new ServletException("Failed to initialize: " + e, e );
 97  0
         }
 98  0
     }
 99  
     
 100  
     public void destroy() {
 101  
         try {
 102  0
             getSubscriptionManager().unsubscribe();
 103  
         }
 104  0
         catch (Exception e) {
 105  0
             log( "Failed to destrory the MBOs: " + e, e );
 106  0
         }
 107  
 
 108  
         try {        
 109  0
             MessengerManager manager = getSubscriptionManager().getMessengerManager();
 110  0
             if ( manager != null ) {
 111  0
                 log( "Closing the Messenger connections" );
 112  0
                 manager.close();
 113  
             }
 114  
         }
 115  0
         catch (Exception e) {
 116  0
             log( "Failed to close the Messenger Manager: " + e, e );
 117  0
         }
 118  0
         getSubscriptionManager().setMessengerManager( null );
 119  0
     }
 120  
     
 121  
     public void service(ServletRequest request, ServletResponse response) throws ServletException {
 122  0
     }
 123  
 
 124  
 
 125  
     // Properties
 126  
     //-------------------------------------------------------------------------    
 127  
     public boolean isContinueOnSubscriptionException() {
 128  0
         return continueOnSubscribeException;
 129  
     }
 130  
     
 131  
     /** 
 132  
      * Sets whether exceptions occurring during subscriptions on startup should 
 133  
      * terminate the initialization
 134  
      */
 135  
     public void setContinueOnSubscribeException(boolean continueOnSubscribeException) {
 136  0
         this.continueOnSubscribeException = continueOnSubscribeException;
 137  0
     }
 138  
 
 139  
     
 140  
     // Implementation methods
 141  
     //-------------------------------------------------------------------------    
 142  
     protected MessengerManager createMessengerManager() throws ServletException {
 143  0
         String config = getURLResource( KEY_CONNECTIONS, "The Messenger connections XML deployment document" );
 144  
 
 145  0
         log( "Creating the Messenger connections from the file: " + config );
 146  
         
 147  
         try {
 148  0
             return MessengerManager.load( config );
 149  
         }
 150  0
         catch (JMSException e) {
 151  0
             log( "Could not parse Messenger connection XML deployment document for URL: " + config, e );
 152  
             
 153  0
             throw new ServletException(
 154  
                 "Could not parse Messenger connection XML deployment document for URL: " + config
 155  
                 + " reason: " + e, e
 156  
             );
 157  
         }
 158  
     }
 159  
     
 160  
     protected SubscriptionList createSubscriptionList() throws ServletException {
 161  0
         String config = getURLResource( KEY_SUBSCRIPTIONS, "The Messenger subscriptions XML deployment document" );
 162  
         
 163  0
         log( "Loading the Messenger subscriptions from: " + config );
 164  
         
 165  
         try {
 166  0
             SubscriptionDigester digester = new SubscriptionDigester();
 167  0
             return (SubscriptionList) digester.parse( config );
 168  
         }
 169  0
         catch (Exception e) {
 170  0
             log( "Could not parse Messenger subscription XML deployment document for URL: " + config, e );
 171  
             
 172  0
             throw new ServletException(
 173  
                 "Could not parse Messenger subscription XML deployment document for URL: " + config
 174  
                 + " reason: " + e, e
 175  
             );
 176  
         }
 177  
     }
 178  
 
 179  
     protected String getURLResource(String key, String description) throws ServletException {
 180  0
         String config = getInitParameter( key );
 181  0
         if ( config == null || config.length() == 0 ) {
 182  0
             throw new ServletException( 
 183  
                 "No initialization parameter for parameter: " + key 
 184  
                 + " description: " + description 
 185  
             );
 186  
         }
 187  
         try {
 188  0
             URL url = getServletContext().getResource( config );
 189  0
             config = url.toString();
 190  
         }
 191  0
         catch (Exception e) {
 192  
             // ignore, must be an absolute URL
 193  0
         }
 194  0
         return config;
 195  
     }
 196  
 
 197  
     /**
 198  
      * Allows derived servlets to handle JMS exceptions differently, such as ignoring certain kinds of
 199  
      * exceptions or performing custom logging etc.
 200  
      */
 201  
     protected void handleJMSException(String message, JMSException exception) throws ServletException {    
 202  0
         log( message, exception );
 203  
         
 204  0
         if ( ! isContinueOnSubscriptionException() ) {
 205  0
             throw new ServletException( message, exception );
 206  
         }
 207  0
     }
 208  
 }