KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > config > PropertyGetter


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.log4j.config;
18
19 import java.beans.*;
20 import java.lang.reflect.*;
21 import org.apache.log4j.Priority;
22 import org.apache.log4j.helpers.LogLog;
23
24
25 /**
26    Used for inferring configuration information for a log4j's component.
27
28    @author Anders Kristensen
29  */

30 public class PropertyGetter {
31   protected static final Object JavaDoc[] NULL_ARG = new Object JavaDoc[] {};
32   protected Object JavaDoc obj;
33   protected PropertyDescriptor[] props;
34
35   public interface PropertyCallback {
36     void foundProperty(Object JavaDoc obj, String JavaDoc prefix, String JavaDoc name, Object JavaDoc value);
37   }
38
39   /**
40     Create a new PropertyGetter for the specified Object. This is done
41     in prepartion for invoking {@link
42     #getProperties(PropertyGetter.PropertyCallback, String)} one or
43     more times.
44
45     @param obj the object for which to set properties */

46   public
47   PropertyGetter(Object JavaDoc obj) throws IntrospectionException {
48     BeanInfo bi = Introspector.getBeanInfo(obj.getClass());
49     props = bi.getPropertyDescriptors();
50     this.obj = obj;
51   }
52
53   public
54   static
55   void getProperties(Object JavaDoc obj, PropertyCallback callback, String JavaDoc prefix) {
56     try {
57       new PropertyGetter(obj).getProperties(callback, prefix);
58     } catch (IntrospectionException ex) {
59       LogLog.error("Failed to introspect object " + obj, ex);
60     }
61   }
62
63   public
64   void getProperties(PropertyCallback callback, String JavaDoc prefix) {
65     for (int i = 0; i < props.length; i++) {
66       Method getter = props[i].getReadMethod();
67       if (getter == null) continue;
68       if (!isHandledType(getter.getReturnType())) {
69     //System.err.println("Ignoring " + props[i].getName() +" " + getter.getReturnType());
70
continue;
71       }
72       String JavaDoc name = props[i].getName();
73       try {
74     Object JavaDoc result = getter.invoke(obj, NULL_ARG);
75     //System.err.println("PROP " + name +": " + result);
76
if (result != null) {
77       callback.foundProperty(obj, prefix, name, result);
78     }
79       } catch (Exception JavaDoc ex) {
80     LogLog.warn("Failed to get value of property " + name);
81       }
82     }
83   }
84
85   protected
86   boolean isHandledType(Class JavaDoc type) {
87     return String JavaDoc.class.isAssignableFrom(type) ||
88       Integer.TYPE.isAssignableFrom(type) ||
89       Long.TYPE.isAssignableFrom(type) ||
90       Boolean.TYPE.isAssignableFrom(type) ||
91       Priority.class.isAssignableFrom(type);
92   }
93 }
94
Popular Tags