org.apache.juli.logging.ch.qos.logback.core.model.processor.StatusListenerModelHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tomcat85-slf4j-logback Show documentation
Show all versions of tomcat85-slf4j-logback Show documentation
Tomcat85 Slf4j Logback Integration
package org.apache.juli.logging.ch.qos.logback.core.model.processor;
import org.apache.juli.logging.ch.qos.logback.core.Context;
import org.apache.juli.logging.ch.qos.logback.core.model.Model;
import org.apache.juli.logging.ch.qos.logback.core.model.StatusListenerModel;
import org.apache.juli.logging.ch.qos.logback.core.spi.ContextAware;
import org.apache.juli.logging.ch.qos.logback.core.spi.LifeCycle;
import org.apache.juli.logging.ch.qos.logback.core.status.StatusListener;
import org.apache.juli.logging.ch.qos.logback.core.util.OptionHelper;
public class StatusListenerModelHandler extends ModelHandlerBase {
boolean inError = false;
Boolean effectivelyAdded = null;
StatusListener statusListener = null;
public StatusListenerModelHandler(Context context) {
super(context);
}
static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) {
return new StatusListenerModelHandler(context);
}
@Override
protected Class getSupportedModelClass() {
return StatusListenerModel.class;
}
@Override
public void handle(ModelInterpretationContext ic, Model model) throws ModelHandlerException {
StatusListenerModel slModel = (StatusListenerModel) model;
String className = slModel.getClassName();
if (OptionHelper.isNullOrEmpty(className)) {
addError("Empty class name for StatusListener");
inError = true;
return;
} else {
className = ic.getImport(className);
}
try {
statusListener = (StatusListener) OptionHelper.instantiateByClassName(className, StatusListener.class,
context);
effectivelyAdded = ic.getContext().getStatusManager().add(statusListener);
if (statusListener instanceof ContextAware) {
((ContextAware) statusListener).setContext(context);
}
addInfo("Added status listener of type [" + slModel.getClassName() + "]");
ic.pushObject(statusListener);
} catch (Exception e) {
inError = true;
addError("Could not create an StatusListener of type [" + slModel.getClassName() + "].", e);
throw new ModelHandlerException(e);
}
}
@Override
public void postHandle(ModelInterpretationContext mic, Model m) {
if (inError) {
return;
}
if (isEffectivelyAdded() && statusListener instanceof LifeCycle) {
((LifeCycle) statusListener).start();
}
Object o = mic.peekObject();
if (o != statusListener) {
addWarn("The object at the of the stack is not the statusListener pushed earlier.");
} else {
mic.popObject();
}
}
private boolean isEffectivelyAdded() {
if (effectivelyAdded == null)
return false;
return effectivelyAdded;
}
}