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

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