/*_############################################################################
_##
_## SNMP4J-AgentJMX - MBeanActionMOScalarSupport.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.OID;
import org.snmp4j.smi.Variable;
import javax.management.ObjectName;
import javax.management.MBeanServerConnection;
import org.snmp4j.PDU;
import org.snmp4j.smi.Integer32;
import org.snmp4j.smi.SMIConstants;
import javax.management.*;
import javax.management.*;
import javax.management.*;
/**
* A MBean action is basically a method call on an MBean. An action in SNMP
* is normally modeled as an enumeration where each value specifies a possible
* action or a parameter set for an action. On the other hand, SNMP enumerations
* are also used to indicate the states of a managed object.
* <p>
* The <code>MBeanActionMOScalarSupport</code> class provides a mapping between
* these two action models for an arbitrary number of scalar instances and
* corresponding actions.
*
* @author Frank Fock
* @version 1.0
*/
public class MBeanActionMOScalarSupport extends AbstractMBeanSupport
implements JMXScalarSupport
{
public MBeanActionMOScalarSupport(MBeanServerConnection server) {
super(server);
}
/**
* Adds an action mapping to the supported mappings.
* @param oid
* the instance OID of a scalar SNMP variable.
* @param mBean
* the action mapping information of the MBean actions/states.
*/
public synchronized void add(OID oid, MBeanActionMOInfo mBean) {
oid2MBeanMap.put(oid, mBean);
}
/**
* Adds a list of action mappings related to a single MBean.
* @param mBeanName
* the <code>ObjectName</code> of the MBean providing the actions.
* @param mBeanScalarAttributeDescriptions
* an two dimensional array of action descriptions. Each description
* contains three elements:
* <ol>
* <li>the OID of the scalar SNMP instance that manages the action,</li>
* <li>an array of MBeanStateInfo instances, and</li>
* <li>an array of MBeanActionInfo instances.</li>
* </ol>
*/
public synchronized void addAll(ObjectName mBeanName,
Object[][] mBeanScalarAttributeDescriptions) {
for (Object[] attrDescr : mBeanScalarAttributeDescriptions) {
MBeanActionMOInfo mBeanInfo;
mBeanInfo = new MBeanActionMOInfo(mBeanName,
(MBeanStateInfo[]) attrDescr[1],
(MBeanActionInfo[]) attrDescr[2]);
oid2MBeanMap.put((OID)attrDescr[0], mBeanInfo);
}
}
public int checkScalarValue(OID scalarInstanceOID, Variable value) {
MBeanActionMOInfo mBeanActionMOInfo = getActionInfo(scalarInstanceOID);
if (mBeanActionMOInfo != null) {
if (value.getSyntax() != SMIConstants.SYNTAX_INTEGER32) {
return PDU.wrongType;
}
int actionID = ((Integer32)value).getValue();
for (MBeanActionInfo action : mBeanActionMOInfo.getActions()) {
if (actionID == action.getActionID()) {
return PDU.noError;
}
}
return PDU.wrongValue;
}
return PDU.resourceUnavailable;
}
public int getScalarValue(OID scalarInstanceOID, Variable value) {
MBeanActionMOInfo mBeanActionMOInfo = getActionInfo(scalarInstanceOID);
if (mBeanActionMOInfo != null) {
// get state
Integer32 v = (Integer32)value;
for (MBeanStateInfo stateInfo : mBeanActionMOInfo.getStates()) {
// check for default state
if (stateInfo.getStateAttribute() == null) {
v.setValue(stateInfo.getStateID());
return PDU.noError;
}
try {
Object attr;
if (stateInfo.getStateAttribute().getName() == null) {
attr = mBeanActionMOInfo.getLastActionResult();
}
else {
attr = MBeanAttributeMOInfo.getAttribute(server,
mBeanActionMOInfo.getObjectName(),
stateInfo.getStateAttribute());
}
if (((attr == null) && (stateInfo.getStateIndication() == null)) ||
((attr != null) && attr.equals(stateInfo.getStateIndication()))) {
v.setValue(stateInfo.getStateID());
return PDU.noError;
}
}
catch (Exception ex) {
// ignore
}
}
return PDU.genErr;
}
return PDU.noSuchName;
}
public int setScalarValue(OID scalarInstanceOID, Variable value) {
MBeanActionMOInfo mBeanActionMOInfo = getActionInfo(scalarInstanceOID);
if (mBeanActionMOInfo != null) {
int actionID = ((Integer32)value).getValue();
for (MBeanActionInfo action : mBeanActionMOInfo.getActions()) {
if (actionID == action.getActionID()) {
try {
Object result = server.invoke(mBeanActionMOInfo.getObjectName(),
action.getMethod(),
action.getParameters(),
action.getSignature());
mBeanActionMOInfo.setLastActionResult(result);
}
catch (Exception ex) {
ex.printStackTrace();
return PDU.genErr;
}
return PDU.noError;
}
}
}
return PDU.genErr;
}
private MBeanActionMOInfo getActionInfo(OID scalarInstanceOID) {
return (MBeanActionMOInfo) getMBeanMOInfo(scalarInstanceOID);
}
}