1 package ist.coach.coachEmfServicesComponents; 2 3 8 9 import java.util.Properties ; 10 import java.io.FileInputStream ; 11 import java.util.Enumeration ; 12 import java.util.StringTokenizer ; 13 import org.opennms.protocols.snmp.SnmpObjectId; 14 15 public class Configuration { 16 17 private static Configuration instance; 18 private static final String configFile = "SnmpTrapDaemon.conf"; 19 private static final String availableTrapsID = "availableTraps"; 20 private static final String rootObjectsID = "rootObjects"; 21 public SnmpTrapConfigData[] availableTraps; 22 public String [] rootObjects; 23 24 25 public static Configuration getInstance() { 26 if (instance == null) { 27 instance = new Configuration(); 28 if (instance.configSanity() == false) 29 return null; 30 } 31 32 return instance; 33 } 34 35 private Configuration() { 36 37 Properties props = new Properties (); 38 try { 39 System.out.println("Loading file " + configFile); 40 props.load(Configuration.class.getResourceAsStream(configFile)); 41 } 42 catch (Exception e) { 43 e.printStackTrace(); 44 return; 45 } 46 47 String availableTrapsString = props.getProperty(availableTrapsID); 48 String rootObjectsString = props.getProperty(rootObjectsID); 49 50 StringTokenizer st = new StringTokenizer (availableTrapsString, " "); 51 int numOfTokens = st.countTokens(); 52 53 this.availableTraps = new SnmpTrapConfigData[numOfTokens]; 54 55 int counter = 0; 56 57 while (st.hasMoreTokens()) { 58 59 this.availableTraps[counter] = new SnmpTrapConfigData(); 60 this.availableTraps[counter].id = new String (st.nextToken().trim()); 61 62 counter ++; 63 64 } 65 66 st = new StringTokenizer (rootObjectsString, " "); 67 numOfTokens = st.countTokens(); 68 69 this.rootObjects = new String [numOfTokens]; 70 71 counter = 0; 72 73 while (st.hasMoreTokens()) { 74 this.rootObjects[counter] = new String (st.nextToken().trim()); 75 } 76 77 Enumeration en = props.propertyNames(); 78 79 while (en.hasMoreElements()) { 80 81 String name = (String ) en.nextElement(); 82 String value = props.getProperty(name); 83 84 for (int i = 0; i < this.availableTraps.length; i++) { 85 86 if (name.startsWith(this.availableTraps[i].id)) { 87 88 if (name.endsWith("_objectclass")) { 89 this.availableTraps[i].objectclass = new String (value); 90 } else 91 if (name.endsWith("_oid")) { 92 this.availableTraps[i].oid = new SnmpObjectId(value); 93 94 } else 95 if (name.endsWith("_identifier")) { 96 this.availableTraps[i].identifier = Integer.parseInt(value); 97 } 98 else 99 if (name.endsWith("_valueIdentifier")) 100 this.availableTraps[i].valueOid = new SnmpObjectId(value); 101 } 102 } 103 } 104 } 105 106 private boolean configSanity() { 107 108 for (int i = 0; i < this.availableTraps.length; i++) { 109 110 if ( (this.availableTraps[i].objectclass == null) || 111 (this.availableTraps[i].oid == null) || 112 (this.availableTraps[i].identifier == -1)) 113 return false; 114 } 115 116 return true; 117 118 } 119 } 120 | Popular Tags |