e84d2ee953efdc2c46bd8e4ee465500fac146653
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / jmx / RootRuntimeBeanRegistratorImpl.java
1 /*
2  * Copyright (c) 2013, 2017 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 RootRuntimeBeanRegistrator {
20     private final InternalJMXRegistrator internalJMXRegistrator;
21     private final ModuleIdentifier moduleIdentifier;
22     private final ObjectName defaultRuntimeON;
23
24     RootRuntimeBeanRegistratorImpl(final InternalJMXRegistrator internalJMXRegistrator,
25             final ModuleIdentifier moduleIdentifier) {
26         this.internalJMXRegistrator = Preconditions.checkNotNull(internalJMXRegistrator);
27         this.moduleIdentifier = moduleIdentifier;
28         defaultRuntimeON = ObjectNameUtil.createRuntimeBeanName(moduleIdentifier.getFactoryName(),
29                 moduleIdentifier.getInstanceName(), Collections.<String, String>emptyMap());
30     }
31
32     @Override
33     public HierarchicalRuntimeBeanRegistrationImpl registerRoot(final RuntimeBean mxBean) {
34         try {
35             internalJMXRegistrator.registerMBean(mxBean, defaultRuntimeON);
36         } catch (final InstanceAlreadyExistsException e) {
37             throw sanitize(e, moduleIdentifier, defaultRuntimeON);
38         }
39         return new HierarchicalRuntimeBeanRegistrationImpl(moduleIdentifier, internalJMXRegistrator,
40                 Collections.<String, String>emptyMap());
41     }
42
43     @Override
44     public void close() {
45         internalJMXRegistrator.close();
46     }
47
48     static IllegalStateException sanitize(final InstanceAlreadyExistsException exception,
49             final ModuleIdentifier moduleIdentifier, final ObjectName on) {
50         throw new IllegalStateException("Could not register runtime bean in " + moduleIdentifier + " under name " + on,
51                 exception);
52     }
53 }