BUG-4514: clean children in InternalJMXRegistrator
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / jmx / ModuleJMXRegistrator.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.config.manager.impl.jmx;
9
10 import com.google.common.base.Preconditions;
11 import java.io.Closeable;
12 import javax.annotation.concurrent.ThreadSafe;
13 import javax.management.InstanceAlreadyExistsException;
14 import javax.management.ObjectName;
15 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
16
17 /**
18  * This subclass is used for registering readable module into JMX, it is also
19  * used as underlying provider in {@link RuntimeBeanRegistratorImpl}. Closing
20  * the instance thus unregisters all JMX beans related to the module excluding
21  * currently open transactions.
22  */
23 @ThreadSafe
24 public class ModuleJMXRegistrator implements Closeable {
25     private final InternalJMXRegistrator childJMXRegistrator;
26
27     ModuleJMXRegistrator(final InternalJMXRegistrator internalJMXRegistrator) {
28         this.childJMXRegistrator = Preconditions.checkNotNull(internalJMXRegistrator);
29     }
30
31     static class ModuleJMXRegistration implements AutoCloseable {
32         private final InternalJMXRegistration internalJMXRegistration;
33
34         ModuleJMXRegistration(final InternalJMXRegistration registration) {
35             this.internalJMXRegistration = registration;
36         }
37
38         @Override
39         public void close() {
40             internalJMXRegistration.close();
41         }
42     }
43
44     public ModuleJMXRegistration registerMBean(final Object object, final ObjectName on)
45             throws InstanceAlreadyExistsException {
46         ObjectNameUtil.checkType(on, ObjectNameUtil.TYPE_MODULE);
47         if (ObjectNameUtil.getTransactionName(on) != null) {
48             throw new IllegalArgumentException(
49                     "Transaction name not expected in " + on);
50         }
51         return new ModuleJMXRegistration(childJMXRegistrator.registerMBean(
52                 object, on));
53     }
54
55     @Override
56     public void close() {
57         childJMXRegistrator.close();
58     }
59
60 }