1616 */
1717package org .apache .logging .log4j .core .config ;
1818
19- import static java .util .Objects .requireNonNull ;
20-
2119import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
2220import java .io .ByteArrayInputStream ;
2321import java .io .ByteArrayOutputStream ;
3432import java .net .URLConnection ;
3533import java .nio .file .Files ;
3634import java .nio .file .Path ;
35+ import java .util .Objects ;
3736import org .apache .logging .log4j .Logger ;
3837import org .apache .logging .log4j .core .net .UrlConnectionFactory ;
3938import org .apache .logging .log4j .core .util .FileUtils ;
@@ -78,7 +77,16 @@ public class ConfigurationSource {
7877 * @param file the file where the input stream originated
7978 */
8079 public ConfigurationSource (final InputStream stream , final File file ) {
81- this (null , new Source (file ), stream , getLastModified (file .toPath ()));
80+ this .stream = Objects .requireNonNull (stream , "stream is null" );
81+ this .data = null ;
82+ this .source = new Source (file );
83+ long modified = 0 ;
84+ try {
85+ modified = file .lastModified ();
86+ } catch (Exception ex ) {
87+ // There is a problem with the file. It will be handled somewhere else.
88+ }
89+ this .currentLastModified = this .initialLastModified = modified ;
8290 }
8391
8492 /**
@@ -89,16 +97,16 @@ public ConfigurationSource(final InputStream stream, final File file) {
8997 * @param path the path where the input stream originated.
9098 */
9199 public ConfigurationSource (final InputStream stream , final Path path ) {
92- this ( null , new Source ( path ), stream , getLastModified ( path ) );
93- }
94-
95- private static long getLastModified ( Path path ) {
100+ this . stream = Objects . requireNonNull ( stream , " stream is null" );
101+ this . data = null ;
102+ this . source = new Source ( path );
103+ long modified = 0 ;
96104 try {
97- return Files .getLastModifiedTime (path ).toMillis ();
98- } catch (Exception ignored ) {
105+ modified = Files .getLastModifiedTime (path ).toMillis ();
106+ } catch (Exception ex ) {
99107 // There is a problem with the file. It will be handled somewhere else.
100108 }
101- return 0L ;
109+ this . currentLastModified = this . initialLastModified = modified ;
102110 }
103111
104112 /**
@@ -120,8 +128,11 @@ public ConfigurationSource(final InputStream stream, final URL url) {
120128 * @param url the URL where the input stream originated
121129 * @param lastModified when the source was last modified.
122130 */
123- public ConfigurationSource (InputStream stream , final URL url , final long lastModified ) {
124- this (null , new Source (url ), stream , lastModified );
131+ public ConfigurationSource (final InputStream stream , final URL url , final long lastModified ) {
132+ this .stream = Objects .requireNonNull (stream , "stream is null" );
133+ this .data = null ;
134+ this .currentLastModified = this .initialLastModified = lastModified ;
135+ this .source = new Source (url );
125136 }
126137
127138 /**
@@ -131,7 +142,7 @@ public ConfigurationSource(InputStream stream, final URL url, final long lastMod
131142 * @param stream the input stream, the caller is responsible for closing this resource.
132143 * @throws IOException if an exception occurred reading from the specified stream
133144 */
134- public ConfigurationSource (InputStream stream ) throws IOException {
145+ public ConfigurationSource (final InputStream stream ) throws IOException {
135146 this (toByteArray (stream ), null , 0 );
136147 }
137148
@@ -142,26 +153,19 @@ public ConfigurationSource(InputStream stream) throws IOException {
142153 * @param data data from the source
143154 * @param lastModified when the source was last modified.
144155 */
145- public ConfigurationSource (Source source , byte [] data , long lastModified ) {
146- this ( data , requireNonNull (source , "source is null" ), lastModified );
147- }
148-
149- private ConfigurationSource ( byte [] data , /*@Nullable*/ Source source , long lastModified ) {
150- this ( requireNonNull ( data , "data is null" ), source , new ByteArrayInputStream ( data ), lastModified ) ;
156+ public ConfigurationSource (final Source source , final byte [] data , final long lastModified ) {
157+ Objects . requireNonNull (source , "source is null" );
158+ this . data = Objects . requireNonNull ( data , "data is null" );
159+ this . stream = new ByteArrayInputStream ( data );
160+ this . currentLastModified = this . initialLastModified = lastModified ;
161+ this . source = source ;
151162 }
152163
153- /**
154- * @throws NullPointerException if both {@code stream} and {@code data} are {@code null}.
155- */
156- private ConfigurationSource (
157- byte /*@Nullable*/ [] data , /*@Nullable*/ Source source , InputStream stream , long lastModified ) {
158- if (data == null && source == null ) {
159- throw new NullPointerException ("both `data` and `source` are null" );
160- }
161- this .stream = stream ;
162- this .data = data ;
163- this .source = source ;
164+ private ConfigurationSource (final byte [] data , final URL url , final long lastModified ) {
165+ this .data = Objects .requireNonNull (data , "data is null" );
166+ this .stream = new ByteArrayInputStream (data );
164167 this .currentLastModified = this .initialLastModified = lastModified ;
168+ this .source = url == null ? null : new Source (url );
165169 }
166170
167171 /**
@@ -194,6 +198,10 @@ private static byte[] toByteArray(final InputStream inputStream) throws IOExcept
194198 return source == null ? null : source .getFile ();
195199 }
196200
201+ private boolean isLocation () {
202+ return source != null && source .getLocation () != null ;
203+ }
204+
197205 /**
198206 * Returns the configuration source URL, or {@code null} if this configuration source is based on a file or has
199207 * neither a file nor an URL.
@@ -279,32 +287,31 @@ public InputStream getInputStream() {
279287 URL url = getURL ();
280288 if (url != null && data != null ) {
281289 // Creates a ConfigurationSource without accessing the URL since the data was provided.
282- return new ConfigurationSource (data , new Source ( url ) , currentLastModified );
290+ return new ConfigurationSource (data , url , currentLastModified );
283291 }
284292 URI uri = getURI ();
285293 if (uri != null ) {
286294 return fromUri (uri );
287295 }
288- return data == null ? null : new ConfigurationSource (data , null , currentLastModified );
296+ return data != null ? new ConfigurationSource (data , null , currentLastModified ) : null ;
289297 }
290298
291299 @ Override
292300 public String toString () {
293- if (source != null ) {
294- return source . getLocation ();
301+ if (isLocation () ) {
302+ return getLocation ();
295303 }
296304 if (this == NULL_SOURCE ) {
297305 return "NULL_SOURCE" ;
298306 }
299- byte [] data = this .data ;
300307 final int length = data == null ? -1 : data .length ;
301308 return "stream (" + length + " bytes, unknown location)" ;
302309 }
303310
304311 /**
305312 * Loads the configuration from a URI.
306313 * @param configLocation A URI representing the location of the configuration.
307- * @return The ConfigurationSource for the configuration or {@code null} .
314+ * @return The ConfigurationSource for the configuration.
308315 */
309316 public static /*@Nullable*/ ConfigurationSource fromUri (final URI configLocation ) {
310317 final File configFile = FileUtils .fileFromUri (configLocation );
0 commit comments