KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > Runtime


1 /*
2  * Copyright (C) 2001 Mika Riekkinen, Joni Suominen
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package alt.jiapi;
20
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24
25 import org.apache.log4j.Appender;
26 import org.apache.log4j.Category;
27 import org.apache.log4j.ConsoleAppender;
28 import org.apache.log4j.Priority;
29 //import org.apache.log4j.SimpleLayout;
30
import org.apache.log4j.PatternLayout;
31
32
33 /**
34  * This class is a class that holds mainly static common methods,
35  * which are used by other Jiapi classes.
36  *
37  * @author Mika Riekkinen
38  * @author Joni Suominen
39  * @version $Revision: 1.8 $ $Date: 2004/03/02 10:11:00 $
40  */

41 public class Runtime {
42     private static Map JavaDoc fieldValues = new HashMap JavaDoc();
43
44     public static synchronized void setFieldValue(String JavaDoc fieldName,
45                                                   Object JavaDoc value) {
46         fieldValues.put(fieldName, value);
47     }
48
49     public static synchronized Object JavaDoc getFieldValue(String JavaDoc fieldName) {
50         Object JavaDoc o = fieldValues.get(fieldName);
51         //System.out.println("GetFieldValue " + fieldName + ": " + o);
52
return o;
53     }
54
55     // NOTE! Since the above method uses just a field name as an index,
56
// would the following method make sense?
57
// public static String createUniqueFieldName();
58

59     public static Category getRootLogCategory() {
60         return SystemPropertyLogConfigurator.getJiapiRoot();
61     }
62
63     public static Category getLogCategory(Class JavaDoc clazz) {
64         SystemPropertyLogConfigurator.configure();
65         return Category.getInstance(clazz);
66     }
67 }
68
69
70 /**
71  * Configures log4j using system properties.
72  */

73 class SystemPropertyLogConfigurator {
74     private static Category jiapiRoot = Category.getInstance("alt.jiapi");
75     private static boolean initialized = false;
76
77     static void configure() {
78         if (initialized) {
79             return;
80         }
81
82         jiapiRoot.addAppender(getAppender());
83         jiapiRoot.setPriority(Priority.FATAL);
84
85         if (System.getProperty("alt.jiapi.debug") != null) {
86             initialize(System.getProperty("alt.jiapi.debug"), Priority.DEBUG);
87         }
88
89         if (System.getProperty("alt.jiapi.trace") != null) {
90             initialize(System.getProperty("alt.jiapi.trace"), Priority.INFO);
91         }
92
93         initialized = true;
94     }
95
96     static Appender getAppender() {
97         ConsoleAppender ca =
98             new ConsoleAppender(new PatternLayout("[%c] %-5p : %m\n"));
99         ca.setName("ConsoleAppender");
100         ca.setTarget("System.out");
101
102         return ca;
103     }
104
105     static Category getJiapiRoot() {
106         return jiapiRoot;
107     }
108
109     private static void initialize(String JavaDoc categoryNames, Priority logLevel) {
110         if (categoryNames.trim().equals("")) {
111             jiapiRoot.setPriority(logLevel);
112         }
113         else {
114             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(categoryNames, ",");
115             while (st.hasMoreTokens()) {
116                 String JavaDoc c = st.nextToken().trim();
117                 Category cat = Category.getInstance(c);
118                 cat.setPriority(logLevel);
119             }
120         }
121     }
122 }
123
124
Popular Tags