[jboss-cvs] JBossAS SVN: r111599 - branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Jun 13 16:55:10 EDT 2011


Author: thauser at redhat.com
Date: 2011-06-13 16:55:10 -0400 (Mon, 13 Jun 2011)
New Revision: 111599

Modified:
   branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerImpl.java
Log:
merging + exposure of bindings SortedMap

Modified: branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerImpl.java
===================================================================
--- branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerImpl.java	2011-06-13 20:53:24 UTC (rev 111598)
+++ branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/agent/RequestHandlerImpl.java	2011-06-13 20:55:10 UTC (rev 111599)
@@ -102,6 +102,11 @@
 		oidKeys = new TreeSet();
 		objectKeys = new TreeSet();
 	}
+	
+	// expose the bindings map, for use by the TrapFactory
+	public SortedMap getBindings(){
+		return this.bindings;
+	}
 
 	// RequestHandler Implementation ---------------------------------
 
@@ -442,33 +447,32 @@
 					var = setValueFor(oid,newVal);
 				}
 				else{
-					log.debug("snmpReceivedSet: no object for SET request");
+					log.error("snmpReceivedSet: no object for SET request");
 					undoSets(modified);
 					makeErrorPdu(response, pdu, errorIndex, PDU.noAccess);
 					return response;
 				}
 			}
 			catch (NoSuchInstanceException e){
-				log.debug("snmpReceivedSet: attempt to set a non-existent instance: " + oid.last() + " of object: " + oid.trim());
+				log.error("snmpReceivedSet: attempt to set a non-existent instance: " + oid.last() + " of object: " + oid.trim(), e);
 				undoSets(modified);
 				makeErrorPdu(response, pdu, errorIndex, PDU.noCreation);
 				return response;
 			}
 			catch (VariableTypeException e){
-				log.debug("snmpReceievedSet: could not convert the given value into an appropriate type: " +newVal);
+				log.error("snmpReceievedSet: could not convert the given value into an appropriate type: " +newVal, e);
 				undoSets(modified);
 				makeErrorPdu(response, pdu, errorIndex, PDU.wrongType);
 				return response;
 			}
 			catch (ReadOnlyException e){
-				log.debug("snmpReceivedSet: attempt to set a read-only attribute: " + newVB);
+				log.error("snmpReceivedSet: attempt to set a read-only attribute: " + newVB, e);
 				undoSets(modified);
 				makeErrorPdu(response, pdu, errorIndex, PDU.notWritable);
 				return response;
 			}
 			catch (Exception e){
-				log.debug("snmpReceivedSet: catastrophe!!! General variable validation error." + " " + e);
-				e.printStackTrace();
+				log.error("snmpReceivedSet: catastrophe!!! General variable validation error.", e);
 				undoSets(modified);
 				makeErrorPdu(response, pdu, errorIndex, PDU.genErr);
 				return response;
@@ -635,7 +639,6 @@
 		   ManagedBean mmb = (ManagedBean)it.next();
 		   String oidPrefix = mmb.getOidPrefix();
 		   List attrs = mmb.getAttributes();
-	   
 		   Iterator aIt = attrs.iterator();
 		   while (aIt.hasNext())
 		   {
@@ -646,13 +649,11 @@
 			  String oid;
 			  if (oidPrefix != null){
 				  oid = oidPrefix + ma.getOid();
-				  ma.setOidPrefix(oidPrefix);
 				  addObjectEntry(new OID(oidPrefix));
 			  }
 			  else{
 				  oid = ma.getOid();
 				  OID objectOID = new OID(oid);
-				  ma.setOidPrefix(objectOID.trim().toString());
       			  addObjectEntry(objectOID.trim());
 			  }
 			  addBindEntry(oid, mmb.getName(), ma.getName(),ma.isReadWrite());			  
@@ -684,7 +685,7 @@
 	
 	private void addBindEntry(String oid, String mmb, String ma, boolean rw){
 	  BindEntry be = new BindEntry(oid, mmb, ma);
-	  be.isReadWrite = rw;
+	  be.setReadWrite(rw);
 	  
 	  OID coid = new OID(oid);
 	  if (log.isTraceEnabled())
@@ -732,37 +733,28 @@
 
 		BindEntry be = findBindEntryForOid(oid);
 		Variable ssy = null;
-		if (be != null)
-     {
+		if (be != null) {
 			if (log.isTraceEnabled())
 				log.debug("getValueFor: Found entry " + be.toString() + " for oid " + oid);
         
-			try
-        {
-			   Object val = server.getAttribute(be.mbean, be.attr.getName());
+			try {
+			   Object val = server.getAttribute(be.getMbean(), be.getAttr().getName());
 			   ssy = prepForPdu(val);
-		}
-		catch (VariableTypeException e){
-			log.debug("getValueFor: didn't find a suitable data type for the requested data");
-			throw e;
-		}
-        catch (Exception e)
-        {
-				log.warn("getValueFor: (" + be.mbean.toString() + ", "
-						+ be.attr.getName() + ": " + e.toString());
-        }
-     }
-     else
-     {		
-    	 	
+			} catch (VariableTypeException e){
+				log.debug("getValueFor: didn't find a suitable data type for the requested data");
+				throw e;
+			} catch (Exception e) {
+					log.warn("getValueFor: (" + be.getMbean().toString() + ", "
+							+ be.getAttr().getName() + ": " + e.toString());
+					throw new NoSuchInstanceException();					
+	        }
+		} else {		
 			log.debug("getValueFor: " + NO_ENTRY_FOUND_FOR_OID + oid);
 			throw new NoSuchInstanceException();
 		}
 		return ssy;
 	}
-
 	
-	
 	/**This method takes an Object that is typically going to be 
 	 * put into a VariableBinding for use in a PDU, and thus must be converted
 	 * into an SNMP type based on it's type. This Object is usually read 
@@ -778,41 +770,28 @@
 	private Variable prepForPdu(final Object val) throws VariableTypeException{
 		Variable result = null;
 		//TODO: all types managed by the PDU
-		if (val instanceof Long)
-        {
+		if(val == null) {
+			result = new Null();
+		} else if (val instanceof Long) {
 			result = new OctetString(((Long)val).toString());
-		}
-		else if (val instanceof Boolean)
-        {
+		} else if (val instanceof Boolean) {
 			if(((Boolean)val).booleanValue())
 				result = new Integer32(1);
 			else 
 				result = new Integer32(0);
-		}
-        else if (val instanceof String)
-        {
+		} else if (val instanceof String) {
         	result = new OctetString((String) val);
-		}
-        else if (val instanceof Integer)
-        {
+		} else if (val instanceof Integer) {
 			result = new Integer32((Integer)val);
-		}
-        else if (val instanceof OID)     
-        {
+		} else if (val instanceof OID) {
         	result = new OID((OID)val);
-        }
-        else if (val instanceof TimeTicks)
-        {
+        } else if (val instanceof TimeTicks) {
         	// the SNMP4J class TimeTicks default toString method is formatted horribly. This 
         	// call emulates the joesnmp SnmpTimeTicks display.
         	result = (TimeTicks) val;
-        }
-        else if (val instanceof Counter32)
-        {
+        } else if (val instanceof Counter32) {
      	   	result = (Counter32) val;
-        }
-        else
-        {
+        } else {
         	throw new VariableTypeException(); // no instance of an SNMP Variable could be created for type
         }
 		return result;
@@ -865,20 +844,14 @@
 		
 	}
 	
-//	/**
-//	 * Set a jmx attribute
-//	 * @param oid The oid to set. This is translated into a mbean / attribute pair
-//	 * @param newVal The new value to set
-//	 * @return null on success, non-null on failure
-//	 * @throws ReadOnlyException If the referred entry is read only.
-//	 */
-	
-	//TODO: currently, if one accesses a String and attempts to set it to an Int or Double value,
-	// the response is success, but the result is failure. This must be changed by investigating 
-	// what value is given from this method when such a situation arises, and then either throwing an exception 
-	// or handling that value in the caller. /** THIS HAS BEEN IMPLEMENTED. AN EXCEPTION SHOULD BE THROWN. **/
-	//TODO: decide what to do in the case of a non writeable entry. (Exception thrown)
-	
+
+	 /** Set a jmx attribute
+	 * @param oid The oid to set. This is translated into a mbean / attribute pair
+	 * @param newVal The new value to set
+	 * @return null on success, non-null on failure
+	 * @throws ReadOnlyException If the referred entry is read only.
+	 * */
+
 	private Variable setValueFor(final OID oid, final Variable newVal) 
 	throws ReadOnlyException, VariableTypeException, NoSuchInstanceException {
 		final boolean trace = log.isTraceEnabled();
@@ -894,7 +867,7 @@
 			if (trace)
 				log.trace("setValueFor: " + be.toString());
          
-			if (be.isReadWrite == false)
+			if (be.isReadWrite() == false)
 			{
 				if (trace)
 					log.trace("setValueFor: this is marked read only");
@@ -903,16 +876,16 @@
 			}
 			try
 			{		
-				Object other = server.getAttribute(be.mbean, be.attr.getName());
+				Object other = server.getAttribute(be.getMbean(), be.getAttr().getName());
 				Object val = convertVariableToValue(newVal, other);
 				
-				if (val.getClass() != other.getClass() ){
+				if (other != null && val.getClass() != other.getClass() ){
 					log.debug("setValueFor: attempt to set an MBean Attribute with the wrong type.");
 					ssy = newVal;
 				}
 								
-				Attribute at = new Attribute(be.attr.getName(), val);
-				server.setAttribute(be.mbean, at);
+				Attribute at = new Attribute(be.getAttr().getName(), val);
+				server.setAttribute(be.getMbean(), at);
 			
 				if (trace)
 					log.trace("setValueFor: set attribute in mbean server");
@@ -1061,214 +1034,15 @@
 	/** This utility method is used to construct an error PDU. This code
 	 * was repeated so many times it was prudent to give it it's own method.
 	 * @param response This PDU is the one being modified into an error PDU.
-	 * @param oid The OID to contain the error Null instance.
-	 * @param errNo The error number defined in the PDU class that indicates a given failure
-	 * @param errInd the VariableBinding in the PDU that caused the error. 
+	 * @param pdu The Offending PDU, to grab VariableBinding list from it
+	 * @param errIndex the VariableBinding in the PDU that caused the error.
+	 * @param errNo The error number defined in the PDU class that indicates a given failure 
 	 */
-	private void makeErrorPdu(PDU response, PDU pdu, int errorIndex, int err){
+	private void makeErrorPdu(PDU response, PDU pdu, int errIndex, int errNo){
 		response.clear();
 		response.addAll(pdu.toArray());
-		response.setErrorIndex(errorIndex);
-		response.setErrorStatus(err);
+		response.setErrorIndex(errIndex);
+		response.setErrorStatus(errNo);
 	}
-
-   // Inner Class ---------------------------------------------------
-   
-	/**
-	 * An entry containing the mapping between oid and mbean/attribute
-	 * 
-	 * @author <a href="mailto:pilhuhn at user.sf.net>">Heiko W. Rupp</a>
-	 */
-	private class BindEntry implements Comparable {
-		private final OID oid;
-
-		private ObjectName mbean;
-		private Attribute attr;
-		private String mName;
-		private String aName;      
-		private boolean isReadWrite = false;
-
-		/**
-		 * Constructs a new BindEntry
-		 * 
-		 * @param oid
-		 *            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 String oidString, final String mbName, final String attrName) {
-			this(new OID(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 OID coid, final String mbName, final String attrName) {
-			oid = coid;
-			this.mName = mbName;
-			this.aName = attrName;
-			try
-         {
-			   mbean = new ObjectName(mbName);
-				attr = new Attribute(attrName, null);
-
-			}
-         catch (Exception e)
-         {
-            log.warn(e.toString());
-				mName = "-unset-";
-				aName = "-unset-";
-			}
-		}
-
-		/**
-		 * 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;
-		}
-
-		public ObjectName getMbean()
-      {
-			return mbean;
-		}
-
-		public OID getOid()
-      {
-			return oid;
-		}
-
-
-		/**
-		 * Compare two BindEntries. Ordering is defined at oid-level.
-		 * 
-		 * @param other
-		 *            The BindEntry to compare to.
-		 * @return 0 on equals, 1 if this is bigger than other
-		 */
-		public int compareTo(Object other)
-      {
-			if (other == null)
-				throw new NullPointerException("Can't compare to NULL");
-
-			if (!(other instanceof BindEntry))
-				throw new ClassCastException("Parameter is no BindEntry");
-
-			// trivial case
-			if (this.equals(other))
-				return 0;
-         
-			BindEntry obe = (BindEntry) other;
-//			if (getOid().equals(obe.getOid()))
-//				return 0;
-
-			int res =oid.compareTo(obe.getOid());
-			return res;
-		}
-
-	}
-
 	
-	
-}
-
-/** Brian Shim commented this out for reference i'm guessing **/
-//try
-//{
-//	SnmpPduRequest response = null;
-//	int pduLength = pdu.getLength();
-//	final boolean trace = log.isTraceEnabled();
-//
-//	if (trace)
-//		log.trace("requestId=" + pdu.getRequestId() + ", pduLength="
-//				+ pduLength + ", getNext=" + getNext);
-//
-//	SnmpVarBind[] vblist = new SnmpVarBind[pduLength];
-//	int errorStatus = SnmpPduPacket.ErrNoError;
-//	int errorIndex = 0;
-//
-//	// Process for each varibind in the request
-//	for (int i = 0; i < pduLength; i++)
-//	{
-//		boolean good = true;
-//		SnmpVarBind vb = pdu.getVarBindAt(i);
-//		SnmpObjectId oid = vb.getName();
-//START OF GETNEXT
-//		if (getNext) //i don't care about GETNEXT, doing GET first
-//		{
-//			/*
-//			 * 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.
-//			 */
-//			ComparableSnmpObjectId coid = new ComparableSnmpObjectId(oid);
-//			oid = getNextOid(coid, true);
-//			if (oid == null)
-//			{
-//				good = false;
-//			}
-//			else
-//			{
-//				pdu.setVarBindAt(i, new SnmpVarBind(oid));
-//			}
-//		}
-//End of GETNEXT
-//		if (oid!=null)
-//			vblist[i] = new SnmpVarBind(oid);
-//		else
-//			vblist[i] = new SnmpVarBind(vb.getName()); // oid passed in
-//		
-//
-//		if (trace)
-//			log.trace("oid=" + oid);
-//
-//		SnmpSyntax result = null;
-//		if (good && bindings != null)
-//			result = getValueFor(oid);
-//
-//		if (trace)
-//			log.trace("got result of " + result);
-//
-//		if (result == null || !good)
-//		{
-//			errorStatus = SnmpPduPacket.ErrNoSuchName;
-//			errorIndex = i + 1;
-//			log.debug("Error Occured " + vb.getName().toString());
-//		} 
-//		else
-//		{
-//			vblist[i].setValue(result);
-//			log.debug("Varbind[" + i + "] := "
-//							+ 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;
-//}
+}
\ No newline at end of file



More information about the jboss-cvs-commits mailing list