|
25 | 25 | import java.util.Collections; |
26 | 26 | import java.util.List; |
27 | 27 | import java.util.Map; |
| 28 | +import java.util.Objects; |
28 | 29 | import java.util.concurrent.locks.Lock; |
29 | 30 | import java.util.concurrent.locks.ReentrantLock; |
30 | 31 | import org.apache.logging.log4j.Level; |
@@ -333,6 +334,73 @@ public Configuration getConfiguration( |
333 | 334 | return getConfiguration(loggerContext, name, configLocation); |
334 | 335 | } |
335 | 336 |
|
| 337 | + /** |
| 338 | + * Creates a Configuration from multiple configuration URIs. |
| 339 | + * If multiple URIs are successfully loaded, they will be combined into a CompositeConfiguration. |
| 340 | + * |
| 341 | + * @param loggerContext the logger context (may be null) |
| 342 | + * @param name the configuration name (may be null) |
| 343 | + * @param uris the list of configuration URIs (must not be null or empty) |
| 344 | + * @return a Configuration created from the provided URIs |
| 345 | + * @throws NullPointerException if uris is null |
| 346 | + * @throws IllegalArgumentException if uris is empty |
| 347 | + * @throws ConfigurationException if no valid configuration could be created |
| 348 | + * from any of the provided URIs |
| 349 | + * @since 2.26.0 |
| 350 | + */ |
| 351 | + public Configuration getConfiguration(final LoggerContext loggerContext, final String name, final List<URI> uris) { |
| 352 | + |
| 353 | + Objects.requireNonNull(uris, "uris parameter cannot be null"); |
| 354 | + |
| 355 | + if (uris.isEmpty()) { |
| 356 | + throw new IllegalArgumentException("URI list cannot be empty"); |
| 357 | + } |
| 358 | + |
| 359 | + if (uris.size() == 1) { |
| 360 | + final Configuration config = getConfiguration(loggerContext, name, uris.get(0)); |
| 361 | + if (config == null) { |
| 362 | + throw new ConfigurationException("Failed to create configuration from: " + uris.get(0)); |
| 363 | + } |
| 364 | + return config; |
| 365 | + } |
| 366 | + |
| 367 | + final List<AbstractConfiguration> configurations = new ArrayList<>(); |
| 368 | + final List<URI> failedUris = new ArrayList<>(); |
| 369 | + |
| 370 | + for (final URI uri : uris) { |
| 371 | + try { |
| 372 | + final Configuration config = getConfiguration(loggerContext, name, uri); |
| 373 | + |
| 374 | + if (config != null) { |
| 375 | + if (config instanceof AbstractConfiguration) { |
| 376 | + configurations.add((AbstractConfiguration) config); |
| 377 | + } else { |
| 378 | + LOGGER.error("Configuration at {} is not an AbstractConfiguration", uri); |
| 379 | + failedUris.add(uri); |
| 380 | + } |
| 381 | + } else { |
| 382 | + LOGGER.debug("Unable to load configuration from: {}", uri); |
| 383 | + failedUris.add(uri); |
| 384 | + } |
| 385 | + } catch (final Exception ex) { |
| 386 | + LOGGER.debug("Error loading configuration from {}: {}", uri, ex.getMessage()); |
| 387 | + failedUris.add(uri); |
| 388 | + } |
| 389 | + } |
| 390 | + |
| 391 | + if (configurations.isEmpty()) { |
| 392 | + throw new ConfigurationException(String.format( |
| 393 | + "Failed to create configuration from any of the %d URIs. Failed URIs: %s", |
| 394 | + uris.size(), failedUris)); |
| 395 | + } |
| 396 | + |
| 397 | + if (!failedUris.isEmpty()) { |
| 398 | + LOGGER.warn("Failed to load configurations from: {}", failedUris); |
| 399 | + } |
| 400 | + |
| 401 | + return configurations.size() == 1 ? configurations.get(0) : new CompositeConfiguration(configurations); |
| 402 | + } |
| 403 | + |
336 | 404 | static boolean isClassLoaderUri(final URI uri) { |
337 | 405 | if (uri == null) { |
338 | 406 | return false; |
|
0 commit comments