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