2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.pdp.rest.jmx;
23 import java.lang.management.ManagementFactory;
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;
31 import org.onap.policy.common.logging.flexlogger.FlexLogger;
32 import org.onap.policy.common.logging.flexlogger.Logger;
33 import org.onap.policy.xacml.api.XACMLErrorConstants;
40 public class PdpRestMBeanListener implements ServletContextListener {
41 private static final String JMX_OBJECT_NAME = "PdpRest:type=PdpRestMonitor";
42 private static final Logger LOGGER = FlexLogger.getLogger(PdpRestMBeanListener.class);
44 private ObjectName objectName;
47 public void contextInitialized(ServletContextEvent contextEvent) {
48 if (LOGGER.isInfoEnabled())
49 LOGGER.info("Registering.");
51 final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
53 objectName = new ObjectName(JMX_OBJECT_NAME);
54 server.registerMBean(PdpRestMonitor.getSingleton(), objectName);
55 LOGGER.info("MBean registered: " + objectName);
56 } catch (Exception e) {
58 LOGGER.warn(e.getMessage(), e);
60 LOGGER.warn(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Unable to Register " +e.getMessage(), e);
66 public void contextDestroyed(ServletContextEvent contextEvent) {
67 if (LOGGER.isInfoEnabled())
68 LOGGER.info("Unregistering");
69 final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
71 objectName = new ObjectName(JMX_OBJECT_NAME);
72 server.unregisterMBean(objectName);
73 if (LOGGER.isInfoEnabled())
74 LOGGER.info("MBean unregistered: " + objectName);
75 } catch (Exception e) {
77 LOGGER.warn(e.getMessage(), e);
79 LOGGER.warn(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Unable to Destroy Context" +e.getMessage(), e);