KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.*;
20 import java.util.*;
21 import org.apache.log4j.*;
22
23 /**
24    Prints the configuration of the log4j default hierarchy
25    (which needs to be auto-initialized) as a propoperties file
26    on a {@link PrintWriter}.
27    
28    @author Anders Kristensen
29  */

30 public class PropertyPrinter implements PropertyGetter.PropertyCallback {
31   protected int numAppenders = 0;
32   protected Hashtable appenderNames = new Hashtable();
33   protected Hashtable layoutNames = new Hashtable();
34   protected PrintWriter out;
35   protected boolean doCapitalize;
36   
37   public
38   PropertyPrinter(PrintWriter out) {
39     this(out, false);
40   }
41   
42   public
43   PropertyPrinter(PrintWriter out, boolean doCapitalize) {
44     this.out = out;
45     this.doCapitalize = doCapitalize;
46     
47     print(out);
48     out.flush();
49   }
50   
51   protected
52   String JavaDoc genAppName() {
53     return "A" + numAppenders++;
54   }
55   
56   /**
57    * Returns true if the specified appender name is considered to have
58    * been generated, that is, if it is of the form A[0-9]+.
59   */

60   protected
61   boolean isGenAppName(String JavaDoc name) {
62     if (name.length() < 2 || name.charAt(0) != 'A') return false;
63     
64     for (int i = 0; i < name.length(); i++) {
65       if (name.charAt(i) < '0' || name.charAt(i) > '9') return false;
66     }
67     return true;
68   }
69   
70   /**
71    * Prints the configuration of the default log4j hierarchy as a Java
72    * properties file on the specified Writer.
73    *
74    * <p>N.B. print() can be invoked only once!
75    */

76   public
77   void print(PrintWriter out) {
78     printOptions(out, Logger.getRootLogger());
79     
80     Enumeration cats = LogManager.getCurrentLoggers();
81     while (cats.hasMoreElements()) {
82       printOptions(out, (Logger) cats.nextElement());
83     }
84   }
85   
86   protected
87   void printOptions(PrintWriter out, Logger cat) {
88     Enumeration appenders = cat.getAllAppenders();
89     Level prio = cat.getLevel();
90     String JavaDoc appenderString = (prio == null ? "" : prio.toString());
91     
92     while (appenders.hasMoreElements()) {
93       Appender app = (Appender) appenders.nextElement();
94       String JavaDoc name;
95       
96       if ((name = (String JavaDoc) appenderNames.get(app)) == null) {
97       
98         // first assign name to the appender
99
if ((name = app.getName()) == null || isGenAppName(name)) {
100             name = genAppName();
101         }
102         appenderNames.put(app, name);
103         
104         printOptions(out, app, "log4j.appender."+name);
105         if (app.getLayout() != null) {
106           printOptions(out, app.getLayout(), "log4j.appender."+name+".layout");
107         }
108       }
109       appenderString += ", " + name;
110     }
111     String JavaDoc catKey = (cat == Logger.getRootLogger())
112         ? "log4j.rootLogger"
113         : "log4j.logger." + cat.getName();
114     if (appenderString != "") {
115       out.println(catKey + "=" + appenderString);
116     }
117   }
118   
119   protected
120   void printOptions(PrintWriter out, Object JavaDoc obj, String JavaDoc fullname) {
121     out.println(fullname + "=" + obj.getClass().getName());
122     PropertyGetter.getProperties(obj, this, fullname + ".");
123   }
124   
125   public void foundProperty(Object JavaDoc obj, String JavaDoc prefix, String JavaDoc name, Object JavaDoc value) {
126     // XXX: Properties encode value.toString()
127
if (obj instanceof Appender && "name".equals(name)) {
128       return;
129     }
130     if (doCapitalize) {
131       name = capitalize(name);
132     }
133     out.println(prefix + name + "=" + value.toString());
134   }
135   
136   public static String JavaDoc capitalize(String JavaDoc name) {
137     if (Character.isLowerCase(name.charAt(0))) {
138       if (name.length() == 1 || Character.isLowerCase(name.charAt(1))) {
139         StringBuffer JavaDoc newname = new StringBuffer JavaDoc(name);
140         newname.setCharAt(0, Character.toUpperCase(name.charAt(0)));
141         return newname.toString();
142       }
143     }
144     return name;
145   }
146   
147   // for testing
148
public static void main(String JavaDoc[] args) {
149     new PropertyPrinter(new PrintWriter(System.out));
150   }
151 }
152
Popular Tags