logmanager.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 822 行 · 第 1/2 页
JAVA
822 行
/**
* Returns an Enumeration of currently registered Logger names.
* Since other threads can register loggers at any time, the
* result could be different any time this method is called.
*
* @return an Enumeration with the names of the currently
* registered Loggers.
*/
public synchronized Enumeration getLoggerNames()
{
return Collections.enumeration(loggers.keySet());
}
/**
* Resets the logging configuration by removing all handlers for
* registered named loggers and setting their level to <code>null</code>.
* The level of the root logger will be set to <code>Level.INFO</code>.
*
* @throws SecurityException if a security manager exists and
* the caller is not granted the permission to control
* the logging infrastructure.
*/
public synchronized void reset()
throws SecurityException
{
/* Throw a SecurityException if the caller does not have the
* permission to control the logging infrastructure.
*/
checkAccess();
properties = new Properties();
Iterator iter = loggers.values().iterator();
while (iter.hasNext())
{
WeakReference ref;
Logger logger;
ref = (WeakReference) iter.next();
if (ref != null)
{
logger = (Logger) ref.get();
if (logger == null)
iter.remove();
else if (logger != rootLogger)
logger.setLevel(null);
}
}
rootLogger.setLevel(Level.INFO);
}
/**
* Configures the logging framework by reading a configuration file.
* The name and location of this file are specified by the system
* property <code>java.util.logging.config.file</code>. If this
* property is not set, the URL
* "{gnu.classpath.home.url}/logging.properties" is taken, where
* "{gnu.classpath.home.url}" stands for the value of the system
* property <code>gnu.classpath.home.url</code>.
*
* <p>The task of configuring the framework is then delegated to
* {@link #readConfiguration(java.io.InputStream)}, which will
* notify registered listeners after having read the properties.
*
* @throws SecurityException if a security manager exists and
* the caller is not granted the permission to control
* the logging infrastructure, or if the caller is
* not granted the permission to read the configuration
* file.
*
* @throws IOException if there is a problem reading in the
* configuration file.
*/
public synchronized void readConfiguration()
throws IOException, SecurityException
{
String path;
InputStream inputStream;
path = System.getProperty("java.util.logging.config.file");
if ((path == null) || (path.length() == 0))
{
String url = (System.getProperty("gnu.classpath.home.url")
+ "/logging.properties");
inputStream = new URL(url).openStream();
}
else
{
inputStream = new java.io.FileInputStream(path);
}
try
{
readConfiguration(inputStream);
}
finally
{
/* Close the stream in order to save
* resources such as file descriptors.
*/
inputStream.close();
}
}
public synchronized void readConfiguration(InputStream inputStream)
throws IOException, SecurityException
{
Properties newProperties;
Enumeration keys;
checkAccess();
newProperties = new Properties();
newProperties.load(inputStream);
this.properties = newProperties;
keys = newProperties.propertyNames();
while (keys.hasMoreElements())
{
String key = (String) keys.nextElement();
String value = newProperties.getProperty(key);
if (value == null)
continue;
if (key.endsWith(".level"))
{
String loggerName = key.substring(0, key.length() - 6);
Logger logger = getLogger(loggerName);
if (logger != null)
{
try
{
logger.setLevel(Level.parse(value));
}
catch (Exception _)
{
}
continue;
}
}
}
/* The API specification does not talk about the
* property name that is distributed with the
* PropertyChangeEvent. With test code, it could
* be determined that the Sun J2SE 1.4 reference
* implementation uses null for the property name.
*/
pcs.firePropertyChange(null, null, null);
}
/**
* Returns the value of a configuration property as a String.
*/
public synchronized String getProperty(String name)
{
if (properties != null)
return properties.getProperty(name);
else
return null;
}
/**
* Returns the value of a configuration property as an integer.
* This function is a helper used by the Classpath implementation
* of java.util.logging, it is <em>not</em> specified in the
* logging API.
*
* @param name the name of the configuration property.
*
* @param defaultValue the value that will be returned if the
* property is not defined, or if its value is not an integer
* number.
*/
static int getIntProperty(String name, int defaultValue)
{
try
{
return Integer.parseInt(getLogManager().getProperty(name));
}
catch (Exception ex)
{
return defaultValue;
}
}
/**
* Returns the value of a configuration property as an integer,
* provided it is inside the acceptable range.
* This function is a helper used by the Classpath implementation
* of java.util.logging, it is <em>not</em> specified in the
* logging API.
*
* @param name the name of the configuration property.
*
* @param minValue the lowest acceptable value.
*
* @param maxValue the highest acceptable value.
*
* @param defaultValue the value that will be returned if the
* property is not defined, or if its value is not an integer
* number, or if it is less than the minimum value,
* or if it is greater than the maximum value.
*/
static int getIntPropertyClamped(String name, int defaultValue,
int minValue, int maxValue)
{
int val = getIntProperty(name, defaultValue);
if ((val < minValue) || (val > maxValue))
val = defaultValue;
return val;
}
/**
* Returns the value of a configuration property as a boolean.
* This function is a helper used by the Classpath implementation
* of java.util.logging, it is <em>not</em> specified in the
* logging API.
*
* @param name the name of the configuration property.
*
* @param defaultValue the value that will be returned if the
* property is not defined, or if its value is neither
* <code>"true"</code> nor <code>"false"</code>.
*/
static boolean getBooleanProperty(String name, boolean defaultValue)
{
try
{
return (new Boolean(getLogManager().getProperty(name)))
.booleanValue();
}
catch (Exception ex)
{
return defaultValue;
}
}
/**
* Returns the value of a configuration property as a Level.
* This function is a helper used by the Classpath implementation
* of java.util.logging, it is <em>not</em> specified in the
* logging API.
*
* @param propertyName the name of the configuration property.
*
* @param defaultValue the value that will be returned if the
* property is not defined, or if
* {@link Level.parse(java.lang.String)} does not like
* the property value.
*/
static Level getLevelProperty(String propertyName, Level defaultValue)
{
try
{
return Level.parse(getLogManager().getProperty(propertyName));
}
catch (Exception ex)
{
return defaultValue;
}
}
/**
* Returns the value of a configuration property as a Class.
* This function is a helper used by the Classpath implementation
* of java.util.logging, it is <em>not</em> specified in the
* logging API.
*
* @param propertyName the name of the configuration property.
*
* @param defaultValue the value that will be returned if the
* property is not defined, or if it does not specify
* the name of a loadable class.
*/
static final Class getClassProperty(String propertyName, Class defaultValue)
{
Class usingClass = null;
try
{
String propertyValue = logManager.getProperty(propertyName);
if (propertyValue != null)
usingClass = Class.forName(propertyValue);
if (usingClass != null)
return usingClass;
}
catch (Exception _)
{
}
return defaultValue;
}
static final Object getInstanceProperty(String propertyName,
Class ofClass,
Class defaultClass)
{
Class klass = getClassProperty(propertyName, defaultClass);
if (klass == null)
return null;
try
{
Object obj = klass.newInstance();
if (ofClass.isInstance(obj))
return obj;
}
catch (Exception _)
{
}
if (defaultClass == null)
return null;
try
{
return defaultClass.newInstance();
}
catch (java.lang.InstantiationException ex)
{
throw new RuntimeException(ex.getMessage());
}
catch (java.lang.IllegalAccessException ex)
{
throw new RuntimeException(ex.getMessage());
}
}
/**
* An instance of <code>LoggingPermission("control")</code>
* that is shared between calls to <code>checkAccess()</code>.
*/
private static final LoggingPermission controlPermission
= new LoggingPermission("control", null);
/**
* Checks whether the current security context allows changing
* the configuration of the logging framework. For the security
* context to be trusted, it has to be granted
* a LoggingPermission("control").
*
* @throws SecurityException if a security manager exists and
* the caller is not granted the permission to control
* the logging infrastructure.
*/
public void checkAccess()
throws SecurityException
{
SecurityManager sm = System.getSecurityManager();
if (sm != null)
sm.checkPermission(controlPermission);
}
/**
* Creates a new instance of a class specified by name.
*
* @param className the name of the class of which a new instance
* should be created.
*
* @param ofClass the class to which the new instance should
* be either an instance or an instance of a subclass.
* FIXME: This description is just terrible.
*
* @return the new instance, or <code>null</code> if
* <code>className</code> is <code>null</code>, if no class
* with that name could be found, if there was an error
* loading that class, or if the constructor of the class
* has thrown an exception.
*/
static final Object createInstance(String className, Class ofClass)
{
Class klass;
if ((className == null) || (className.length() == 0))
return null;
try
{
klass = Class.forName(className);
if (!ofClass.isAssignableFrom(klass))
return null;
return klass.newInstance();
}
catch (Exception _)
{
return null;
}
catch (java.lang.LinkageError _)
{
return null;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?