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

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Jun 13 17:05:28 EDT 2011


Author: thauser at redhat.com
Date: 2011-06-13 17:05:28 -0400 (Mon, 13 Jun 2011)
New Revision: 111607

Modified:
   branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/Generator.java
   branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/MIBGenerator.java
   branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/Parser.java
   branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/ParserNotificationBindings.java
Log:
refactoring; removal of redundant variables; adding inputstream constructor to Parser

Modified: branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/Generator.java
===================================================================
--- branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/Generator.java	2011-06-13 21:00:50 UTC (rev 111606)
+++ branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/Generator.java	2011-06-13 21:05:28 UTC (rev 111607)
@@ -48,13 +48,7 @@
 * objects gathered from the parsing of XMLs to generate MIBs. Types are determined by either an
 * attribute in the attributes.xml (snmp-type), or, if missing, a check on the attribute's type after 
 * querying the server. 
-* 
-* TODO:
-* 1) Parse the XML itself, given input files. (Done in Parser.java)
-* 2) Create an entrypoint into the program and create a JAR during build of JBPAPP_5_1
-* 3) Enable user to define the names of OIDs themselves somehow.
-* 
-* 
+*
 *  @author <a href="mailto:tom.hauser at gmail.com>Tom Hauser</a>
 **/
 
@@ -82,6 +76,8 @@
 		this.maList = null;
 	}
 	
+	
+	
 	/**
 	 * Constructor for use when also parsing Notifications.xml
 	 * @param outputResName the output filename
@@ -97,10 +93,11 @@
 		this.maList = maList;
 		this.oidDefMap = new HashMap<String, OIDDef>();
 		this.nmList = nmList;
+		this.mibnList = new ArrayList<MIBNotification>();
 	}
 	
 	/** 
-	 * Most used constructor. only attributes from the parser
+	 * Constructor without notifications mapping. Called from MIBGenerator.
 	 * 
 	 * @param outputResName the output filename
 	 * @param maList the list of MappedAttributes, received from the Parser.
@@ -153,25 +150,18 @@
 	 * Does nothing really itself. Which methods are called depends on the -r flag.
 	 */
 
-	public void writeFile(boolean reverse){
+	public void writeFile(){
 		try{
 			PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(outputResName)));
-			if (!reverse){
-				createMibObjects();
-				//createMibNotifications();
-				writeMibHeader(out);
-				writeMibImports(out);
-				writeMibObjectDefinitions(out);
-				writeMibObjects(out);
-				//writeMibNotificationDefinitions(out);
-				//writeMibNotifications(out);
-				out.println("END");
-			}
-			else {
-				writeXmlHeader(out);
-				writeXmlDefinitions(out);
-			}
-			
+			createMibObjects();
+			createMibNotifications();
+			writeMibHeader(out);
+			writeMibImports(out);
+			writeMibObjectDefinitions(out);
+			writeMibObjects(out);
+			//writeMibNotificationDefinitions(out);
+			writeMibNotifications(out);
+			out.println("END");
 			out.close();
 		}
 		catch (Exception e){e.printStackTrace();}
