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
034 /**
035 * @version $Rev: 764390 $ $Date: 2009-04-13 06:08:16 -0400 (Mon, 13 Apr 2009) $
036 */
037 public class ApplicationLog4jConfigurationGBean {
038
039 private static final String ADDITIVITY_PREFIX = "log4j.additivity.";
040 private static final String CATEGORY_PREFIX = "log4j.category.";
041 private static final String LOGGER_PREFIX = "log4j.logger.";
042 private static final String APPENDER_PREFIX = "log4j.appender.";
043 private static final String RENDERER_PREFIX = "log4j.renderer.";
044
045 public ApplicationLog4jConfigurationGBean(String log4jResource, String log4jFile, ServerInfo serverInfo, ClassLoader classloader) throws IOException {
046 InputStream in;
047 if (log4jFile != null) {
048 File file = serverInfo.resolveServer(log4jFile);
049 in = new FileInputStream(file);
050 } else if (log4jResource != null) {
051 in = classloader.getResourceAsStream(log4jResource);
052 if (in == null) {
053 throw new NullPointerException("No log4j properties resource found at " + log4jResource);
054 }
055 } else {
056 return;
057 }
058 Properties props = new Properties();
059 try {
060 props.load(in);
061 } finally {
062 in.close();
063 }
064 try {
065 Class log4jConfigClass = classloader.loadClass("org.apache.log4j.PropertyConfigurator");
066 if (log4jConfigClass.getClassLoader() == ClassLoader.getSystemClassLoader()) {
067 //remove any global log4j configuration
068 for (Iterator it = props.keySet().iterator(); it.hasNext();) {
069 String key = (String) it.next();
070 if (key.startsWith(CATEGORY_PREFIX)
071 || key.startsWith(LOGGER_PREFIX)
072 || key.startsWith(ADDITIVITY_PREFIX)
073 || key.startsWith(APPENDER_PREFIX)
074 || key.startsWith(RENDERER_PREFIX)) {
075 continue;
076 }
077 it.remove();
078 }
079 }
080 log4jConfigClass.getMethod("configure", Properties.class).invoke(null, props);
081 } catch (Exception e) {
082 }
083 }
084
085 public static final GBeanInfo GBEAN_INFO;
086
087 static {
088 GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(ApplicationLog4jConfigurationGBean.class, "SystemLog");
089 infoBuilder.setPriority(2);
090 infoBuilder.addAttribute("log4jResource", String.class, true);
091 infoBuilder.addAttribute("log4jFile", String.class, true);
092 infoBuilder.addAttribute("classLoader", ClassLoader.class, false);
093
094 infoBuilder.addReference("ServerInfo", ServerInfo.class, "GBean");
095
096 infoBuilder.setConstructor(new String[]{"log4jResource", "log4jFile", "ServerInfo", "classLoader"});
097
098 GBEAN_INFO = infoBuilder.getBeanInfo();
099 }
100
101 public static GBeanInfo getGBeanInfo() {
102 return GBEAN_INFO;
103 }
104
105 }