Commit c58f195f by Sven de Marothy Committed by Michael Koch

LogManager.java: Reformatted.

2005-04-22  Sven de Marothy  <sven@physto.se>

	* java/util/logging/LogManager.java: Reformatted.
	(readConfiguration): If a logger for a key is not found, create one.

From-SVN: r98543
parent 6a15d30b
2005-04-22 Sven de Marothy <sven@physto.se>
* java/util/logging/LogManager.java: Reformatted.
(readConfiguration): If a logger for a key is not found, create one.
2005-04-22 Andrew John Hughes <gnu_andrew@member.fsf.org> 2005-04-22 Andrew John Hughes <gnu_andrew@member.fsf.org>
* java/rmi/Naming.java: * java/rmi/Naming.java:
......
...@@ -103,17 +103,14 @@ public class LogManager ...@@ -103,17 +103,14 @@ public class LogManager
* The singleton LogManager instance. * The singleton LogManager instance.
*/ */
private static LogManager logManager; private static LogManager logManager;
/** /**
* The registered named loggers; maps the name of a Logger to * The registered named loggers; maps the name of a Logger to
* a WeakReference to it. * a WeakReference to it.
*/ */
private Map loggers; private Map loggers;
final Logger rootLogger; final Logger rootLogger;
/** /**
* The properties for the logging framework which have been * The properties for the logging framework which have been
* read in last. * read in last.
...@@ -132,20 +129,19 @@ public class LogManager ...@@ -132,20 +129,19 @@ public class LogManager
* behave differently from the reference implementation in * behave differently from the reference implementation in
* this case. * this case.
*/ */
private final PropertyChangeSupport pcs private final PropertyChangeSupport pcs = new PropertyChangeSupport( /* source bean */
= new PropertyChangeSupport(/* source bean */ LogManager.class); LogManager.class);
protected LogManager() protected LogManager()
{ {
if (logManager != null) if (logManager != null)
throw new IllegalStateException( throw new IllegalStateException("there can be only one LogManager; use LogManager.getLogManager()");
"there can be only one LogManager; use LogManager.getLogManager()");
logManager = this; logManager = this;
loggers = new java.util.HashMap(); loggers = new java.util.HashMap();
rootLogger = new Logger("", null); rootLogger = new Logger("", null);
addLogger(rootLogger); addLogger(rootLogger);
/* Make sure that Logger.global has the rootLogger as its parent. /* Make sure that Logger.global has the rootLogger as its parent.
* *
* Logger.global is set during class initialization of Logger, * Logger.global is set during class initialization of Logger,
...@@ -166,7 +162,6 @@ public class LogManager ...@@ -166,7 +162,6 @@ public class LogManager
Logger.getLogger("global").setUseParentHandlers(true); Logger.getLogger("global").setUseParentHandlers(true);
} }
/** /**
* Returns the globally shared LogManager instance. * Returns the globally shared LogManager instance.
*/ */
...@@ -176,32 +171,30 @@ public class LogManager ...@@ -176,32 +171,30 @@ public class LogManager
} }
static static
{
makeLogManager();
/* The Javadoc description of the class explains
* what is going on here.
*/
Object configurator = createInstance(
System.getProperty("java.util.logging.config.class"),
/* must be instance of */ Object.class);
try
{
if (configurator == null)
getLogManager().readConfiguration();
}
catch (IOException ex)
{ {
/* FIXME: Is it ok to ignore exceptions here? */ makeLogManager();
/* The Javadoc description of the class explains
* what is going on here.
*/
Object configurator = createInstance(System.getProperty("java.util.logging.config.class"),
/* must be instance of */ Object.class);
try
{
if (configurator == null)
getLogManager().readConfiguration();
}
catch (IOException ex)
{
/* FIXME: Is it ok to ignore exceptions here? */
}
} }
}
private static LogManager makeLogManager() private static LogManager makeLogManager()
{ {
String managerClassName; String managerClassName;
LogManager manager; LogManager manager;
managerClassName = System.getProperty("java.util.logging.manager"); managerClassName = System.getProperty("java.util.logging.manager");
manager = (LogManager) createInstance(managerClassName, LogManager.class); manager = (LogManager) createInstance(managerClassName, LogManager.class);
...@@ -210,12 +203,11 @@ public class LogManager ...@@ -210,12 +203,11 @@ public class LogManager
if (managerClassName != null) if (managerClassName != null)
System.err.println("WARNING: System property \"java.util.logging.manager\"" System.err.println("WARNING: System property \"java.util.logging.manager\""
+ " should be the name of a subclass of java.util.logging.LogManager"); + " should be the name of a subclass of java.util.logging.LogManager");
return new LogManager(); return new LogManager();
} }
/** /**
* Registers a listener which will be notified when the * Registers a listener which will be notified when the
* logging properties are re-read. * logging properties are re-read.
...@@ -228,7 +220,6 @@ public class LogManager ...@@ -228,7 +220,6 @@ public class LogManager
pcs.addPropertyChangeListener(listener); pcs.addPropertyChangeListener(listener);
} }
/** /**
* Unregisters a listener. * Unregisters a listener.
* *
...@@ -242,7 +233,6 @@ public class LogManager ...@@ -242,7 +233,6 @@ public class LogManager
pcs.removePropertyChangeListener(listener); pcs.removePropertyChangeListener(listener);
} }
/** /**
* Adds a named logger. If a logger with the same name has * Adds a named logger. If a logger with the same name has
* already been registered, the method returns <code>false</code> * already been registered, the method returns <code>false</code>
...@@ -271,9 +261,8 @@ public class LogManager ...@@ -271,9 +261,8 @@ public class LogManager
* that LogManager does its synchronization on the globally * that LogManager does its synchronization on the globally
* shared instance of LogManager. * shared instance of LogManager.
*/ */
String name; String name;
WeakReference ref; WeakReference ref;
/* This will throw a NullPointerException if logger is null, /* This will throw a NullPointerException if logger is null,
* as required by the API specification. * as required by the API specification.
...@@ -282,18 +271,18 @@ public class LogManager ...@@ -282,18 +271,18 @@ public class LogManager
ref = (WeakReference) loggers.get(name); ref = (WeakReference) loggers.get(name);
if (ref != null) if (ref != null)
{ {
if (ref.get() != null) if (ref.get() != null)
return false; return false;
/* There has been a logger under this name in the past, /* There has been a logger under this name in the past,
* but it has been garbage collected. * but it has been garbage collected.
*/ */
loggers.remove(ref); loggers.remove(ref);
} }
/* Adding a named logger requires a security permission. */ /* Adding a named logger requires a security permission. */
if ((name != null) && !name.equals("")) if ((name != null) && ! name.equals(""))
checkAccess(); checkAccess();
Logger parent = findAncestor(logger); Logger parent = findAncestor(logger);
...@@ -308,27 +297,28 @@ public class LogManager ...@@ -308,27 +297,28 @@ public class LogManager
* its parent to "foo.bar". * its parent to "foo.bar".
*/ */
if (parent != rootLogger) if (parent != rootLogger)
{
for (Iterator iter = loggers.keySet().iterator(); iter.hasNext();)
{ {
Logger possChild = (Logger) ((WeakReference) loggers.get(iter.next())).get(); for (Iterator iter = loggers.keySet().iterator(); iter.hasNext();)
if ((possChild == null) || (possChild == logger) || (possChild.getParent() != parent)) {
continue; Logger possChild = (Logger) ((WeakReference) loggers.get(iter.next()))
.get();
if ((possChild == null) || (possChild == logger)
|| (possChild.getParent() != parent))
continue;
if (!possChild.getName().startsWith(name)) if (! possChild.getName().startsWith(name))
continue; continue;
if (possChild.getName().charAt(name.length()) != '.') if (possChild.getName().charAt(name.length()) != '.')
continue; continue;
possChild.setParent(logger); possChild.setParent(logger);
}
} }
}
return true; return true;
} }
/** /**
* Finds the closest ancestor for a logger among the currently * Finds the closest ancestor for a logger among the currently
* registered ones. For example, if the currently registered * registered ones. For example, if the currently registered
...@@ -348,40 +338,39 @@ public class LogManager ...@@ -348,40 +338,39 @@ public class LogManager
private synchronized Logger findAncestor(Logger child) private synchronized Logger findAncestor(Logger child)
{ {
String childName = child.getName(); String childName = child.getName();
int childNameLength = childName.length(); int childNameLength = childName.length();
Logger best = rootLogger; Logger best = rootLogger;
int bestNameLength = 0; int bestNameLength = 0;
Logger cand; Logger cand;
String candName; String candName;
int candNameLength; int candNameLength;
if (child == rootLogger) if (child == rootLogger)
return null; return null;
for (Iterator iter = loggers.keySet().iterator(); iter.hasNext();) for (Iterator iter = loggers.keySet().iterator(); iter.hasNext();)
{
candName = (String) iter.next();
candNameLength = candName.length();
if (candNameLength > bestNameLength
&& childNameLength > candNameLength
&& childName.startsWith(candName)
&& childName.charAt(candNameLength) == '.')
{ {
cand = (Logger) ((WeakReference) loggers.get(candName)).get(); candName = (String) iter.next();
if ((cand == null) || (cand == child)) candNameLength = candName.length();
continue;
if (candNameLength > bestNameLength
&& childNameLength > candNameLength
&& childName.startsWith(candName)
&& childName.charAt(candNameLength) == '.')
{
cand = (Logger) ((WeakReference) loggers.get(candName)).get();
if ((cand == null) || (cand == child))
continue;
bestNameLength = candName.length(); bestNameLength = candName.length();
best = cand; best = cand;
}
} }
}
return best; return best;
} }
/** /**
* Returns a Logger given its name. * Returns a Logger given its name.
* *
...@@ -395,7 +384,7 @@ public class LogManager ...@@ -395,7 +384,7 @@ public class LogManager
*/ */
public synchronized Logger getLogger(String name) public synchronized Logger getLogger(String name)
{ {
WeakReference ref; WeakReference ref;
/* Throw a NullPointerException if name is null. */ /* Throw a NullPointerException if name is null. */
name.getClass(); name.getClass();
...@@ -407,7 +396,6 @@ public class LogManager ...@@ -407,7 +396,6 @@ public class LogManager
return null; return null;
} }
/** /**
* Returns an Enumeration of currently registered Logger names. * Returns an Enumeration of currently registered Logger names.
* Since other threads can register loggers at any time, the * Since other threads can register loggers at any time, the
...@@ -421,7 +409,6 @@ public class LogManager ...@@ -421,7 +409,6 @@ public class LogManager
return Collections.enumeration(loggers.keySet()); return Collections.enumeration(loggers.keySet());
} }
/** /**
* Resets the logging configuration by removing all handlers for * Resets the logging configuration by removing all handlers for
* registered named loggers and setting their level to <code>null</code>. * registered named loggers and setting their level to <code>null</code>.
...@@ -431,8 +418,7 @@ public class LogManager ...@@ -431,8 +418,7 @@ public class LogManager
* the caller is not granted the permission to control * the caller is not granted the permission to control
* the logging infrastructure. * the logging infrastructure.
*/ */
public synchronized void reset() public synchronized void reset() throws SecurityException
throws SecurityException
{ {
/* Throw a SecurityException if the caller does not have the /* Throw a SecurityException if the caller does not have the
* permission to control the logging infrastructure. * permission to control the logging infrastructure.
...@@ -443,26 +429,25 @@ public class LogManager ...@@ -443,26 +429,25 @@ public class LogManager
Iterator iter = loggers.values().iterator(); Iterator iter = loggers.values().iterator();
while (iter.hasNext()) while (iter.hasNext())
{
WeakReference ref;
Logger logger;
ref = (WeakReference) iter.next();
if (ref != null)
{ {
logger = (Logger) ref.get(); WeakReference ref;
Logger logger;
if (logger == null) ref = (WeakReference) iter.next();
iter.remove(); if (ref != null)
else if (logger != rootLogger) {
logger.setLevel(null); logger = (Logger) ref.get();
if (logger == null)
iter.remove();
else if (logger != rootLogger)
logger.setLevel(null);
}
} }
}
rootLogger.setLevel(Level.INFO); rootLogger.setLevel(Level.INFO);
} }
/** /**
* Configures the logging framework by reading a configuration file. * Configures the logging framework by reading a configuration file.
* The name and location of this file are specified by the system * The name and location of this file are specified by the system
...@@ -488,97 +473,99 @@ public class LogManager ...@@ -488,97 +473,99 @@ public class LogManager
public synchronized void readConfiguration() public synchronized void readConfiguration()
throws IOException, SecurityException throws IOException, SecurityException
{ {
String path; String path;
InputStream inputStream; InputStream inputStream;
path = System.getProperty("java.util.logging.config.file"); path = System.getProperty("java.util.logging.config.file");
if ((path == null) || (path.length() == 0)) if ((path == null) || (path.length() == 0))
{ {
String url = (System.getProperty("gnu.classpath.home.url") String url = (System.getProperty("gnu.classpath.home.url")
+ "/logging.properties"); + "/logging.properties");
inputStream = new URL(url).openStream(); inputStream = new URL(url).openStream();
} }
else else
{
inputStream = new java.io.FileInputStream(path); inputStream = new java.io.FileInputStream(path);
}
try try
{ {
readConfiguration(inputStream); readConfiguration(inputStream);
} }
finally finally
{ {
/* Close the stream in order to save /* Close the stream in order to save
* resources such as file descriptors. * resources such as file descriptors.
*/ */
inputStream.close(); inputStream.close();
} }
} }
public synchronized void readConfiguration(InputStream inputStream) public synchronized void readConfiguration(InputStream inputStream)
throws IOException, SecurityException throws IOException, SecurityException
{ {
Properties newProperties; Properties newProperties;
Enumeration keys; Enumeration keys;
checkAccess(); checkAccess();
newProperties = new Properties(); newProperties = new Properties();
newProperties.load(inputStream); newProperties.load(inputStream);
this.properties = newProperties; this.properties = newProperties;
keys = newProperties.propertyNames(); keys = newProperties.propertyNames();
while (keys.hasMoreElements()) while (keys.hasMoreElements())
{
String key = ((String) keys.nextElement()).trim();
String value = newProperties.getProperty(key);
if (value == null)
continue;
value = value.trim();
if("handlers".equals(key))
{
StringTokenizer tokenizer = new StringTokenizer(value);
while(tokenizer.hasMoreTokens())
{
String handlerName = tokenizer.nextToken();
try
{
Class handlerClass = Class.forName(handlerName);
getLogger("").addHandler((Handler)handlerClass.newInstance());
}
catch (ClassCastException ex)
{
System.err.println("[LogManager] class " + handlerName + " is not subclass of java.util.logging.Handler");
}
catch (Exception ex)
{
//System.out.println("[LogManager.readConfiguration]"+ex);
}
}
}
if (key.endsWith(".level"))
{ {
String loggerName = key.substring(0, key.length() - 6); String key = ((String) keys.nextElement()).trim();
Logger logger = getLogger(loggerName); String value = newProperties.getProperty(key);
if (logger != null)
{ if (value == null)
try continue;
value = value.trim();
if ("handlers".equals(key))
{ {
logger.setLevel(Level.parse(value)); StringTokenizer tokenizer = new StringTokenizer(value);
while (tokenizer.hasMoreTokens())
{
String handlerName = tokenizer.nextToken();
try
{
Class handlerClass = Class.forName(handlerName);
getLogger("").addHandler((Handler) handlerClass
.newInstance());
}
catch (ClassCastException ex)
{
System.err.println("[LogManager] class " + handlerName
+ " is not subclass of java.util.logging.Handler");
}
catch (Exception ex)
{
//System.out.println("[LogManager.readConfiguration]"+ex);
}
}
} }
catch (Exception _)
if (key.endsWith(".level"))
{ {
//System.out.println("[LogManager.readConfiguration] "+_); String loggerName = key.substring(0, key.length() - 6);
Logger logger = getLogger(loggerName);
if (logger == null)
{
logger = Logger.getLogger(loggerName);
addLogger(logger);
}
try
{
logger.setLevel(Level.parse(value));
}
catch (Exception _)
{
//System.out.println("[LogManager.readConfiguration] "+_);
}
continue;
} }
continue;
}
} }
}
/* The API specification does not talk about the /* The API specification does not talk about the
* property name that is distributed with the * property name that is distributed with the
...@@ -589,7 +576,6 @@ public class LogManager ...@@ -589,7 +576,6 @@ public class LogManager
pcs.firePropertyChange(null, null, null); pcs.firePropertyChange(null, null, null);
} }
/** /**
* Returns the value of a configuration property as a String. * Returns the value of a configuration property as a String.
*/ */
...@@ -601,7 +587,6 @@ public class LogManager ...@@ -601,7 +587,6 @@ public class LogManager
return null; return null;
} }
/** /**
* Returns the value of a configuration property as an integer. * Returns the value of a configuration property as an integer.
* This function is a helper used by the Classpath implementation * This function is a helper used by the Classpath implementation
...@@ -617,16 +602,15 @@ public class LogManager ...@@ -617,16 +602,15 @@ public class LogManager
static int getIntProperty(String name, int defaultValue) static int getIntProperty(String name, int defaultValue)
{ {
try try
{ {
return Integer.parseInt(getLogManager().getProperty(name)); return Integer.parseInt(getLogManager().getProperty(name));
} }
catch (Exception ex) catch (Exception ex)
{ {
return defaultValue; return defaultValue;
} }
} }
/** /**
* Returns the value of a configuration property as an integer, * Returns the value of a configuration property as an integer,
* provided it is inside the acceptable range. * provided it is inside the acceptable range.
...@@ -646,7 +630,7 @@ public class LogManager ...@@ -646,7 +630,7 @@ public class LogManager
* or if it is greater than the maximum value. * or if it is greater than the maximum value.
*/ */
static int getIntPropertyClamped(String name, int defaultValue, static int getIntPropertyClamped(String name, int defaultValue,
int minValue, int maxValue) int minValue, int maxValue)
{ {
int val = getIntProperty(name, defaultValue); int val = getIntProperty(name, defaultValue);
if ((val < minValue) || (val > maxValue)) if ((val < minValue) || (val > maxValue))
...@@ -654,7 +638,6 @@ public class LogManager ...@@ -654,7 +638,6 @@ public class LogManager
return val; return val;
} }
/** /**
* Returns the value of a configuration property as a boolean. * Returns the value of a configuration property as a boolean.
* This function is a helper used by the Classpath implementation * This function is a helper used by the Classpath implementation
...@@ -670,17 +653,15 @@ public class LogManager ...@@ -670,17 +653,15 @@ public class LogManager
static boolean getBooleanProperty(String name, boolean defaultValue) static boolean getBooleanProperty(String name, boolean defaultValue)
{ {
try try
{ {
return (new Boolean(getLogManager().getProperty(name))) return (new Boolean(getLogManager().getProperty(name))).booleanValue();
.booleanValue(); }
}
catch (Exception ex) catch (Exception ex)
{ {
return defaultValue; return defaultValue;
} }
} }
/** /**
* Returns the value of a configuration property as a Level. * Returns the value of a configuration property as a Level.
* This function is a helper used by the Classpath implementation * This function is a helper used by the Classpath implementation
...@@ -697,16 +678,15 @@ public class LogManager ...@@ -697,16 +678,15 @@ public class LogManager
static Level getLevelProperty(String propertyName, Level defaultValue) static Level getLevelProperty(String propertyName, Level defaultValue)
{ {
try try
{ {
return Level.parse(getLogManager().getProperty(propertyName)); return Level.parse(getLogManager().getProperty(propertyName));
} }
catch (Exception ex) catch (Exception ex)
{ {
return defaultValue; return defaultValue;
} }
} }
/** /**
* Returns the value of a configuration property as a Class. * Returns the value of a configuration property as a Class.
* This function is a helper used by the Classpath implementation * This function is a helper used by the Classpath implementation
...@@ -724,64 +704,60 @@ public class LogManager ...@@ -724,64 +704,60 @@ public class LogManager
Class usingClass = null; Class usingClass = null;
try try
{ {
String propertyValue = logManager.getProperty(propertyName); String propertyValue = logManager.getProperty(propertyName);
if (propertyValue != null) if (propertyValue != null)
usingClass = Class.forName(propertyValue); usingClass = Class.forName(propertyValue);
if (usingClass != null) if (usingClass != null)
return usingClass; return usingClass;
} }
catch (Exception _) catch (Exception _)
{ {
} }
return defaultValue; return defaultValue;
} }
static final Object getInstanceProperty(String propertyName, Class ofClass,
static final Object getInstanceProperty(String propertyName, Class defaultClass)
Class ofClass,
Class defaultClass)
{ {
Class klass = getClassProperty(propertyName, defaultClass); Class klass = getClassProperty(propertyName, defaultClass);
if (klass == null) if (klass == null)
return null; return null;
try try
{ {
Object obj = klass.newInstance(); Object obj = klass.newInstance();
if (ofClass.isInstance(obj)) if (ofClass.isInstance(obj))
return obj; return obj;
} }
catch (Exception _) catch (Exception _)
{ {
} }
if (defaultClass == null) if (defaultClass == null)
return null; return null;
try try
{ {
return defaultClass.newInstance(); return defaultClass.newInstance();
} }
catch (java.lang.InstantiationException ex) catch (java.lang.InstantiationException ex)
{ {
throw new RuntimeException(ex.getMessage()); throw new RuntimeException(ex.getMessage());
} }
catch (java.lang.IllegalAccessException ex) catch (java.lang.IllegalAccessException ex)
{ {
throw new RuntimeException(ex.getMessage()); throw new RuntimeException(ex.getMessage());
} }
} }
/** /**
* An instance of <code>LoggingPermission("control")</code> * An instance of <code>LoggingPermission("control")</code>
* that is shared between calls to <code>checkAccess()</code>. * that is shared between calls to <code>checkAccess()</code>.
*/ */
private static final LoggingPermission controlPermission private static final LoggingPermission controlPermission = new LoggingPermission("control",
= new LoggingPermission("control", null); null);
/** /**
* Checks whether the current security context allows changing * Checks whether the current security context allows changing
...@@ -793,21 +769,19 @@ public class LogManager ...@@ -793,21 +769,19 @@ public class LogManager
* the caller is not granted the permission to control * the caller is not granted the permission to control
* the logging infrastructure. * the logging infrastructure.
*/ */
public void checkAccess() public void checkAccess() throws SecurityException
throws SecurityException
{ {
SecurityManager sm = System.getSecurityManager(); SecurityManager sm = System.getSecurityManager();
if (sm != null) if (sm != null)
sm.checkPermission(controlPermission); sm.checkPermission(controlPermission);
} }
/**
/**
* Creates a new instance of a class specified by name. * Creates a new instance of a class specified by name.
* *
* @param className the name of the class of which a new instance * @param className the name of the class of which a new instance
* should be created. * should be created.
* *
* @param ofClass the class to which the new instance should * @param ofClass the class to which the new instance should
* be either an instance or an instance of a subclass. * be either an instance or an instance of a subclass.
* FIXME: This description is just terrible. * FIXME: This description is just terrible.
...@@ -820,26 +794,26 @@ public class LogManager ...@@ -820,26 +794,26 @@ public class LogManager
*/ */
static final Object createInstance(String className, Class ofClass) static final Object createInstance(String className, Class ofClass)
{ {
Class klass; Class klass;
if ((className == null) || (className.length() == 0)) if ((className == null) || (className.length() == 0))
return null; return null;
try try
{ {
klass = Class.forName(className); klass = Class.forName(className);
if (!ofClass.isAssignableFrom(klass)) if (! ofClass.isAssignableFrom(klass))
return null; return null;
return klass.newInstance(); return klass.newInstance();
} }
catch (Exception _) catch (Exception _)
{ {
return null; return null;
} }
catch (java.lang.LinkageError _) catch (java.lang.LinkageError _)
{ {
return null; return null;
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment