KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*_############################################################################
2   _##
3   _## SNMP4J-AgentJMX - JMXTableModel.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
27 import org.snmp4j.agent.mo.*;
28 import org.snmp4j.mp.SnmpConstants;
29 import org.snmp4j.smi.AbstractVariable;
30 import org.snmp4j.smi.OID;
31 import org.snmp4j.smi.Variable;
32 import org.snmp4j.agent.mo.jmx.util.JMXArrayIndexKey;
33
34 /**
35  * The <code>JMXTableModel</code> implements the {@link MOTableModel} interface
36  * with the support of a {@link JMXTableSupport} instance.
37  *
38  * @author Frank Fock
39  * @version 1.0
40  */

41 public class JMXTableModel implements MOTableModel {
42
43   private OID tableOID;
44   private JMXTableSupport table;
45   private MOColumn[] columns;
46   private MOTableRowFactory rowFactory = new JMXMutableTableRowFactory();
47
48   public JMXTableModel(OID tableEntryOID,
49                        JMXTableSupport table,
50                        MOColumn[] columns) {
51     this.table = table;
52     this.tableOID = tableEntryOID;
53     this.columns = columns;
54   }
55
56   private Variable[] getInitialRowValues() {
57     Variable[] values = new Variable[getColumnCount()];
58     for (int i = 0; (i < values.length); i++) {
59       if (columns[i] instanceof MOMutableColumn) {
60         values[i] = ((MOMutableColumn) columns[i]).getDefaultValue();
61         if (values[i] == null) {
62           values[i] = AbstractVariable.createFromSyntax(columns[i].getSyntax());
63         }
64       }
65       else {
66         values[i] = AbstractVariable.createFromSyntax(columns[i].getSyntax());
67       }
68     }
69     return values;
70   }
71
72
73   public boolean containsRow(OID index) {
74     int i=0;
75     for (Iterator JavaDoc it = table.rowIdIterator(tableOID); it.hasNext(); i++) {
76       Object JavaDoc rowKey = it.next();
77       OID rowIndex = table.mapToIndex(tableOID, rowKey, i);
78       if (index.equals(rowIndex)) {
79         return true;
80       }
81     }
82     return false;
83   }
84
85   public OID firstIndex() {
86     Iterator JavaDoc it = table.rowIdIterator(tableOID);
87     if (it.hasNext()) {
88       return table.mapToIndex(tableOID, it.next(), 0);
89     }
90     return null;
91   }
92
93   public MOTableRow firstRow() {
94     OID firstIndex = firstIndex();
95     if (firstIndex != null) {
96       MOTableRow row = rowFactory.createRow(firstIndex, getInitialRowValues());
97       table.getRow(tableOID, row);
98       return row;
99     }
100     return null;
101   }
102
103   public int getColumnCount() {
104     return columns.length;
105   }
106
107   public MOTableRow getRow(OID index) {
108     MOTableRow row = rowFactory.createRow(index, getInitialRowValues());
109     int status = table.getRow(tableOID, row);
110     if (status == SnmpConstants.SNMP_ERROR_SUCCESS) {
111       return row;
112     }
113     return null;
114   }
115
116   public int getRowCount() {
117     return table.getRowCount(tableOID);
118   }
119
120   public Iterator JavaDoc iterator() {
121     return new JMXTableRowIterator(table.rowIdIterator(tableOID));
122   }
123
124   /**
125    * Returns the last row index in this model.
126    *
127    * @return the last index OID of this model.
128    */

129   public OID lastIndex() {
130     return table.getLastIndex(tableOID);
131   }
132
133   /**
134    * Returns the last row contained in this model.
135    *
136    * @return the <code>MOTableRow</code> with the greatest index or
137    * <code>null</code> if the model is empty.
138    */

139   public MOTableRow lastRow() {
140     OID lastIndex = lastIndex();
141     if (lastIndex != null) {
142       MOTableRow row = rowFactory.createRow(lastIndex, getInitialRowValues());
143       table.getRow(tableOID, row);
144       return row;
145     }
146     return null;
147   }
148
149   /**
150    * Returns an iterator on a view of the rows of this table model whose index
151    * values are greater or equal <code>lowerBound</code>.
152    *
153    * @param lowerBound the lower bound index (inclusive). If
154    * <code>lowerBound</code> is <code>null</code> the returned iterator is
155    * the same as returned by {@link #iterator}.
156    * @return an <code>Iterator</code> over the
157    */

158   public Iterator JavaDoc tailIterator(OID lowerBound) {
159     int i=0;
160     Object JavaDoc rowId = table.mapToRowId(tableOID, lowerBound);
161     if (rowId != null) {
162       return new JMXTableRowIterator(table.rowIdTailIterator(tableOID, rowId));
163     }
164     else {
165       for (Iterator JavaDoc it = table.rowIdIterator(tableOID); it.hasNext(); i++) {
166         Object JavaDoc key = it.next();
167         OID index = table.mapToIndex(tableOID, key, i+1);
168         if ((lowerBound == null) || (index.compareTo(lowerBound) >= 0)) {
169           return new JMXTableRowIterator(it, key, i+1);
170         }
171       }
172     }
173     return null;
174   }
175
176   public class JMXTableRowIterator implements Iterator JavaDoc {
177
178     private Object JavaDoc nextKey;
179     private Iterator JavaDoc keys;
180     private int nativeIndex = 0;
181
182     private JMXTableRowIterator(Iterator JavaDoc keys) {
183       this.keys = keys;
184     }
185
186     private JMXTableRowIterator(Iterator JavaDoc keys, Object JavaDoc firstKey,
187                                 int firstNativeIndex) {
188       this(keys);
189       this.nextKey = firstKey;
190       this.nativeIndex = firstNativeIndex;
191     }
192
193     public boolean hasNext() {
194       return (nextKey != null) || keys.hasNext();
195     }
196
197     public Object JavaDoc next() {
198       Object JavaDoc key = nextKey;
199       if (key == null) {
200         key = keys.next();
201       }
202       else {
203         nextKey = null;
204       }
205       if (key instanceof JMXArrayIndexKey) {
206         nativeIndex = ((JMXArrayIndexKey)key).getIndex();
207       }
208       OID index = table.mapToIndex(tableOID, key, nativeIndex++);
209       MOTableRow row = rowFactory.createRow(index, getInitialRowValues());
210       int status = table.getRow(tableOID, row);
211       if (status == SnmpConstants.SNMP_ERROR_SUCCESS) {
212         return row;
213       }
214       return null;
215     }
216
217     public void remove() {
218       throw new UnsupportedOperationException JavaDoc();
219     }
220   }
221
222   public MOTableRowFactory getRowFactory() {
223     return rowFactory;
224   }
225
226   public void setRowFactory(MOTableRowFactory rowFactory) {
227     this.rowFactory = rowFactory;
228   }
229
230   class JMXMutableTableRowFactory extends DefaultMOMutableRow2PCFactory {
231
232     public MOTableRow createRow(OID index, Variable[] values) throws
233         UnsupportedOperationException JavaDoc {
234       return new JMXMutableRow2PC(index, values);
235     }
236
237   }
238
239   class JMXMutableRow2PC extends DefaultMOMutableRow2PC {
240     public JMXMutableRow2PC(OID index, Variable[] values) {
241       super(index, values);
242     }
243
244     public void setValue(int column, Variable value) {
245       super.setValue(column, value);
246       table.setRow(tableOID, this, column);
247     }
248
249   }
250 }
251
Popular Tags