001: /*_############################################################################
002: _##
003: _## SNMP4J-AgentJMX - MBeanAttributeMOTableSupport.java
004: _##
005: _## Copyright (C) 2006-2007 Frank Fock (SNMP4J.org)
006: _##
007: _## This program is free software; you can redistribute it and/or modify
008: _## it under the terms of the GNU General Public License version 2 as
009: _## published by the Free Software Foundation.
010: _##
011: _## This program is distributed in the hope that it will be useful,
012: _## but WITHOUT ANY WARRANTY; without even the implied warranty of
013: _## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: _## GNU General Public License for more details.
015: _##
016: _## You should have received a copy of the GNU General Public License
017: _## along with this program; if not, write to the Free Software
018: _## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
019: _## MA 02110-1301 USA
020: _##
021: _##########################################################################*/
022:
023: package org.snmp4j.agent.mo.jmx;
024:
025: import java.util.Iterator;
026: import javax.management.MBeanServerConnection;
027:
028: import org.snmp4j.agent.mo.MOTableRow;
029: import org.snmp4j.smi.OID;
030: import java.util.Set;
031: import javax.management.ObjectName;
032: import java.util.Collection;
033: import java.util.TreeMap;
034: import java.util.SortedMap;
035: import java.util.List;
036: import org.snmp4j.PDU;
037: import org.snmp4j.agent.mo.jmx.types.SMIVariant;
038: import org.snmp4j.agent.mo.jmx.types.*;
039: import javax.management.ObjectInstance;
040: import javax.management.Attribute; // For JavaDoc:
041: import org.snmp4j.agent.mo.MOTable;
042:
043: /**
044: * The <code>MBeanAttributeMOTableSupport</code> maps SNMP conceptual tables
045: * ({@link MOTable}) to MBean attributes and vice versa.
046: *
047: * @author Frank Fock
048: * @version 1.0
049: */
050: public class MBeanAttributeMOTableSupport extends AbstractMBeanSupport
051: implements JMXTableSupport {
052:
053: public MBeanAttributeMOTableSupport(MBeanServerConnection server) {
054: super (server);
055: }
056:
057: /**
058: * Adds a table to MBean attributes mapping.
059: * @param tableOID
060: * the entry OID of the table (including the .1).
061: * @param mBeanInfo
062: * a <code>MBeanAttributeMOTableInfo</code> instance describing the
063: * actual mapping.
064: */
065: public synchronized void add(OID tableOID,
066: MBeanAttributeMOTableInfo mBeanInfo) {
067: oid2MBeanMap.put(tableOID, mBeanInfo);
068: }
069:
070: /**
071: * Adds a list of table to MBean attributes mappings.
072: * @param tableDescriptions
073: * an two dimensional array of table descriptions. Each description
074: * contains two elements:
075: * <ol>
076: * <li>the <code>OID</code> of the table entry (thus including the .1),</li>
077: * <li>a {@link MBeanAttributeMOTableInfo} instance.</li>
078: * </ol>
079: */
080: public synchronized void addAll(Object[][] tableDescriptions) {
081: for (Object[] tableDescr : tableDescriptions) {
082: MBeanAttributeMOTableInfo mBeanInfo = (MBeanAttributeMOTableInfo) tableDescr[1];
083: oid2MBeanMap.put((OID) tableDescr[0], mBeanInfo);
084: }
085: }
086:
087: public OID getLastIndex(OID tableOID) {
088: MBeanAttributeMOTableInfo mBeanInfo = (MBeanAttributeMOTableInfo) getMBeanMOInfo(tableOID);
089: if (mBeanInfo != null) {
090: try {
091: if (mBeanInfo.getKeyProvider() == null) {
092: OID maxIndex = new OID();
093: Set<ObjectInstance> mBeans = mBeanInfo
094: .getMBeanNames(server);
095: for (ObjectInstance mBean : mBeans) {
096: Object key = mBeanInfo.getKey(server, mBean
097: .getObjectName());
098: OID index = mBeanInfo.getIndexSupport()
099: .mapToIndex(key);
100: if (index.compareTo(maxIndex) > 0) {
101: maxIndex = index;
102: }
103: }
104: return maxIndex;
105: } else {
106: MBeanAttributeMOInfo keyProvider = mBeanInfo
107: .getKeyProvider();
108: Object keys = keyProvider.getAttribute(server);
109: if (keys instanceof Object[]) {
110: Object[] k = (Object[]) keys;
111: if (k.length == 0) {
112: return null;
113: }
114: return mapToIndex(tableOID, k[k.length - 1],
115: k.length - 1);
116: } else if (keys instanceof List) {
117: List k = (List) keys;
118: if (k.isEmpty()) {
119: return null;
120: }
121: int lastID = k.size() - 1;
122: return mapToIndex(tableOID, k.get(lastID),
123: lastID);
124: } else if (keys instanceof Collection) {
125: int i = 0;
126: for (Iterator it = ((Collection) keys)
127: .iterator(); it.hasNext(); i++) {
128: Object k = it.next();
129: if (!it.hasNext()) {
130: return mapToIndex(tableOID, k, i);
131: }
132: }
133: } else {
134: throw new ClassCastException(keys.getClass()
135: + " is not a supported list");
136: }
137: }
138: } catch (Exception ex) {
139: throw new RuntimeException(ex);
140: }
141: }
142: return null;
143: }
144:
145: public int getRow(OID tableOID, MOTableRow row) {
146: MBeanAttributeMOTableInfo mBeanInfo = (MBeanAttributeMOTableInfo) getMBeanMOInfo(tableOID);
147: if (mBeanInfo != null) {
148: Object key = mBeanInfo.getIndexSupport()
149: .mapToRowIdentifier(row.getIndex());
150: ObjectName oname = mBeanInfo.getIndexSupport()
151: .mapToRowMBean(key);
152: if (oname == null) {
153: try {
154: TypedAttribute[] columns = mBeanInfo.getColumns();
155: Object value = mBeanInfo.getKeyProvider()
156: .getRowValues(server, key);
157: if (value instanceof Object[]) {
158: for (int i = 0; i < ((Object[]) value).length
159: && (i < row.size()); i++) {
160: TypedAttribute col = columns[i];
161: Object v = ((Object[]) value)[i];
162: SMIVariant smiValue = new SMIVariant(row
163: .getValue(i));
164: v = col.transformFromNative(v, null);
165: smiValue.setValue(v);
166: }
167: } else {
168: for (int i = 0; ((columns == null) || (i < columns.length))
169: && (i < row.size()); i++) {
170: SMIVariant smiValue = new SMIVariant(row
171: .getValue(i));
172: Object v;
173: if (columns == null) {
174: v = value;
175: } else {
176: v = columns[i].transformFromNative(
177: value, null);
178: }
179: smiValue.setValue(v);
180: }
181: }
182: return PDU.noError;
183: } catch (Exception ex) {
184: ex.printStackTrace();
185: }
186: return PDU.resourceUnavailable;
187: }
188: TypedAttribute[] columns = mBeanInfo.getColumns();
189: for (int i = 0; (i < columns.length) && (i < row.size()); i++) {
190: TypedAttribute col = columns[i];
191: SMIVariant colVariant = new SMIVariant(row.getValue(i));
192: try {
193: Object colValue = server.getAttribute(oname, col
194: .getName());
195: colValue = col.transformFromNative(colValue, oname);
196: int status = colVariant.setValue(colValue);
197: if (status != PDU.noError) {
198: return status;
199: }
200: } catch (Exception ex) {
201: // ex.printStackTrace();
202: }
203: }
204: return PDU.noError;
205: }
206: return PDU.resourceUnavailable;
207: }
208:
209: public int setRow(OID tableOID, MOTableRow row, int column) {
210: MBeanAttributeMOTableInfo mBeanInfo = (MBeanAttributeMOTableInfo) getMBeanMOInfo(tableOID);
211: if (mBeanInfo != null) {
212: Object key = mBeanInfo.getIndexSupport()
213: .mapToRowIdentifier(row.getIndex());
214: if ((mBeanInfo.getKeyProvider() == null) && (key == null)) {
215: try {
216: Set<ObjectInstance> mBeans = mBeanInfo
217: .getMBeanNames(server);
218: for (ObjectInstance mBean : mBeans) {
219: Object k = mBeanInfo.getKey(server, mBean
220: .getObjectName());
221: OID i = mBeanInfo.getIndexSupport().mapToIndex(
222: k);
223: if (row.getIndex().equals(i)) {
224: key = k;
225: break;
226: }
227: }
228: } catch (Exception ex1) {
229: ex1.printStackTrace();
230: }
231: }
232: if (key == null) {
233: return PDU.noSuchName;
234: }
235: ObjectName oname = mBeanInfo.getIndexSupport()
236: .mapToRowMBean(key);
237: TypedAttribute[] columns = mBeanInfo.getColumns();
238: if (column < columns.length) {
239: TypedAttribute col = columns[column];
240: SMIVariant colVariant = new SMIVariant(row
241: .getValue(column));
242: Object colValue = colVariant.getValue(col.getType());
243: try {
244: colValue = col.transformToNative(colValue,
245: (col.isNativeValueAlwaysNeeded() ? server
246: .getAttribute(oname, col.getName())
247: : null), oname);
248: if (colValue != null) {
249: server.setAttribute(oname, new Attribute(col
250: .getName(), colValue));
251: }
252: } catch (Exception ex) {
253: ex.printStackTrace();
254: }
255: }
256: return PDU.noError;
257: }
258: return PDU.resourceUnavailable;
259: }
260:
261: public int getRowCount(OID tableOID) {
262: MBeanAttributeMOTableInfo mBeanInfo = (MBeanAttributeMOTableInfo) getMBeanMOInfo(tableOID);
263: if (mBeanInfo != null) {
264: try {
265: if (mBeanInfo.getKeyProvider() == null) {
266: Set<ObjectInstance> mBeans = mBeanInfo
267: .getMBeanNames(server);
268: return mBeans.size();
269: } else {
270: MBeanAttributeKeyProvider keyProvider = mBeanInfo
271: .getKeyProvider();
272: return keyProvider.getKeyCount(server);
273: }
274: } catch (Exception ex) {
275: ex.printStackTrace();
276: }
277: }
278: return 0;
279: }
280:
281: public Iterator rowIdIterator(OID tableOID) {
282: MBeanAttributeMOTableInfo mBeanInfo = (MBeanAttributeMOTableInfo) getMBeanMOInfo(tableOID);
283: if (mBeanInfo != null) {
284: try {
285: if (mBeanInfo.getKeyProvider() == null) {
286: Set<ObjectInstance> mBeans = mBeanInfo
287: .getMBeanNames(server);
288: SortedMap indexes = new TreeMap();
289: for (ObjectInstance mBean : mBeans) {
290: Object key = mBeanInfo.getKey(server, mBean
291: .getObjectName());
292: OID index = mBeanInfo.getIndexSupport()
293: .mapToIndex(key);
294: indexes.put(index, key);
295: }
296: return indexes.values().iterator();
297: } else {
298: MBeanAttributeKeyProvider keyProvider = mBeanInfo
299: .getKeyProvider();
300: return keyProvider.keyIterator(server);
301: }
302: } catch (Exception ex) {
303: ex.printStackTrace();
304: }
305: }
306: return null;
307: }
308:
309: public OID mapToIndex(OID tableOID, Object nativeRowId,
310: int nativeIndex) {
311: MBeanAttributeMOTableInfo mBeanInfo = (MBeanAttributeMOTableInfo) getMBeanMOInfo(tableOID);
312: if (mBeanInfo != null) {
313: Object rowId = mBeanInfo.getIndexSupport()
314: .getRowIdentifier(nativeRowId, nativeIndex);
315: return mBeanInfo.getIndexSupport().mapToIndex(rowId);
316: }
317: return null;
318: }
319:
320: public Object mapToRowId(OID tableOID, OID rowIndex) {
321: MBeanAttributeMOTableInfo mBeanInfo = (MBeanAttributeMOTableInfo) getMBeanMOInfo(tableOID);
322: if (mBeanInfo != null) {
323: return mBeanInfo.getIndexSupport().mapToRowIdentifier(
324: rowIndex);
325: }
326: return null;
327: }
328:
329: public Iterator rowIdTailIterator(OID tableOID, Object firstRowId) {
330: MBeanAttributeMOTableInfo mBeanInfo = (MBeanAttributeMOTableInfo) getMBeanMOInfo(tableOID);
331: if (mBeanInfo != null) {
332: try {
333: if (mBeanInfo.getKeyProvider() == null) {
334: Set<ObjectInstance> mBeans = mBeanInfo
335: .getMBeanNames(server);
336: TreeMap indexes = new TreeMap();
337: OID firstIndex = null;
338: for (ObjectInstance mBean : mBeans) {
339: Object key = mBeanInfo.getKey(server, mBean
340: .getObjectName());
341: OID index = mBeanInfo.getIndexSupport()
342: .mapToIndex(key);
343: if (key.equals(firstRowId)) {
344: firstIndex = index;
345: }
346: indexes.put(index, key);
347: }
348: if (firstIndex == null) {
349: return indexes.values().iterator();
350: }
351: return indexes.tailMap(firstIndex).values()
352: .iterator();
353: } else {
354: MBeanAttributeKeyProvider keyProvider = mBeanInfo
355: .getKeyProvider();
356: return keyProvider.keyTailIterator(server,
357: firstRowId);
358: }
359: } catch (Exception ex) {
360: ex.printStackTrace();
361: }
362: }
363: return null;
364: }
365: }
|