QManLifeCycleManager.java
01 /*
02  *
03  * Licensed to the Apache Software Foundation (ASF) under one
04  * or more contributor license agreements.  See the NOTICE file
05  * distributed with this work for additional information
06  * regarding copyright ownership.  The ASF licenses this file
07  * to you under the Apache License, Version 2.0 (the
08  * "License"); you may not use this file except in compliance
09  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  */
21 package org.apache.qpid.management.servlet;
22 
23 import javax.servlet.ServletContext;
24 import javax.servlet.ServletContextEvent;
25 import javax.servlet.ServletContextListener;
26 
27 import org.apache.qpid.management.Messages;
28 import org.apache.qpid.management.Names;
29 import org.apache.qpid.management.domain.services.QMan;
30 import org.apache.qpid.management.domain.services.StartupFailureException;
31 import org.apache.qpid.transport.util.Logger;
32 
33 /**
34  * QMan JMX lifecycle manager.
35  * Provides lifecycle management of QMan JMX core including startup and shutdown. 
36  
37  @author Andrea Gazzarini
38  */
39 public class QManLifeCycleManager implements ServletContextListener 
40 {
41   private final static Logger LOGGER = Logger.get(QManLifeCycleManager.class);
42   
43   /**
44    * Starts QMan JMX Core.
45    
46    @param event the application context event.
47    */
48   public void contextInitialized(ServletContextEvent event
49   {
50     try 
51     {
52       QMan qman = new QMan();
53       qman.start();
54       event.getServletContext().setAttribute(
55           Names.APPLICATION_NAME, 
56           qman);
57     catch (StartupFailureException exception
58     {
59       LOGGER.error(
60           exception, 
61           Messages.QMAN_100030_JMX_CORE_STARTUP_FAILURE);
62     }
63   }
64   
65   /**
66    * Sutdown QMan JMX Core.
67    
68    @param event the application context event.
69    */
70   public void contextDestroyed(ServletContextEvent event
71   {
72     ServletContext context = event.getServletContext();
73     
74     QMan qman = (QMancontext.getAttribute(Names.APPLICATION_NAME);    
75     qman.stop();
76     
77     context.removeAttribute(Names.APPLICATION_NAME);
78   }  
79 }