001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with 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,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020
021 package org.apache.geronimo.system.logging.log4j;
022
023 import java.io.File;
024 import java.io.FileInputStream;
025 import java.io.IOException;
026 import java.io.InputStream;
027 import java.util.Iterator;
028 import java.util.Properties;
029
030 import org.apache.geronimo.gbean.GBeanInfo;
031 import org.apache.geronimo.gbean.GBeanInfoBuilder;
032 import org.apache.geronimo.system.serverinfo.ServerInfo;
033 import org.apache.log4j.PropertyConfigurator;
034
035 /**
036 * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
037 */
038 public class ApplicationLog4jConfigurationGBean {
039
040 private static final String ADDITIVITY_PREFIX = "log4j.additivity.";
041 private static final String CATEGORY_PREFIX = "log4j.category.";
042 private static final String LOGGER_PREFIX = "log4j.logger.";
043 private static final String APPENDER_PREFIX = "log4j.appender.";
044 private static final String RENDERER_PREFIX = "log4j.renderer.";
045
046 public ApplicationLog4jConfigurationGBean(String log4jResource, String log4jFile, ServerInfo serverInfo, ClassLoader classloader) throws IOException {
047 InputStream in;
048 if (log4jFile != null) {
049 File file = serverInfo.resolveServer(log4jFile);
050 in = new FileInputStream(file);
051 } else if (log4jResource != null) {
052 in = classloader.getResourceAsStream(log4jResource);
053 if (in == null) {
054 throw new NullPointerException("No log4j properties resource found at " + log4jResource);
055 }
056 } else {
057 return;
058 }
059 Properties props = new Properties();
060 try {
061 props.load(in);
062 } finally {
063 in.close();
064 }
065 //remove any global log4j configuration
066 for (Iterator it = props.keySet().iterator(); it.hasNext(); ) {
067 String key = (String) it.next();
068 if (key.startsWith(CATEGORY_PREFIX)
069 || key.startsWith(LOGGER_PREFIX)
070 || key.startsWith(ADDITIVITY_PREFIX)
071 || key.startsWith(APPENDER_PREFIX)
072 || key.startsWith(RENDERER_PREFIX)) {
073 continue;
074 }
075 it.remove();
076 }
077
078 PropertyConfigurator.configure(props);
079 }
080
081 public static final GBeanInfo GBEAN_INFO;
082
083 static {
084 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(ApplicationLog4jConfigurationGBean.class, "SystemLog");
085 infoBuilder.setPriority(2);
086 infoBuilder.addAttribute("log4jResource", String.class, true);
087 infoBuilder.addAttribute("log4jFile", String.class, true);
088 infoBuilder.addAttribute("classloader", ClassLoader.class, false);
089
090 infoBuilder.addReference("ServerInfo", ServerInfo.class, "GBean");
091
092 infoBuilder.setConstructor(new String[]{"log4jResource", "log4jFile", "ServerInfo", "classloader"});
093
094 GBEAN_INFO = infoBuilder.getBeanInfo();
095 }
096
097 public static GBeanInfo getGBeanInfo() {
098 return GBEAN_INFO;
099 }
100
101 }