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.OptionHelper;
19  
20  import java.io.File;
21  
22  /**
23   * In conjunction with {@link PropertyAction}
24   * sets the named variable to "true" if the file specified by
25   * {@link #setPath(String) path} property exists, to "false" otherwise.
26   *
27   * @see #getPropertyValue()
28   *
29   * @author Ceki Gülcü
30   */
31  public class FileExistsPropertyDefiner extends PropertyDefinerBase {
32  
33      String path;
34  
35      public String getPath() {
36          return path;
37      }
38  
39      /**
40       * The path for the file to search for.
41       *
42       * @param path
43       */
44      public void setPath(String path) {
45          this.path = path;
46      }
47  
48      /**
49       * Returns "true" if the file specified by {@link #setPath(String) path}
50       * property exists. Returns "false" otherwise.
51       *
52       * @return "true"|"false" depending on the existence of file
53       */
54      public String getPropertyValue() {
55          if (OptionHelper.isNullOrEmptyOrAllSpaces(path)) {
56              addError("The \"path\" property must be set.");
57              return null;
58          }
59  
60          File file = new File(path);
61          return booleanAsStr(file.exists());
62      }
63  }