16
16
*/
17
17
package org .apache .logging .log4j .core .config ;
18
18
19
- import static java .util .Objects .requireNonNull ;
20
-
21
19
import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
22
20
import java .io .ByteArrayInputStream ;
23
21
import java .io .ByteArrayOutputStream ;
34
32
import java .net .URLConnection ;
35
33
import java .nio .file .Files ;
36
34
import java .nio .file .Path ;
35
+ import java .util .Objects ;
37
36
import org .apache .logging .log4j .Logger ;
38
37
import org .apache .logging .log4j .core .net .UrlConnectionFactory ;
39
38
import org .apache .logging .log4j .core .util .FileUtils ;
@@ -78,7 +77,16 @@ public class ConfigurationSource {
78
77
* @param file the file where the input stream originated
79
78
*/
80
79
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 ;
82
90
}
83
91
84
92
/**
@@ -89,16 +97,16 @@ public ConfigurationSource(final InputStream stream, final File file) {
89
97
* @param path the path where the input stream originated.
90
98
*/
91
99
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 ;
96
104
try {
97
- return Files .getLastModifiedTime (path ).toMillis ();
98
- } catch (Exception ignored ) {
105
+ modified = Files .getLastModifiedTime (path ).toMillis ();
106
+ } catch (Exception ex ) {
99
107
// There is a problem with the file. It will be handled somewhere else.
100
108
}
101
- return 0L ;
109
+ this . currentLastModified = this . initialLastModified = modified ;
102
110
}
103
111
104
112
/**
@@ -120,8 +128,11 @@ public ConfigurationSource(final InputStream stream, final URL url) {
120
128
* @param url the URL where the input stream originated
121
129
* @param lastModified when the source was last modified.
122
130
*/
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 );
125
136
}
126
137
127
138
/**
@@ -131,7 +142,7 @@ public ConfigurationSource(InputStream stream, final URL url, final long lastMod
131
142
* @param stream the input stream, the caller is responsible for closing this resource.
132
143
* @throws IOException if an exception occurred reading from the specified stream
133
144
*/
134
- public ConfigurationSource (InputStream stream ) throws IOException {
145
+ public ConfigurationSource (final InputStream stream ) throws IOException {
135
146
this (toByteArray (stream ), null , 0 );
136
147
}
137
148
@@ -142,26 +153,19 @@ public ConfigurationSource(InputStream stream) throws IOException {
142
153
* @param data data from the source
143
154
* @param lastModified when the source was last modified.
144
155
*/
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 ;
151
162
}
152
163
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 );
164
167
this .currentLastModified = this .initialLastModified = lastModified ;
168
+ this .source = url == null ? null : new Source (url );
165
169
}
166
170
167
171
/**
@@ -194,6 +198,10 @@ private static byte[] toByteArray(final InputStream inputStream) throws IOExcept
194
198
return source == null ? null : source .getFile ();
195
199
}
196
200
201
+ private boolean isLocation () {
202
+ return source != null && source .getLocation () != null ;
203
+ }
204
+
197
205
/**
198
206
* Returns the configuration source URL, or {@code null} if this configuration source is based on a file or has
199
207
* neither a file nor an URL.
@@ -279,32 +287,31 @@ public InputStream getInputStream() {
279
287
URL url = getURL ();
280
288
if (url != null && data != null ) {
281
289
// 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 );
283
291
}
284
292
URI uri = getURI ();
285
293
if (uri != null ) {
286
294
return fromUri (uri );
287
295
}
288
- return data == null ? null : new ConfigurationSource (data , null , currentLastModified );
296
+ return data != null ? new ConfigurationSource (data , null , currentLastModified ) : null ;
289
297
}
290
298
291
299
@ Override
292
300
public String toString () {
293
- if (source != null ) {
294
- return source . getLocation ();
301
+ if (isLocation () ) {
302
+ return getLocation ();
295
303
}
296
304
if (this == NULL_SOURCE ) {
297
305
return "NULL_SOURCE" ;
298
306
}
299
- byte [] data = this .data ;
300
307
final int length = data == null ? -1 : data .length ;
301
308
return "stream (" + length + " bytes, unknown location)" ;
302
309
}
303
310
304
311
/**
305
312
* Loads the configuration from a URI.
306
313
* @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.
308
315
*/
309
316
public static /*@Nullable*/ ConfigurationSource fromUri (final URI configLocation ) {
310
317
final File configFile = FileUtils .fileFromUri (configLocation );
0 commit comments