KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snmp4j > agent > mo > jmx > MBeanActionMOScalarSupport


1 /*_############################################################################
2   _##
3   _## SNMP4J-AgentJMX - MBeanActionMOScalarSupport.java
4   _##
5   _## Copyright (C) 2006-2007 Frank Fock (SNMP4J.org)
6   _##
7   _## This program is free software; you can redistribute it and/or modify
8   _## it under the terms of the GNU General Public License version 2 as
9   _## published by the Free Software Foundation.
10   _##
11   _## This program is distributed in the hope that it will be useful,
12   _## but WITHOUT ANY WARRANTY; without even the implied warranty of
13   _## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14   _## GNU General Public License for more details.
15   _##
16   _## You should have received a copy of the GNU General Public License
17   _## along with this program; if not, write to the Free Software
18   _## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19   _## MA 02110-1301 USA
20   _##
21   _##########################################################################*/

22
23 package org.snmp4j.agent.mo.jmx;
24
25 import org.snmp4j.smi.OID;
26 import org.snmp4j.smi.Variable;
27 import javax.management.ObjectName JavaDoc;
28 import javax.management.MBeanServerConnection JavaDoc;
29 import org.snmp4j.PDU;
30 import org.snmp4j.smi.Integer32;
31 import org.snmp4j.smi.SMIConstants;
32 import javax.management.*;
33 import javax.management.*;
34 import javax.management.*;
35
36 /**
37  * A MBean action is basically a method call on an MBean. An action in SNMP
38  * is normally modeled as an enumeration where each value specifies a possible
39  * action or a parameter set for an action. On the other hand, SNMP enumerations
40  * are also used to indicate the states of a managed object.
41  * <p>
42  * The <code>MBeanActionMOScalarSupport</code> class provides a mapping between
43  * these two action models for an arbitrary number of scalar instances and
44  * corresponding actions.
45  *
46  * @author Frank Fock
47  * @version 1.0
48  */

49 public class MBeanActionMOScalarSupport extends AbstractMBeanSupport
50     implements JMXScalarSupport
51 {
52
53   public MBeanActionMOScalarSupport(MBeanServerConnection JavaDoc server) {
54     super(server);
55   }
56
57   /**
58    * Adds an action mapping to the supported mappings.
59    * @param oid
60    * the instance OID of a scalar SNMP variable.
61    * @param mBean
62    * the action mapping information of the MBean actions/states.
63    */

64   public synchronized void add(OID oid, MBeanActionMOInfo mBean) {
65     oid2MBeanMap.put(oid, mBean);
66   }
67
68   /**
69    * Adds a list of action mappings related to a single MBean.
70    * @param mBeanName
71    * the <code>ObjectName</code> of the MBean providing the actions.
72    * @param mBeanScalarAttributeDescriptions
73    * an two dimensional array of action descriptions. Each description
74    * contains three elements:
75    * <ol>
76    * <li>the OID of the scalar SNMP instance that manages the action,</li>
77    * <li>an array of MBeanStateInfo instances, and</li>
78    * <li>an array of MBeanActionInfo instances.</li>
79    * </ol>
80    */

81   public synchronized void addAll(ObjectName JavaDoc mBeanName,
82                                   Object JavaDoc[][] mBeanScalarAttributeDescriptions) {
83     for (Object JavaDoc[] attrDescr : mBeanScalarAttributeDescriptions) {
84       MBeanActionMOInfo mBeanInfo;
85       mBeanInfo = new MBeanActionMOInfo(mBeanName,
86                                         (MBeanStateInfo[]) attrDescr[1],
87                                         (MBeanActionInfo[]) attrDescr[2]);
88       oid2MBeanMap.put((OID)attrDescr[0], mBeanInfo);
89     }
90   }
91
92   public int checkScalarValue(OID scalarInstanceOID, Variable value) {
93     MBeanActionMOInfo mBeanActionMOInfo = getActionInfo(scalarInstanceOID);
94     if (mBeanActionMOInfo != null) {
95       if (value.getSyntax() != SMIConstants.SYNTAX_INTEGER32) {
96         return PDU.wrongType;
97       }
98       int actionID = ((Integer32)value).getValue();
99       for (MBeanActionInfo action : mBeanActionMOInfo.getActions()) {
100         if (actionID == action.getActionID()) {
101           return PDU.noError;
102         }
103       }
104       return PDU.wrongValue;
105     }
106     return PDU.resourceUnavailable;
107   }
108
109   public int getScalarValue(OID scalarInstanceOID, Variable value) {
110     MBeanActionMOInfo mBeanActionMOInfo = getActionInfo(scalarInstanceOID);
111     if (mBeanActionMOInfo != null) {
112       // get state
113
Integer32 v = (Integer32)value;
114       for (MBeanStateInfo stateInfo : mBeanActionMOInfo.getStates()) {
115         // check for default state
116
if (stateInfo.getStateAttribute() == null) {
117           v.setValue(stateInfo.getStateID());
118           return PDU.noError;
119         }
120         try {
121           Object JavaDoc attr;
122           if (stateInfo.getStateAttribute().getName() == null) {
123             attr = mBeanActionMOInfo.getLastActionResult();
124           }
125           else {
126             attr = MBeanAttributeMOInfo.getAttribute(server,
127                 mBeanActionMOInfo.getObjectName(),
128                 stateInfo.getStateAttribute());
129           }
130           if (((attr == null) && (stateInfo.getStateIndication() == null)) ||
131               ((attr != null) && attr.equals(stateInfo.getStateIndication()))) {
132             v.setValue(stateInfo.getStateID());
133             return PDU.noError;
134           }
135         }
136         catch (Exception JavaDoc ex) {
137           // ignore
138
}
139       }
140       return PDU.genErr;
141     }
142     return PDU.noSuchName;
143   }
144
145   public int setScalarValue(OID scalarInstanceOID, Variable value) {
146     MBeanActionMOInfo mBeanActionMOInfo = getActionInfo(scalarInstanceOID);
147     if (mBeanActionMOInfo != null) {
148       int actionID = ((Integer32)value).getValue();
149       for (MBeanActionInfo action : mBeanActionMOInfo.getActions()) {
150         if (actionID == action.getActionID()) {
151           try {
152             Object JavaDoc result = server.invoke(mBeanActionMOInfo.getObjectName(),
153                                           action.getMethod(),
154                                           action.getParameters(),
155                                           action.getSignature());
156             mBeanActionMOInfo.setLastActionResult(result);
157           }
158           catch (Exception JavaDoc ex) {
159             ex.printStackTrace();
160             return PDU.genErr;
161           }
162           return PDU.noError;
163         }
164       }
165     }
166     return PDU.genErr;
167   }
168
169   private MBeanActionMOInfo getActionInfo(OID scalarInstanceOID) {
170     return (MBeanActionMOInfo) getMBeanMOInfo(scalarInstanceOID);
171   }
172 }
173
Popular Tags