/*_############################################################################
_##
_## SNMP4J-AgentJMX - MBeanAttributeMOScalarSupport.java
_##
_## Copyright (C) 2006-2009 Frank Fock (SNMP4J.org)
_##
_## This program is free software; you can redistribute it and/or modify
_## it under the terms of the GNU General Public License version 2 as
_## published by the Free Software Foundation.
_##
_## This program is distributed in the hope that it will be useful,
_## but WITHOUT ANY WARRANTY; without even the implied warranty of
_## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
_## GNU General Public License for more details.
_##
_## You should have received a copy of the GNU General Public License
_## along with this program; if not, write to the Free Software
_## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
_## MA 02110-1301 USA
_##
_##########################################################################*/
package org.snmp4j.agent.mo.jmx;
import org.snmp4j.smi.*;
import java.util.Map;
import javax.management.MBeanServerConnection;
import org.snmp4j.PDU;
import org.snmp4j.agent.mo.jmx.types.SMIVariant;
import org.snmp4j.agent.mo.jmx.types.TypedAttribute;
import org.snmp4j.agent.mo.jmx.types.CombinedTypedAttribute;
import javax.management.ObjectName;
// For JavaDoc
import org.snmp4j.agent.mo.MOScalar;
/**
* <code>MBeanAttributeMOScalarSupport</code> objects map zero or more MBean
* attributes to their corresponding {@link MOScalar} instance.
*
* @author Frank Fock
* @version 1.0
*/
public class MBeanAttributeMOScalarSupport extends AbstractMBeanSupport
implements JMXScalarSupport {
public MBeanAttributeMOScalarSupport(MBeanServerConnection server) {
super(server);
}
/**
* Adds a scalar to MBean attribute mapping.
* @param oid
* the instance OID (including the .0) of the SNMP scalar object.
* @param mBean
* an attribute description of a MBean.
*/
public synchronized void add(OID oid, MBeanAttributeMOInfo mBean) {
oid2MBeanMap.put(oid, mBean);
}
/**
* Adds a list of scalar to attribute mappings for the specified MBean.
* @param mBeanName
* the name of the MBean providing the attributes.
* @param mBeanScalarAttributeDescriptions
* an two dimensional array of scalar descriptions. Each description
* contains three elements:
* <ol>
* <li>the <code>OID</code> of the scalar SNMP instance,</li>
* <li>the name of the attribute as <code>String</code>, and</li>
* <li>the <code>Class</code> of the attributes value.</li>
* </ol>
*/
public synchronized void addAll(ObjectName mBeanName,
Object[][] mBeanScalarAttributeDescriptions) {
for (Object[] attrDescr : mBeanScalarAttributeDescriptions) {
MBeanAttributeMOInfo mBeanInfo;
if (attrDescr[1] instanceof CombinedTypedAttribute) {
mBeanInfo = new MBeanMultiAttributeMOInfo(mBeanName,
(CombinedTypedAttribute)
attrDescr[1]);
}
else if (attrDescr[1] instanceof TypedAttribute) {
mBeanInfo = new MBeanAttributeMOInfo(mBeanName,
(TypedAttribute) attrDescr[1]);
}
else {
mBeanInfo = new MBeanAttributeMOInfo(mBeanName,
(String) attrDescr[1],
(Class) attrDescr[2]);
}
oid2MBeanMap.put((OID)attrDescr[0], mBeanInfo);
}
}
/**
* Checks the value of the specified object instance and type.
*
* @param scalarInstanceOID the instance OID of the target object.
* @param value the instance's new value.
* @return zero on success or a SNMP error status value if setting the value
* would fail.
*/
public int checkScalarValue(OID scalarInstanceOID, Variable value) {
MBeanAttributeMOInfo mBeanAttrMOInfo =
(MBeanAttributeMOInfo) getMBeanMOInfo(scalarInstanceOID);
if (mBeanAttrMOInfo != null) {
Object attr = mBeanAttrMOInfo.getAttribute().transformSMI2Object(value);
if (attr == null) {
return PDU.wrongValue;
}
return PDU.noError;
}
return PDU.resourceUnavailable;
}
/**
* Gets the actual value for the specified object instance and type.
*
* @param scalarInstanceOID the instance OID of the target object.
* @param value the instance to hold the return value.
* @return zero on success or a SNMP error status value if fetching the
* value fails.
*/
public int getScalarValue(OID scalarInstanceOID, Variable value) {
MBeanAttributeMOInfo mBeanAttrMOInfo =
(MBeanAttributeMOInfo) getMBeanMOInfo(scalarInstanceOID);
if (mBeanAttrMOInfo != null) {
try {
Object attr = mBeanAttrMOInfo.getAttribute(server);
if (attr == null) {
return PDU.noError;
}
SMIVariant v = new SMIVariant(value);
return v.setValue(attr);
}
catch (Exception ex) {
ex.printStackTrace();
}
return PDU.genErr;
}
return PDU.noSuchName;
}
/**
* Sets the value of the specified object instance and type.
*
* @param scalarInstanceOID the instance OID of the target object.
* @param value the instance's new value.
* @return zero on success or a SNMP error status value if setting the value
* fails.
*/
public int setScalarValue(OID scalarInstanceOID, Variable value) {
MBeanAttributeMOInfo mBeanAttrMOInfo =
(MBeanAttributeMOInfo) getMBeanMOInfo(scalarInstanceOID);
if (mBeanAttrMOInfo != null) {
try {
Object attr = mBeanAttrMOInfo.getAttribute().transformSMI2Object(value);
mBeanAttrMOInfo.setAttribute(server, attr);
return PDU.noError;
}
catch (Exception ex) {
ex.printStackTrace();
}
}
return PDU.genErr;
}
}