/*
* Copyright (C) 2001 Mika Riekkinen, Joni Suominen
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package alt.jiapi;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import org.apache.log4j.Appender;
import org.apache.log4j.Category;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Priority;
//import org.apache.log4j.SimpleLayout;
import org.apache.log4j.PatternLayout;
/**
* This class is a class that holds mainly static common methods,
* which are used by other Jiapi classes.
*
* @author Mika Riekkinen
* @author Joni Suominen
* @version $Revision: 1.8 $ $Date: 2004/03/02 10:11:00 $
*/
public class Runtime {
private static Map fieldValues = new HashMap();
public static synchronized void setFieldValue(String fieldName,
Object value) {
fieldValues.put(fieldName, value);
}
public static synchronized Object getFieldValue(String fieldName) {
Object o = fieldValues.get(fieldName);
//System.out.println("GetFieldValue " + fieldName + ": " + o);
return o;
}
// NOTE! Since the above method uses just a field name as an index,
// would the following method make sense?
// public static String createUniqueFieldName();
public static Category getRootLogCategory() {
return SystemPropertyLogConfigurator.getJiapiRoot();
}
public static Category getLogCategory(Class clazz) {
SystemPropertyLogConfigurator.configure();
return Category.getInstance(clazz);
}
}
/**
* Configures log4j using system properties.
*/
class SystemPropertyLogConfigurator {
private static Category jiapiRoot = Category.getInstance("alt.jiapi");
private static boolean initialized = false;
static void configure() {
if (initialized) {
return;
}
jiapiRoot.addAppender(getAppender());
jiapiRoot.setPriority(Priority.FATAL);
if (System.getProperty("alt.jiapi.debug") != null) {
initialize(System.getProperty("alt.jiapi.debug"), Priority.DEBUG);
}
if (System.getProperty("alt.jiapi.trace") != null) {
initialize(System.getProperty("alt.jiapi.trace"), Priority.INFO);
}
initialized = true;
}
static Appender getAppender() {
ConsoleAppender ca =
new ConsoleAppender(new PatternLayout("[%c] %-5p : %m\n"));
ca.setName("ConsoleAppender");
ca.setTarget("System.out");
return ca;
}
static Category getJiapiRoot() {
return jiapiRoot;
}
private static void initialize(String categoryNames, Priority logLevel) {
if (categoryNames.trim().equals("")) {
jiapiRoot.setPriority(logLevel);
}
else {
StringTokenizer st = new StringTokenizer(categoryNames, ",");
while (st.hasMoreTokens()) {
String c = st.nextToken().trim();
Category cat = Category.getInstance(c);
cat.setPriority(logLevel);
}
}
}
}