KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ist > coach > coachEmfServicesComponents > Configuration


1 package ist.coach.coachEmfServicesComponents;
2
3 /**
4  * @author Administrator (digi@intracom.gr)
5  *
6  * Copyright (c) Intracom S.A., N.T., 2003
7  */

8
9 import java.util.Properties JavaDoc;
10 import java.io.FileInputStream JavaDoc;
11 import java.util.Enumeration JavaDoc;
12 import java.util.StringTokenizer JavaDoc;
13 import org.opennms.protocols.snmp.SnmpObjectId;
14
15 public class Configuration {
16     
17     private static Configuration instance;
18     private static final String JavaDoc configFile = "SnmpTrapDaemon.conf";
19     private static final String JavaDoc availableTrapsID = "availableTraps";
20     private static final String JavaDoc rootObjectsID = "rootObjects";
21     public SnmpTrapConfigData[] availableTraps;
22     public String JavaDoc[] 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 JavaDoc props = new Properties JavaDoc();
38         try {
39             System.out.println("Loading file " + configFile);
40             props.load(Configuration.class.getResourceAsStream(configFile));
41         }
42         catch (Exception JavaDoc e) {
43             e.printStackTrace();
44             return;
45         }
46         
47         String JavaDoc availableTrapsString = props.getProperty(availableTrapsID);
48         String JavaDoc rootObjectsString = props.getProperty(rootObjectsID);
49         
50         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(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 JavaDoc(st.nextToken().trim());
61             
62             counter ++;
63             
64         }
65         
66         st = new StringTokenizer JavaDoc(rootObjectsString, " ");
67         numOfTokens = st.countTokens();
68         
69         this.rootObjects = new String JavaDoc[numOfTokens];
70         
71         counter = 0;
72         
73         while (st.hasMoreTokens()) {
74             this.rootObjects[counter] = new String JavaDoc(st.nextToken().trim());
75         }
76         
77         Enumeration JavaDoc en = props.propertyNames();
78         
79         while (en.hasMoreElements()) {
80             
81             String JavaDoc name = (String JavaDoc) en.nextElement();
82             String JavaDoc 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 JavaDoc(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