View Javadoc
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 java.io.FileInputStream;
17  import java.io.FileNotFoundException;
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.net.URL;
21  import java.util.Properties;
22  
23  import org.xml.sax.Attributes;
24  
25  import ch.qos.logback.core.joran.action.ActionUtil.Scope;
26  import ch.qos.logback.core.joran.spi.InterpretationContext;
27  import ch.qos.logback.core.pattern.util.RegularEscapeUtil;
28  import ch.qos.logback.core.util.Loader;
29  import ch.qos.logback.core.util.OptionHelper;
30  
31  /**
32   * This class serves as a base for other actions, which similar to the ANT
33   * <property> task which add/set properties of a given object.
34   * 
35   * This action sets new substitution properties in the logging context by name,
36   * value pair, or adds all the properties passed in "file" or "resource"
37   * attribute.
38   * 
39   * @author Ceki Gülcü
40   */
41  public class PropertyAction extends Action {
42  
43      static final String RESOURCE_ATTRIBUTE = "resource";
44  
45      static String INVALID_ATTRIBUTES = "In <property> element, either the \"file\" attribute alone, or "
46                      + "the \"resource\" element alone, or both the \"name\" and \"value\" attributes must be set.";
47  
48      /**
49       * Set a new property for the execution context by name, value pair, or adds
50       * all the properties found in the given file.
51       * 
52       */
53      public void begin(InterpretationContext ec, String localName, Attributes attributes) {
54  
55          if ("substitutionProperty".equals(localName)) {
56              addWarn("[substitutionProperty] element has been deprecated. Please use the [property] element instead.");
57          }
58  
59          String name = attributes.getValue(NAME_ATTRIBUTE);
60          String value = attributes.getValue(VALUE_ATTRIBUTE);
61          String scopeStr = attributes.getValue(SCOPE_ATTRIBUTE);
62  
63          Scope scope = ActionUtil.stringToScope(scopeStr);
64  
65          if (checkFileAttributeSanity(attributes)) {
66              String file = attributes.getValue(FILE_ATTRIBUTE);
67              file = ec.subst(file);
68              try {
69                  FileInputStream istream = new FileInputStream(file);
70                  loadAndSetProperties(ec, istream, scope);
71              } catch (FileNotFoundException e) {
72                  addError("Could not find properties file [" + file + "].");
73              } catch (IOException e1) {
74                  addError("Could not read properties file [" + file + "].", e1);
75              }
76          } else if (checkResourceAttributeSanity(attributes)) {
77              String resource = attributes.getValue(RESOURCE_ATTRIBUTE);
78              resource = ec.subst(resource);
79              URL resourceURL = Loader.getResourceBySelfClassLoader(resource);
80              if (resourceURL == null) {
81                  addError("Could not find resource [" + resource + "].");
82              } else {
83                  try {
84                      InputStream istream = resourceURL.openStream();
85                      loadAndSetProperties(ec, istream, scope);
86                  } catch (IOException e) {
87                      addError("Could not read resource file [" + resource + "].", e);
88                  }
89              }
90          } else if (checkValueNameAttributesSanity(attributes)) {
91              value = RegularEscapeUtil.basicEscape(value);
92              // now remove both leading and trailing spaces
93              value = value.trim();
94              value = ec.subst(value);
95              ActionUtil.setProperty(ec, name, value, scope);
96  
97          } else {
98              addError(INVALID_ATTRIBUTES);
99          }
100     }
101 
102     void loadAndSetProperties(InterpretationContext ec, InputStream istream, Scope scope) throws IOException {
103         Properties props = new Properties();
104         props.load(istream);
105         istream.close();
106         ActionUtil.setProperties(ec, props, scope);
107     }
108 
109     boolean checkFileAttributeSanity(Attributes attributes) {
110         String file = attributes.getValue(FILE_ATTRIBUTE);
111         String name = attributes.getValue(NAME_ATTRIBUTE);
112         String value = attributes.getValue(VALUE_ATTRIBUTE);
113         String resource = attributes.getValue(RESOURCE_ATTRIBUTE);
114 
115         return !(OptionHelper.isEmpty(file)) && (OptionHelper.isEmpty(name) && OptionHelper.isEmpty(value) && OptionHelper.isEmpty(resource));
116     }
117 
118     boolean checkResourceAttributeSanity(Attributes attributes) {
119         String file = attributes.getValue(FILE_ATTRIBUTE);
120         String name = attributes.getValue(NAME_ATTRIBUTE);
121         String value = attributes.getValue(VALUE_ATTRIBUTE);
122         String resource = attributes.getValue(RESOURCE_ATTRIBUTE);
123 
124         return !(OptionHelper.isEmpty(resource)) && (OptionHelper.isEmpty(name) && OptionHelper.isEmpty(value) && OptionHelper.isEmpty(file));
125     }
126 
127     boolean checkValueNameAttributesSanity(Attributes attributes) {
128         String file = attributes.getValue(FILE_ATTRIBUTE);
129         String name = attributes.getValue(NAME_ATTRIBUTE);
130         String value = attributes.getValue(VALUE_ATTRIBUTE);
131         String resource = attributes.getValue(RESOURCE_ATTRIBUTE);
132 
133         return (!(OptionHelper.isEmpty(name) || OptionHelper.isEmpty(value)) && (OptionHelper.isEmpty(file) && OptionHelper.isEmpty(resource)));
134     }
135 
136     public void end(InterpretationContext ec, String name) {
137     }
138 
139     public void finish(InterpretationContext ec) {
140     }
141 }