View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, 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.joran.spi.InterpretationContext;
17  import ch.qos.logback.core.joran.spi.ActionException;
18  import ch.qos.logback.core.util.OptionHelper;
19  import ch.qos.logback.core.spi.PropertyDefiner;
20  import org.xml.sax.Attributes;
21  
22  /**
23   * Instantiate class for define property value. Get future property name and
24   * property definer class from attributes. Some property definer properties
25   * could be used. After defining put new property to context.
26   * 
27   * @author Aleksey Didik
28   */
29  public class DefinePropertyAction extends Action {
30  
31    String propertyName;
32    PropertyDefiner definer;
33    boolean inError;
34  
35    public void begin(InterpretationContext ec, String localName,
36        Attributes attributes) throws ActionException {
37      // reset variables
38      propertyName = null;
39      definer = null;
40      inError = false;
41  
42      // read future property name
43      propertyName = attributes.getValue(NAME_ATTRIBUTE);
44      if (OptionHelper.isEmpty(propertyName)) {
45        addError("Missing property name for property definer. Near [" + localName
46            + "] line " + getLineNumber(ec));
47        inError = true;
48        return;
49      }
50  
51      // read property definer class name
52      String className = attributes.getValue(CLASS_ATTRIBUTE);
53      if (OptionHelper.isEmpty(className)) {
54        addError("Missing class name for property definer. Near [" + localName
55            + "] line " + getLineNumber(ec));
56        inError = true;
57        return;
58      }
59  
60      // try to instantiate property definer
61      try {
62        addInfo("About to instantiate property definer of type [" + className
63            + "]");
64        definer = (PropertyDefiner) OptionHelper.instantiateByClassName(
65            className, PropertyDefiner.class, context);
66        definer.setContext(context);
67        ec.pushObject(definer);
68      } catch (Exception oops) {
69        inError = true;
70        addError("Could not create an PropertyDefiner of type [" + className
71            + "].", oops);
72        throw new ActionException(oops);
73      }
74    }
75  
76    /**
77     * Now property definer is initialized by all properties and we can put
78     * property value to context
79     */
80    public void end(InterpretationContext ec, String name) {
81      if (inError) {
82        return;
83      }
84  
85      Object o = ec.peekObject();
86  
87      if (o != definer) {
88        addWarn("The object at the of the stack is not the property definer for property named ["
89            + propertyName + "] pushed earlier.");
90      } else {
91        addInfo("Popping property definer for property named [" + propertyName
92            + "] from the object stack");
93        ec.popObject();
94        // let's put defined property and value to context but only if it is
95        // not null
96        String propertyValue = definer.getPropertyValue();
97        if(propertyValue != null) {
98          context.putProperty(propertyName, propertyValue);
99        }
100     }
101   }
102 }