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: InitMessengerServlet.java 155459 2005-02-26 13:24:44Z dirkv $
009     */
010    package org.apache.commons.messenger;
011    
012    import java.net.URL;
013    
014    import javax.servlet.ServletException;
015    import javax.servlet.http.HttpServlet;
016    
017    
018    /** <p><code>InitMessengerServlet</code> is a simple servlet that
019     * will initialize the MessengerManager from a URL specified in the
020     * web.xml deployment descriptor.</p>
021     *
022     * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
023     * @version $Revision: 155459 $
024     */
025    public class InitMessengerServlet extends HttpServlet {
026    
027        private static boolean initialized = false;
028        
029        public InitMessengerServlet() {
030        }
031        
032        public void init() throws ServletException {
033            if ( ! initialized ) {
034                initialized = true;
035                
036                getServletContext().log( "About to initialize MessengerManager" );
037                
038                String config = getRequiredInitParmeter( "config", "The URL of the Messenger XML deployment document" );
039                try {            
040                    URL url = getServletContext().getResource( config );
041                    MessengerManager.configure( url.toString() );
042                }
043                catch (Exception e) {
044                    throw new ServletException( "Failed to initialise MessengerManager from config: " + config + ". Reason : " + e, e );
045                }
046            }
047        }
048        
049        // Implementation methods
050        //-------------------------------------------------------------------------        
051        protected String getRequiredInitParmeter(String key, String description) throws ServletException {
052            String value = getInitParameter( key );
053            if ( value == null || value.length() == 0 ) {
054                throw new ServletException( 
055                    "No initialization parameter for parameter: " + key 
056                    + " description: " + description 
057                );
058            }
059            return value;
060        }
061    }
062