config-manager: final parameters
[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(final 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(final NestableJMXRegistrator parentRegistrator, final String nullableTransactionName){
46             currentJMXRegistrator = parentRegistrator.createChild();
47             this.nullableTransactionName = nullableTransactionName;
48         }
49
50         @Override
51         public String getNullableTransactionName() {
52             return nullableTransactionName;
53         }
54
55
56         @Override
57         public ServiceReferenceJMXRegistration registerMBean(final ServiceReferenceMXBeanImpl object,
58                                                              final ObjectName on) throws InstanceAlreadyExistsException {
59             String actualTransactionName = ObjectNameUtil.getTransactionName(on);
60             boolean broken = false;
61             broken |= (nullableTransactionName == null) != (actualTransactionName == null);
62             broken |= (nullableTransactionName != null) && !nullableTransactionName.equals(actualTransactionName);
63             if (broken) {
64                 throw new IllegalArgumentException("Transaction name mismatch between expected "
65                         + nullableTransactionName + ", got " + actualTransactionName + " in " + on);
66             }
67             if (!ObjectNameUtil.isServiceReference(on)) {
68                 throw new IllegalArgumentException("Invalid type of " + on);
69             }
70             return new ServiceReferenceJMXRegistration(currentJMXRegistrator.registerMBean(object, on));
71         }
72
73
74         @Override
75         public void close() {
76             currentJMXRegistrator.close();
77         }
78         public interface ServiceReferenceTransactionRegistratorFactory {
79             ServiceReferenceRegistrator create();
80         }
81     }
82
83
84     class ServiceReferenceTransactionRegistratorFactoryImpl implements ServiceReferenceTransactionRegistratorFactory {
85         private final NestableJMXRegistrator parentRegistrator;
86         private final String nullableTransactionName;
87
88         public ServiceReferenceTransactionRegistratorFactoryImpl(final TransactionModuleJMXRegistrator parentRegistrator,
89                                                              final String nullableTransactionName) {
90             this.parentRegistrator = parentRegistrator;
91             this.nullableTransactionName = nullableTransactionName;
92         }
93
94         public ServiceReferenceTransactionRegistratorFactoryImpl(final BaseJMXRegistrator baseJMXRegistrator) {
95             this.parentRegistrator = baseJMXRegistrator;
96             this.nullableTransactionName = null;
97         }
98
99         @Override
100         public ServiceReferenceRegistrator create() {
101             return new ServiceReferenceRegistratorImpl(parentRegistrator, nullableTransactionName);
102         }
103     }
104 }