View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  
18  package org.apache.logging.log4j.core.util;
19  
20  import java.io.ByteArrayInputStream;
21  import java.io.ByteArrayOutputStream;
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.FileNotFoundException;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.net.MalformedURLException;
28  import java.net.URI;
29  import java.net.URISyntaxException;
30  import java.net.URL;
31  import java.util.Objects;
32  
33  import org.apache.logging.log4j.Level;
34  import org.apache.logging.log4j.core.config.ConfigurationFactory;
35  import org.apache.logging.log4j.core.config.ConfigurationSource;
36  import org.apache.logging.log4j.util.LoaderUtil;
37  
38  /**
39   * Represents the source for the logging configuration.
40   */
41  public class Source {
42  
43      /**
44       * Captures a URI or File.
45       */
46  
47      private final File file;
48      private final URI uri;
49      private final String location;
50  
51      /**
52       * Constructs a Source from a ConfigurationSource.
53       * @param source The ConfigurationSource.
54       */
55      public Source(ConfigurationSource source) {
56          this.file = source.getFile();
57          this.uri = source.getURI();
58          this.location = source.getLocation();
59      }
60  
61      /**
62       * Constructs a new {@code Source} with the specified file.
63       * file.
64       *
65       * @param file the file where the input stream originated
66       */
67      public Source(final File file) {
68          this.file = Objects.requireNonNull(file, "file is null");
69          this.location = file.getAbsolutePath();
70          this.uri = null;
71      }
72  
73      /**
74       * Constructs a new {@code Source} from the specified URI.
75       *
76       * @param uri the URL where the input stream originated
77       */
78      public Source(final URI uri, final long lastModified) {
79          this.uri = Objects.requireNonNull(uri, "URI is null");
80          this.location = uri.toString();
81          this.file = null;
82      }
83  
84      /**
85       * Returns the file configuration source, or {@code null} if this configuration source is based on an URL or has
86       * neither a file nor an URL.
87       *
88       * @return the configuration source file, or {@code null}
89       */
90      public File getFile() {
91          return file;
92      }
93  
94      /**
95       * Returns the configuration source URL, or {@code null} if this configuration source is based on a file or has
96       * neither a file nor an URL.
97       *
98       * @return the configuration source URL, or {@code null}
99       */
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 }