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: 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      public ManagerServlet() {
48      }
49      
50      public SubscriptionManager getSubscriptionManager() {
51          SubscriptionManager answer = (SubscriptionManager) getServletContext().getAttribute( "subscriptionManager" );
52          if (answer == null) {
53              answer = new SubscriptionManager();
54              getServletContext().setAttribute( "subscriptionManager", answer );
55          }
56          return answer;
57      }
58  
59      // Servlet methods
60      //-------------------------------------------------------------------------    
61      
62      public synchronized void init() throws ServletException {        
63          String text = getServletContext().getInitParameter( "continueOnSubscribeException" );
64          if ( text != null && text.equals( "true" ) ) {
65              continueOnSubscribeException = true;
66          }
67          
68          // ensure Messenger is initialised
69          try {
70              SubscriptionManager subscriber = getSubscriptionManager();
71              MessengerManager manager = subscriber.getMessengerManager();
72              if ( manager == null ) {
73                  manager = createMessengerManager();
74      
75                  subscriber.setMessengerManager( manager );
76                  subscriber.setSubscriptionList( createSubscriptionList() );
77                  subscriber.setServletContext( getServletContext() );
78                  
79                  // load the subscriptions....
80                  subscriber.subscribe();
81                  
82                  // now lets start all the connections...
83                  for (Iterator iter = manager.getMessengerNames(); iter.hasNext(); ) {
84                      String name = (String) iter.next();
85                      Messenger messenger = manager.getMessenger( name );
86                      try {
87                          messenger.getConnection().start();
88                      }
89                      catch (JMSException e) {
90                          log( "Caught exception trying to start messenger: " + name + ". Exception: " + e, e );
91                      }
92                  }
93              }
94          }
95          catch (JMSException e) {
96              throw new ServletException("Failed to initialize: " + e, e );
97          }
98      }
99      
100     public void destroy() {
101         try {
102             getSubscriptionManager().unsubscribe();
103         }
104         catch (Exception e) {
105             log( "Failed to destrory the MBOs: " + e, e );
106         }
107 
108         try {        
109             MessengerManager manager = getSubscriptionManager().getMessengerManager();
110             if ( manager != null ) {
111                 log( "Closing the Messenger connections" );
112                 manager.close();
113             }
114         }
115         catch (Exception e) {
116             log( "Failed to close the Messenger Manager: " + e, e );
117         }
118         getSubscriptionManager().setMessengerManager( null );
119     }
120     
121     public void service(ServletRequest request, ServletResponse response) throws ServletException {
122     }
123 
124 
125     // Properties
126     //-------------------------------------------------------------------------    
127     public boolean isContinueOnSubscriptionException() {
128         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         this.continueOnSubscribeException = continueOnSubscribeException;
137     }
138 
139     
140     // Implementation methods
141     //-------------------------------------------------------------------------    
142     protected MessengerManager createMessengerManager() throws ServletException {
143         String config = getURLResource( KEY_CONNECTIONS, "The Messenger connections XML deployment document" );
144 
145         log( "Creating the Messenger connections from the file: " + config );
146         
147         try {
148             return MessengerManager.load( config );
149         }
150         catch (JMSException e) {
151             log( "Could not parse Messenger connection XML deployment document for URL: " + config, e );
152             
153             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         String config = getURLResource( KEY_SUBSCRIPTIONS, "The Messenger subscriptions XML deployment document" );
162         
163         log( "Loading the Messenger subscriptions from: " + config );
164         
165         try {
166             SubscriptionDigester digester = new SubscriptionDigester();
167             return (SubscriptionList) digester.parse( config );
168         }
169         catch (Exception e) {
170             log( "Could not parse Messenger subscription XML deployment document for URL: " + config, e );
171             
172             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         String config = getInitParameter( key );
181         if ( config == null || config.length() == 0 ) {
182             throw new ServletException( 
183                 "No initialization parameter for parameter: " + key 
184                 + " description: " + description 
185             );
186         }
187         try {
188             URL url = getServletContext().getResource( config );
189             config = url.toString();
190         }
191         catch (Exception e) {
192             // ignore, must be an absolute URL
193         }
194         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         log( message, exception );
203         
204         if ( ! isContinueOnSubscriptionException() ) {
205             throw new ServletException( message, exception );
206         }
207     }
208 }