View Javadoc

1   /**
2    * LOGBack: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 1999-2006, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  
11  package ch.qos.logback.core.joran.action;
12  
13  import org.xml.sax.Attributes;
14  
15  import ch.qos.logback.core.joran.spi.ActionException;
16  import ch.qos.logback.core.joran.spi.InterpretationContext;
17  import ch.qos.logback.core.spi.LifeCycle;
18  import ch.qos.logback.core.status.StatusListener;
19  import ch.qos.logback.core.util.OptionHelper;
20  
21  
22  public class StatusListenerAction extends Action {
23  
24   
25    boolean inError = false;
26    StatusListener statusListener = null;
27    
28    public void begin(InterpretationContext ec, String name, Attributes attributes) throws ActionException {
29      inError = false;
30      String className = attributes.getValue(CLASS_ATTRIBUTE);
31      if(OptionHelper.isEmpty(className)) {
32        addError(
33          "Missing class name for statusListener. Near ["
34            + name + "] line " + getLineNumber(ec));
35        inError = true;
36        return;
37      }
38      
39      try {
40        statusListener = (StatusListener) OptionHelper.instantiateByClassName(
41            className, StatusListener.class, context);
42        ec.getContext().getStatusManager().add(statusListener);
43        ec.pushObject(statusListener);
44      } catch (Exception e) {
45        inError = true;
46        addError(
47          "Could not create an StatusListener of type ["+className+"].", e);
48        throw new ActionException(e);
49      }
50      
51    }
52   
53    public void finish(InterpretationContext ec) {
54    }
55  
56    public void end(InterpretationContext ec, String e) {
57      if (inError) {
58        return;
59      }
60      if (statusListener instanceof LifeCycle) {
61        ((LifeCycle) statusListener).start();
62      }
63      Object o = ec.peekObject();
64      if (o != statusListener) {
65        addWarn(
66          "The object at the of the stack is not the statusListener pushed earlier.");
67      } else {
68        ec.popObject();
69      }
70    }
71  }