1 /**
2  * Logback: the reliable, generic, fast and flexible logging framework.
3  * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4  *
5  * This program and the accompanying materials are dual-licensed under
6  * either the terms of the Eclipse Public License v1.0 as published by
7  * the Eclipse Foundation
8  *
9  *   or (per the licensee's choosing)
10  *
11  * under the terms of the GNU Lesser General Public License version 2.1
12  * as published by the Free Software Foundation.
13  */

14 package ch.qos.logback.core.joran.action;
15
16 import ch.qos.logback.core.spi.ContextAware;
17 import org.xml.sax.Attributes;
18
19 import ch.qos.logback.core.joran.spi.ActionException;
20 import ch.qos.logback.core.joran.spi.InterpretationContext;
21 import ch.qos.logback.core.spi.LifeCycle;
22 import ch.qos.logback.core.status.StatusListener;
23 import ch.qos.logback.core.util.OptionHelper;
24
25 public class StatusListenerAction extends Action {
26
27     boolean inError = false;
28     Boolean effectivelyAdded = null;
29     StatusListener statusListener = null;
30
31     public void begin(InterpretationContext ec, String name, Attributes attributes) throws ActionException {
32         inError = false;
33         effectivelyAdded = null;
34         String className = attributes.getValue(CLASS_ATTRIBUTE);
35         if (OptionHelper.isEmpty(className)) {
36             addError("Missing class name for statusListener. Near [" + name + "] line " + getLineNumber(ec));
37             inError = true;
38             return;
39         }
40
41         try {
42             statusListener = (StatusListener) OptionHelper.instantiateByClassName(className, StatusListener.class, context);
43             effectivelyAdded = ec.getContext().getStatusManager().add(statusListener);
44             if (statusListener instanceof ContextAware) {
45                 ((ContextAware) statusListener).setContext(context);
46             }
47             addInfo("Added status listener of type [" + className + "]");
48             ec.pushObject(statusListener);
49         } catch (Exception e) {
50             inError = true;
51             addError("Could not create an StatusListener of type [" + className + "].", e);
52             throw new ActionException(e);
53         }
54
55     }
56
57     public void finish(InterpretationContext ec) {
58     }
59
60     public void end(InterpretationContext ec, String e) {
61         if (inError) {
62             return;
63         }
64         if (isEffectivelyAdded() && statusListener instanceof LifeCycle) {
65             ((LifeCycle) statusListener).start();
66         }
67         Object o = ec.peekObject();
68         if (o != statusListener) {
69             addWarn("The object at the of the stack is not the statusListener pushed earlier.");
70         } else {
71             ec.popObject();
72         }
73     }
74
75     private boolean isEffectivelyAdded() {
76         if (effectivelyAdded == null)
77             return false;
78         return effectivelyAdded;
79     }
80 }
81