Mechanical code cleanup (config)
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / jmx / ServiceReferenceRegistrator.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 javax.management.InstanceAlreadyExistsException;
11 import javax.management.ObjectName;
12 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
13
14 public interface ServiceReferenceRegistrator extends AutoCloseable {
15
16     String getNullableTransactionName();
17
18     ServiceReferenceJMXRegistration registerMBean(ServiceReferenceMXBeanImpl object,
19                                                           ObjectName on) throws InstanceAlreadyExistsException;
20
21     @Override
22     void close();
23
24     class ServiceReferenceJMXRegistration implements AutoCloseable {
25         private final InternalJMXRegistration registration;
26
27         ServiceReferenceJMXRegistration(InternalJMXRegistration registration) {
28             this.registration = registration;
29         }
30
31         @Override
32         public void close() {
33             registration.close();
34         }
35     }
36
37     interface ServiceReferenceTransactionRegistratorFactory {
38         ServiceReferenceRegistrator create();
39     }
40
41     class ServiceReferenceRegistratorImpl implements ServiceReferenceRegistrator {
42         private final InternalJMXRegistrator currentJMXRegistrator;
43         private final String nullableTransactionName;
44
45         public ServiceReferenceRegistratorImpl(NestableJMXRegistrator parentRegistrator, String nullableTransactionName){
46             currentJMXRegistrator = parentRegistrator.createChild();
47             this.nullableTransactionName = nullableTransactionName;
48         }
49
50         public String getNullableTransactionName() {
51             return nullableTransactionName;
52         }
53
54
55         public ServiceReferenceJMXRegistration registerMBean(ServiceReferenceMXBeanImpl object,
56                                                              ObjectName on) throws InstanceAlreadyExistsException {
57             String actualTransactionName = ObjectNameUtil.getTransactionName(on);
58             boolean broken = false;
59             broken |= (nullableTransactionName == null) != (actualTransactionName == null);
60             broken |= (nullableTransactionName != null) && nullableTransactionName.equals(actualTransactionName) == false;
61             if (broken) {
62                 throw new IllegalArgumentException("Transaction name mismatch between expected "
63                         + nullableTransactionName + ", got " + actualTransactionName + " in " + on);
64             }
65             if (ObjectNameUtil.isServiceReference(on) == false) {
66                 throw new IllegalArgumentException("Invalid type of " + on);
67             }
68             return new ServiceReferenceJMXRegistration(currentJMXRegistrator.registerMBean(object, on));
69         }
70
71
72         @Override
73         public void close() {
74             currentJMXRegistrator.close();
75         }
76         public interface ServiceReferenceTransactionRegistratorFactory {
77             ServiceReferenceRegistrator create();
78         }
79     }
80
81
82     class ServiceReferenceTransactionRegistratorFactoryImpl implements ServiceReferenceTransactionRegistratorFactory {
83         private final NestableJMXRegistrator parentRegistrator;
84         private final String nullableTransactionName;
85
86         public ServiceReferenceTransactionRegistratorFactoryImpl(TransactionModuleJMXRegistrator parentRegistrator,
87                                                              String nullableTransactionName) {
88             this.parentRegistrator = parentRegistrator;
89             this.nullableTransactionName = nullableTransactionName;
90         }
91
92         public ServiceReferenceTransactionRegistratorFactoryImpl(BaseJMXRegistrator baseJMXRegistrator) {
93             this.parentRegistrator = baseJMXRegistrator;
94             this.nullableTransactionName = null;
95         }
96
97         public ServiceReferenceRegistrator create() {
98             return new ServiceReferenceRegistratorImpl(parentRegistrator, nullableTransactionName);
99         }
100     }
101 }

©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.