Persist service references as separate MBeans.
[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 org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
11 import org.opendaylight.controller.config.manager.impl.jmx.InternalJMXRegistrator.InternalJMXRegistration;
12
13 import javax.management.InstanceAlreadyExistsException;
14 import javax.management.ObjectName;
15
16 public interface ServiceReferenceRegistrator extends AutoCloseable {
17
18     public String getNullableTransactionName();
19
20     ServiceReferenceJMXRegistration registerMBean(ServiceReferenceMXBeanImpl object,
21                                                           ObjectName on) throws InstanceAlreadyExistsException;
22
23     @Override
24     void close();
25
26     public static class ServiceReferenceJMXRegistration implements AutoCloseable {
27         private final InternalJMXRegistration registration;
28
29         ServiceReferenceJMXRegistration(InternalJMXRegistration registration) {
30             this.registration = registration;
31         }
32
33         @Override
34         public void close() {
35             registration.close();
36         }
37     }
38
39     public static interface ServiceReferenceTransactionRegistratorFactory {
40         public ServiceReferenceRegistrator create();
41     }
42
43     public static class ServiceReferenceRegistratorImpl implements ServiceReferenceRegistrator {
44         private final InternalJMXRegistrator currentJMXRegistrator;
45         private final String nullableTransactionName;
46
47         public ServiceReferenceRegistratorImpl(NestableJMXRegistrator parentRegistrator, String nullableTransactionName){
48             currentJMXRegistrator = parentRegistrator.createChild();
49             this.nullableTransactionName = nullableTransactionName;
50         }
51
52         public String getNullableTransactionName() {
53             return nullableTransactionName;
54         }
55
56
57         public ServiceReferenceJMXRegistration registerMBean(ServiceReferenceMXBeanImpl object,
58                                                              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) == false;
63             if (broken) {
64                 throw new IllegalArgumentException("Transaction name mismatch between expected "
65                         + nullableTransactionName + ", got " + actualTransactionName + " in " + on);
66             }
67             if (ObjectNameUtil.isServiceReference(on) == false) {
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 static interface ServiceReferenceTransactionRegistratorFactory {
79             public ServiceReferenceRegistrator create();
80         }
81     }
82
83
84     public static class ServiceReferenceTransactionRegistratorFactoryImpl implements ServiceReferenceTransactionRegistratorFactory {
85         private final NestableJMXRegistrator parentRegistrator;
86         private final String nullableTransactionName;
87
88         public ServiceReferenceTransactionRegistratorFactoryImpl(TransactionModuleJMXRegistrator parentRegistrator,
89                                                              String nullableTransactionName) {
90             this.parentRegistrator = parentRegistrator;
91             this.nullableTransactionName = nullableTransactionName;
92         }
93
94         public ServiceReferenceTransactionRegistratorFactoryImpl(BaseJMXRegistrator baseJMXRegistrator) {
95             this.parentRegistrator = baseJMXRegistrator;
96             this.nullableTransactionName = null;
97         }
98
99         public ServiceReferenceRegistrator create() {
100             return new ServiceReferenceRegistratorImpl(parentRegistrator, nullableTransactionName);
101         }
102     }
103 }