KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*_############################################################################
2   _##
3   _## SNMP4J-AgentJMX - MBeanAttributeKeyProvider.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.Collection JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.List JavaDoc;
29 import javax.management.MBeanServerConnection JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.io.IOException JavaDoc;
32 import javax.management.MBeanException JavaDoc;
33 import javax.management.AttributeNotFoundException JavaDoc;
34 import javax.management.InstanceNotFoundException JavaDoc;
35 import javax.management.ReflectionException JavaDoc;
36 import org.snmp4j.agent.mo.jmx.util.JMXArrayIndexKey;
37 import org.snmp4j.agent.mo.jmx.types.*;
38 import java.util.AbstractList JavaDoc;
39 import java.util.Collections JavaDoc;
40 import java.util.ArrayList JavaDoc;
41
42 /**
43  * The <code>MBeanAttributeKeyProvider</code> provides the row keys of a
44  * conceptual table from a MBean attribute. The keys are returned in the same
45  * order as provided by the MBean attribute by default. If the
46  * <code>keysNeedSorting</code> is set, keys are returned always in their
47  * natural order.
48  *
49  * @author Frank Fock
50  * @version 1.0
51  */

52 public class MBeanAttributeKeyProvider extends MBeanAttributeMOInfo {
53
54   private boolean keysNeedSorting;
55
56   /**
57    * Creates a key provider with a MBean name and a attribute description.
58    * @param name
59    * a MBean's <code>ObjectName</code>.
60    * @param attribute
61    * the description of an attribute of the MBean identified by
62    * <code>name</code>.
63    */

64   public MBeanAttributeKeyProvider(ObjectName JavaDoc name, TypedAttribute attribute) {
65     super(name, attribute);
66   }
67
68   /**
69    * Creates a key provider with a MBean name and a attribute description.
70    * @param name
71    * a MBean's <code>ObjectName</code>.
72    * @param attribute
73    * the description of an attribute of the MBean identified by
74    * <code>name</code>.
75    * @param keysNeedSorting
76    * if <code>true</code> keys will be sorted by their natural order.
77    */

78   public MBeanAttributeKeyProvider(ObjectName JavaDoc name, TypedAttribute attribute,
79                                    boolean keysNeedSorting) {
80     super(name, attribute);
81     this.keysNeedSorting = keysNeedSorting;
82   }
83
84   /**
85    * Returns an iterator on the keys provided by this MBean attribute.
86    * @param server
87    * the <code>MBeanServerConnection</code> to be used to access the MBean.
88    * @return Iterator
89    * an iterator providing the keys returned by the key provider attribute.
90    * @throws IOException
91    * @throws MBeanException
92    * @throws AttributeNotFoundException
93    * @throws InstanceNotFoundException
94    * @throws ReflectionException
95    */

96   public Iterator JavaDoc keyIterator(MBeanServerConnection JavaDoc server) throws IOException JavaDoc,
97       MBeanException JavaDoc, AttributeNotFoundException JavaDoc, InstanceNotFoundException JavaDoc,
98       ReflectionException JavaDoc
99   {
100     Object JavaDoc keys = getAttribute(server);
101     if (keys instanceof Collection JavaDoc) {
102       if (keysNeedSorting) {
103         List JavaDoc l = new ArrayList JavaDoc((Collection JavaDoc)keys);
104         Collections.sort(l);
105         keys = l;
106       }
107       return ((Collection JavaDoc)keys).iterator();
108     }
109     else if (keys instanceof Object JavaDoc[]) {
110       List JavaDoc l = Arrays.asList((Object JavaDoc[])keys);
111       return getAscendingIterator(l);
112     }
113     else if (keys instanceof long[]) {
114       List JavaDoc l = asList((long[])keys);
115       return getAscendingIterator(l);
116     }
117     else if (keys instanceof int[]) {
118       List JavaDoc l = asList((int[])keys);
119       return getAscendingIterator(l);
120     }
121     else {
122       throw new ClassCastException JavaDoc(keys.getClass()+
123                                    " is not a supported list");
124     }
125   }
126
127   private Iterator JavaDoc getAscendingIterator(List JavaDoc l) {
128     if (keysNeedSorting) {
129       l = new ArrayList JavaDoc(l);
130       Collections.sort(l);
131     }
132     return l.iterator();
133   }
134
135   public static List JavaDoc<Integer JavaDoc> asList(final int[] a) {
136     return new AbstractList JavaDoc<Integer JavaDoc>() {
137       public Integer JavaDoc get(int i) { return a[i]; }
138       // Throws NullPointerException if val == null
139
public Integer JavaDoc set(int i, Integer JavaDoc val) {
140         Integer JavaDoc oldVal = a[i];
141         a[i] = val;
142         return oldVal;
143       }
144       public int size() { return a.length; }
145     };
146   }
147
148   public static List JavaDoc<Long JavaDoc> asList(final long[] a) {
149     return new AbstractList JavaDoc<Long JavaDoc>() {
150       public Long JavaDoc get(int i) { return a[i]; }
151       // Throws NullPointerException if val == null
152
public Long JavaDoc set(int i, Integer JavaDoc val) {
153         Long JavaDoc oldVal = a[i];
154         a[i] = val;
155         return oldVal;
156       }
157       public int size() { return a.length; }
158     };
159   }
160
161   /**
162    * Returns an iterator on the keys provided by this MBean attribute starting
163    * from the supplied row key.
164    *
165    * @param server
166    * the <code>MBeanServerConnection</code> to be used to access the MBean.
167    * @param firstRowId
168    * the lower bound (including) row key for the iterator.
169    * @return Iterator
170    * an iterator providing the keys returned by the key provider attribute.
171    * @throws IOException
172    * @throws MBeanException
173    * @throws AttributeNotFoundException
174    * @throws InstanceNotFoundException
175    * @throws ReflectionException
176    */

177   public Iterator JavaDoc keyTailIterator(MBeanServerConnection JavaDoc server,
178                                   Object JavaDoc firstRowId) throws IOException JavaDoc,
179       MBeanException JavaDoc, AttributeNotFoundException JavaDoc, InstanceNotFoundException JavaDoc,
180       ReflectionException JavaDoc
181   {
182     Object JavaDoc keys = getAttribute(server);
183     if (keys instanceof Collection JavaDoc) {
184       keys = ((Collection JavaDoc)keys).toArray();
185     }
186     if (keys instanceof Object JavaDoc[]) {
187       if (keysNeedSorting) {
188         Arrays.sort((Object JavaDoc[])keys);
189       }
190       List JavaDoc l = Arrays.asList((Object JavaDoc[])keys);
191       int pos = 0;
192       if (firstRowId instanceof JMXArrayIndexKey) {
193         pos = ((JMXArrayIndexKey)firstRowId).getIndex();
194       }
195       else {
196         pos = Arrays.binarySearch((Object JavaDoc[]) keys, firstRowId);
197       }
198       if (Math.abs(pos) >= l.size()) {
199         return Collections.emptyList().iterator();
200       }
201       return createTailIterator(l.listIterator(Math.abs(pos)), pos);
202     }
203     else if (keys instanceof long[]) {
204       if (keysNeedSorting) {
205         Arrays.sort((long[])keys);
206       }
207       List JavaDoc l = asList((long[])keys);
208       int pos = 0;
209       if (firstRowId instanceof JMXArrayIndexKey) {
210         pos = ((JMXArrayIndexKey)firstRowId).getIndex();
211       }
212       else {
213         pos = Arrays.binarySearch((long[]) keys, (Long JavaDoc)firstRowId);
214       }
215       if (Math.abs(pos) >= l.size()) {
216         return Collections.emptyList().iterator();
217       }
218       return createTailIterator(l.listIterator(Math.abs(pos)), pos);
219     }
220     else if (keys instanceof int[]) {
221       if (keysNeedSorting) {
222         Arrays.sort((int[])keys);
223       }
224       List JavaDoc l = asList((int[])keys);
225       int pos = 0;
226       if (firstRowId instanceof JMXArrayIndexKey) {
227         pos = ((JMXArrayIndexKey)firstRowId).getIndex();
228       }
229       else {
230         pos = Arrays.binarySearch((int[]) keys, (Integer JavaDoc)firstRowId);
231       }
232       if (Math.abs(pos) >= l.size()) {
233         return Collections.emptyList().iterator();
234       }
235       return createTailIterator(l.listIterator(Math.abs(pos)), pos);
236     }
237     else {
238       throw new ClassCastException JavaDoc(keys.getClass()+
239                                    " is not a supported list");
240     }
241   }
242
243   protected Iterator JavaDoc createTailIterator(Iterator JavaDoc it, int indexPos) {
244     return it;
245   }
246
247   /**
248    * Returns the number of row keys available.
249    * @param server
250    * the <code>MBeanServerConnection</code> to be used to access the MBean.
251    * @return
252    * the number of keys.
253    * @throws IOException
254    * @throws MBeanException
255    * @throws AttributeNotFoundException
256    * @throws InstanceNotFoundException
257    * @throws ReflectionException
258    */

259   public int getKeyCount(MBeanServerConnection JavaDoc server) throws IOException JavaDoc,
260       MBeanException JavaDoc, AttributeNotFoundException JavaDoc, InstanceNotFoundException JavaDoc,
261       ReflectionException JavaDoc
262   {
263     Object JavaDoc keys = getAttribute(server);
264     if (keys instanceof Collection JavaDoc) {
265       return ((Collection JavaDoc)keys).size();
266     }
267     else if (keys instanceof Object JavaDoc[]) {
268       return ((Object JavaDoc[])keys).length;
269     }
270     else if (keys instanceof long[]) {
271       return ((long[])keys).length;
272     }
273     else if (keys instanceof int[]) {
274       return ((int[])keys).length;
275     }
276     return 0;
277   }
278
279   public Object JavaDoc getRowValues(MBeanServerConnection JavaDoc server,
280                              Object JavaDoc indexObject) throws IOException JavaDoc,
281       MBeanException JavaDoc, AttributeNotFoundException JavaDoc, InstanceNotFoundException JavaDoc,
282       ReflectionException JavaDoc
283   {
284     return indexObject;
285   }
286
287 }
288
Popular Tags