Source Code Cross Referenced for LoggingConfiguration.java in  » Database-ORM » db-ojb » org » apache » ojb » broker » util » logging » 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 » Database ORM » db ojb » org.apache.ojb.broker.util.logging 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.apache.ojb.broker.util.logging;
002:
003:        /* Copyright 2002-2005 The Apache Software Foundation
004:         *
005:         * Licensed under the Apache License, Version 2.0 (the "License");
006:         * you may not use this file except in compliance with the License.
007:         * You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        import java.io.InputStream;
019:        import java.io.File;
020:        import java.util.Properties;
021:        import java.net.URL;
022:
023:        import org.apache.ojb.broker.util.ClassHelper;
024:        import org.apache.ojb.broker.util.configuration.impl.ConfigurationAbstractImpl;
025:        import org.apache.commons.lang.SystemUtils;
026:
027:        /**
028:         * Provides the configuration for the logging. Note that this is separated from the OJB
029:         * configuration.
030:         * 
031:         * @version $Id: LoggingConfiguration.java,v 1.8.2.4 2005/12/21 22:28:16 tomdz Exp $
032:         */
033:        public class LoggingConfiguration extends ConfigurationAbstractImpl {
034:            /** The commons-logging property denoting which log to use. This property
035:             *  is repeated here to avoid making this class dependent upon commons-logging */
036:            public static final String PROPERTY_COMMONS_LOGGING_LOG = "org.apache.commons.logging.Log";
037:            /** The commons-logging property denoting which log factory to use. This property
038:             *  is repeated here to avoid making this class dependent upon commons-logging */
039:            public static final String PROPERTY_COMMONS_LOGGING_LOGFACTORY = "org.apache.commons.logging.LogFactory";
040:            /** The property denoting the OJB logger class */
041:            public static final String PROPERTY_OJB_LOGGERCLASS = "org.apache.ojb.broker.util.logging.Logger.class";
042:            /** The property denoting the config file for the OJB logger class */
043:            public static final String PROPERTY_OJB_LOGGERCONFIGFILE = "org.apache.ojb.broker.util.logging.Logger.configFile";
044:            /** Default filename of the OJB logging properties file */
045:            public static final String OJB_LOGGING_PROPERTIES_FILE = "OJB-logging.properties";
046:            /** Default log level */
047:            public static final String OJB_DEFAULT_LOG_LEVEL = "WARN";
048:            /** Default boot log level */
049:            public static final String OJB_DEFAULT_BOOT_LOG_LEVEL = "INFO";
050:
051:            /** The logger class */
052:            private Class _loggerClass;
053:            /** The config file for the logger */
054:            private String _loggerConfigFile;
055:
056:            /**
057:             * Creates a new logging configuration object which automatically initializes itself.
058:             */
059:            public LoggingConfiguration() {
060:                super ();
061:            }
062:
063:            /* (non-Javadoc)
064:             * @see org.apache.ojb.broker.util.configuration.impl.ConfigurationAbstractImpl#load()
065:             */
066:            protected void load() {
067:                Logger bootLogger = LoggerFactory.getBootLogger();
068:
069:                // first we check whether the system property
070:                //   org.apache.ojb.broker.util.logging.Logger
071:                // is set (or its alias LoggerClass which is deprecated)
072:                ClassLoader contextLoader = ClassHelper.getClassLoader();
073:                String loggerClassName;
074:
075:                _loggerClass = null;
076:                properties = new Properties();
077:                loggerClassName = getLoggerClass(System.getProperties());
078:                _loggerConfigFile = getLoggerConfigFile(System.getProperties());
079:
080:                InputStream ojbLogPropFile;
081:                if (loggerClassName == null) {
082:                    // now we're trying to load the OJB-logging.properties file
083:                    String ojbLogPropFilePath = System.getProperty(
084:                            OJB_LOGGING_PROPERTIES_FILE,
085:                            OJB_LOGGING_PROPERTIES_FILE);
086:                    try {
087:                        URL ojbLoggingURL = ClassHelper
088:                                .getResource(ojbLogPropFilePath);
089:                        if (ojbLoggingURL == null) {
090:                            ojbLoggingURL = (new File(ojbLogPropFilePath))
091:                                    .toURL();
092:                        }
093:                        ojbLogPropFile = ojbLoggingURL.openStream();
094:                        try {
095:                            bootLogger.info("Found logging properties file: "
096:                                    + ojbLogPropFilePath);
097:                            properties.load(ojbLogPropFile);
098:                            _loggerConfigFile = getLoggerConfigFile(properties);
099:                            loggerClassName = getLoggerClass(properties);
100:                        } finally {
101:                            ojbLogPropFile.close();
102:                        }
103:                    } catch (Exception ex) {
104:                        if (loggerClassName == null) {
105:                            bootLogger
106:                                    .warn("Can't read logging properties file using path '"
107:                                            + ojbLogPropFilePath
108:                                            + "', message is: "
109:                                            + SystemUtils.LINE_SEPARATOR
110:                                            + ex.getMessage()
111:                                            + SystemUtils.LINE_SEPARATOR
112:                                            + "Will try to load logging properties from OJB.properties file");
113:                        } else {
114:                            bootLogger.info(
115:                                    "Problems while closing resources for path '"
116:                                            + ojbLogPropFilePath
117:                                            + "', message is: "
118:                                            + SystemUtils.LINE_SEPARATOR
119:                                            + ex.getMessage(), ex);
120:                        }
121:                    }
122:                }
123:                if (loggerClassName == null) {
124:                    // deprecated: load the OJB.properties file
125:                    // this is not good because we have all OJB properties in this config
126:                    String ojbPropFile = System.getProperty("OJB.properties",
127:                            "OJB.properties");
128:
129:                    try {
130:                        ojbLogPropFile = contextLoader
131:                                .getResourceAsStream(ojbPropFile);
132:                        if (ojbLogPropFile != null) {
133:                            try {
134:                                properties.load(ojbLogPropFile);
135:                                loggerClassName = getLoggerClass(properties);
136:                                _loggerConfigFile = getLoggerConfigFile(properties);
137:                                if (loggerClassName != null) {
138:                                    // deprecation warning for after 1.0
139:                                    bootLogger
140:                                            .warn("Please use a separate '"
141:                                                    + OJB_LOGGING_PROPERTIES_FILE
142:                                                    + "' file to specify your logging settings");
143:                                }
144:                            } finally {
145:                                ojbLogPropFile.close();
146:                            }
147:                        }
148:                    } catch (Exception ex) {
149:                    }
150:                }
151:                if (loggerClassName != null) {
152:                    try {
153:                        _loggerClass = ClassHelper.getClass(loggerClassName);
154:                        bootLogger.info("Logging: Found logger class '"
155:                                + loggerClassName);
156:                    } catch (ClassNotFoundException ex) {
157:                        _loggerClass = PoorMansLoggerImpl.class;
158:                        bootLogger.warn("Could not load logger class "
159:                                + loggerClassName + ", defaulting to "
160:                                + _loggerClass.getName(), ex);
161:                    }
162:                } else {
163:                    // still no logger configured - lets check whether commons-logging is configured
164:                    if ((System.getProperty(PROPERTY_COMMONS_LOGGING_LOG) != null)
165:                            || (System
166:                                    .getProperty(PROPERTY_COMMONS_LOGGING_LOGFACTORY) != null)) {
167:                        // yep, so use commons-logging
168:                        _loggerClass = CommonsLoggerImpl.class;
169:                        bootLogger
170:                                .info("Logging: Found commons logging properties, use "
171:                                        + _loggerClass);
172:                    } else {
173:                        // but perhaps there is a log4j.properties file ?
174:                        try {
175:                            ojbLogPropFile = contextLoader
176:                                    .getResourceAsStream("log4j.properties");
177:                            if (ojbLogPropFile != null) {
178:                                // yep, so use log4j
179:                                _loggerClass = Log4jLoggerImpl.class;
180:                                _loggerConfigFile = "log4j.properties";
181:                                bootLogger
182:                                        .info("Logging: Found 'log4j.properties' file, use "
183:                                                + _loggerClass);
184:                                ojbLogPropFile.close();
185:                            }
186:                        } catch (Exception ex) {
187:                        }
188:                        if (_loggerClass == null) {
189:                            // or a commons-logging.properties file ?
190:                            try {
191:                                ojbLogPropFile = contextLoader
192:                                        .getResourceAsStream("commons-logging.properties");
193:                                if (ojbLogPropFile != null) {
194:                                    // yep, so use commons-logging
195:                                    _loggerClass = CommonsLoggerImpl.class;
196:                                    _loggerConfigFile = "commons-logging.properties";
197:                                    bootLogger
198:                                            .info("Logging: Found 'commons-logging.properties' file, use "
199:                                                    + _loggerClass);
200:                                    ojbLogPropFile.close();
201:                                }
202:                            } catch (Exception ex) {
203:                            }
204:                            if (_loggerClass == null) {
205:                                // no, so default to poor man's logging
206:                                bootLogger
207:                                        .info("** Can't find logging configuration file, use default logger **");
208:                                _loggerClass = PoorMansLoggerImpl.class;
209:                            }
210:                        }
211:                    }
212:                }
213:            }
214:
215:            private String getLoggerClass(Properties props) {
216:                String loggerClassName = props
217:                        .getProperty(PROPERTY_OJB_LOGGERCLASS);
218:
219:                if (loggerClassName == null) {
220:                    loggerClassName = props.getProperty("LoggerClass");
221:                }
222:                return loggerClassName;
223:            }
224:
225:            private String getLoggerConfigFile(Properties props) {
226:                String loggerConfigFile = props
227:                        .getProperty(PROPERTY_OJB_LOGGERCONFIGFILE);
228:
229:                if (loggerConfigFile == null) {
230:                    loggerConfigFile = props.getProperty("LoggerConfigFile");
231:                }
232:                return loggerConfigFile;
233:            }
234:
235:            public String getLogLevel(String loggerName) {
236:                /*
237:                arminw:
238:                use ROOT.LogLevel property to define global
239:                default log level
240:                 */
241:                return getString(loggerName + ".LogLevel", getString(
242:                        "ROOT.LogLevel", OJB_DEFAULT_LOG_LEVEL));
243:            }
244:
245:            /* (non-Javadoc)
246:             * @see org.apache.ojb.broker.util.configuration.Configuration#setLogger(org.apache.ojb.broker.util.logging.Logger)
247:             */
248:            public void setLogger(Logger loggerInstance) {
249:                // ignored - only logging via the boot logger
250:            }
251:
252:            /**
253:             * Returns the logger class.
254:             * 
255:             * @return The logger class
256:             */
257:            public Class getLoggerClass() {
258:                return _loggerClass;
259:            }
260:
261:            /**
262:             * Returns the name of the config file for the logger.
263:             * 
264:             * @return The config file if it was configured
265:             */
266:            public String getLoggerConfigFile() {
267:                return _loggerConfigFile;
268:            }
269:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.