JAL-3048 test updated for AlignExportSettings changes
[jalview.git] / srcjar / org / apache / log4j / config / PropertyGetter.java
1 /*
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  */
17
18 package org.apache.log4j.config;
19
20 import org.apache.log4j.Priority;
21 import org.apache.log4j.helpers.LogLog;
22
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;
30
31
32 /**
33    Used for inferring configuration information for a log4j's component.
34
35    @author  Anders Kristensen
36  */
37 public class PropertyGetter {
38   protected static final Object[] NULL_ARG = new Object[] {};
39   protected Object obj;
40   protected PropertyDescriptor[] props;
41
42   public interface PropertyCallback {
43     void foundProperty(Object obj, String prefix, String name, Object value);
44   }
45
46   /**
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
50     more times.
51
52     @param obj the object for which to set properties */
53   public
54   PropertyGetter(Object obj) throws IntrospectionException {
55     BeanInfo bi = Introspector.getBeanInfo(obj.getClass());
56     props = bi.getPropertyDescriptors();
57     this.obj = obj;
58   }
59
60   public
61   static
62   void getProperties(Object obj, PropertyCallback callback, String prefix) {
63     try {
64       new PropertyGetter(obj).getProperties(callback, prefix);
65     } catch (IntrospectionException ex) {
66       LogLog.error("Failed to introspect object " + obj, ex);
67     }
68   }
69
70   public
71   void getProperties(PropertyCallback callback, String prefix) {
72     for (int i = 0; i < props.length; i++) {
73       Method getter = props[i].getReadMethod();
74       if (getter == null) {
75         continue;
76     }
77       if (!isHandledType(getter.getReturnType())) {
78         //System.err.println("Ignoring " + props[i].getName() +" " + getter.getReturnType());
79         continue;
80       }
81       String name = props[i].getName();
82       try {
83         Object result = getter.invoke(obj, NULL_ARG);
84         //System.err.println("PROP " + name +": " + result);
85         if (result != null) {
86           callback.foundProperty(obj, prefix, name, result);
87         }
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();
94         }
95         LogLog.warn("Failed to get value of property " + name);
96       } catch (RuntimeException ex) {
97             LogLog.warn("Failed to get value of property " + name);
98       }
99     }
100   }
101
102   protected
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);
109   }
110 }