Skip to content

Commit 755497d

Browse files
committed
Revert "Single constructor for ConfigurationSource"
This reverts commit 35973f7.
1 parent a01f7e4 commit 755497d

File tree

5 files changed

+69
-79
lines changed

5 files changed

+69
-79
lines changed

log4j-core-test/src/test/java/org/apache/logging/log4j/core/util/WatchManagerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.concurrent.LinkedBlockingQueue;
3737
import java.util.concurrent.TimeUnit;
3838
import org.apache.logging.log4j.core.config.ConfigurationScheduler;
39+
import org.apache.logging.log4j.core.config.ConfigurationSource;
3940
import org.junit.jupiter.api.AfterEach;
4041
import org.junit.jupiter.api.BeforeEach;
4142
import org.junit.jupiter.api.Test;
@@ -143,10 +144,9 @@ void testWatchManagerResetFile() throws Exception {
143144
*/
144145
@Test
145146
void testWatchManagerCallsWatcher() {
146-
Source source = mock(Source.class);
147147
Watcher watcher = mock(Watcher.class);
148148
when(watcher.isModified()).thenReturn(false);
149-
watchManager.watch(source, watcher);
149+
watchManager.watch(new Source(ConfigurationSource.NULL_SOURCE), watcher);
150150
verify(watcher, timeout(2000)).isModified();
151151
verify(watcher, never()).modified();
152152
when(watcher.isModified()).thenReturn(true);

log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717
package org.apache.logging.log4j.core.config;
1818

1919
import java.io.ByteArrayOutputStream;
20-
import java.io.File;
2120
import java.io.IOException;
2221
import java.io.InputStream;
2322
import java.io.Serializable;
2423
import java.lang.ref.WeakReference;
25-
import java.net.URI;
2624
import java.util.ArrayList;
2725
import java.util.Arrays;
2826
import java.util.Collection;
@@ -281,10 +279,9 @@ protected void initializeWatchers(
281279
if (configSource != null && (configSource.getFile() != null || configSource.getURL() != null)) {
282280
if (monitorIntervalSeconds > 0) {
283281
watchManager.setIntervalSeconds(monitorIntervalSeconds);
284-
File file = configSource.getFile();
285-
if (file != null) {
286-
final Source cfgSource = new Source(file);
287-
final long lastModified = file.lastModified();
282+
if (configSource.getFile() != null) {
283+
final Source cfgSource = new Source(configSource);
284+
final long lastModified = configSource.getFile().lastModified();
288285
final ConfigurationFileWatcher watcher =
289286
new ConfigurationFileWatcher(this, reconfigurable, listeners, lastModified);
290287
watchManager.watch(cfgSource, watcher);
@@ -300,10 +297,8 @@ protected void initializeWatchers(
300297
}
301298

302299
private void monitorSource(final Reconfigurable reconfigurable, final ConfigurationSource configSource) {
303-
URI uri = configSource.getURI();
304-
if (uri != null && configSource.getLastModified() > 0) {
305-
File file = configSource.getFile();
306-
final Source cfgSource = file != null ? new Source(file) : new Source(uri);
300+
if (configSource.getLastModified() > 0) {
301+
final Source cfgSource = new Source(configSource);
307302
final Watcher watcher = WatcherFactory.getInstance(pluginPackages)
308303
.newWatcher(cfgSource, this, reconfigurable, listeners, configSource.getLastModified());
309304
if (watcher != null) {

log4j-core/src/main/java/org/apache/logging/log4j/core/config/ConfigurationSource.java

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
*/
1717
package org.apache.logging.log4j.core.config;
1818

19-
import static java.util.Objects.requireNonNull;
20-
2119
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2220
import java.io.ByteArrayInputStream;
2321
import java.io.ByteArrayOutputStream;
@@ -34,6 +32,7 @@
3432
import java.net.URLConnection;
3533
import java.nio.file.Files;
3634
import java.nio.file.Path;
35+
import java.util.Objects;
3736
import org.apache.logging.log4j.Logger;
3837
import org.apache.logging.log4j.core.net.UrlConnectionFactory;
3938
import 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);

log4j-core/src/main/java/org/apache/logging/log4j/core/util/Loader.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.apache.logging.log4j.status.StatusLogger;
2424
import org.apache.logging.log4j.util.LoaderUtil;
2525
import org.apache.logging.log4j.util.PropertiesUtil;
26-
import org.jspecify.annotations.Nullable;
2726

2827
/**
2928
* Load resources (or images) from various sources.
@@ -85,9 +84,9 @@ public static ClassLoader getClassLoader(final Class<?> class1, final Class<?> c
8584
* </ol>
8685
* @param resource The resource to load.
8786
* @param defaultLoader The default ClassLoader.
88-
* @return A URL to the resource or {@code null}.
87+
* @return A URL to the resource.
8988
*/
90-
public static @Nullable URL getResource(final String resource, final ClassLoader defaultLoader) {
89+
public static URL getResource(final String resource, final ClassLoader defaultLoader) {
9190
try {
9291
ClassLoader classLoader = getThreadContextClassLoader();
9392
if (classLoader != null) {

0 commit comments

Comments
 (0)