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.Loader;
018import ch.qos.logback.core.util.OptionHelper;
019
020import java.net.URL;
021
022/**
023 * In conjunction with {@link ch.qos.logback.core.joran.action.PropertyAction}
024 * sets the named variable to "true" if the {@link #setResource(String)
025 * resource} specified by the user is available on the class path, "false"
026 * otherwise.
027 *
028 * @see #getPropertyValue()
029 *
030 * @author XuHuisheng
031 * @author Ceki Gulcu
032 * @since 1.1.0
033 */
034public class ResourceExistsPropertyDefiner extends PropertyDefinerBase {
035
036    String resourceStr;
037
038    public String getResource() {
039        return resourceStr;
040    }
041
042    /**
043     * The resource to search for on the class path.
044     *
045     * @param resource
046     */
047    public void setResource(String resource) {
048        this.resourceStr = resource;
049    }
050
051    /**
052     * Returns the string "true" if the {@link #setResource(String) resource}
053     * specified by the user is available on the class path, "false" otherwise.
054     *
055     * @return "true"|"false" depending on the availability of resource on the
056     *         classpath
057     */
058    public String getPropertyValue() {
059        if (OptionHelper.isNullOrEmptyOrAllSpaces(resourceStr)) {
060            addError("The \"resource\" property must be set.");
061            return null;
062        }
063
064        URL resourceURL = Loader.getResourceBySelfClassLoader(resourceStr);
065        return booleanAsStr(resourceURL != null);
066    }
067
068}