KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*_############################################################################
2   _##
3   _## SNMP4J-AgentJMX - MBeanArrayIndexKeyProvider.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 javax.management.ObjectName JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import javax.management.MBeanServerConnection JavaDoc;
28 import java.io.IOException JavaDoc;
29 import javax.management.MBeanException JavaDoc;
30 import javax.management.AttributeNotFoundException JavaDoc;
31 import javax.management.InstanceNotFoundException JavaDoc;
32 import javax.management.ReflectionException JavaDoc;
33 import org.snmp4j.agent.mo.jmx.util.JMXArrayIndexKeyIterator;
34 import org.snmp4j.agent.mo.jmx.types.*;
35 import org.snmp4j.agent.mo.jmx.util.JMXArrayIndexKey;
36 import java.util.Collection JavaDoc;
37 import java.util.List JavaDoc;
38
39 /**
40  * To map the array indexes of an array provided by a MBean to SNMP table row
41  * indexes, the <code>MBeanArrayIndexKeyProvider</code> can be used.
42  *
43  * @author Frank Fock
44  * @version 1.0
45  */

46 public class MBeanArrayIndexKeyProvider extends MBeanAttributeKeyProvider {
47
48   /**
49    * Creates an index mapping based on a MBean attribute that provides the
50    * keys of the table rows. The attribute may return an instance of one of
51    * the following supported classes:
52    * <ul>
53    * <li>Object[]</li>
54    * <li>{@link List}</li>
55    * <li>{@link Collection}</li>
56    * </ul>
57    * @param name
58    * the MBean's <code>ObjectName</code>.
59    * @param attribute
60    * the description of the attribute that provides the row keys.
61    */

62   public MBeanArrayIndexKeyProvider(ObjectName JavaDoc name, TypedAttribute attribute) {
63     super(name, attribute);
64   }
65
66   public Iterator JavaDoc keyIterator(MBeanServerConnection JavaDoc server) throws IOException JavaDoc,
67       MBeanException JavaDoc, AttributeNotFoundException JavaDoc, InstanceNotFoundException JavaDoc,
68       ReflectionException JavaDoc {
69     return new JMXArrayIndexKeyIterator(super.keyIterator(server));
70   }
71
72   protected Iterator JavaDoc createTailIterator(Iterator JavaDoc it, int pos) {
73     return new JMXArrayIndexKeyIterator(it, pos);
74   }
75
76   /**
77    * Gets the row value(s) for the specified index object. If the index object
78    * is not an instance of {@link JMXArrayIndexKey} the index object itself is
79    * returned. Otherwise, the n-th object of the objects provided by the MBean
80    * attribute supplied at creation time is returned where <code>n</code>
81    * corresponds to the index object's index value.
82    *
83    * @param server
84    * a <code>MBeanServerConnection</code> to use to access the MBean.
85    * @param indexObject
86    * a <code>JMXArrayIndexKey</code> denoting the row index/value(s) to
87    * return.
88    * @return
89    * <code>indexObject</code> if it is not an instance of
90    * <code>JMXArrayIndexKey</code> or otherwise the n-th key (or row value).
91    * @throws IOException
92    * @throws MBeanException
93    * @throws AttributeNotFoundException
94    * @throws InstanceNotFoundException
95    * @throws ReflectionException
96    */

97   public Object JavaDoc getRowValues(MBeanServerConnection JavaDoc server,
98                              Object JavaDoc indexObject) throws IOException JavaDoc,
99       MBeanException JavaDoc, AttributeNotFoundException JavaDoc, InstanceNotFoundException JavaDoc,
100       ReflectionException JavaDoc
101   {
102     if (indexObject instanceof JMXArrayIndexKey) {
103       int index = ((JMXArrayIndexKey) indexObject).getIndex();
104       Object JavaDoc keys = getAttribute(server);
105       if (keys instanceof List JavaDoc) {
106         return ((List JavaDoc) keys).get(index);
107       }
108       if (keys instanceof Object JavaDoc[]) {
109         return ((Object JavaDoc[]) keys)[index];
110       }
111       else if (keys instanceof Collection JavaDoc) {
112         Iterator JavaDoc it = ((Collection JavaDoc) keys).iterator();
113         for (int i = 0; (i < index) && (it.hasNext()); i++) {
114           it.next();
115         }
116         if (it.hasNext()) {
117           return it.next();
118         }
119       }
120     }
121     return indexObject;
122   }
123
124 }
125
Popular Tags