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