|
From: Heiko W. R. <hw...@pi...> - 2006-07-10 14:37:42
|
User: pilhuhn
Date: 06/07/10 10:37:39
Modified: varia/src/main/org/jboss/jmx/adaptor/snmp/agent Tag:
Branch_4_0 ReadOnlyException.java
RequestHandlerImpl.java
Added: varia/src/main/org/jboss/jmx/adaptor/snmp/agent Tag:
Branch_4_0 ComparableSnmpObjectId.java
Log:
Handle oid sorting and comparing through a ComparableSnmpObjectId-Class.
With use of it, fix JBAS-3178.
Revision Changes Path
No revision
No revision
1.1.2.8 +8 -1 contrib/varia/src/main/org/jboss/jmx/adaptor/snmp/agent/ReadOnlyException.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: ReadOnlyException.java
===================================================================
RCS file: /cvsroot/jboss/contrib/varia/src/main/org/jboss/jmx/adaptor/snmp/agent/ReadOnlyException.java,v
retrieving revision 1.1.2.7
retrieving revision 1.1.2.8
diff -u -b -r1.1.2.7 -r1.1.2.8
--- ReadOnlyException.java 30 May 2006 19:31:12 -0000 1.1.2.7
+++ ReadOnlyException.java 10 Jul 2006 14:37:39 -0000 1.1.2.8
@@ -21,6 +21,8 @@
*/
package org.jboss.jmx.adaptor.snmp.agent;
+import org.opennms.protocols.snmp.SnmpObjectId;
+
/**
* This exception is thrown when an entry is read only and
* a client wants to write to it.
@@ -34,4 +36,9 @@
{
super("OID " + oid + " is read only");
}
+
+ public ReadOnlyException(SnmpObjectId oid)
+ {
+ super("OID " + oid + " is read only");
+ }
}
1.1.2.2 +198 -198 contrib/varia/src/main/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerImpl.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: RequestHandlerImpl.java
===================================================================
RCS file: /cvsroot/jboss/contrib/varia/src/main/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerImpl.java,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -u -b -r1.1.2.1 -r1.1.2.2
--- RequestHandlerImpl.java 6 May 2006 22:48:00 -0000 1.1.2.1
+++ RequestHandlerImpl.java 10 Jul 2006 14:37:39 -0000 1.1.2.2
@@ -25,9 +25,10 @@
import java.net.InetAddress;
import java.util.Iterator;
import java.util.List;
-import java.util.Set;
import java.util.SortedMap;
+import java.util.SortedSet;
import java.util.TreeMap;
+import java.util.TreeSet;
import javax.management.Attribute;
import javax.management.MBeanServer;
@@ -57,7 +58,7 @@
*
* @author <a href="mailto:hw...@pi...>">Heiko W. Rupp</a>
* @author <a href="mailto:dim...@jb...">Dimitris Andreadis</a>
- * @version $Revision: 1.1.2.1 $
+ * @version $Revision: 1.1.2.2 $
*/
public class RequestHandlerImpl extends RequestHandlerSupport
implements Reconfigurable
@@ -67,9 +68,12 @@
private static final String NO_ENTRY_FOUND_FOR_OID = "No entry found for oid ";
private static final String SKIP_ENTRY = " - skipping entry";
+
/** Bindings from oid to mbean */
protected SortedMap bindings = new TreeMap();
+ private SortedSet oidKeys = null;
+
/** Has this RequestHandler instance been initialized? */
private boolean initialized = false;
@@ -80,7 +84,8 @@
*/
public RequestHandlerImpl()
{
- // empty
+ bindings = new TreeMap();
+ oidKeys = new TreeSet();
}
// RequestHandler Implementation ---------------------------------
@@ -96,8 +101,8 @@
public void initialize(String resourceName, MBeanServer server, Logger log, Clock uptime)
throws Exception
{
+ log.debug("initialize() with res=" + resourceName);
super.initialize(resourceName, server, log, uptime);
- log.debug("initialized() with res=" + resourceName);
if (resourceName != null)
initializeBindings();
else
@@ -147,6 +152,8 @@
*/
public SnmpPduRequest snmpReceivedGet(SnmpPduPacket pdu, boolean getNext)
{
+ try
+ {
SnmpPduRequest response = null;
int pduLength = pdu.getLength();
final boolean trace = log.isTraceEnabled();
@@ -169,30 +176,33 @@
{
/*
* We call getNextOid() to find out what is the next valid OID
- * instance in the supported MIB (sub-)tree. Assign that OID to
- * the VB List and then proceed same as that of get request. If
- * the passed oid is already the last, we flag it.
- */
- String oids = oid.toString();
- try
+ * instance in the supported MIB (sub-)tree. Assign that OID to the
+ * VB List and then proceed same as that of get request. If the
+ * passed oid is already the last, we flag it.
+ */
+ ComparableSnmpObjectId coid = new ComparableSnmpObjectId(oid);
+ oid = getNextOid(coid, true);
+ if (oid == null)
{
- oids = getNextOid(oid.toString());
- oid = new SnmpObjectId(oids);
- pdu.setVarBindAt(i, new SnmpVarBind(oid));
+ good = false;
}
- catch (NoSuchFieldException nse)
- { // abused
- good = false; // no next oid found
+ else
+ {
+ pdu.setVarBindAt(i, new SnmpVarBind(oid));
}
}
+ if (oid!=null)
vblist[i] = new SnmpVarBind(oid);
+ else
+ vblist[i] = new SnmpVarBind(vb.getName()); // oid passed in
+
if (trace)
- log.trace("oid=" + oid.toString());
+ log.trace("oid=" + oid);
SnmpSyntax result = null;
- if (bindings != null)
- result = getValueFor(oid.toString());
+ if (good && bindings != null)
+ result = getValueFor(oid);
if (trace)
log.trace("got result of " + result);
@@ -201,7 +211,7 @@
{
errorStatus = SnmpPduPacket.ErrNoSuchName;
errorIndex = i + 1;
- // log.debug("Error Occured " + vb.getName().toString());
+ log.debug("Error Occured " + vb.getName().toString());
}
else
{
@@ -210,11 +220,17 @@
+ vblist[i].getName().toString());
log.debug(" --> " + vblist[i].getValue().toString());
}
- }
+ } // for ...
response = new SnmpPduRequest(SnmpPduPacket.RESPONSE, vblist);
response.setErrorStatus(errorStatus);
response.setErrorIndex(errorIndex);
return response;
+ } catch (Exception e)
+ {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ return null;
+ }
}
/**
@@ -250,7 +266,7 @@
SnmpSyntax result = null;
try
{
- result = setValueFor(oid.toString(),newVal);
+ result = setValueFor(oid,newVal);
}
catch (ReadOnlyException e)
{
@@ -389,8 +405,10 @@
return;
}
log.debug("Found " + mappings.size() + " attribute mappings");
+ /**
+ * We have the MBeans now. Put them into the bindungs.
+ */
- // Turn the parsed mappings into bindings.
Iterator it = mappings.iterator();
while (it.hasNext())
{
@@ -410,11 +428,11 @@
BindEntry be = new BindEntry(oid, mmb.getName(), ma.getName());
be.isReadWrite = ma.isReadWrite();
+ ComparableSnmpObjectId coid = new ComparableSnmpObjectId(oid);
+
if (log.isTraceEnabled())
log.trace("New bind entry " + be);
-
- if (bindings.containsKey(oid))
- {
+ if (bindings.containsKey(coid)) {
log.info("Duplicate oid " + oid + SKIP_ENTRY);
continue;
}
@@ -428,7 +446,9 @@
log.info("Invalid attribute name " + ma.getName() + " for oid " + oid + SKIP_ENTRY);
continue;
}
- bindings.put(oid, be);
+ bindings.put(coid, be);
+ oidKeys.add(coid);
+
}
}
}
@@ -440,8 +460,8 @@
* The oid we want a value for
* @return SnmpNull if no value present
*/
- private SnmpSyntax getValueFor(final String oid)
- {
+ private SnmpSyntax getValueFor(final SnmpObjectId oid) {
+
BindEntry be = findBindEntryForOid(oid);
SnmpSyntax ssy = null;
if (be != null)
@@ -496,7 +516,7 @@
* @return null on success, non-null on failure
* @throws ReadOnlyException If the referred entry is read only.
*/
- private SnmpSyntax setValueFor(final String oid, final SnmpSyntax newVal) throws ReadOnlyException
+ private SnmpSyntax setValueFor(final SnmpObjectId oid, final SnmpSyntax newVal) throws ReadOnlyException
{
final boolean trace = log.isTraceEnabled();
@@ -563,111 +583,88 @@
return ssy;
}
+
/**
* Lookup a BinEntry on the given oid. If the oid ends in .0,
* then the .0 will be stripped of before the search.
* @param oid The oid look up.
* @return a bind entry or null.
*/
- private BindEntry findBindEntryForOid(final String oid)
+ private BindEntry findBindEntryForOid(final SnmpObjectId oid) {
+
+ ComparableSnmpObjectId coid= new ComparableSnmpObjectId(oid);
+
+ if (coid.isLeaf())
{
- String localOid = oid;
- if (oid.endsWith(".0"))
- localOid = oid.substring(0,oid.length()-2);
+ coid = coid.removeLastPart();
+ }
+ BindEntry be = (BindEntry)bindings.get(coid);
- BindEntry be = (BindEntry) bindings.get(localOid);
return be;
}
/**
- * Tries to retrieve the next oid following the current one
- *
- * @param in
- * an oid
- * @return an oid
- * @throws NoSuchFieldException
- * if no next oid can be found
- */
- private String getNextOid(final String in) throws NoSuchFieldException
- {
- final boolean trace = log.isTraceEnabled();
+ * Return the next oid that is larger than ours.
+ * @param oid the starting oid
+ * @param stayInSubtree if true, the next oid will not have a different prefix than the one of oid.
+ * @return the next oid or null if none found.
+ */
+ private ComparableSnmpObjectId getNextOid(final ComparableSnmpObjectId oid, boolean stayInSubtree) {
+ ComparableSnmpObjectId coid = new ComparableSnmpObjectId(oid);
+
+
+ if (coid.isLeaf())
+ coid = coid.removeLastPart();
+
+ SortedSet ret;
+ ret= oidKeys.tailSet(oid); // get oids >= oid
+ Iterator it = ret.iterator();
+ ComparableSnmpObjectId roid=null;
- String roid = in.substring(0, in.lastIndexOf('.'));
-
- if (trace)
- log.trace("in: " + in + " roi: " + roid);
-
- Set oids = bindings.keySet();
- Iterator it = oids.iterator();
- while (it.hasNext())
+ /*
+ * If there are elements in the tail set, then
+ * - get first one.
+ * - if first is input (which it is supposed to be according to the contract of
+ * SortedSet.tailSet() , then get next, which is the
+ * one we look for.
+ */
+ if (it.hasNext())
{
- String id = (String) it.next();
- int comp = compareOid(in, id);
- if (trace)
- log.trace("--- found id " + id + " comp = " + comp);
-
- if (comp >= 0)
- continue;
-
- if (trace)
- log.trace("Returning " + id);
-
- return id;
+ roid = (ComparableSnmpObjectId)it.next(); // oid
}
- if (trace)
- log.trace("## no next oid found for " + in);
- throw new NoSuchFieldException();
+ if (roid == null)
+ {
+ return null; // roid is null,
}
- /**
- * Compare the two passed oid-strings.
- *
- * @param oid1
- * @param oid2
- * @return 0 when equal, 1 if oid2 > oid1 and -1 if oid1>oid2
- */
- private int compareOid(final String oid1, final String oid2)
+ if (roid.compareTo(coid)==0) // input elment
{
- int res;
- int nd1 = countDots(oid1);
- int nd2 = countDots(oid2);
- res = nd1 - nd2;
- if (res != 0) // different number of dots -> not equal
- return res;
-
- // equal number of dots. Check for same prefix (subtree)
- String tpref = oid1.substring(0, oid1.lastIndexOf("."));
- String opref = oid2.substring(0, oid2.lastIndexOf("."));
-
- res = tpref.compareTo(opref);
-
- // same prefix
- if (res == 0)
+ // if there is a next element, then it is ours.
+ if (it.hasNext())
{
- String tsuf = oid1.substring(oid1.lastIndexOf(".") + 1);
- String osuf = oid2.substring(oid2.lastIndexOf(".") + 1);
- int tval = Integer.parseInt(tsuf);
- int oval = Integer.parseInt(osuf);
-
- res = tval - oval;
+ roid = (ComparableSnmpObjectId)it.next();
+ }
+ else
+ {
+ roid = null; // end of list
}
- return res;
}
- /**
- * Count the number of dots in the passed String (an oid actually)
+ /*
+ * Check if still in subtree if requested to stay within
*/
- private int countDots(final String in)
+ if (stayInSubtree && roid != null)
{
- int count = 0;
- for (int i = 0; i < in.length(); i++)
- if (in.charAt(i) == '.')
- count++;
+ ComparableSnmpObjectId parent = coid.removeLastPart();
+ if (!parent.isRootOf(roid))
+ roid = null;
+ }
- return count;
+ return roid;
}
+
// Inner Class ---------------------------------------------------
/**
@@ -675,10 +672,9 @@
*
* @author <a href="mailto:pi...@us...>">Heiko W. Rupp</a>
*/
- private class BindEntry implements Comparable
- {
- private final String oid;
- private final int numDots;
+ private class BindEntry implements Comparable {
+ private final ComparableSnmpObjectId oid;
+
private ObjectName mbean;
private Attribute attr;
private String mName;
@@ -695,10 +691,20 @@
* @param attrName
* The name of the attribute to query
*/
- BindEntry(final String oid, final String mbName, final String attrName)
- {
- this.oid = oid;
- numDots = countDots(oid);
+ BindEntry(final String oidString, final String mbName, final String attrName) {
+ this(new ComparableSnmpObjectId(oidString),
+ mbName,
+ attrName);
+ }
+
+ /**
+ * Constructs a new BindEntry.
+ * @param coid The SNMP-oid, this entry will use.
+ * @param mbName The name of an MBean with attribute to query
+ * @param attrName The name of the attribute to query
+ */
+ BindEntry(final ComparableSnmpObjectId coid, final String mbName, final String attrName) {
+ oid = coid;
this.mName = mbName;
this.aName = attrName;
try
@@ -715,8 +721,21 @@
}
}
- public Attribute getAttr()
- {
+ /**
+ * A string representation of this BindEntry
+ */
+ public String toString() {
+ StringBuffer buf = new StringBuffer();
+ buf.append("[oid=");
+ buf.append(oid).append(", mbean=");
+ buf.append(mName).append(", attr=");
+ buf.append(aName).append(", rw=");
+ buf.append(isReadWrite).append("]");
+
+ return buf.toString();
+ }
+
+ public Attribute getAttr() {
return attr;
}
@@ -725,17 +744,12 @@
return mbean;
}
- public String getOid()
+ public ComparableSnmpObjectId getOid()
{
return oid;
}
- public int getNumDots()
- {
- return numDots;
- }
-
/**
* Compare two BindEntries. Ordering is defined at oid-level.
*
@@ -759,24 +773,10 @@
if (getOid().equals(obe.getOid()))
return 0;
- int res = compareOid(oid, obe.getOid());
+ int res =oid.compare(obe.getOid());
return res;
}
- /**
- * A string representation of this BindEntry
- */
- public String toString()
- {
- StringBuffer buf = new StringBuffer();
- buf.append("[oid=");
- buf.append(oid).append(", mbean=");
- buf.append(mName).append(", attr=");
- buf.append(aName).append(", rw=");
- buf.append(isReadWrite).append("]");
-
- return buf.toString();
- }
}
}
No revision
No revision
1.1.2.2 +139 -0 contrib/varia/src/main/org/jboss/jmx/adaptor/snmp/agent/ComparableSnmpObjectId.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: ComparableSnmpObjectId.java
===================================================================
RCS file: ComparableSnmpObjectId.java
diff -N ComparableSnmpObjectId.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ComparableSnmpObjectId.java 10 Jul 2006 14:37:39 -0000 1.1.2.2
@@ -0,0 +1,139 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.jmx.adaptor.snmp.agent;
+
+import org.opennms.protocols.snmp.SnmpObjectId;
+
+/**
+ * Provide SnmpObjectIds that are Comparable to be used
+ * in SortedSets etc.
+ * @author <a href="mailto:hw...@pi...">Heiko W. Rupp</a>
+ * @version $Revision: 1.1.2.2 $
+ */
+public class ComparableSnmpObjectId extends SnmpObjectId implements Comparable
+{
+
+ public ComparableSnmpObjectId(String oid)
+ {
+ super(oid);
+ }
+
+ public ComparableSnmpObjectId(SnmpObjectId oid)
+ {
+ super(oid);
+ }
+
+ public ComparableSnmpObjectId(int[] identifiers)
+ {
+ super(identifiers);
+ }
+
+ /**
+ * Compare to the passed object. Uses compare()
+ * from the underlying snmp-library
+ * @see SnmpObjectId.compare()
+ * @param o Object to compare with (Usually a ComparableSnmpObjectId)
+ * @return -1, if no SnmpObjectId passed in, the result of the underlying compare otherwise.
+ */
+ public int compareTo(Object o)
+ {
+
+ if (o==null)
+ return -1;
+
+ if (!(o instanceof SnmpObjectId))
+ return -1;
+
+ return this.compare((SnmpObjectId)o);
+ }
+
+ /**
+ * This object is a leaf if the last part of the oid parts is a 0 (Zero)
+ * @return true if the oid ends in 0
+ */
+ public boolean isLeaf()
+ {
+ int[] ids = getIdentifiers();
+ if (ids.length==0) { // no data present (should not happen)
+ return false;
+ }
+ if (ids[ids.length-1]==0)
+ {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Removes the last oid-component.
+ * Example .1.2.3.4.0 as input yields .1.2.3.4 as output
+ * @return an oid with the last part removed.
+ */
+ public ComparableSnmpObjectId removeLastPart()
+ {
+ int[] ids = getIdentifiers();
+
+ int[] outs = new int[ids.length-1];
+ int len = ids.length-1;
+ for (int i = 0; i<len ; i++)
+ {
+ outs[i]=ids[i];
+ }
+ ComparableSnmpObjectId out = new ComparableSnmpObjectId(outs);
+ return out;
+ }
+
+ /**
+ * Build an oid where the second last component
+ * (after removing a .0 of a leaf) is increased by 1.
+ * The last component is removed, to the result actually forms
+ * the root of a subtree, that is adjacent to the subtree this
+ * object is in.
+ * Example .1.2.3.4.0 -> .1.2.4
+ * Example .1.2.3.4.5 -> .1.2.4
+ * @return
+ */
+ public ComparableSnmpObjectId getNextArc()
+ {
+ ComparableSnmpObjectId cid = this;
+ if (isLeaf())
+ {
+ cid = removeLastPart();
+ }
+ cid = cid.removeLastPart();
+
+ int[] ids = cid.getIdentifiers();
+ int[] ods = new int[ids.length];
+ System.arraycopy(ids,
+ 0,
+ ods,
+ 0,
+ ids.length);
+
+ int len = ods.length-1;
+ ods[len]++;
+
+ ComparableSnmpObjectId ret = new ComparableSnmpObjectId(ods);
+ return ret;
+ }
+
+}
|