001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.property;
015
016import ch.qos.logback.core.PropertyDefinerBase;
017import ch.qos.logback.core.util.OptionHelper;
018
019import java.io.File;
020
021/**
022 * In conjunction with {@link ch.qos.logback.core.joran.action.PropertyAction}
023 * sets the named variable to "true" if the file specified by
024 * {@link #setPath(String) path} property exists, to "false" otherwise.
025 *
026 * @see #getPropertyValue()
027 *
028 * @author Ceki Gülcü
029 */
030public class FileExistsPropertyDefiner extends PropertyDefinerBase {
031
032    String path;
033
034    public String getPath() {
035        return path;
036    }
037
038    /**
039     * The path for the file to search for.
040     *
041     * @param path
042     */
043    public void setPath(String path) {
044        this.path = path;
045    }
046
047    /**
048     * Returns "true" if the file specified by {@link #setPath(String) path}
049     * property exists. Returns "false" otherwise.
050     *
051     * @return "true"|"false" depending on the existence of file
052     */
053    public String getPropertyValue() {
054        if (OptionHelper.isNullOrEmptyOrAllSpaces(path)) {
055            addError("The \"path\" property must be set.");
056            return null;
057        }
058
059        File file = new File(path);
060        return booleanAsStr(file.exists());
061    }
062}