KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*_############################################################################
2   _##
3   _## SNMP4J-AgentJMX - MBeanAttributeMOTableSupport.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 java.util.Iterator JavaDoc;
26 import javax.management.MBeanServerConnection JavaDoc;
27
28 import org.snmp4j.agent.mo.MOTableRow;
29 import org.snmp4j.smi.OID;
30 import java.util.Set JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.util.TreeMap JavaDoc;
34 import java.util.SortedMap JavaDoc;
35 import java.util.List JavaDoc;
36 import org.snmp4j.PDU;
37 import org.snmp4j.agent.mo.jmx.types.SMIVariant;
38 import org.snmp4j.agent.mo.jmx.types.*;
39 import javax.management.ObjectInstance JavaDoc;
40 import javax.management.Attribute JavaDoc;
41 // For JavaDoc:
42
import org.snmp4j.agent.mo.MOTable;
43
44 /**
45  * The <code>MBeanAttributeMOTableSupport</code> maps SNMP conceptual tables
46  * ({@link MOTable}) to MBean attributes and vice versa.
47  *
48  * @author Frank Fock
49  * @version 1.0
50  */

51 public class MBeanAttributeMOTableSupport extends AbstractMBeanSupport
52     implements JMXTableSupport {
53
54   public MBeanAttributeMOTableSupport(MBeanServerConnection JavaDoc server) {
55     super(server);
56   }
57
58   /**
59    * Adds a table to MBean attributes mapping.
60    * @param tableOID
61    * the entry OID of the table (including the .1).
62    * @param mBeanInfo
63    * a <code>MBeanAttributeMOTableInfo</code> instance describing the
64    * actual mapping.
65    */

66   public synchronized void add(OID tableOID,
67                                MBeanAttributeMOTableInfo mBeanInfo) {
68     oid2MBeanMap.put(tableOID, mBeanInfo);
69   }
70
71   /**
72    * Adds a list of table to MBean attributes mappings.
73    * @param tableDescriptions
74    * an two dimensional array of table descriptions. Each description
75    * contains two elements:
76    * <ol>
77    * <li>the <code>OID</code> of the table entry (thus including the .1),</li>
78    * <li>a {@link MBeanAttributeMOTableInfo} instance.</li>
79    * </ol>
80    */

81   public synchronized void addAll(Object JavaDoc[][] tableDescriptions) {
82     for (Object JavaDoc[] tableDescr : tableDescriptions) {
83       MBeanAttributeMOTableInfo mBeanInfo =
84           (MBeanAttributeMOTableInfo)tableDescr[1];
85       oid2MBeanMap.put((OID)tableDescr[0], mBeanInfo);
86     }
87   }
88
89   public OID getLastIndex(OID tableOID) {
90     MBeanAttributeMOTableInfo mBeanInfo =
91         (MBeanAttributeMOTableInfo) getMBeanMOInfo(tableOID);
92     if (mBeanInfo != null) {
93       try {
94         if (mBeanInfo.getKeyProvider() == null) {
95           OID maxIndex = new OID();
96           Set JavaDoc<ObjectInstance JavaDoc> mBeans = mBeanInfo.getMBeanNames(server);
97           for (ObjectInstance JavaDoc mBean : mBeans) {
98             Object JavaDoc key = mBeanInfo.getKey(server, mBean.getObjectName());
99             OID index = mBeanInfo.getIndexSupport().mapToIndex(key);
100             if (index.compareTo(maxIndex) > 0) {
101               maxIndex = index;
102             }
103           }
104           return maxIndex;
105         }
106         else {
107           MBeanAttributeMOInfo keyProvider = mBeanInfo.getKeyProvider();
108           Object JavaDoc keys = keyProvider.getAttribute(server);
109           if (keys instanceof Object JavaDoc[]) {
110             Object JavaDoc[] k = (Object JavaDoc[])keys;
111             if (k.length == 0) {
112               return null;
113             }
114             return mapToIndex(tableOID, k[k.length-1], k.length-1);
115           }
116           else if (keys instanceof List JavaDoc) {
117             List JavaDoc k = (List JavaDoc)keys;
118             if (k.isEmpty()) {
119               return null;
120             }
121             int lastID = k.size()-1;
122             return mapToIndex(tableOID, k.get(lastID), lastID);
123           }
124           else if (keys instanceof Collection JavaDoc) {
125             int i=0;
126             for (Iterator JavaDoc it = ((Collection JavaDoc)keys).iterator(); it.hasNext(); i++) {
127               Object JavaDoc k = it.next();
128               if (!it.hasNext()) {
129                 return mapToIndex(tableOID, k, i);
130               }
131             }
132           }
133           else {
134             throw new ClassCastException JavaDoc(keys.getClass()+
135                                          " is not a supported list");
136           }
137         }
138       }
139       catch (Exception JavaDoc ex) {
140         throw new RuntimeException JavaDoc(ex);
141       }
142     }
143     return null;
144   }
145
146   public int getRow(OID tableOID, MOTableRow row) {
147     MBeanAttributeMOTableInfo mBeanInfo =
148         (MBeanAttributeMOTableInfo) getMBeanMOInfo(tableOID);
149     if (mBeanInfo != null) {
150       Object JavaDoc key =
151           mBeanInfo.getIndexSupport().mapToRowIdentifier(row.getIndex());
152       ObjectName JavaDoc oname = mBeanInfo.getIndexSupport().mapToRowMBean(key);
153       if (oname == null) {
154         try {
155           TypedAttribute[] columns = mBeanInfo.getColumns();
156           Object JavaDoc value =
157               mBeanInfo.getKeyProvider().getRowValues(server, key);
158           if (value instanceof Object JavaDoc[]) {
159             for (int i=0; i<((Object JavaDoc[])value).length && (i<row.size()); i++) {
160               TypedAttribute col = columns[i];
161               Object JavaDoc v = ((Object JavaDoc[])value)[i];
162               SMIVariant smiValue = new SMIVariant(row.getValue(i));
163               v = col.transformFromNative(v, null);
164               smiValue.setValue(v);
165             }
166           }
167           else {
168             for (int i=0; ((columns == null) || (i<columns.length)) &&
169                  (i<row.size()); i++) {
170               SMIVariant smiValue = new SMIVariant(row.getValue(i));
171               Object JavaDoc v;
172               if (columns == null) {
173                 v = value;
174               }
175               else {
176                 v = columns[i].transformFromNative(value, null);
177               }
178               smiValue.setValue(v);
179             }
180           }
181           return PDU.noError;
182         }
183         catch (Exception JavaDoc 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 JavaDoc colValue = server.getAttribute(oname, col.getName());
194           colValue = col.transformFromNative(colValue, oname);
195           int status = colVariant.setValue(colValue);
196           if (status != PDU.noError) {
197             return status;
198           }
199         }
200         catch (Exception JavaDoc 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 =
211         (MBeanAttributeMOTableInfo) getMBeanMOInfo(tableOID);
212     if (mBeanInfo != null) {
213       Object JavaDoc key =
214           mBeanInfo.getIndexSupport().mapToRowIdentifier(row.getIndex());
215       if ((mBeanInfo.getKeyProvider() == null) && (key == null)) {
216         try {
217           Set JavaDoc<ObjectInstance JavaDoc> mBeans = mBeanInfo.getMBeanNames(server);
218           for (ObjectInstance JavaDoc mBean : mBeans) {
219             Object JavaDoc k = mBeanInfo.getKey(server, mBean.getObjectName());
220             OID i = mBeanInfo.getIndexSupport().mapToIndex(k);
221             if (row.getIndex().equals(i)) {
222               key = k;
223               break;
224             }
225           }
226         }
227         catch (Exception JavaDoc ex1) {
228           ex1.printStackTrace();
229         }
230       }
231       if (key == null) {
232         return PDU.noSuchName;
233       }
234       ObjectName JavaDoc oname = mBeanInfo.getIndexSupport().mapToRowMBean(key);
235       TypedAttribute[] columns = mBeanInfo.getColumns();
236       if (column<columns.length) {
237         TypedAttribute col = columns[column];
238         SMIVariant colVariant = new SMIVariant(row.getValue(column));
239         Object JavaDoc colValue = colVariant.getValue(col.getType());
240         try {
241           colValue = col.transformToNative(colValue,
242                                            (col.isNativeValueAlwaysNeeded() ?
243                                             server.getAttribute(oname, col.getName()):
244                                             null),
245                                            oname);
246           if (colValue != null) {
247             server.setAttribute(oname, new Attribute JavaDoc(col.getName(), colValue));
248           }
249         }
250         catch (Exception JavaDoc ex) {
251           ex.printStackTrace();
252         }
253       }
254       return PDU.noError;
255     }
256     return PDU.resourceUnavailable;
257   }
258
259
260   public int getRowCount(OID tableOID) {
261     MBeanAttributeMOTableInfo mBeanInfo =
262         (MBeanAttributeMOTableInfo) getMBeanMOInfo(tableOID);
263     if (mBeanInfo != null) {
264       try {
265         if (mBeanInfo.getKeyProvider() == null) {
266           Set JavaDoc<ObjectInstance JavaDoc> mBeans = mBeanInfo.getMBeanNames(server);
267           return mBeans.size();
268         }
269         else {
270           MBeanAttributeKeyProvider keyProvider = mBeanInfo.getKeyProvider();
271           return keyProvider.getKeyCount(server);
272         }
273       }
274       catch (Exception JavaDoc ex) {
275         ex.printStackTrace();
276       }
277     }
278     return 0;
279   }
280
281   public Iterator JavaDoc rowIdIterator(OID tableOID) {
282     MBeanAttributeMOTableInfo mBeanInfo =
283         (MBeanAttributeMOTableInfo) getMBeanMOInfo(tableOID);
284     if (mBeanInfo != null) {
285       try {
286         if (mBeanInfo.getKeyProvider() == null) {
287           Set JavaDoc<ObjectInstance JavaDoc> mBeans = mBeanInfo.getMBeanNames(server);
288           SortedMap JavaDoc indexes = new TreeMap JavaDoc();
289           for (ObjectInstance JavaDoc mBean : mBeans) {
290             Object JavaDoc key = mBeanInfo.getKey(server, mBean.getObjectName());
291             OID index = mBeanInfo.getIndexSupport().mapToIndex(key);
292             indexes.put(index, key);
293           }
294           return indexes.values().iterator();
295         }
296         else {
297           MBeanAttributeKeyProvider keyProvider = mBeanInfo.getKeyProvider();
298           return keyProvider.keyIterator(server);
299         }
300       }
301       catch (Exception JavaDoc ex) {
302         ex.printStackTrace();
303       }
304     }
305     return null;
306   }
307
308   public OID mapToIndex(OID tableOID, Object JavaDoc nativeRowId, int nativeIndex) {
309     MBeanAttributeMOTableInfo mBeanInfo =
310         (MBeanAttributeMOTableInfo) getMBeanMOInfo(tableOID);
311     if (mBeanInfo != null) {
312       Object JavaDoc rowId =
313           mBeanInfo.getIndexSupport().getRowIdentifier(nativeRowId, nativeIndex);
314       return mBeanInfo.getIndexSupport().mapToIndex(rowId);
315     }
316     return null;
317   }
318
319   public Object JavaDoc mapToRowId(OID tableOID, OID rowIndex) {
320     MBeanAttributeMOTableInfo mBeanInfo =
321         (MBeanAttributeMOTableInfo) getMBeanMOInfo(tableOID);
322     if (mBeanInfo != null) {
323       return mBeanInfo.getIndexSupport().mapToRowIdentifier(rowIndex);
324     }
325     return null;
326   }
327
328   public Iterator JavaDoc rowIdTailIterator(OID tableOID, Object JavaDoc firstRowId) {
329     MBeanAttributeMOTableInfo mBeanInfo =
330         (MBeanAttributeMOTableInfo) getMBeanMOInfo(tableOID);
331     if (mBeanInfo != null) {
332       try {
333         if (mBeanInfo.getKeyProvider() == null) {
334           Set JavaDoc<ObjectInstance JavaDoc> mBeans = mBeanInfo.getMBeanNames(server);
335           TreeMap JavaDoc indexes = new TreeMap JavaDoc();
336           OID firstIndex = null;
337           for (ObjectInstance JavaDoc mBean : mBeans) {
338             Object JavaDoc key = mBeanInfo.getKey(server, mBean.getObjectName());
339             OID index = mBeanInfo.getIndexSupport().mapToIndex(key);
340             if (key.equals(firstRowId)) {
341               firstIndex = index;
342             }
343             indexes.put(index, key);
344           }
345           if (firstIndex == null) {
346             return indexes.values().iterator();
347           }
348           return indexes.tailMap(firstIndex).values().iterator();
349         }
350         else {
351           MBeanAttributeKeyProvider keyProvider = mBeanInfo.getKeyProvider();
352           return keyProvider.keyTailIterator(server, firstRowId);
353         }
354       }
355       catch (Exception JavaDoc ex) {
356         ex.printStackTrace();
357       }
358     }
359     return null;
360   }
361 }
362
Popular Tags