Initial OpenECOMP policy/engine commit
[policy/engine.git] / PyPDPServer / src / main / java / org / openecomp / policy / pypdp / jmx / PyPdpMBeanListener.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * 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, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.policy.pypdp.jmx;
22
23 import java.lang.management.ManagementFactory;
24
25 import javax.management.MBeanServer;
26 import javax.management.ObjectName;
27 import javax.servlet.ServletContextEvent;
28 import javax.servlet.ServletContextListener;
29 import javax.servlet.annotation.WebListener;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
35 import org.openecomp.policy.common.logging.flexlogger.Logger;
36
37 @WebListener
38 public class PyPdpMBeanListener implements ServletContextListener {
39         private static final String JMX_OBJECT_NAME = "PyPdp:type=PyPdpMonitor";
40 //      private static final Log logger = LogFactory.getLog(PyPdpMBeanListener.class);
41         private static final Logger logger = FlexLogger.getLogger(PyPdpMBeanListener.class);
42         
43         private ObjectName objectName;
44
45         @Override
46         public void contextInitialized(ServletContextEvent contextEvent) {
47         if (logger.isInfoEnabled())
48                 logger.info("Registering.");
49         
50         final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
51         try {
52             objectName = new ObjectName(JMX_OBJECT_NAME);
53             server.registerMBean(PyPdpMonitor.singleton, objectName);
54             logger.info("MBean registered: " + objectName);
55         } catch (Exception e) {
56             logger.warn(e.getMessage(), e);
57         }
58         }
59         
60         @Override
61         public void contextDestroyed(ServletContextEvent arg0) {
62                 if (logger.isInfoEnabled())
63                         logger.info("Unregistering");
64         final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
65         try {
66             objectName = new ObjectName(JMX_OBJECT_NAME);
67             server.unregisterMBean(objectName);
68             if (logger.isInfoEnabled())
69                 logger.info("MBean unregistered: " + objectName);
70         } catch (Exception e) {
71             logger.warn(e.getMessage(), e);
72         }
73         }
74
75 }