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.joran.action.PropertyAction;
018import ch.qos.logback.core.util.OptionHelper;
019
020import java.io.File;
021
022/**
023 * In conjunction with {@link PropertyAction}
024 * sets the named variable to "true" if the file specified by
025 * {@link #setPath(String) path} property exists, to "false" otherwise.
026 *
027 * @see #getPropertyValue()
028 *
029 * @author Ceki Gülcü
030 */
031public class FileExistsPropertyDefiner extends PropertyDefinerBase {
032
033    String path;
034
035    public String getPath() {
036        return path;
037    }
038
039    /**
040     * The path for the file to search for.
041     *
042     * @param path
043     */
044    public void setPath(String path) {
045        this.path = path;
046    }
047
048    /**
049     * Returns "true" if the file specified by {@link #setPath(String) path}
050     * property exists. Returns "false" otherwise.
051     *
052     * @return "true"|"false" depending on the existence of file
053     */
054    public String getPropertyValue() {
055        if (OptionHelper.isNullOrEmptyOrAllSpaces(path)) {
056            addError("The \"path\" property must be set.");
057            return null;
058        }
059
060        File file = new File(path);
061        return booleanAsStr(file.exists());
062    }
063}