1 4 package net.sourceforge.tracelog.config; 5 6 import java.io.BufferedReader ; 7 import java.io.File ; 8 import java.io.FileReader ; 9 import java.io.FileWriter ; 10 import java.io.PrintWriter ; 11 import java.util.HashMap ; 12 import java.util.Iterator ; 13 import java.util.LinkedList ; 14 import java.util.List ; 15 import java.util.Set ; 16 import java.util.StringTokenizer ; 17 18 19 import org.apache.log4j.Logger; 20 import org.eclipse.swt.SWT; 21 22 public class NewConfigFile extends ConfigFile { 23 private static Logger log = Logger.getLogger(NewConfigFile.class); 24 25 private static final String SERVER_CONFIG_PROP_KEY_PREFIX = "server.config."; 26 private static final String SERVER_CONFIG_DELIM = "|"; 27 28 private HashMap <String , String > configFileProperties = new HashMap <String , String >(); 29 30 NewConfigFile() { 31 super(); 32 super.configFile = new File (System.getProperty("user.home") + projectProperties.getNewConfigFilePath()); 33 convertFromOldConfigFile(); 34 } 35 36 public List <LogBean> getConfig() { 37 38 List <LogBean> logBeanList = null; 39 40 if (super.isConfigFileExists()) { 42 readConfigFile(); 43 logBeanList = getServerConfigs(); 44 } 45 else { 46 createConfigFile(); 47 } 48 49 if (logBeanList == null) { 53 logBeanList = getDefaultServerLogConfiguration(); 54 saveConfig(logBeanList); 55 } 56 57 return logBeanList; 58 } 59 60 private void createConfigFile() { 61 try { 62 configFile.getParentFile().mkdirs(); 64 65 configFile.createNewFile(); 67 } 68 catch (Exception e) { 69 log.error("Cannot create configuration folder: " + e.getMessage()); 70 } 71 } 72 73 private void readConfigFile() { 74 try { 75 BufferedReader rd = new BufferedReader (new FileReader (configFile)); 76 String line = ""; 77 String key = ""; 78 String value = ""; 79 int idx = 0; 80 81 while ((line = rd.readLine()) != null) { 82 83 if (!line.startsWith("#")) { 85 idx = line.indexOf("="); 86 key = line.substring(0, idx); 87 value = line.substring(idx + 1); 88 89 configFileProperties.put(key, value); 90 } 91 } 92 93 rd.close(); 94 } 95 catch (Exception e) { 96 log.error(e.getMessage()); 97 } 98 } 99 100 private List <LogBean> getServerConfigs() { 101 List <LogBean> logBeanList = new LinkedList <LogBean>(); 102 Set <String > keySet = configFileProperties.keySet(); 103 104 for (Iterator <String > ite = keySet.iterator(); ite.hasNext();) { 105 String key = ite.next(); 106 if (key.startsWith(SERVER_CONFIG_PROP_KEY_PREFIX)) { 107 StringTokenizer tkn = new StringTokenizer (configFileProperties.get(key), SERVER_CONFIG_DELIM); 108 int logOrder = Integer.parseInt(key.substring(key.lastIndexOf(".") + 1)); 109 String logName = tkn.nextToken(); 110 String logPath = tkn.nextToken(); 111 int foregroundColor = Integer.parseInt(tkn.nextToken()); 112 int backgroundColor = Integer.parseInt(tkn.nextToken()); 113 114 logBeanList.add(new LogBean(logOrder, logName, logPath, foregroundColor, backgroundColor)); 115 } 116 } 117 118 return logBeanList; 119 } 120 121 public void saveConfig(List <LogBean> logBeanList) { 122 removeOldServerConfigProperties(); 123 createNewServerConfigProperties(logBeanList); 124 saveToFile(); 125 } 126 127 private void removeOldServerConfigProperties() { 128 Set <String > keySet = configFileProperties.keySet(); 129 List <String > oldServerConfigKeys = new LinkedList <String >(); 130 131 for (Iterator <String > ite = keySet.iterator(); ite.hasNext();) { 133 String key = ite.next(); 134 if (key.startsWith(SERVER_CONFIG_PROP_KEY_PREFIX)) { 135 oldServerConfigKeys.add(key); 136 } 137 } 138 139 for (int i = 0; i < oldServerConfigKeys.size(); ++i) { 141 configFileProperties.remove(oldServerConfigKeys.get(i)); 142 } 143 } 144 145 private void createNewServerConfigProperties(List <LogBean> logBeanList) { 146 for (int i = 0; i < logBeanList.size(); ++i) { 147 String key = SERVER_CONFIG_PROP_KEY_PREFIX + (i + 1); 148 LogBean logBean = logBeanList.get(i); 149 String value = logBean.getLogName() + SERVER_CONFIG_DELIM + logBean.getLogFilePath() + SERVER_CONFIG_DELIM + logBean.getForegroundColor() 150 + SERVER_CONFIG_DELIM + logBean.getBackgroundColor(); 151 configFileProperties.put(key, value); 152 } 153 } 154 155 159 private void saveToFile() { 160 try { 161 PrintWriter pw = new PrintWriter (new FileWriter (configFile)); 162 163 Set <String > keySet = configFileProperties.keySet(); 164 165 for (Iterator <String > ite = keySet.iterator(); ite.hasNext();) { 166 String key = ite.next(); 167 String value = configFileProperties.get(key); 168 pw.println(key + "=" + value); 169 } 170 171 pw.close(); 172 } 173 catch (Exception e) { 174 log.error(e.getMessage()); 175 } 176 } 177 178 189 private void convertFromOldConfigFile() { 190 191 OldConfigFile oldConfigFile = new OldConfigFile(); 192 193 if (!super.isConfigFileExists() && oldConfigFile.isConfigFileExists()) { 196 List <LogBean> oldLogBeanList = oldConfigFile.getConfig(); 197 198 for (int i = 0; i < oldLogBeanList.size(); ++i) { 199 LogBean logBean = oldLogBeanList.get(i); 200 logBean.setForegroundColor(SWT.COLOR_BLACK); 201 logBean.setBackgroundColor(SWT.COLOR_WHITE); 202 } 203 204 saveConfig(oldLogBeanList); 205 } 206 } 207 208 213 private List <LogBean> getDefaultServerLogConfiguration() { 214 List <LogBean> logBeanList = new LinkedList <LogBean>(); 215 216 String [] sampleServerConfigs = projectProperties.getSampleServerConfigs(); 217 218 for (int i = 0; i < sampleServerConfigs.length; ++i) { 219 String logName = sampleServerConfigs[i].substring(0, sampleServerConfigs[i].indexOf(SERVER_CONFIG_DELIM)); 220 String logPath = sampleServerConfigs[i].substring(sampleServerConfigs[i].indexOf(SERVER_CONFIG_DELIM) + 1); 221 logBeanList.add(new LogBean((i + 1), logName, logPath, SWT.COLOR_BLACK, SWT.COLOR_WHITE)); 222 } 223 224 return logBeanList; 225 } 226 227 @Override 228 public UserConfig getUserConfig() { 229 try { 230 throw new Exception ("Not implemented"); 231 } 232 catch (Exception e) { 233 log.error(e); 234 e.printStackTrace(); 235 } 236 237 return null; 238 } 239 240 @Override 241 public void saveUserConfig(UserConfig userConfig) { 242 try { 243 throw new Exception ("Not implemented"); 244 } 245 catch (Exception e) { 246 log.error(e); 247 e.printStackTrace(); 248 } 249 } 250 251 } 252 | Popular Tags |