Source Code Cross Referenced for JuliLogStreamFactory.java in  » J2EE » openejb3 » org » apache » openejb » util » 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 » J2EE » openejb3 » org.apache.openejb.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         *
003:         * Licensed to the Apache Software Foundation (ASF) under one or more
004:         * contributor license agreements.  See the NOTICE file distributed with
005:         * this work for additional information regarding copyright ownership.
006:         * The ASF licenses this file to You under the Apache License, Version 2.0
007:         * (the "License"); you may not use this file except in compliance with
008:         * the License.  You may obtain a copy of the License at
009:         *
010:         *     http://www.apache.org/licenses/LICENSE-2.0
011:         *
012:         *  Unless required by applicable law or agreed to in writing, software
013:         *  distributed under the License is distributed on an "AS IS" BASIS,
014:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015:         *  See the License for the specific language governing permissions and
016:         *  limitations under the License.
017:         */package org.apache.openejb.util;
018:
019:        import org.apache.log4j.PropertyConfigurator;
020:        import org.apache.openejb.loader.FileUtils;
021:        import org.apache.openejb.loader.SystemInstance;
022:
023:        import java.io.BufferedInputStream;
024:        import java.io.BufferedOutputStream;
025:        import java.io.ByteArrayInputStream;
026:        import java.io.ByteArrayOutputStream;
027:        import java.io.File;
028:        import java.io.FileInputStream;
029:        import java.io.FileOutputStream;
030:        import java.io.IOException;
031:        import java.io.InputStream;
032:        import java.net.URL;
033:        import java.util.ArrayList;
034:        import java.util.List;
035:        import java.util.Map;
036:        import java.util.Properties;
037:        import java.util.logging.ConsoleHandler;
038:        import java.util.logging.LogManager;
039:        import java.util.logging.Logger;
040:        import java.util.logging.SimpleFormatter;
041:
042:        public class JuliLogStreamFactory implements  LogStreamFactory {
043:            private static final String LOGGING_PROPERTIES_FILE = "logging.properties";
044:            private static final String DEFAULT_LOGGING_PROPERTIES_FILE = "juli.properties";
045:            private static final String EMBEDDED_PROPERTIES_FILE = "embedded.juli.properties";
046:
047:            public LogStream createLogStream(LogCategory logCategory) {
048:                return new JuliLogStream(logCategory);
049:            }
050:
051:            public JuliLogStreamFactory() {
052:                try {
053:                    String prop = System.getProperty("openejb.logger.external",
054:                            "false");
055:                    boolean externalLogging = Boolean.parseBoolean(prop);
056:                    if (!externalLogging) {
057:                        configureInternal();
058:                    }
059:                } catch (Exception e) {
060:                    // The fall back here is that if log4j.configuration system property is set, then that configuration file will be used.
061:                    e.printStackTrace();
062:                }
063:            }
064:
065:            private void configureInternal() throws IOException {
066:                File confDir = SystemInstance.get().getBase().getDirectory(
067:                        "conf");
068:                File loggingPropertiesFile = new File(confDir,
069:                        LOGGING_PROPERTIES_FILE);
070:                if (confDir.exists()) {
071:                    if (loggingPropertiesFile.exists()) {
072:                        FileInputStream in = null;
073:                        try {
074:                            in = new FileInputStream(loggingPropertiesFile);
075:                            LogManager.getLogManager().readConfiguration(in);
076:                        } finally {
077:                            if (in != null) {
078:                                try {
079:                                    in.close();
080:                                } catch (IOException e) {
081:                                }
082:                            }
083:                        }
084:                    } else {
085:                        // install our logging.properties file into the conf dir
086:                        installLoggingPropertiesFile(loggingPropertiesFile);
087:                    }
088:                } else {
089:                    // no conf directory, so we assume we are embedded
090:                    // configure log4j directly
091:                    configureEmbedded();
092:                }
093:            }
094:
095:            private void preprocessProperties(Properties properties) {
096:                FileUtils base = SystemInstance.get().getBase();
097:                File confDir = new File(base.getDirectory(), "conf");
098:                File baseDir = base.getDirectory();
099:                File userDir = new File("foo").getParentFile();
100:
101:                File[] paths = { confDir, baseDir, userDir };
102:
103:                List<File> missing = new ArrayList<File>();
104:
105:                for (Map.Entry<Object, Object> entry : properties.entrySet()) {
106:                    String key = (String) entry.getKey();
107:                    String value = (String) entry.getValue();
108:
109:                    if (key.endsWith(".File")) {
110:                        boolean found = false;
111:                        for (int i = 0; i < paths.length && !found; i++) {
112:                            File path = paths[i];
113:                            File logfile = new File(path, value);
114:                            if (logfile.getParentFile().exists()) {
115:                                properties.setProperty(key, logfile
116:                                        .getAbsolutePath());
117:                                found = true;
118:                            }
119:                        }
120:
121:                        if (!found) {
122:                            File logfile = new File(paths[0], value);
123:                            missing.add(logfile);
124:                        }
125:                    }
126:                }
127:
128:                if (missing.size() > 0) {
129:                    java.util.logging.Logger logger = getFallabckLogger();
130:
131:                    logger
132:                            .severe("Logging may not operate as expected.  The directories for the following files do not exist so no file can be created.  See the list below.");
133:                    for (int i = 0; i < missing.size(); i++) {
134:                        File file = missing.get(i);
135:                        logger.severe("[" + i + "] " + file.getAbsolutePath());
136:                    }
137:                }
138:            }
139:
140:            private Logger getFallabckLogger() {
141:                java.util.logging.Logger logger = Logger
142:                        .getLogger("OpenEJB.logging");
143:                ConsoleHandler consoleHandler = new ConsoleHandler();
144:                consoleHandler.setFormatter(new SimpleFormatter());
145:                logger.addHandler(consoleHandler);
146:                return logger;
147:            }
148:
149:            private void configureEmbedded() {
150:                URL resource = Thread.currentThread().getContextClassLoader()
151:                        .getResource(EMBEDDED_PROPERTIES_FILE);
152:                if (resource != null)
153:                    PropertyConfigurator.configure(resource);
154:                else
155:                    System.out
156:                            .println("FATAL ERROR WHILE CONFIGURING LOGGING!!!. MISSING embedded.logging.properties FILE ");
157:            }
158:
159:            private void installLoggingPropertiesFile(File loggingPropertiesFile)
160:                    throws IOException {
161:                URL resource = Thread.currentThread().getContextClassLoader()
162:                        .getResource(DEFAULT_LOGGING_PROPERTIES_FILE);
163:                if (resource == null) {
164:                    System.out
165:                            .println("FATAL ERROR WHILE CONFIGURING LOGGING!!!. MISSING logging.properties FILE ");
166:                    return;
167:                }
168:                InputStream in = resource.openStream();
169:                in = new BufferedInputStream(in);
170:                ByteArrayOutputStream bao = new ByteArrayOutputStream();
171:                byte buf[] = new byte[4096];
172:                for (int count = in.read(buf); count >= 0; count = in.read(buf)) {
173:                    bao.write(buf, 0, count);
174:                }
175:                byte[] byteArray = bao.toByteArray();
176:                ByteArrayInputStream bis = new ByteArrayInputStream(byteArray);
177:
178:                Properties props = new Properties();
179:                props.load(bis);
180:                preprocessProperties(props);
181:                BufferedOutputStream bout = new BufferedOutputStream(
182:                        new FileOutputStream(loggingPropertiesFile));
183:                bout.write(byteArray);
184:                PropertyConfigurator.configure(props);
185:                try {
186:                    bout.close();
187:                } catch (IOException e) {
188:
189:                }
190:                try {
191:                    in.close();
192:                } catch (IOException e) {
193:
194:                }
195:            }
196:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.