1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.logging.log4j.core.config;
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.util.FileUtils;
35 import org.apache.logging.log4j.core.util.Loader;
36 import org.apache.logging.log4j.util.LoaderUtil;
37
38
39
40
41 public class ConfigurationSource {
42
43
44
45
46 public static final ConfigurationSource NULL_SOURCE = new ConfigurationSource(new byte[0]);
47
48 private final File file;
49 private final URL url;
50 private final String location;
51 private final InputStream stream;
52 private final byte[] data;
53
54
55
56
57
58
59
60
61 public ConfigurationSource(final InputStream stream, final File file) {
62 this.stream = Objects.requireNonNull(stream, "stream is null");
63 this.file = Objects.requireNonNull(file, "file is null");
64 this.location = file.getAbsolutePath();
65 this.url = null;
66 this.data = null;
67 }
68
69
70
71
72
73
74
75
76 public ConfigurationSource(final InputStream stream, final URL url) {
77 this.stream = Objects.requireNonNull(stream, "stream is null");
78 this.url = Objects.requireNonNull(url, "URL is null");
79 this.location = url.toString();
80 this.file = null;
81 this.data = null;
82 }
83
84
85
86
87
88
89
90
91 public ConfigurationSource(final InputStream stream) throws IOException {
92 this(toByteArray(stream));
93 }
94
95 private ConfigurationSource(final byte[] data) {
96 this.data = Objects.requireNonNull(data, "data is null");
97 this.stream = new ByteArrayInputStream(data);
98 this.file = null;
99 this.url = null;
100 this.location = null;
101 }
102
103
104
105
106
107
108
109
110 private static byte[] toByteArray(final InputStream inputStream) throws IOException {
111 final int buffSize = Math.max(4096, inputStream.available());
112 final ByteArrayOutputStream contents = new ByteArrayOutputStream(buffSize);
113 final byte[] buff = new byte[buffSize];
114
115 int length = inputStream.read(buff);
116 while (length > 0) {
117 contents.write(buff, 0, length);
118 length = inputStream.read(buff);
119 }
120 return contents.toByteArray();
121 }
122
123
124
125
126
127
128
129 public File getFile() {
130 return file;
131 }
132
133
134
135
136
137
138
139 public URL getURL() {
140 return url;
141 }
142
143
144
145
146
147 public URI getURI() {
148 URI sourceURI = null;
149 if (url != null) {
150 try {
151 sourceURI = url.toURI();
152 } catch (final URISyntaxException ex) {
153
154 }
155 }
156 if (sourceURI == null && file != null) {
157 sourceURI = file.toURI();
158 }
159 if (sourceURI == null && location != null) {
160 try {
161 sourceURI = new URI(location);
162 } catch (final URISyntaxException ex) {
163
164 try {
165 sourceURI = new URI("file://" + location);
166 } catch (final URISyntaxException uriEx) {
167
168 }
169 }
170 }
171 return sourceURI;
172 }
173
174
175
176
177
178
179
180 public String getLocation() {
181 return location;
182 }
183
184
185
186
187
188
189 public InputStream getInputStream() {
190 return stream;
191 }
192
193
194
195
196
197
198
199 public ConfigurationSource resetInputStream() throws IOException {
200 if (file != null) {
201 return new ConfigurationSource(new FileInputStream(file), file);
202 } else if (url != null) {
203 return new ConfigurationSource(url.openStream(), url);
204 } else {
205 return new ConfigurationSource(data);
206 }
207 }
208
209 @Override
210 public String toString() {
211 if (location != null) {
212 return location;
213 }
214 if (this == NULL_SOURCE) {
215 return "NULL_SOURCE";
216 }
217 final int length = data == null ? -1 : data.length;
218 return "stream (" + length + " bytes, unknown location)";
219 }
220
221
222
223
224
225
226 public static ConfigurationSource fromUri(final URI configLocation) {
227 final File configFile = FileUtils.fileFromUri(configLocation);
228 if (configFile != null && configFile.exists() && configFile.canRead()) {
229 try {
230 return new ConfigurationSource(new FileInputStream(configFile), configFile);
231 } catch (final FileNotFoundException ex) {
232 ConfigurationFactory.LOGGER.error("Cannot locate file {}", configLocation.getPath(), ex);
233 }
234 }
235 if (ConfigurationFactory.isClassLoaderUri(configLocation)) {
236 final ClassLoader loader = LoaderUtil.getThreadContextClassLoader();
237 final String path = ConfigurationFactory.extractClassLoaderUriPath(configLocation);
238 final ConfigurationSource source = fromResource(path, loader);
239 if (source != null) {
240 return source;
241 }
242 }
243 if (!configLocation.isAbsolute()) {
244 ConfigurationFactory.LOGGER.error("File not found in file system or classpath: {}", configLocation.toString());
245 return null;
246 }
247 try {
248 return new ConfigurationSource(configLocation.toURL().openStream(), configLocation.toURL());
249 } catch (final MalformedURLException ex) {
250 ConfigurationFactory.LOGGER.error("Invalid URL {}", configLocation.toString(), ex);
251 } catch (final Exception ex) {
252 ConfigurationFactory.LOGGER.error("Unable to access {}", configLocation.toString(), ex);
253 }
254 return null;
255 }
256
257
258
259
260
261
262
263 public static ConfigurationSource fromResource(final String resource, final ClassLoader loader) {
264 final URL url = Loader.getResource(resource, loader);
265 if (url == null) {
266 return null;
267 }
268 InputStream is = null;
269 try {
270 is = url.openStream();
271 } catch (final IOException ioe) {
272 ConfigurationFactory.LOGGER.catching(Level.DEBUG, ioe);
273 return null;
274 }
275 if (is == null) {
276 return null;
277 }
278
279 if (FileUtils.isFile(url)) {
280 try {
281 return new ConfigurationSource(is, FileUtils.fileFromUri(url.toURI()));
282 } catch (final URISyntaxException ex) {
283
284 ConfigurationFactory.LOGGER.catching(Level.DEBUG, ex);
285 }
286 }
287 return new ConfigurationSource(is, url);
288 }
289 }