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.openejb.loader.SystemInstance;
020: import org.apache.openejb.loader.FileUtils;
021: import org.apache.log4j.PropertyConfigurator;
022: import org.apache.log4j.SimpleLayout;
023: import org.apache.log4j.ConsoleAppender;
024:
025: import java.io.IOException;
026: import java.io.File;
027: import java.io.BufferedInputStream;
028: import java.io.FileInputStream;
029: import java.io.InputStream;
030: import java.io.ByteArrayOutputStream;
031: import java.io.ByteArrayInputStream;
032: import java.io.BufferedOutputStream;
033: import java.io.FileOutputStream;
034: import java.util.Properties;
035: import java.util.List;
036: import java.util.ArrayList;
037: import java.util.Map;
038: import java.net.URL;
039:
040: public class Log4jLogStreamFactory implements LogStreamFactory {
041: private static final String LOGGING_PROPERTIES_FILE = "logging.properties";
042: private static final String EMBEDDED_PROPERTIES_FILE = "embedded.logging.properties";
043:
044: public LogStream createLogStream(LogCategory logCategory) {
045: return new Log4jLogStream(logCategory);
046: }
047:
048: public Log4jLogStreamFactory() {
049: try {
050: String prop = System.getProperty("openejb.logger.external",
051: "false");
052: boolean externalLogging = Boolean.parseBoolean(prop);
053: if (!externalLogging)
054: configureInternal();
055: } catch (Exception e) {
056: // The fall back here is that if log4j.configuration system property is set, then that configuration file will be used.
057: e.printStackTrace();
058: }
059: }
060:
061: private void configureInternal() throws IOException {
062: // OpenJPA should use Log4j also
063: System.setProperty("openjpa.Log", "log4j");
064:
065: File confDir = SystemInstance.get().getBase().getDirectory(
066: "conf");
067: File loggingPropertiesFile = new File(confDir,
068: LOGGING_PROPERTIES_FILE);
069: if (confDir.exists()) {
070: if (loggingPropertiesFile.exists()) {
071: // load logging.properties file
072: BufferedInputStream bis = new BufferedInputStream(
073: new FileInputStream(loggingPropertiesFile));
074: Properties props = new Properties();
075: props.load(bis);
076: preprocessProperties(props);
077: PropertyConfigurator.configure(props);
078: try {
079: bis.close();
080: } catch (IOException e) {
081:
082: }
083: } else {
084: // install our logging.properties file into the conf dir
085: installLoggingPropertiesFile(loggingPropertiesFile);
086: }
087: } else {
088: // no conf directory, so we assume we are embedded
089: // configure log4j directly
090: configureEmbedded();
091: }
092: }
093:
094: private void preprocessProperties(Properties properties) {
095: FileUtils base = SystemInstance.get().getBase();
096: File confDir = new File(base.getDirectory(), "conf");
097: File baseDir = base.getDirectory();
098: File userDir = new File("foo").getParentFile();
099:
100: File[] paths = { confDir, baseDir, userDir };
101:
102: List<File> missing = new ArrayList<File>();
103:
104: for (Map.Entry<Object, Object> entry : properties.entrySet()) {
105: String key = (String) entry.getKey();
106: String value = (String) entry.getValue();
107:
108: if (key.endsWith(".File")) {
109: boolean found = false;
110: for (int i = 0; i < paths.length && !found; i++) {
111: File path = paths[i];
112: File logfile = new File(path, value);
113: if (logfile.getParentFile().exists()) {
114: properties.setProperty(key, logfile
115: .getAbsolutePath());
116: found = true;
117: }
118: }
119:
120: if (!found) {
121: File logfile = new File(paths[0], value);
122: missing.add(logfile);
123: }
124: }
125: }
126:
127: if (missing.size() > 0) {
128: org.apache.log4j.Logger logger = getFallabckLogger();
129:
130: logger
131: .error("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.");
132: for (int i = 0; i < missing.size(); i++) {
133: File file = missing.get(i);
134: logger.error("[" + i + "] " + file.getAbsolutePath());
135: }
136: }
137: }
138:
139: private org.apache.log4j.Logger getFallabckLogger() {
140: org.apache.log4j.Logger logger = org.apache.log4j.Logger
141: .getLogger("OpenEJB.logging");
142:
143: SimpleLayout simpleLayout = new SimpleLayout();
144: ConsoleAppender newAppender = new ConsoleAppender(simpleLayout);
145: logger.addAppender(newAppender);
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(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: }
|