Source Code Cross Referenced for NewConfigFile.java in  » Development » Tracelog » net » sourceforge » tracelog » config » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Development » Tracelog » net.sourceforge.tracelog.config 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.