Source Code Cross Referenced for LogAdmin.java in  » ESB » wso2-esb » org » wso2 » esb » services » 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 » ESB » wso2 esb » org.wso2.esb.services 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:        package org.wso2.esb.services;
017:
018:        import org.apache.axis2.AxisFault;
019:        import org.apache.commons.logging.Log;
020:        import org.apache.commons.logging.LogFactory;
021:        import org.apache.log4j.Level;
022:        import org.apache.log4j.LogManager;
023:        import org.apache.log4j.Logger;
024:        import org.apache.log4j.PropertyConfigurator;
025:        import org.apache.synapse.SynapseConstants;
026:        import org.wso2.esb.services.tos.LogConfigData;
027:
028:        import java.io.*;
029:        import java.util.Enumeration;
030:        import java.util.HashMap;
031:        import java.util.Map;
032:        import java.util.Properties;
033:
034:        /**
035:         * This is a POJO for the log configuration
036:         */
037:        public class LogAdmin extends AbstractESBAdmin {
038:            private static final Log log = LogFactory.getLog(LogAdmin.class);
039:
040:            // these are the loggers we give users to change logging levels 
041:            // from the log settings interface
042:            private static String LOGGER_ROOT_ID = "loggerRoot";
043:
044:            private static String LOGGER_SERVICE_ID = "loggerService";
045:            private static String LOGGER_SERVICE_NAME = "SERVICE_LOGGER";
046:            private static String LOGGER_TRACE_ID = "loggerTrace";
047:            private static String LOGGER_TRACE_NAME = "TRACE_LOGGER";
048:            private static Map loggerMap = new HashMap();
049:
050:            // logger map (key=id value=logger name)
051:            static {
052:                loggerMap.put(LOGGER_SERVICE_ID, LOGGER_SERVICE_NAME);
053:                loggerMap.put(LOGGER_TRACE_ID, LOGGER_TRACE_NAME);
054:            }
055:
056:            /**
057:             * Get log4j.properties, logger levels
058:             * read from the file system.
059:             * 
060:             * @return 
061:             * @throws AxisFault
062:             */
063:            public LogConfigData getLogConfig() throws AxisFault {
064:                try {
065:                    log.info("get log configuration");
066:
067:                    // initialized to blank so that going xml will have <x></x>
068:                    // instead of null elements
069:                    String loggerLevelRoot = "";
070:                    String loggerLevelService = "";
071:                    String loggerLevelTrace = "";
072:
073:                    String logConfigLocation = getLogConfigLocation();
074:
075:                    // read the log4j.properties file
076:                    StringBuffer logConfig = new StringBuffer(1024);
077:                    BufferedReader reader = new BufferedReader(new FileReader(
078:                            logConfigLocation));
079:                    String line = null;
080:                    while ((line = reader.readLine()) != null) {
081:                        line = line.trim();
082:                        logConfig.append(line + "\n");
083:                    }
084:                    reader.close();
085:
086:                    // find current logger levels
087:                    Logger rootLogger = Logger.getRootLogger();
088:                    if (rootLogger != null) {
089:                        if (rootLogger.getLevel() != null)
090:                            loggerLevelRoot = rootLogger.getLevel().toString();
091:                    }
092:
093:                    Enumeration loggers = LogManager.getCurrentLoggers();
094:                    while (loggers.hasMoreElements()) {
095:                        Logger logger = (Logger) loggers.nextElement();
096:                        if (LOGGER_SERVICE_NAME.equals(logger.getName())) {
097:                            if (logger.getLevel() != null)
098:                                loggerLevelService = logger.getLevel()
099:                                        .toString();
100:                        } else if (LOGGER_TRACE_NAME.equals(logger.getName())) {
101:                            if (logger.getLevel() != null)
102:                                loggerLevelTrace = logger.getLevel().toString();
103:                        }
104:                    }
105:
106:                    // data are log4j.properties file + selected current logger levels
107:                    LogConfigData logConfigData = new LogConfigData();
108:                    logConfigData.setLoggerLevelRoot(loggerLevelRoot);
109:                    logConfigData.setLoggerLevelService(loggerLevelService);
110:                    logConfigData.setLoggerLevelTrace(loggerLevelTrace);
111:                    logConfigData.setLogText(logConfig.toString());
112:
113:                    return logConfigData;
114:
115:                } catch (IOException e) {
116:                    handleFault(log, "log4j.properties read error:"
117:                            + e.getMessage(), e);
118:                    return null;
119:                }
120:            }
121:
122:            /**
123:             * Update the running log4j with the given log4j.properties.
124:             * 
125:             * @param logConfig log4j.properties contents
126:             * @throws AxisFault
127:             */
128:            public boolean updateLogConfig(String logConfig) throws AxisFault {
129:                InputStream is = new ByteArrayInputStream(logConfig.getBytes());
130:                Properties properties = new Properties();
131:                try {
132:                    properties.load(is);
133:
134:                } catch (IOException e) {
135:                    handleFault(log, "log4j.properties update error:"
136:                            + e.getMessage(), e);
137:                    return false;
138:                }
139:
140:                // update runtime log4j
141:                PropertyConfigurator.configure(properties);
142:                log.info("Log configuration updated");
143:                return true;
144:            }
145:
146:            /**
147:             * Save given log4j.properties to default location.
148:             * 
149:             * @param logConfig log4j.properties contents
150:             * @throws AxisFault
151:             */
152:            public boolean saveLogConfig(String logConfig) throws AxisFault {
153:                if (logConfig == null) {
154:                    log.error("saving log configuration called with null");
155:                    return false;
156:                }
157:
158:                String logConfigurationLocation = getLogConfigLocation();
159:
160:                try {
161:                    OutputStream out = new java.io.FileOutputStream(new File(
162:                            logConfigurationLocation));
163:                    out.write(logConfig.getBytes());
164:                    out.close();
165:
166:                } catch (IOException e) {
167:                    handleFault(log, "log4j.properties write error:"
168:                            + e.getMessage(), e);
169:                    return false;
170:                }
171:
172:                // update runtime log4j
173:                PropertyConfigurator.configure(logConfigurationLocation);
174:                log.info("log4j.properties saved");
175:                return true;
176:            }
177:
178:            /**
179:             * Get log4j.properties location
180:             * @return
181:             */
182:            private String getLogConfigLocation() {
183:                String logConfigurationXMLLocation = "conf/log4j.properties";
184:
185:                // from synapse home
186:                String synapseHome = System
187:                        .getProperty(SynapseConstants.SYNAPSE_HOME);
188:                if (synapseHome != null) {
189:                    logConfigurationXMLLocation = synapseHome
190:                            + "/webapp/WEB-INF/classes/conf/log4j.properties";
191:
192:                } else {
193:                    log
194:                            .warn("synapse.home not set while reading log configuration");
195:                }
196:                return logConfigurationXMLLocation;
197:            }
198:
199:            /**
200:             * Change logger level in the log4j.properties file.
201:             * 
202:             * @param loggerId logger ID from the loggerMap object
203:             * @param level e.g. INFO, DEBUG
204:             * @throws AxisFault
205:             */
206:            public boolean changeLogConfigLevel(String loggerId, String level)
207:                    throws AxisFault {
208:                // find the logger name from id
209:                log.info("change log level loggerId=" + loggerId + " level="
210:                        + level);
211:
212:                // get logger and change level
213:                Logger logger = null;
214:                if (LOGGER_ROOT_ID.equals(loggerId)) {
215:                    logger = Logger.getRootLogger();
216:
217:                } else {
218:                    String loggerName = (String) loggerMap.get(loggerId);
219:                    if (loggerName == null) {
220:                        log.error("logger id not found:" + level);
221:                        return false;
222:                    }
223:
224:                    logger = Logger.getLogger(loggerName);
225:                }
226:                if (logger != null)
227:                    logger.setLevel(Level.toLevel(level));
228:
229:                return true;
230:            }
231:
232:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.