1 package com.dwipal; 2 3 import java.io.*; 4 import java.util.*; 5 6 public class DwSettings 7 { 8 public static ResourceBundle resources; 9 public static DwIPRecord LAST_IP_RECORD=new DwIPRecord(); 10 public static String PROPERTY_FILENAME="ITraffic"; 11 12 String fileName; 13 14 DwSettings() { 15 loadSettingsFile("itraffic.conf"); 16 } 17 18 DwSettings(String fileName) { 19 loadSettingsFile(fileName); 20 loadProperties(); 21 } 22 23 private void loadSettingsFile(String fileName) { 24 this.fileName=fileName; 25 } 26 public String getUser() { 27 return getSetting("User"); 28 } 29 public String getPass(String userName) { 30 return getSetting("Passwd "+ userName ); 31 } 32 33 34 public String getSetting(String settingName) { 35 FileReader fin; 36 try { 37 fin=new FileReader(new File(fileName)); 38 }catch(Exception e) { 39 dispError("Error.. Cannot load settings file " + fileName + " ...\n" + e.toString()); 40 return null; 41 } 42 43 44 String settingLine=""; 45 try { 46 BufferedReader in=new BufferedReader(fin); 47 if(in==null) { 48 dispError("Settings file not loaded"); 49 return null; 50 } 51 53 while(settingLine!=null) { 54 settingLine=in.readLine(); 55 if(settingLine==null) return null; 56 if(settingLine.startsWith("#")) continue; 57 if(settingLine.trim().length() <3) continue; 58 int Index=settingLine.indexOf("="); 60 if(settingLine.substring(0,Index).trim().equals(settingName)==true) 61 return(settingLine.substring(Index+1)); 62 } 63 } 64 catch(Exception e) { 65 dispError("Error.. Cannot read the settings file.\n"+e.toString()); 66 } 67 return null; 68 } 69 70 private void dispError(String e) { 71 System.err.println(e); 72 } 73 74 public static void loadProperties() { 75 resources=ResourceBundle.getBundle(PROPERTY_FILENAME,Locale.getDefault()); 76 String parVal; 77 78 parVal=getResourceParameter("lastipaddress"); 79 LAST_IP_RECORD.ipAddress=(parVal!=null)?parVal:"127.0.0.1"; 80 81 parVal=getResourceParameter("lastport"); 82 int parInt; 83 try { 84 parInt=Integer.parseInt(getResourceParameter("lastport")); 85 }catch(Exception e) { 86 parInt=161; 87 } 88 LAST_IP_RECORD.port =parInt; 89 90 parVal=getResourceParameter("lastgetcommunity"); 91 LAST_IP_RECORD.getCommunity=(parVal!=null)?parVal:"public"; 92 93 parVal=getResourceParameter("lastsetcommunity"); 94 LAST_IP_RECORD.setCommunity=(parVal!=null)?parVal:"public"; 95 96 } 97 98 99 private static String getResourceParameter(String parmName) { 100 String param = null; 101 try{ 102 103 param = resources.getString(parmName) ; 104 }catch(Exception e){ 105 System.out.println("Exception in getting parameter: " + parmName + " " + e); 106 param = null; 107 } 108 return param.trim(); 109 } 110 111 } 112 113 114 class DwIPRecord 115 { 116 public String ipAddress="127.0.0.1"; 117 public int port=161; 118 public String getCommunity="public"; 119 public String setCommunity="public"; 120 } 121 | Popular Tags |