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