[POLICY-52] pdp-d: PolicyEngine junits
[policy/drools-pdp.git] / policy-core / src / main / java / org / openecomp / policy / drools / core / jmx / PdpJmxListener.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-core
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.drools.core.jmx;
22
23 import java.lang.management.ManagementFactory;
24
25 import javax.management.InstanceAlreadyExistsException;
26 import javax.management.InstanceNotFoundException;
27 import javax.management.MBeanRegistrationException;
28 import javax.management.MBeanServer;
29 import javax.management.MalformedObjectNameException;
30 import javax.management.NotCompliantMBeanException;
31 import javax.management.ObjectName;
32
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class PdpJmxListener {
37
38         public static final Logger logger = LoggerFactory.getLogger(PdpJmxListener.class);
39         
40         public static void stop() {
41                 final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
42                 try {
43                         server.unregisterMBean(new ObjectName("PolicyEngine:type=PdpJmx"));
44                 } catch (MBeanRegistrationException | InstanceNotFoundException
45                                 | MalformedObjectNameException e) {
46                         logger.error("PdpJmxListener.stop(): " + 
47                                          "Could not unregister PolicyEngine:type=PdpJmx MBean " +
48                                              "with the MBean server", e);
49                 }
50                 
51         }
52
53         
54         public static void start() {
55                 final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
56                 try {
57                         server.registerMBean(PdpJmx.getInstance(), new ObjectName("PolicyEngine:type=PdpJmx"));
58                 } catch (InstanceAlreadyExistsException | MBeanRegistrationException
59                                 | NotCompliantMBeanException | MalformedObjectNameException e) {
60                         logger.error("PdpJmxListener.start(): " + 
61                                      "Could not unregister PolicyEngine:type=PdpJmx MBean " +
62                                          "with the MBean server", e);
63                 }
64                 
65         }
66
67 }