Initial code drop of yang model driven configuration system
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / jmx / TransactionJMXRegistrator.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.io.Closeable;
11 import java.util.Set;
12
13 import javax.management.InstanceAlreadyExistsException;
14 import javax.management.ObjectName;
15 import javax.management.QueryExp;
16
17 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
18 import org.opendaylight.controller.config.manager.impl.jmx.InternalJMXRegistrator.InternalJMXRegistration;
19
20 /**
21  * Contains constraints on passed {@link ObjectName} parameters. Only allow (un)
22  * registration of ObjectNames that have expected transaction name.
23  */
24 public class TransactionJMXRegistrator implements Closeable {
25     private final InternalJMXRegistrator childJMXRegistrator;
26     private final String transactionName;
27
28     TransactionJMXRegistrator(InternalJMXRegistrator internalJMXRegistrator,
29             String transactionName) {
30         this.childJMXRegistrator = internalJMXRegistrator.createChild();
31         this.transactionName = transactionName;
32     }
33
34     public static class TransactionJMXRegistration implements AutoCloseable {
35         private final InternalJMXRegistration registration;
36
37         TransactionJMXRegistration(InternalJMXRegistration registration) {
38             this.registration = registration;
39         }
40
41         @Override
42         public void close() {
43             registration.close();
44         }
45     }
46
47     public TransactionJMXRegistration registerMBean(Object object, ObjectName on)
48             throws InstanceAlreadyExistsException {
49         if (!transactionName.equals(ObjectNameUtil.getTransactionName(on)))
50             throw new IllegalArgumentException(
51                     "Transaction name mismatch between expected "
52                             + transactionName + " " + "and " + on);
53         ObjectNameUtil.checkType(on, ObjectNameUtil.TYPE_CONFIG_TRANSACTION);
54         return new TransactionJMXRegistration(
55                 childJMXRegistrator.registerMBean(object, on));
56     }
57
58     public Set<ObjectName> queryNames(ObjectName name, QueryExp query) {
59         return childJMXRegistrator.queryNames(name, query);
60     }
61
62     public TransactionModuleJMXRegistrator createTransactionModuleJMXRegistrator() {
63         return new TransactionModuleJMXRegistrator(childJMXRegistrator,
64                 transactionName);
65     }
66
67     @Override
68     public void close() {
69         childJMXRegistrator.close();
70     }
71 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.