config-manager: final parameters
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / dynamicmbean / DynamicReadableWrapper.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.dynamicmbean;
9
10 import javax.management.Attribute;
11 import javax.management.AttributeList;
12 import javax.management.AttributeNotFoundException;
13 import javax.management.InvalidAttributeValueException;
14 import javax.management.MBeanException;
15 import javax.management.MBeanServer;
16 import javax.management.ReflectionException;
17 import org.opendaylight.controller.config.api.DynamicMBeanWithInstance;
18 import org.opendaylight.controller.config.api.ModuleIdentifier;
19 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
20 import org.opendaylight.controller.config.spi.Module;
21
22 /**
23  * Wraps {@link org.opendaylight.controller.config.spi.Module} in a
24  * {@link DynamicMBeanWithInstance}. Setting attributes is disabled.
25  */
26 public class DynamicReadableWrapper extends AbstractDynamicWrapper implements
27         DynamicMBeanWithInstance {
28     private final AutoCloseable instance;
29
30     /**
31      * @param module
32      * @param instance
33      *            for recreating Module.
34      *
35      */
36     public DynamicReadableWrapper(final Module module, final AutoCloseable instance,
37             final ModuleIdentifier moduleIdentifier, final MBeanServer internalServer,
38             final MBeanServer configMBeanServer) {
39         super(module, false, moduleIdentifier, ObjectNameUtil
40                 .createReadOnlyModuleON(moduleIdentifier),
41                 getEmptyOperations(), internalServer, configMBeanServer);
42         this.instance = instance;
43     }
44
45     @Override
46     public Module getModule() {
47         return module;
48     }
49
50     @Override
51     public AutoCloseable getInstance() {
52         return instance;
53     }
54
55     @Override
56     public Object invoke(final String actionName, final Object[] params, final String[] signature)
57             throws MBeanException, ReflectionException {
58         if ("getInstance".equals(actionName)
59                 && (params == null || params.length == 0)
60                 && (signature == null || signature.length == 0)) {
61             return getInstance();
62         }
63         return super.invoke(actionName, params, signature);
64     }
65
66     @Override
67     public Object getAttribute(final String attributeName)
68             throws AttributeNotFoundException, MBeanException,
69             ReflectionException {
70         if ("getInstance".equals(attributeName)) {
71             return getInstance();
72         }
73         return super.getAttribute(attributeName);
74     }
75
76     @Override
77     public void setAttribute(final Attribute attribute)
78             throws AttributeNotFoundException, InvalidAttributeValueException,
79             MBeanException, ReflectionException {
80         throw new UnsupportedOperationException(
81                 "setAttributes is not supported on " + moduleIdentifier);
82     }
83
84     @Override
85     public AttributeList setAttributes(final AttributeList attributes) {
86         throw new UnsupportedOperationException(
87                 "setAttributes is not supported on " + moduleIdentifier);
88     }
89 }