001: package net.sourceforge.tracelog.config;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.io.FileOutputStream;
006: import java.io.ObjectInputStream;
007: import java.io.ObjectOutputStream;
008: import java.util.LinkedList;
009: import java.util.List;
010:
011: import org.apache.log4j.Logger;
012: import org.eclipse.swt.SWT;
013:
014: /**
015: * OldConfigFile is responsible in querying/persisting log information from/to
016: * the configuration file stored in the user home directory.
017: *
018: * @author Choon-Chern Lim (Mike)
019: */
020: public class OldConfigFile extends ConfigFile {
021: private static Logger log = Logger.getLogger(OldConfigFile.class);
022:
023: /**
024: * Sets up config file.
025: */
026: OldConfigFile() {
027: super ();
028: super .configFile = new File(System.getProperty("user.home")
029: + projectProperties.getOldConfigFilePath());
030: }
031:
032: /**
033: * Deserializes file into list of LogFile objects.
034: *
035: * @return List of LogFile objects
036: */
037: @SuppressWarnings("unchecked")
038: public List<LogBean> getConfig() {
039:
040: List<LogBean> logBeanList = null;
041:
042: // if config file exists, then try to deserialize it
043: if (super .isConfigFileExists()) {
044: try {
045: ObjectInputStream ois = new ObjectInputStream(
046: new FileInputStream(configFile));
047: logBeanList = (List) ois.readObject();
048: ois.close();
049: } catch (Exception e) {
050: log.error("Cannot read log file: " + e.getMessage());
051: }
052: } else {
053: try {
054: // create application folder(s) first
055: configFile.getParentFile().mkdirs();
056:
057: // then create the config file
058: configFile.createNewFile();
059: } catch (Exception e) {
060: log.error("Cannot create configuration folder: "
061: + e.getMessage());
062: }
063: }
064:
065: // list is still null, then the file either doesn't exist or the
066: // serialized object is corrupted. If that is the case, then repopulate
067: // it.
068: if (logBeanList == null) {
069: logBeanList = getDefaultServerLogConfiguration();
070: saveConfig(logBeanList);
071: }
072:
073: return logBeanList;
074: }
075:
076: @Override
077: public UserConfig getUserConfig() {
078: try {
079: throw new Exception("Not implemented");
080: } catch (Exception e) {
081: log.error(e);
082: e.printStackTrace();
083: }
084:
085: return null;
086: }
087:
088: /**
089: * Serializes LogFile objects into file.
090: *
091: * @param logBeanList
092: * List of LogFile objects
093: */
094: public void saveConfig(List<LogBean> logBeanList) {
095: try {
096: ObjectOutputStream oos = new ObjectOutputStream(
097: new FileOutputStream(configFile));
098: oos.writeObject(logBeanList);
099: oos.close();
100: } catch (Exception e) {
101: e.printStackTrace();
102: }
103: }
104:
105: @Override
106: public void saveUserConfig(UserConfig userConfig) {
107: try {
108: throw new Exception("Not implemented");
109: } catch (Exception e) {
110: log.error(e);
111: e.printStackTrace();
112: }
113: }
114:
115: /**
116: * Creates the default server log configuration if one doesn't exist.
117: *
118: * @return List of LogFile objects
119: */
120: private List<LogBean> getDefaultServerLogConfiguration() {
121: List<LogBean> logBeanList = new LinkedList<LogBean>();
122:
123: String[] sampleServerConfigs = projectProperties
124: .getSampleServerConfigs();
125:
126: for (int i = 0; i < sampleServerConfigs.length; ++i) {
127: String logName = sampleServerConfigs[i].substring(0,
128: sampleServerConfigs[i].indexOf("|"));
129: String logPath = sampleServerConfigs[i]
130: .substring(sampleServerConfigs[i].indexOf("|") + 1);
131: logBeanList.add(new LogBean((i + 1), logName, logPath,
132: SWT.COLOR_BLACK, SWT.COLOR_WHITE));
133: }
134:
135: return logBeanList;
136: // return Util.setLogFontColor(logBeanList);
137: }
138:
139: }
|