BUG-4514: clean children in InternalJMXRegistrator
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / jmx / RootRuntimeBeanRegistratorImpl.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.util.Collections;
12 import javax.management.InstanceAlreadyExistsException;
13 import javax.management.ObjectName;
14 import org.opendaylight.controller.config.api.ModuleIdentifier;
15 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
16 import org.opendaylight.controller.config.api.runtime.RootRuntimeBeanRegistrator;
17 import org.opendaylight.controller.config.api.runtime.RuntimeBean;
18
19 public class RootRuntimeBeanRegistratorImpl implements
20         RootRuntimeBeanRegistrator {
21     private final InternalJMXRegistrator internalJMXRegistrator;
22     private final ModuleIdentifier moduleIdentifier;
23     private final ObjectName defaultRuntimeON;
24
25     RootRuntimeBeanRegistratorImpl(final InternalJMXRegistrator internalJMXRegistrator,
26             final ModuleIdentifier moduleIdentifier) {
27         this.internalJMXRegistrator = Preconditions.checkNotNull(internalJMXRegistrator);
28         this.moduleIdentifier = moduleIdentifier;
29         defaultRuntimeON = ObjectNameUtil.createRuntimeBeanName(
30                 moduleIdentifier.getFactoryName(),
31                 moduleIdentifier.getInstanceName(),
32                 Collections.<String, String> emptyMap());
33     }
34
35     @Override
36     public HierarchicalRuntimeBeanRegistrationImpl registerRoot(final RuntimeBean mxBean) {
37         try {
38             internalJMXRegistrator.registerMBean(mxBean, defaultRuntimeON);
39         } catch (InstanceAlreadyExistsException e) {
40             throw sanitize(e, moduleIdentifier, defaultRuntimeON);
41         }
42         return new HierarchicalRuntimeBeanRegistrationImpl(moduleIdentifier,
43                 internalJMXRegistrator, Collections.<String, String> emptyMap());
44     }
45
46     @Override
47     public void close() {
48         internalJMXRegistrator.close();
49     }
50
51     static IllegalStateException sanitize(final InstanceAlreadyExistsException e,
52             final ModuleIdentifier moduleIdentifier, final ObjectName on) {
53         throw new IllegalStateException("Could not register runtime bean in "
54                 + moduleIdentifier + " under name " + on, e);
55
56     }
57 }