use of org.apache.log4j.spi.OptionHandler in project logging-log4j2 by apache.
the class PropertySetter method setProperties.
/**
* Set the properties for the object that match the
* <code>prefix</code> passed as parameter.
* @param properties The properties.
* @param prefix The prefix of the properties to use.
*/
public void setProperties(Properties properties, String prefix) {
int len = prefix.length();
for (String key : properties.stringPropertyNames()) {
// handle only properties that start with the desired prefix.
if (key.startsWith(prefix)) {
// ignore key if it contains dots after the prefix
if (key.indexOf('.', len + 1) > 0) {
continue;
}
String value = OptionConverter.findAndSubst(key, properties);
key = key.substring(len);
if (("layout".equals(key) || "errorhandler".equals(key)) && obj instanceof Appender) {
continue;
}
//
// if the property type is an OptionHandler
// (for example, triggeringPolicy of org.apache.log4j.rolling.RollingFileAppender)
PropertyDescriptor prop = getPropertyDescriptor(Introspector.decapitalize(key));
if (prop != null && OptionHandler.class.isAssignableFrom(prop.getPropertyType()) && prop.getWriteMethod() != null) {
OptionHandler opt = (OptionHandler) OptionConverter.instantiateByKey(properties, prefix + key, prop.getPropertyType(), null);
PropertySetter setter = new PropertySetter(opt);
setter.setProperties(properties, prefix + key + ".");
try {
prop.getWriteMethod().invoke(this.obj, opt);
} catch (InvocationTargetException ex) {
if (ex.getTargetException() instanceof InterruptedException || ex.getTargetException() instanceof InterruptedIOException) {
Thread.currentThread().interrupt();
}
LOGGER.warn("Failed to set property [{}] to value \"{}\".", key, value, ex);
} catch (IllegalAccessException | RuntimeException ex) {
LOGGER.warn("Failed to set property [{}] to value \"{}\".", key, value, ex);
}
continue;
}
setProperty(key, value);
}
}
activate();
}
use of org.apache.log4j.spi.OptionHandler in project logging-log4j2 by apache.
the class PropertyConfigurator method parseAppender.
Appender parseAppender(final Properties properties, final String appenderName) {
Appender appender = registryGet(appenderName);
if ((appender != null)) {
LogLog.debug("Appender \"" + appenderName + "\" was already parsed.");
return appender;
}
// Appender was not previously initialized.
final String prefix = APPENDER_PREFIX + appenderName;
final String layoutPrefix = prefix + ".layout";
appender = (Appender) OptionConverter.instantiateByKey(properties, prefix, org.apache.log4j.Appender.class, null);
if (appender == null) {
LogLog.error("Could not instantiate appender named \"" + appenderName + "\".");
return null;
}
appender.setName(appenderName);
if (appender instanceof OptionHandler) {
if (appender.requiresLayout()) {
final Layout layout = (Layout) OptionConverter.instantiateByKey(properties, layoutPrefix, Layout.class, null);
if (layout != null) {
appender.setLayout(layout);
LogLog.debug("Parsing layout options for \"" + appenderName + "\".");
// configureOptionHandler(layout, layoutPrefix + ".", props);
PropertySetter.setProperties(layout, properties, layoutPrefix + ".");
LogLog.debug("End of parsing for \"" + appenderName + "\".");
}
}
final String errorHandlerPrefix = prefix + ".errorhandler";
final String errorHandlerClass = OptionConverter.findAndSubst(errorHandlerPrefix, properties);
if (errorHandlerClass != null) {
final ErrorHandler eh = (ErrorHandler) OptionConverter.instantiateByKey(properties, errorHandlerPrefix, ErrorHandler.class, null);
if (eh != null) {
appender.setErrorHandler(eh);
LogLog.debug("Parsing errorhandler options for \"" + appenderName + "\".");
parseErrorHandler(eh, errorHandlerPrefix, properties, repository);
final Properties edited = new Properties();
final String[] keys = new String[] { errorHandlerPrefix + "." + ROOT_REF, errorHandlerPrefix + "." + LOGGER_REF, errorHandlerPrefix + "." + APPENDER_REF_TAG };
for (final Object element : properties.entrySet()) {
final Map.Entry entry = (Map.Entry) element;
int i = 0;
for (; i < keys.length; i++) {
if (keys[i].equals(entry.getKey())) {
break;
}
}
if (i == keys.length) {
edited.put(entry.getKey(), entry.getValue());
}
}
PropertySetter.setProperties(eh, edited, errorHandlerPrefix + ".");
LogLog.debug("End of errorhandler parsing for \"" + appenderName + "\".");
}
}
// configureOptionHandler((OptionHandler) appender, prefix + ".", props);
PropertySetter.setProperties(appender, properties, prefix + ".");
LogLog.debug("Parsed \"" + appenderName + "\" options.");
}
parseAppenderFilters(properties, appenderName, appender);
registryPut(appender);
return appender;
}
Aggregations