2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.apache.log4j.config;
20 import org.apache.log4j.Priority;
21 import org.apache.log4j.helpers.LogLog;
23 import java.beans.BeanInfo;
24 import java.beans.IntrospectionException;
25 import java.beans.Introspector;
26 import java.beans.PropertyDescriptor;
27 import java.io.InterruptedIOException;
28 import java.lang.reflect.InvocationTargetException;
29 import java.lang.reflect.Method;
33 Used for inferring configuration information for a log4j's component.
35 @author Anders Kristensen
37 public class PropertyGetter {
38 protected static final Object[] NULL_ARG = new Object[] {};
40 protected PropertyDescriptor[] props;
42 public interface PropertyCallback {
43 void foundProperty(Object obj, String prefix, String name, Object value);
47 Create a new PropertyGetter for the specified Object. This is done
48 in prepartion for invoking {@link
49 #getProperties(PropertyGetter.PropertyCallback, String)} one or
52 @param obj the object for which to set properties */
54 PropertyGetter(Object obj) throws IntrospectionException {
55 BeanInfo bi = Introspector.getBeanInfo(obj.getClass());
56 props = bi.getPropertyDescriptors();
62 void getProperties(Object obj, PropertyCallback callback, String prefix) {
64 new PropertyGetter(obj).getProperties(callback, prefix);
65 } catch (IntrospectionException ex) {
66 LogLog.error("Failed to introspect object " + obj, ex);
71 void getProperties(PropertyCallback callback, String prefix) {
72 for (int i = 0; i < props.length; i++) {
73 Method getter = props[i].getReadMethod();
77 if (!isHandledType(getter.getReturnType())) {
78 //System.err.println("Ignoring " + props[i].getName() +" " + getter.getReturnType());
81 String name = props[i].getName();
83 Object result = getter.invoke(obj, NULL_ARG);
84 //System.err.println("PROP " + name +": " + result);
86 callback.foundProperty(obj, prefix, name, result);
88 } catch (IllegalAccessException ex) {
89 LogLog.warn("Failed to get value of property " + name);
90 } catch (InvocationTargetException ex) {
91 if (ex.getTargetException() instanceof InterruptedException
92 || ex.getTargetException() instanceof InterruptedIOException) {
93 Thread.currentThread().interrupt();
95 LogLog.warn("Failed to get value of property " + name);
96 } catch (RuntimeException ex) {
97 LogLog.warn("Failed to get value of property " + name);
103 boolean isHandledType(Class type) {
104 return String.class.isAssignableFrom(type) ||
105 Integer.TYPE.isAssignableFrom(type) ||
106 Long.TYPE.isAssignableFrom(type) ||
107 Boolean.TYPE.isAssignableFrom(type) ||
108 Priority.class.isAssignableFrom(type);