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