@@ -260,27 +250,24 @@
 	 */
 	
 	private void createMibNotifications(){
-		Iterator<Mapping> mIt = nmList.iterator();
-		while (mIt.hasNext()){
-			MIBNotification mibN = new MIBNotification(mIt.next());
+		Iterator<Mapping> nIt = nmList.iterator();
+		while (nIt.hasNext()){
+			MIBNotification mibN = new MIBNotification(nIt.next());
 			mibnList.add(mibN);
 		}
 	}
 	
 	/** 
-	 * Needs implementation. Writing the xml header out of a given MIB.
+	 * Method to write the gathered Notifications out into the MIB
 	 * @param out
 	 */
-	private void writeXmlHeader(PrintWriter out){}
-	
-	/**
-	 * Needs implementation. Write the actual bindings out, given an MIB
-	 * @param out
-	 */
-	private void writeXmlDefinitions(PrintWriter out){}
-	
-	
-	
+	private void writeMibNotifications(PrintWriter out){
+		Iterator<MIBNotification> nIt = mibnList.iterator();
+		while(nIt.hasNext()){
+			out.println(nIt.next());
+		}
+	}
+
 	/* Internal Classes ----- */
 	
 
@@ -331,7 +318,7 @@
 			// We need the last element in the array to be the registered OID, so we can put the correct 
 			// name in the output MIB
 			this.objectId = temp[temp.length-1];
-			this.fullOid = ma.getOidPrefix() + this.objectId;
+			this.fullOid = ma.getOidPrefix()+"."+this.objectId;
 			// check if there is already an OID definition for this prefix. If there is make this MIBObject's 
 			// oidDef reflect that
 			if (oidDefMap.containsKey(ma.getOidPrefix())){
@@ -399,7 +386,12 @@
 			private ArrayList<String> objects;
 			
 			public MIBNotification(Mapping mp){
-				this.name = mp.getName();
+				this.objects = new ArrayList<String>();
+				if (mp.getName()!=null)
+			 		this.name = mp.getName();
+			    else
+					this.name = "UNDEFINED";
+				
 				this.status = "current";
 				this.description = ""; // TODO: improve this!!
 				ArrayList<VarBind> vbList = (ArrayList<VarBind>)mp.getVarBindList().getVarBindList();
@@ -407,7 +399,7 @@
 				// the OID of a v2 trap = <enterpriseid>.0.<specificid>
 				// for predefined traps, see RFC1907 / 3413 / 3418 / http://www.oid-info.com/get/1.3.6.1.6.3.13
 				String oidPrefix = mp.getEnterprise()+".0";
-				this.objectId = "."+String.valueOf(mp.getSpecific());
+				this.objectId = String.valueOf(mp.getSpecific());
 				// check if there is already an OID definition for this prefix. If there is make this MIBObject's 
 				// oidDef reflect that
 				if (oidDefMap.containsKey(oidPrefix)){
@@ -415,11 +407,11 @@
 				}
 				// if they aren't part of the Map, then we add them to the map
 				else{
-					if (mp.getName() != null){
+					if (mp.getEnterpriseName() != null){
 						// the name to be used is in the xml being parsed. use it.
-						OIDDef newOidDef = new OIDDef(mp.getName(),oidPrefix);
+						OIDDef newOidDef = new OIDDef(mp.getEnterpriseName(),oidPrefix);
 						oidDefMap.put(oidPrefix, newOidDef);
-						this.oidDef=mp.getName();
+						this.oidDef=mp.getEnterpriseName();
 					}
 					else { // everything failed. the attributes.xml doesn't specify a name for the oid definition to be used,
 						   // nor do we already know about it.
@@ -455,21 +447,20 @@
 				}					
 				// have all the oids. compare these to the MIBObject's full OID. if it fails, 
 				// put OID into the Objects ArrayList.
-				Iterator<MIBObject> miboIt = miboList.iterator();
+				int index = 0;
 				Iterator<String> oidIt = oids.iterator();
-				
-				// regrettably nested iterator queries. needed to 
+ 
+			nextObject:
 				while (oidIt.hasNext()){
-					while (miboIt.hasNext()){
-						MIBObject mibo = miboIt.next();
-						String oidString = oidIt.next();
-						if (oidString.equals(mibo.getFullOid())){
-							this.objects.add(mibo.getName());
-							continue;
+					String oidString = oidIt.next();
+					for(index=0; index<miboList.size();index++){
+						if (oidString.equals(miboList.get(index).getFullOid())){
+							this.objects.add(miboList.get(index).getName());
+							continue nextObject;
 						}
-					// if we get here; there is no matching MIBObject for this oid. add the oid itself.
-					this.objects.add(oidString);
 					}
+					// if we get here; there is no matching MIBObject for this oid. put UNKNOWNOBJECT here instead.
+					this.objects.add("UNKNOWNOBJECT");
 				}
 			}
 			
@@ -479,16 +470,15 @@
 			
 			public String printObjects(){
 				StringBuffer buf = new StringBuffer();
-				buf.append("{ ");
-				int index = 0;
-				while (index < this.objects.size()){
+				buf.append("{\n");
+				for( int index = 0; index < this.objects.size(); index++){
 					if (index == this.objects.size()-1)
-						buf.append(this.objects.get(index));
+						buf.append("\t\t"+this.objects.get(index));
 					else
-						buf.append(this.objects.get(index)).append(",\n");
+						buf.append("\t\t"+this.objects.get(index)).append(",\n");
 				}
 				
-				buf.append(" }");
+				buf.append(" }\n");
 				return buf.toString();
 			
 			}

Modified: branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/MIBGenerator.java
===================================================================
--- branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/MIBGenerator.java	2011-06-13 21:00:50 UTC (rev 111606)
+++ branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/MIBGenerator.java	2011-06-13 21:05:28 UTC (rev 111607)
@@ -56,8 +56,6 @@
 		 * 		--oiddef=<FILENAME> : add the OID definitions in the given file to the generator
 		 */
 		CmdLineParser.Option help = cmdParser.addBooleanOption('h', "help");
-		CmdLineParser.Option reverse = cmdParser.addBooleanOption('r', "reverse");
-	 	CmdLineParser.Option snmptype = cmdParser.addBooleanOption('s', "snmptype");
 	 	CmdLineParser.Option append = cmdParser.addBooleanOption('a', "append");
 	 	CmdLineParser.Option importOpt = cmdParser.addStringOption('i', "import");
 	 	CmdLineParser.Option oiddef = cmdParser.addStringOption('o', "oiddef");
@@ -69,12 +67,11 @@
 	 		printUsageMessage();
 	 	}
 	 	
-	 	Boolean reverseVal = (Boolean)cmdParser.getOptionValue(reverse, Boolean.FALSE);
 	 	String [] remArgs = cmdParser.getRemainingArgs();
 		Parser parser = new Parser(remArgs[0], remArgs[1]);
-		parser.parse(reverseVal);
+		parser.parse();
 		Generator generator = new Generator(remArgs[2], parser.getMaList(), parser.getMbList(), parser.getNmList());
-		generator.writeFile(reverseVal);		
+		generator.writeFile();		
 	}
 	
 	

Modified: branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/Parser.java
===================================================================
--- branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/Parser.java	2011-06-13 21:00:50 UTC (rev 111606)
+++ branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/Parser.java	2011-06-13 21:05:28 UTC (rev 111607)
@@ -32,7 +32,6 @@
 import org.jboss.jmx.adaptor.snmp.config.attribute.AttributeMappings;
 import org.jboss.jmx.adaptor.snmp.config.attribute.ManagedBean;
 import org.jboss.jmx.adaptor.snmp.config.notification.Mapping;
-import org.jboss.jmx.adaptor.snmp.generator.ParserAttributeBindings;
 
 import org.jboss.xb.binding.ObjectModelFactory;
 import org.jboss.xb.binding.Unmarshaller;
@@ -52,25 +51,37 @@
 public class Parser {
 	String inputAttrFile;
 	String inputNotificationFile;
+	InputStream inputAttrStream; //if we are given this in the constructor, use it instead of defining it in parseAttributesXml()
+	InputStream inputNotiStream;
 	ArrayList<MappedAttribute> maList; 
 	ArrayList<Mapping> nmList;//list of notifications that we care about.
 	AttributeMappings mbList; 
 	
+	public Parser(InputStream aIs, InputStream nIs){
+		this.inputAttrFile = null;
+		this.inputNotificationFile = null;
+		this.inputAttrStream = aIs;
+		this.inputNotiStream = nIs;
+		this.maList = new ArrayList<MappedAttribute>(1);
+		this.mbList = new AttributeMappings();
+		this.nmList = new ArrayList<Mapping>(1);
+	}
+	
 	// constructor with a single input file; simple, just parse this one file.
 	public Parser(String inputAttrFile){
 		this.inputAttrFile = inputAttrFile;
 		this.inputNotificationFile = null;
-		maList = new ArrayList<MappedAttribute>(10);
-		mbList = new AttributeMappings();
+		this.maList = new ArrayList<MappedAttribute>(1);
+		this.mbList = new AttributeMappings();
 		
 	}
 	
 	public Parser(String inputAttrFile, String inputNotificationFile){
 		this.inputAttrFile = inputAttrFile;
 		this.inputNotificationFile = inputNotificationFile;
-		maList = new ArrayList<MappedAttribute>(10);
-		mbList = new AttributeMappings();
-		nmList = new ArrayList<Mapping>(10);
+		this.maList = new ArrayList<MappedAttribute>(1);
+		this.mbList = new AttributeMappings();
+		this.nmList = new ArrayList<Mapping>(1);
 	}
 	 
 	public String getNotiFile(){
@@ -114,35 +125,30 @@
 	}
 
 	/**
-	 * Based on the name of the input / output file, we either parse
-	 * an MIB or an XML, and from this construct a nescessary ArrayList of ManagedAttributes to write out	 
+	 * 
 	 * */
 	
-	public void parse(boolean reverse){
+	public void parse(){
 		// TODO: refine the checks here. they are not enough 
-		if (!reverse){
-			try {
-			parseAttributesXml();
-			if (this.inputNotificationFile !=null)
-				parseNotificationsXml();
-			}
-			catch (Exception e){
-				e.printStackTrace();
-				System.exit(1);				
-			}
+		try {
+		parseAttributesXml();
+		parseNotificationsXml();
 		}
-		else{
-			parseMib();
+		catch (Exception e){
+			e.printStackTrace();
+			System.exit(1);				
 		}		
 	}
 	
 	
 	private void parseNotificationsXml() throws Exception{
 		ObjectModelFactory omf = new ParserNotificationBindings();
-		InputStream is = null;
+		InputStream is = this.inputNotiStream;
 		ArrayList<Mapping> mappings = null;
 		try{
-			is = new FileInputStream(this.inputNotificationFile);
+			if(is == null){
+				is = new FileInputStream(this.inputNotificationFile);
+			}
 			
 			Unmarshaller unmarshaller = UnmarshallerFactory.newInstance().newUnmarshaller();
 			
@@ -180,11 +186,13 @@
 	 */
 	private void parseAttributesXml() throws Exception{
 		ObjectModelFactory omf = new ParserAttributeBindings();
-		InputStream is = null;
+		InputStream is = this.inputAttrStream;
 		AttributeMappings mappings = null;
 		try{
 		   // locate resource
-		   is = new FileInputStream(this.inputAttrFile);
+		   if(is == null){
+			   is = new FileInputStream(this.inputAttrFile);
+		   }
 		   
 		   // create unmarshaller
 		   Unmarshaller unmarshaller = UnmarshallerFactory.newInstance().newUnmarshaller();
@@ -253,6 +261,4 @@
 		}
 		return retVal;		
 	}
-	
-	private void parseMib(){}
 }// end Parser
\ No newline at end of file

Modified: branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/ParserNotificationBindings.java
===================================================================
--- branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/ParserNotificationBindings.java	2011-06-13 21:00:50 UTC (rev 111606)
+++ branches/snmp4j-int/varia/src/main/java/org/jboss/jmx/adaptor/snmp/generator/ParserNotificationBindings.java	2011-06-13 21:05:28 UTC (rev 111607)
@@ -122,7 +122,10 @@
 					m.setSecurityName(value);
 				} else if ("name".equals(localName)){
 					m.setName(value);
-				}				
+				} else if ("enterpriseName".equals(localName)){
+					m.setEnterpriseName(value);
+				}
+				
 			} else if (o instanceof VarBind) {
 				VarBind vb = (VarBind) o;
 



More information about the jboss-cvs-commits mailing list