001: package com.dwipal;
002:
003: import java.io.*;
004: import java.util.*;
005:
006: public class DwSettings {
007: public static ResourceBundle resources;
008: public static DwIPRecord LAST_IP_RECORD = new DwIPRecord();
009: public static String PROPERTY_FILENAME = "ITraffic";
010:
011: String fileName;
012:
013: DwSettings() {
014: loadSettingsFile("itraffic.conf");
015: }
016:
017: DwSettings(String fileName) {
018: loadSettingsFile(fileName);
019: loadProperties();
020: }
021:
022: private void loadSettingsFile(String fileName) {
023: this .fileName = fileName;
024: }
025:
026: public String getUser() {
027: return getSetting("User");
028: }
029:
030: public String getPass(String userName) {
031: return getSetting("Passwd " + userName);
032: }
033:
034: public String getSetting(String settingName) {
035: FileReader fin;
036: try {
037: fin = new FileReader(new File(fileName));
038: } catch (Exception e) {
039: dispError("Error.. Cannot load settings file " + fileName
040: + " ...\n" + e.toString());
041: return null;
042: }
043:
044: String settingLine = "";
045: try {
046: BufferedReader in = new BufferedReader(fin);
047: if (in == null) {
048: dispError("Settings file not loaded");
049: return null;
050: }
051: // System.out.println(in);
052:
053: while (settingLine != null) {
054: settingLine = in.readLine();
055: if (settingLine == null)
056: return null;
057: if (settingLine.startsWith("#"))
058: continue;
059: if (settingLine.trim().length() < 3)
060: continue;
061: // System.out.println(settingLine);
062: int Index = settingLine.indexOf("=");
063: if (settingLine.substring(0, Index).trim().equals(
064: settingName) == true)
065: return (settingLine.substring(Index + 1));
066: }
067: } catch (Exception e) {
068: dispError("Error.. Cannot read the settings file.\n"
069: + e.toString());
070: }
071: return null;
072: }
073:
074: private void dispError(String e) {
075: System.err.println(e);
076: }
077:
078: public static void loadProperties() {
079: resources = ResourceBundle.getBundle(PROPERTY_FILENAME, Locale
080: .getDefault());
081: String parVal;
082:
083: parVal = getResourceParameter("lastipaddress");
084: LAST_IP_RECORD.ipAddress = (parVal != null) ? parVal
085: : "127.0.0.1";
086:
087: parVal = getResourceParameter("lastport");
088: int parInt;
089: try {
090: parInt = Integer.parseInt(getResourceParameter("lastport"));
091: } catch (Exception e) {
092: parInt = 161;
093: }
094: LAST_IP_RECORD.port = parInt;
095:
096: parVal = getResourceParameter("lastgetcommunity");
097: LAST_IP_RECORD.getCommunity = (parVal != null) ? parVal
098: : "public";
099:
100: parVal = getResourceParameter("lastsetcommunity");
101: LAST_IP_RECORD.setCommunity = (parVal != null) ? parVal
102: : "public";
103:
104: }
105:
106: private static String getResourceParameter(String parmName) {
107: String param = null;
108: try {
109:
110: param = resources.getString(parmName);
111: } catch (Exception e) {
112: System.out.println("Exception in getting parameter: "
113: + parmName + " " + e);
114: param = null;
115: }
116: return param.trim();
117: }
118:
119: }
120:
121: class DwIPRecord {
122: public String ipAddress = "127.0.0.1";
123: public int port = 161;
124: public String getCommunity = "public";
125: public String setCommunity = "public";
126: }
|