KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * NewConfigFile.java May 8, 2007
3  */

4 package net.sourceforge.tracelog.config;
5
6 import java.io.BufferedReader JavaDoc;
7 import java.io.File JavaDoc;
8 import java.io.FileReader JavaDoc;
9 import java.io.FileWriter JavaDoc;
10 import java.io.PrintWriter JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.LinkedList JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Set JavaDoc;
16 import java.util.StringTokenizer JavaDoc;
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 JavaDoc SERVER_CONFIG_PROP_KEY_PREFIX = "server.config.";
26     private static final String JavaDoc SERVER_CONFIG_DELIM = "|";
27
28     private HashMap JavaDoc<String JavaDoc, String JavaDoc> configFileProperties = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
29
30     NewConfigFile() {
31         super();
32         super.configFile = new File JavaDoc(System.getProperty("user.home") + projectProperties.getNewConfigFilePath());
33         convertFromOldConfigFile();
34     }
35
36     public List JavaDoc<LogBean> getConfig() {
37
38         List JavaDoc<LogBean> logBeanList = null;
39
40         // if config file exists, then try to deserialize it
41
if (super.isConfigFileExists()) {
42             readConfigFile();
43             logBeanList = getServerConfigs();
44         }
45         else {
46             createConfigFile();
47         }
48
49         // list is still null, then the file either doesn't exist or the
50
// serialized object is corrupted. If that is the case, then repopulate
51
// it.
52
if (logBeanList == null) {
53             logBeanList = getDefaultServerLogConfiguration();
54             saveConfig(logBeanList);
55         }
56
57         return logBeanList;
58     }
59
60     private void createConfigFile() {
61         try {
62             // create application folder(s) first
63
configFile.getParentFile().mkdirs();
64
65             // then create the config file
66
configFile.createNewFile();
67         }
68         catch (Exception JavaDoc e) {
69             log.error("Cannot create configuration folder: " + e.getMessage());
70         }
71     }
72
73     private void readConfigFile() {
74         try {
75             BufferedReader JavaDoc rd = new BufferedReader JavaDoc(new FileReader JavaDoc(configFile));
76             String JavaDoc line = "";
77             String JavaDoc key = "";
78             String JavaDoc value = "";
79             int idx = 0;
80
81             while ((line = rd.readLine()) != null) {
82
83                 // not a comment line, store all configurations
84
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 JavaDoc e) {
96             log.error(e.getMessage());
97         }
98     }
99
100     private List JavaDoc<LogBean> getServerConfigs() {
101         List JavaDoc<LogBean> logBeanList = new LinkedList JavaDoc<LogBean>();
102         Set JavaDoc<String JavaDoc> keySet = configFileProperties.keySet();
103
104         for (Iterator JavaDoc<String JavaDoc> ite = keySet.iterator(); ite.hasNext();) {
105             String JavaDoc key = ite.next();
106             if (key.startsWith(SERVER_CONFIG_PROP_KEY_PREFIX)) {
107                 StringTokenizer JavaDoc tkn = new StringTokenizer JavaDoc(configFileProperties.get(key), SERVER_CONFIG_DELIM);
108                 int logOrder = Integer.parseInt(key.substring(key.lastIndexOf(".") + 1));
109                 String JavaDoc logName = tkn.nextToken();
110                 String JavaDoc 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 JavaDoc<LogBean> logBeanList) {
122         removeOldServerConfigProperties();
123         createNewServerConfigProperties(logBeanList);
124         saveToFile();
125     }
126
127     private void removeOldServerConfigProperties() {
128         Set JavaDoc<String JavaDoc> keySet = configFileProperties.keySet();
129         List JavaDoc<String JavaDoc> oldServerConfigKeys = new LinkedList JavaDoc<String JavaDoc>();
130
131         // look for all old server config keys in the properties file
132
for (Iterator JavaDoc<String JavaDoc> ite = keySet.iterator(); ite.hasNext();) {
133             String JavaDoc key = ite.next();
134             if (key.startsWith(SERVER_CONFIG_PROP_KEY_PREFIX)) {
135                 oldServerConfigKeys.add(key);
136             }
137         }
138
139         // remove them
140
for (int i = 0; i < oldServerConfigKeys.size(); ++i) {
141             configFileProperties.remove(oldServerConfigKeys.get(i));
142         }
143     }
144
145     private void createNewServerConfigProperties(List JavaDoc<LogBean> logBeanList) {
146         for (int i = 0; i < logBeanList.size(); ++i) {
147             String JavaDoc key = SERVER_CONFIG_PROP_KEY_PREFIX + (i + 1);
148             LogBean logBean = logBeanList.get(i);
149             String JavaDoc 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     /**
156      * Enumerates the config file properties and persists them into the config
157      * file.
158      */

159     private void saveToFile() {
160         try {
161             PrintWriter JavaDoc pw = new PrintWriter JavaDoc(new FileWriter JavaDoc(configFile));
162
163             Set JavaDoc<String JavaDoc> keySet = configFileProperties.keySet();
164
165             for (Iterator JavaDoc<String JavaDoc> ite = keySet.iterator(); ite.hasNext();) {
166                 String JavaDoc key = ite.next();
167                 String JavaDoc value = configFileProperties.get(key);
168                 pw.println(key + "=" + value);
169             }
170
171             pw.close();
172         }
173         catch (Exception JavaDoc e) {
174             log.error(e.getMessage());
175         }
176     }
177
178     /**
179      * Converts old config file into new config file. It first checks if the new
180      * config file exists. If the new config file exists, then there is really
181      * no need to perform the conversion. However, if it doesn't exist and the
182      * old config file exists, then the old config file content is read and
183      * copied over to the new config file.
184      * <p>
185      * This API is only needed for several future releases to ensure that
186      * existing users have their config files converted properly. Then, this API
187      * is be removed safely.
188      */

189     private void convertFromOldConfigFile() {
190
191         OldConfigFile oldConfigFile = new OldConfigFile();
192
193         // new config file does not exist and old config file exists, then
194
// perform conversion
195
if (!super.isConfigFileExists() && oldConfigFile.isConfigFileExists()) {
196             List JavaDoc<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     /**
209      * Creates the default server log configuration if one doesn't exist.
210      *
211      * @return List of LogFile objects
212      */

213     private List JavaDoc<LogBean> getDefaultServerLogConfiguration() {
214         List JavaDoc<LogBean> logBeanList = new LinkedList JavaDoc<LogBean>();
215
216         String JavaDoc[] sampleServerConfigs = projectProperties.getSampleServerConfigs();
217
218         for (int i = 0; i < sampleServerConfigs.length; ++i) {
219             String JavaDoc logName = sampleServerConfigs[i].substring(0, sampleServerConfigs[i].indexOf(SERVER_CONFIG_DELIM));
220             String JavaDoc 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 JavaDoc
228     public UserConfig getUserConfig() {
229         try {
230             throw new Exception JavaDoc("Not implemented");
231         }
232         catch (Exception JavaDoc e) {
233             log.error(e);
234             e.printStackTrace();
235         }
236
237         return null;
238     }
239
240     @Override JavaDoc
241     public void saveUserConfig(UserConfig userConfig) {
242         try {
243             throw new Exception JavaDoc("Not implemented");
244         }
245         catch (Exception JavaDoc e) {
246             log.error(e);
247             e.printStackTrace();
248         }
249     }
250
251 }
252
Popular Tags