1 package ch.qos.logback.core.model.processor; 2 3 import static ch.qos.logback.core.joran.action.Action.CLASS_ATTRIBUTE; 4 5 import java.util.Map; 6 7 import ch.qos.logback.core.Context; 8 import ch.qos.logback.core.CoreConstants; 9 import ch.qos.logback.core.boolex.EventEvaluator; 10 import ch.qos.logback.core.joran.spi.DefaultNestedComponentRegistry; 11 import ch.qos.logback.core.model.EventEvaluatorModel; 12 import ch.qos.logback.core.model.Model; 13 import ch.qos.logback.core.spi.LifeCycle; 14 import ch.qos.logback.core.util.OptionHelper; 15 16 public class EventEvaluatorModelHandler extends ModelHandlerBase { 17 18 EventEvaluator<?> evaluator; 19 boolean inError = false; 20 21 public EventEvaluatorModelHandler(Context context) { 22 super(context); 23 } 24 25 static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) { 26 return new EventEvaluatorModelHandler(context); 27 } 28 29 @Override 30 protected Class<EventEvaluatorModel> getSupportedModelClass() { 31 return EventEvaluatorModel.class; 32 } 33 34 @Override 35 public void handle(ModelInterpretationContext intercon, Model model) throws ModelHandlerException { 36 EventEvaluatorModel eem = (EventEvaluatorModel) model; 37 38 String className = eem.getClassName(); 39 if (OptionHelper.isNullOrEmptyOrAllSpaces(className)) { 40 String defaultClassName = defaultClassName(intercon, eem); 41 if (OptionHelper.isNullOrEmptyOrAllSpaces(defaultClassName)) { 42 inError = true; 43 addError("Mandatory \"" + CLASS_ATTRIBUTE + "\" attribute missing for <evaluator>"); 44 addError("No default classname could be found."); 45 return; 46 } else { 47 addInfo("Assuming default evaluator class [" + defaultClassName + "]"); 48 className = defaultClassName; 49 } 50 } else { 51 className = intercon.getImport(className); 52 } 53 54 String evaluatorName = intercon.subst(eem.getName()); 55 try { 56 evaluator = (EventEvaluator<?>) OptionHelper.instantiateByClassName(className, 57 ch.qos.logback.core.boolex.EventEvaluator.class, context); 58 evaluator.setContext(this.context); 59 evaluator.setName(evaluatorName); 60 intercon.pushObject(evaluator); 61 62 } catch (Exception oops) { 63 inError = true; 64 addError("Could not create evaluator of type " + className + "].", oops); 65 } 66 67 } 68 69 private String defaultClassName(ModelInterpretationContext mic, EventEvaluatorModel model) { 70 DefaultNestedComponentRegistry registry = mic.getDefaultNestedComponentRegistry(); 71 return registry.findDefaultComponentTypeByTag(model.getTag()); 72 } 73 74 @Override 75 public void postHandle(ModelInterpretationContext intercon, Model model) throws ModelHandlerException { 76 if (inError) { 77 return; 78 } 79 80 if (evaluator instanceof LifeCycle) { 81 ((LifeCycle) evaluator).start(); 82 addInfo("Starting evaluator named [" + evaluator.getName() + "]"); 83 } 84 85 Object o = intercon.peekObject(); 86 87 if (o != evaluator) { 88 addWarn("The object on the top the of the stack is not the evaluator pushed earlier."); 89 } else { 90 intercon.popObject(); 91 92 try { 93 @SuppressWarnings("unchecked") 94 Map<String, EventEvaluator<?>> evaluatorMap = (Map<String, EventEvaluator<?>>) context 95 .getObject(CoreConstants.EVALUATOR_MAP); 96 if (evaluatorMap == null) { 97 addError("Could not find EvaluatorMap"); 98 } else { 99 evaluatorMap.put(evaluator.getName(), evaluator); 100 } 101 } catch (Exception ex) { 102 addError("Could not set evaluator named [" + evaluator + "].", ex); 103 } 104 } 105 } 106 107 }