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.property;
15  
16  import ch.qos.logback.core.PropertyDefinerBase;
17  import ch.qos.logback.core.util.Loader;
18  import ch.qos.logback.core.util.OptionHelper;
19  
20  import java.net.URL;
21  
22  /**
23   * In conjunction with {@link ch.qos.logback.core.joran.action.PropertyAction}
24   * sets the named variable to "true" if the {@link #setResource(String)
25   * resource} specified by the user is available on the class path, "false"
26   * otherwise.
27   *
28   * @see #getPropertyValue()
29   *
30   * @author XuHuisheng
31   * @author Ceki Gulcu
32   * @since 1.1.0
33   */
34  public class ResourceExistsPropertyDefiner extends PropertyDefinerBase {
35  
36      String resourceStr;
37  
38      public String getResource() {
39          return resourceStr;
40      }
41  
42      /**
43       * The resource to search for on the class path.
44       *
45       * @param resource
46       */
47      public void setResource(String resource) {
48          this.resourceStr = resource;
49      }
50  
51      /**
52       * Returns the string "true" if the {@link #setResource(String) resource}
53       * specified by the user is available on the class path, "false" otherwise.
54       *
55       * @return "true"|"false" depending on the availability of resource on the
56       *         classpath
57       */
58      public String getPropertyValue() {
59          if (OptionHelper.isNullOrEmpty(resourceStr)) {
60              addError("The \"resource\" property must be set.");
61              return null;
62          }
63  
64          URL resourceURL = Loader.getResourceBySelfClassLoader(resourceStr);
65          return booleanAsStr(resourceURL != null);
66      }
67  
68  }