001: package net.sourceforge.tracelog.config;
002:
003: import java.io.BufferedReader;
004: import java.io.File;
005: import java.io.FileReader;
006: import java.io.FileWriter;
007: import java.io.PrintWriter;
008: import java.util.HashMap;
009: import java.util.Iterator;
010: import java.util.LinkedList;
011: import java.util.List;
012: import java.util.Set;
013: import java.util.StringTokenizer;
014:
015: import org.apache.log4j.Logger;
016: import org.eclipse.swt.SWT;
017:
018: public class NewConfigFile extends ConfigFile {
019: private static Logger log = Logger.getLogger(NewConfigFile.class);
020:
021: private static final String SERVER_CONFIG_DELIM = "|";
022: private static final String SERVER_CONFIG_PROP_KEY_PREFIX = "server.config.";
023:
024: private HashMap<String, String> configFileProperties = new HashMap<String, String>();
025:
026: NewConfigFile() {
027: super ();
028: super .configFile = new File(System.getProperty("user.home")
029: + projectProperties.getNewConfigFilePath());
030: convertFromOldConfigFile();
031: }
032:
033: public List<LogBean> getConfig() {
034:
035: List<LogBean> logBeanList = null;
036:
037: // if config file exists, then try to deserialize it
038: if (super .isConfigFileExists()) {
039: readConfigFile();
040: logBeanList = getServerConfigs();
041: } else {
042: createConfigFile();
043: }
044:
045: // list is still null, then the file either doesn't exist or the
046: // serialized object is corrupted. If that is the case, then repopulate
047: // it.
048: if (logBeanList == null) {
049: logBeanList = getDefaultServerLogConfiguration();
050: saveConfig(logBeanList);
051: }
052:
053: return logBeanList;
054: }
055:
056: @Override
057: public UserConfig getUserConfig() {
058: try {
059: throw new Exception("Not implemented");
060: } catch (Exception e) {
061: log.error(e);
062: e.printStackTrace();
063: }
064:
065: return null;
066: }
067:
068: public void saveConfig(List<LogBean> logBeanList) {
069: removeOldServerConfigProperties();
070: createNewServerConfigProperties(logBeanList);
071: saveToFile();
072: }
073:
074: @Override
075: public void saveUserConfig(UserConfig userConfig) {
076: try {
077: throw new Exception("Not implemented");
078: } catch (Exception e) {
079: log.error(e);
080: e.printStackTrace();
081: }
082: }
083:
084: /**
085: * Converts old config file into new config file. It first checks if the new
086: * config file exists. If the new config file exists, then there is really
087: * no need to perform the conversion. However, if it doesn't exist and the
088: * old config file exists, then the old config file content is read and
089: * copied over to the new config file.
090: * <p>
091: * This API is only needed for several future releases to ensure that
092: * existing users have their config files converted properly. Then, this API
093: * is be removed safely.
094: */
095: private void convertFromOldConfigFile() {
096:
097: OldConfigFile oldConfigFile = new OldConfigFile();
098:
099: // new config file does not exist and old config file exists, then
100: // perform conversion
101: if (!super .isConfigFileExists()
102: && oldConfigFile.isConfigFileExists()) {
103: List<LogBean> oldLogBeanList = oldConfigFile.getConfig();
104:
105: for (int i = 0; i < oldLogBeanList.size(); ++i) {
106: LogBean logBean = oldLogBeanList.get(i);
107: logBean.setForegroundColor(SWT.COLOR_BLACK);
108: logBean.setBackgroundColor(SWT.COLOR_WHITE);
109: }
110:
111: saveConfig(oldLogBeanList);
112: }
113: }
114:
115: private void createConfigFile() {
116: try {
117: // create application folder(s) first
118: configFile.getParentFile().mkdirs();
119:
120: // then create the config file
121: configFile.createNewFile();
122: } catch (Exception e) {
123: log.error("Cannot create configuration folder: "
124: + e.getMessage());
125: }
126: }
127:
128: private void createNewServerConfigProperties(
129: List<LogBean> logBeanList) {
130: for (int i = 0; i < logBeanList.size(); ++i) {
131: String key = SERVER_CONFIG_PROP_KEY_PREFIX + (i + 1);
132: LogBean logBean = logBeanList.get(i);
133: String value = logBean.getLogName() + SERVER_CONFIG_DELIM
134: + logBean.getLogFilePath() + SERVER_CONFIG_DELIM
135: + logBean.getForegroundColor()
136: + SERVER_CONFIG_DELIM
137: + logBean.getBackgroundColor();
138: configFileProperties.put(key, value);
139: }
140: }
141:
142: /**
143: * Creates the default server log configuration if one doesn't exist.
144: *
145: * @return List of LogFile objects
146: */
147: private List<LogBean> getDefaultServerLogConfiguration() {
148: List<LogBean> logBeanList = new LinkedList<LogBean>();
149:
150: String[] sampleServerConfigs = projectProperties
151: .getSampleServerConfigs();
152:
153: for (int i = 0; i < sampleServerConfigs.length; ++i) {
154: String logName = sampleServerConfigs[i]
155: .substring(0, sampleServerConfigs[i]
156: .indexOf(SERVER_CONFIG_DELIM));
157: String logPath = sampleServerConfigs[i]
158: .substring(sampleServerConfigs[i]
159: .indexOf(SERVER_CONFIG_DELIM) + 1);
160: logBeanList.add(new LogBean((i + 1), logName, logPath,
161: SWT.COLOR_BLACK, SWT.COLOR_WHITE));
162: }
163:
164: return logBeanList;
165: }
166:
167: private List<LogBean> getServerConfigs() {
168: List<LogBean> logBeanList = new LinkedList<LogBean>();
169: Set<String> keySet = configFileProperties.keySet();
170:
171: for (Iterator<String> ite = keySet.iterator(); ite.hasNext();) {
172: String key = ite.next();
173: if (key.startsWith(SERVER_CONFIG_PROP_KEY_PREFIX)) {
174: StringTokenizer tkn = new StringTokenizer(
175: configFileProperties.get(key),
176: SERVER_CONFIG_DELIM);
177: int logOrder = Integer.parseInt(key.substring(key
178: .lastIndexOf(".") + 1));
179: String logName = tkn.nextToken();
180: String logPath = tkn.nextToken();
181: int foregroundColor = Integer.parseInt(tkn.nextToken());
182: int backgroundColor = Integer.parseInt(tkn.nextToken());
183:
184: logBeanList.add(new LogBean(logOrder, logName, logPath,
185: foregroundColor, backgroundColor));
186: }
187: }
188:
189: return logBeanList;
190: }
191:
192: private void readConfigFile() {
193: try {
194: BufferedReader rd = new BufferedReader(new FileReader(
195: configFile));
196: String line = "";
197: String key = "";
198: String value = "";
199: int idx = 0;
200:
201: while ((line = rd.readLine()) != null) {
202:
203: // not a comment line, store all configurations
204: if (!line.startsWith("#")) {
205: idx = line.indexOf("=");
206: key = line.substring(0, idx);
207: value = line.substring(idx + 1);
208:
209: configFileProperties.put(key, value);
210: }
211: }
212:
213: rd.close();
214: } catch (Exception e) {
215: log.error(e.getMessage());
216: }
217: }
218:
219: private void removeOldServerConfigProperties() {
220: Set<String> keySet = configFileProperties.keySet();
221: List<String> oldServerConfigKeys = new LinkedList<String>();
222:
223: // look for all old server config keys in the properties file
224: for (Iterator<String> ite = keySet.iterator(); ite.hasNext();) {
225: String key = ite.next();
226: if (key.startsWith(SERVER_CONFIG_PROP_KEY_PREFIX)) {
227: oldServerConfigKeys.add(key);
228: }
229: }
230:
231: // remove them
232: for (int i = 0; i < oldServerConfigKeys.size(); ++i) {
233: configFileProperties.remove(oldServerConfigKeys.get(i));
234: }
235: }
236:
237: /**
238: * Enumerates the config file properties and persists them into the config
239: * file.
240: */
241: private void saveToFile() {
242: try {
243: PrintWriter pw = new PrintWriter(new FileWriter(configFile));
244:
245: Set<String> keySet = configFileProperties.keySet();
246:
247: for (Iterator<String> ite = keySet.iterator(); ite
248: .hasNext();) {
249: String key = ite.next();
250: String value = configFileProperties.get(key);
251: pw.println(key + "=" + value);
252: }
253:
254: pw.close();
255: } catch (Exception e) {
256: log.error(e.getMessage());
257: }
258: }
259:
260: }
|