KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*_############################################################################
2   _##
3   _## SNMP4J-AgentJMX - MBeanAttributeMOScalarSupport.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.*;
26 import java.util.Map JavaDoc;
27 import javax.management.MBeanServerConnection JavaDoc;
28 import org.snmp4j.PDU;
29 import org.snmp4j.agent.mo.jmx.types.SMIVariant;
30 import org.snmp4j.agent.mo.jmx.types.TypedAttribute;
31 import org.snmp4j.agent.mo.jmx.types.CombinedTypedAttribute;
32 import javax.management.ObjectName JavaDoc;
33 // For JavaDoc
34
import org.snmp4j.agent.mo.MOScalar;
35
36 /**
37  * <code>MBeanAttributeMOScalarSupport</code> objects map zero or more MBean
38  * attributes to their corresponding {@link MOScalar} instance.
39  *
40  * @author Frank Fock
41  * @version 1.0
42  */

43 public class MBeanAttributeMOScalarSupport extends AbstractMBeanSupport
44     implements JMXScalarSupport {
45
46   public MBeanAttributeMOScalarSupport(MBeanServerConnection JavaDoc server) {
47     super(server);
48   }
49
50   /**
51    * Adds a scalar to MBean attribute mapping.
52    * @param oid
53    * the instance OID (including the .0) of the SNMP scalar object.
54    * @param mBean
55    * an attribute description of a MBean.
56    */

57   public synchronized void add(OID oid, MBeanAttributeMOInfo mBean) {
58     oid2MBeanMap.put(oid, mBean);
59   }
60
61   /**
62    * Adds a list of scalar to attribute mappings for the specified MBean.
63    * @param mBeanName
64    * the name of the MBean providing the attributes.
65    * @param mBeanScalarAttributeDescriptions
66    * an two dimensional array of scalar descriptions. Each description
67    * contains three elements:
68    * <ol>
69    * <li>the <code>OID</code> of the scalar SNMP instance,</li>
70    * <li>the name of the attribute as <code>String</code>, and</li>
71    * <li>the <code>Class</code> of the attributes value.</li>
72    * </ol>
73    */

74   public synchronized void addAll(ObjectName JavaDoc mBeanName,
75                                   Object JavaDoc[][] mBeanScalarAttributeDescriptions) {
76     for (Object JavaDoc[] attrDescr : mBeanScalarAttributeDescriptions) {
77       MBeanAttributeMOInfo mBeanInfo;
78       if (attrDescr[1] instanceof CombinedTypedAttribute) {
79         mBeanInfo = new MBeanMultiAttributeMOInfo(mBeanName,
80                                                   (CombinedTypedAttribute)
81                                                   attrDescr[1]);
82       }
83       else if (attrDescr[1] instanceof TypedAttribute) {
84         mBeanInfo = new MBeanAttributeMOInfo(mBeanName,
85                                              (TypedAttribute) attrDescr[1]);
86       }
87       else {
88         mBeanInfo = new MBeanAttributeMOInfo(mBeanName,
89                                              (String JavaDoc) attrDescr[1],
90                                              (Class JavaDoc) attrDescr[2]);
91       }
92       oid2MBeanMap.put((OID)attrDescr[0], mBeanInfo);
93     }
94   }
95
96   /**
97    * Checks the value of the specified object instance and type.
98    *
99    * @param scalarInstanceOID the instance OID of the target object.
100    * @param value the instance's new value.
101    * @return zero on success or a SNMP error status value if setting the value
102    * fails.
103    */

104   public int checkScalarValue(OID scalarInstanceOID, Variable value) {
105     MBeanAttributeMOInfo mBeanAttrMOInfo =
106         (MBeanAttributeMOInfo) getMBeanMOInfo(scalarInstanceOID);
107     if (mBeanAttrMOInfo != null) {
108       /**@todo implement check callback */
109       return PDU.noError;
110     }
111     return PDU.resourceUnavailable;
112   }
113
114   /**
115    * Gets the actual value for the specified object instance and type.
116    *
117    * @param scalarInstanceOID the instance OID of the target object.
118    * @param value the instance to hold the return value.
119    * @return zero on success or a SNMP error status value if fetching the
120    * value fails.
121    */

122   public int getScalarValue(OID scalarInstanceOID, Variable value) {
123     MBeanAttributeMOInfo mBeanAttrMOInfo =
124         (MBeanAttributeMOInfo) getMBeanMOInfo(scalarInstanceOID);
125     if (mBeanAttrMOInfo != null) {
126       try {
127         Object JavaDoc attr = mBeanAttrMOInfo.getAttribute(server);
128         if (attr == null) {
129           return PDU.noError;
130         }
131         SMIVariant v = new SMIVariant(value);
132         return v.setValue(attr);
133       }
134       catch (Exception JavaDoc ex) {
135         ex.printStackTrace();
136       }
137       return PDU.genErr;
138     }
139     return PDU.noSuchName;
140   }
141
142   /**
143    * Sets the value of the specified object instance and type.
144    *
145    * @param scalarInstanceOID the instance OID of the target object.
146    * @param value the instance's new value.
147    * @return zero on success or a SNMP error status value if setting the value
148    * fails.
149    */

150   public int setScalarValue(OID scalarInstanceOID, Variable value) {
151     MBeanAttributeMOInfo mBeanAttrMOInfo =
152         (MBeanAttributeMOInfo) getMBeanMOInfo(scalarInstanceOID);
153     if (mBeanAttrMOInfo != null) {
154       try {
155         Object JavaDoc attr = mBeanAttrMOInfo.getAttribute().transformSMI2Object(value);
156         mBeanAttrMOInfo.setAttribute(server, attr);
157         return PDU.noError;
158       }
159       catch (Exception JavaDoc ex) {
160         ex.printStackTrace();
161       }
162     }
163     return PDU.genErr;
164   }
165 }
166
Popular Tags