KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > tracelog > config > OldConfigFile


1 package net.sourceforge.tracelog.config;
2
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.FileOutputStream JavaDoc;
6 import java.io.ObjectInputStream JavaDoc;
7 import java.io.ObjectOutputStream JavaDoc;
8 import java.util.LinkedList JavaDoc;
9 import java.util.List JavaDoc;
10
11
12 import org.apache.log4j.Logger;
13 import org.eclipse.swt.SWT;
14
15 /**
16  * OldConfigFile is responsible in querying/persisting log information from/to
17  * the configuration file stored in the user home directory.
18  *
19  * @author Choon-Chern Lim (Mike)
20  */

21 public class OldConfigFile extends ConfigFile {
22     private static Logger log = Logger.getLogger(OldConfigFile.class);
23
24     /**
25      * Sets up config file.
26      */

27     OldConfigFile() {
28         super();
29         super.configFile = new File JavaDoc(System.getProperty("user.home") + projectProperties.getOldConfigFilePath());
30     }
31
32     /**
33      * Deserializes file into list of LogFile objects.
34      *
35      * @return List of LogFile objects
36      */

37     @SuppressWarnings JavaDoc("unchecked")
38     public List JavaDoc<LogBean> getConfig() {
39
40         List JavaDoc<LogBean> logBeanList = null;
41
42         // if config file exists, then try to deserialize it
43
if (super.isConfigFileExists()) {
44             try {
45                 ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(new FileInputStream JavaDoc(configFile));
46                 logBeanList = (List JavaDoc) ois.readObject();
47                 ois.close();
48             }
49             catch (Exception JavaDoc e) {
50                 log.error("Cannot read log file: " + e.getMessage());
51             }
52         }
53         else {
54             try {
55                 // create application folder(s) first
56
configFile.getParentFile().mkdirs();
57
58                 // then create the config file
59
configFile.createNewFile();
60             }
61             catch (Exception JavaDoc e) {
62                 log.error("Cannot create configuration folder: " + e.getMessage());
63             }
64         }
65
66         // list is still null, then the file either doesn't exist or the
67
// serialized object is corrupted. If that is the case, then repopulate
68
// it.
69
if (logBeanList == null) {
70             logBeanList = getDefaultServerLogConfiguration();
71             saveConfig(logBeanList);
72         }
73
74         return logBeanList;
75     }
76
77     /**
78      * Serializes LogFile objects into file.
79      *
80      * @param logBeanList
81      * List of LogFile objects
82      */

83     public void saveConfig(List JavaDoc<LogBean> logBeanList) {
84         try {
85             ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(new FileOutputStream JavaDoc(configFile));
86             oos.writeObject(logBeanList);
87             oos.close();
88         }
89         catch (Exception JavaDoc e) {
90             e.printStackTrace();
91         }
92     }
93
94     /**
95      * Creates the default server log configuration if one doesn't exist.
96      *
97      * @return List of LogFile objects
98      */

99     private List JavaDoc<LogBean> getDefaultServerLogConfiguration() {
100         List JavaDoc<LogBean> logBeanList = new LinkedList JavaDoc<LogBean>();
101
102         String JavaDoc[] sampleServerConfigs = projectProperties.getSampleServerConfigs();
103
104         for (int i = 0; i < sampleServerConfigs.length; ++i) {
105             String JavaDoc logName = sampleServerConfigs[i].substring(0, sampleServerConfigs[i].indexOf("|"));
106             String JavaDoc logPath = sampleServerConfigs[i].substring(sampleServerConfigs[i].indexOf("|") + 1);
107             logBeanList.add(new LogBean((i + 1), logName, logPath, SWT.COLOR_BLACK, SWT.COLOR_WHITE));
108         }
109
110         return logBeanList;
111         // return Util.setLogFontColor(logBeanList);
112
}
113
114     @Override JavaDoc
115     public UserConfig getUserConfig() {
116         try {
117             throw new Exception JavaDoc("Not implemented");
118         }
119         catch (Exception JavaDoc e) {
120             log.error(e);
121             e.printStackTrace();
122         }
123
124         return null;
125     }
126
127     @Override JavaDoc
128     public void saveUserConfig(UserConfig userConfig) {
129         try {
130             throw new Exception JavaDoc("Not implemented");
131         }
132         catch (Exception JavaDoc e) {
133             log.error(e);
134             e.printStackTrace();
135         }
136     }
137
138 }
139
Popular Tags