Merge changes Ia7cd7eda,If961a029,I9f6a57a2,Id8551489
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / jmx / HierarchicalRuntimeBeanRegistrationImpl.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 java.util.HashMap;
11 import java.util.Map;
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.HierarchicalRuntimeBeanRegistration;
17 import org.opendaylight.controller.config.api.runtime.RuntimeBean;
18
19 public class HierarchicalRuntimeBeanRegistrationImpl implements
20         HierarchicalRuntimeBeanRegistration {
21     private final ModuleIdentifier moduleIdentifier;
22     private final InternalJMXRegistrator internalJMXRegistrator;
23     private final Map<String, String> properties;
24
25     public HierarchicalRuntimeBeanRegistrationImpl(
26             ModuleIdentifier moduleIdentifier,
27             InternalJMXRegistrator internalJMXRegistrator,
28             Map<String, String> properties) {
29         this.moduleIdentifier = moduleIdentifier;
30         this.internalJMXRegistrator = internalJMXRegistrator;
31         this.properties = properties;
32     }
33
34     @Override
35     public ObjectName getObjectName() {
36         return ObjectNameUtil.createRuntimeBeanName(
37                 moduleIdentifier.getFactoryName(),
38                 moduleIdentifier.getInstanceName(), properties);
39     }
40
41     @Override
42     public HierarchicalRuntimeBeanRegistrationImpl register(String key,
43             String value, RuntimeBean mxBean) {
44         Map<String, String> currentProperties = new HashMap<>(properties);
45         currentProperties.put(key, value);
46         ObjectName on = ObjectNameUtil.createRuntimeBeanName(
47                 moduleIdentifier.getFactoryName(),
48                 moduleIdentifier.getInstanceName(), currentProperties);
49         InternalJMXRegistrator child = internalJMXRegistrator.createChild();
50         try {
51             child.registerMBean(mxBean, on);
52         } catch (InstanceAlreadyExistsException e) {
53             throw RootRuntimeBeanRegistratorImpl.sanitize(e, moduleIdentifier,
54                     on);
55         }
56         return new HierarchicalRuntimeBeanRegistrationImpl(moduleIdentifier,
57                 child, currentProperties);
58     }
59
60     @Override
61     public void close() {
62         internalJMXRegistrator.close();
63     }
64 }