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: }
|