001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017
018package org.apache.logging.log4j.core.util;
019
020import java.io.File;
021import java.net.URI;
022import java.util.Objects;
023
024import org.apache.logging.log4j.core.config.ConfigurationSource;
025
026/**
027 * Represents the source for the logging configuration.
028 */
029public class Source {
030
031    /**
032     * Captures a URI or File.
033     */
034
035    private final File file;
036    private final URI uri;
037    private final String location;
038
039    /**
040     * Constructs a Source from a ConfigurationSource.
041     * @param source The ConfigurationSource.
042     */
043    public Source(ConfigurationSource source) {
044        this.file = source.getFile();
045        this.uri = source.getURI();
046        this.location = source.getLocation();
047    }
048
049    /**
050     * Constructs a new {@code Source} with the specified file.
051     * file.
052     *
053     * @param file the file where the input stream originated
054     */
055    public Source(final File file) {
056        this.file = Objects.requireNonNull(file, "file is null");
057        this.location = file.getAbsolutePath();
058        this.uri = null;
059    }
060
061    /**
062     * Constructs a new {@code Source} from the specified URI.
063     *
064     * @param uri the URL where the input stream originated
065     */
066    public Source(final URI uri, final long lastModified) {
067        this.uri = Objects.requireNonNull(uri, "URI is null");
068        this.location = uri.toString();
069        this.file = null;
070    }
071
072    /**
073     * Returns the file configuration source, or {@code null} if this configuration source is based on an URL or has
074     * neither a file nor an URL.
075     *
076     * @return the configuration source file, or {@code null}
077     */
078    public File getFile() {
079        return file;
080    }
081
082    /**
083     * Returns the configuration source URL, or {@code null} if this configuration source is based on a file or has
084     * neither a file nor an URL.
085     *
086     * @return the configuration source URL, or {@code null}
087     */
088    public URI getURI() {
089        return uri;
090    }
091
092    /**
093     * Returns a string describing the configuration source file or URL, or {@code null} if this configuration source
094     * has neither a file nor an URL.
095     *
096     * @return a string describing the configuration source file or URL, or {@code null}
097     */
098    public String getLocation() {
099        return location;
100    }
101
102    @Override
103    public String toString() {
104        return location;
105    }
106
107    @Override
108    public boolean equals(Object o) {
109        if (this == o) {
110            return true;
111        }
112        if (!(o instanceof Source)) {
113            return false;
114        }
115        Source source = (Source) o;
116        return Objects.equals(location, source.location);
117    }
118
119    @Override
120    public int hashCode() {
121        return 31 + Objects.hashCode(location);
122    }
123}