Source Code Cross Referenced for VariableManager.java in  » Development » jLo » org » jzonic » jlo » 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 » jLo » org.jzonic.jlo 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.jzonic.jlo;
002:
003:        import java.io.BufferedReader;
004:        import java.io.InputStreamReader;
005:        import java.util.HashMap;
006:        import java.util.Iterator;
007:        import java.util.Properties;
008:
009:        /**
010:         * This class manages all variables for all log configurations.
011:         * Every handler or formatter can use this class to replace
012:         * any variables that are used inside of the parameters.
013:         * Please refer to the specific handler or formatter to see
014:         * which parameters support the use of variables.
015:         *
016:         * @author Andreas Mecky
017:         * @author Terry Dye
018:         */
019:        public class VariableManager {
020:
021:            private static VariableManager vm = null;
022:            private HashMap varMapping;
023:            private Properties envVariables;
024:
025:            private VariableManager() {
026:                varMapping = new HashMap();
027:                envVariables = getEnvVars();
028:            }
029:
030:            /**
031:             * This method will return an instance of the VariableManager
032:             *
033:             * @return the one and only instance of the VariableManager
034:             */
035:            public static VariableManager getInstance() {
036:                if (vm == null) {
037:                    vm = new VariableManager();
038:                }
039:                return vm;
040:            }
041:
042:            /** 
043:             * This method adds a variable for a given configuration.
044:             *
045:             * @param varName name of the variable
046:             * @param varValue the value for this variable
047:             * @param configName the configuration to which this variable belongs
048:             */
049:            public void addVariable(String varName, String varValue,
050:                    String configName) {
051:                HashMap vars = (HashMap) varMapping.get(configName);
052:                if (vars == null) {
053:                    vars = new HashMap();
054:                }
055:                vars.put(varName, varValue);
056:                varMapping.put(configName, vars);
057:            }
058:
059:            /**
060:             * This method will replace all variables inside one String that
061:             * are in this configuration. A variable is used as ${name}.
062:             * This occurance will be replaced with the specific value
063:             * or will be left as is if the variable name is not found
064:             * in the specific configuration. This method calls replaceEnvVars
065:             * at the end to replace environment variables.
066:             *
067:             * @param text the line of text which contains variables
068:             * @param configName the name of the configuration
069:             * @return a String where all variables are replaced with their values
070:             */
071:            public String replaceVariables(String text, String configName) {
072:                HashMap vars = (HashMap) varMapping.get(configName);
073:                if (vars != null && vars.size() > 0) {
074:                    Iterator it = vars.keySet().iterator();
075:                    int pos = 0;
076:                    // walk through all variables
077:                    while (it.hasNext()) {
078:                        String currentKey = (String) it.next();
079:                        String value = (String) vars.get(currentKey);
080:                        currentKey = "${" + currentKey + "}";
081:                        pos = text.indexOf(currentKey);
082:                        // check if we have found a variable
083:                        while (pos != -1) {
084:                            // cut the line into 2 pieces and put in the
085:                            // value of the variable
086:                            String firstPart = text.substring(0, pos);
087:                            String secondPart = text.substring(pos
088:                                    + currentKey.length());
089:                            text = firstPart + value + secondPart;
090:                            pos = text.indexOf(currentKey);
091:                        }
092:                    }
093:                }
094:                text = replaceSystemVar(text);
095:                return replaceEnvVar(text);
096:            }
097:
098:            /**
099:             * This method returns the number of variables
100:             * for one specific configuration. If the configuration
101:             * does not exist it will return 0;
102:             *
103:             * @param configName the name of the configuration
104:             * @return the number of variables
105:             */
106:            public int getVariableCount(String configName) {
107:                if (varMapping.containsKey(configName)) {
108:                    return ((HashMap) varMapping.get(configName)).size();
109:                } else {
110:                    return 0;
111:                }
112:            }
113:
114:            public String replaceSystemVar(String text) {
115:                if (text != null) {
116:                    int idx = text.indexOf("${system:");
117:                    // walk through the text and replace it
118:                    while (idx != -1) {
119:                        String firstPart = text.substring(0, idx);
120:                        String envVar = text.substring(idx + 9, text
121:                                .indexOf("}"));
122:                        String secondPart = text.substring(idx
123:                                + envVar.length() + 10);
124:                        String value = System.getProperty(envVar);
125:                        if (value == null) {
126:                            value = "${system:" + envVar + "}";
127:                        }
128:                        text = firstPart + value + secondPart;
129:                        idx = text.indexOf("${system:", idx + 1);
130:                    }
131:                }
132:                return text;
133:            }
134:
135:            /**
136:             * This method replace all occurences ${env:xxx} with
137:             * the value of an environment variable. If the env-variable
138:             * does not exist then the part is not converted. The
139:             * method gets called from replaceVariables
140:             *
141:             * @param text the line of text that will be processed
142:             * @return a String with the replaced env-variables.
143:             */
144:            public String replaceEnvVar(String text) {
145:                if (text != null) {
146:                    int idx = text.indexOf("${env:");
147:                    // walk through the text and replace it
148:                    while (idx != -1) {
149:                        String firstPart = text.substring(0, idx);
150:                        String envVar = text.substring(idx + 6, text
151:                                .indexOf("}"));
152:                        String secondPart = text.substring(idx
153:                                + envVar.length() + 7);
154:                        String value = envVariables.getProperty(envVar);
155:                        if (value == null) {
156:                            value = "${env:" + envVar + "}";
157:                        }
158:                        text = firstPart + value + secondPart;
159:                        idx = text.indexOf("${env:", idx + 1);
160:                    }
161:                }
162:                return text;
163:            }
164:
165:            // Thanx to http://www.rgagnon.com/howto.html for
166:            // this implementation.
167:            private Properties getEnvVars() {
168:                Process p = null;
169:                Properties envVars = new Properties();
170:                try {
171:                    Runtime r = Runtime.getRuntime();
172:                    String OS = System.getProperty("os.name").toLowerCase();
173:                    // System.out.println(OS);
174:                    if (OS.indexOf("windows 9") > -1) {
175:                        p = r.exec("command.com /c set");
176:                    } else if ((OS.indexOf("nt") > -1)
177:                            || (OS.indexOf("windows 2000") > -1 || (OS
178:                                    .indexOf("windows xp") > -1))) {
179:                        // thanks to JuanFran for the xp fix!
180:                        p = r.exec("cmd.exe /c set");
181:                    } else {
182:                        // our last hope, we assume Unix (thanks to H. Ware for the fix)
183:                        p = r.exec("env");
184:                    }
185:                    BufferedReader br = new BufferedReader(
186:                            new InputStreamReader(p.getInputStream()));
187:                    String line;
188:                    while ((line = br.readLine()) != null) {
189:                        int idx = line.indexOf('=');
190:                        String key = line.substring(0, idx);
191:                        String value = line.substring(idx + 1);
192:                        envVars.setProperty(key, value);
193:                        //System.out.println( key + " = " + value );
194:                    }
195:                } catch (Exception e) {
196:                    // we do not care here. Just no env vars for the user. Sorry.
197:                }
198:                return envVars;
199:            }
200:
201:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.