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.ByteArrayInputStream; 021import java.io.ByteArrayOutputStream; 022import java.io.File; 023import java.io.FileInputStream; 024import java.io.FileNotFoundException; 025import java.io.IOException; 026import java.io.InputStream; 027import java.net.MalformedURLException; 028import java.net.URI; 029import java.net.URISyntaxException; 030import java.net.URL; 031import java.util.Objects; 032 033import org.apache.logging.log4j.Level; 034import org.apache.logging.log4j.core.config.ConfigurationFactory; 035import org.apache.logging.log4j.core.config.ConfigurationSource; 036import org.apache.logging.log4j.util.LoaderUtil; 037 038/** 039 * Represents the source for the logging configuration. 040 */ 041public class Source { 042 043 /** 044 * Captures a URI or File. 045 */ 046 047 private final File file; 048 private final URI uri; 049 private final String location; 050 051 /** 052 * Constructs a Source from a ConfigurationSource. 053 * @param source The ConfigurationSource. 054 */ 055 public Source(ConfigurationSource source) { 056 this.file = source.getFile(); 057 this.uri = source.getURI(); 058 this.location = source.getLocation(); 059 } 060 061 /** 062 * Constructs a new {@code Source} with the specified file. 063 * file. 064 * 065 * @param file the file where the input stream originated 066 */ 067 public Source(final File file) { 068 this.file = Objects.requireNonNull(file, "file is null"); 069 this.location = file.getAbsolutePath(); 070 this.uri = null; 071 } 072 073 /** 074 * Constructs a new {@code Source} from the specified URI. 075 * 076 * @param uri the URL where the input stream originated 077 */ 078 public Source(final URI uri, final long lastModified) { 079 this.uri = Objects.requireNonNull(uri, "URI is null"); 080 this.location = uri.toString(); 081 this.file = null; 082 } 083 084 /** 085 * Returns the file configuration source, or {@code null} if this configuration source is based on an URL or has 086 * neither a file nor an URL. 087 * 088 * @return the configuration source file, or {@code null} 089 */ 090 public File getFile() { 091 return file; 092 } 093 094 /** 095 * Returns the configuration source URL, or {@code null} if this configuration source is based on a file or has 096 * neither a file nor an URL. 097 * 098 * @return the configuration source URL, or {@code null} 099 */ 100 public URI getURI() { 101 return uri; 102 } 103 104 /** 105 * Returns a string describing the configuration source file or URL, or {@code null} if this configuration source 106 * has neither a file nor an URL. 107 * 108 * @return a string describing the configuration source file or URL, or {@code null} 109 */ 110 public String getLocation() { 111 return location; 112 } 113 114 @Override 115 public String toString() { 116 return location; 117 } 118 119 @Override 120 public boolean equals(Object o) { 121 if (this == o) { 122 return true; 123 } 124 if (!(o instanceof Source)) { 125 return false; 126 } 127 Source source = (Source) o; 128 return Objects.equals(location, source.location); 129 } 130 131 @Override 132 public int hashCode() { 133 return Objects.hash(location); 134 } 135}