Skip to content

Commit 39daa18

Browse files
authored
Merge pull request #124 from ndw/envar
Load properties from the environment
2 parents 15315d9 + 142186e commit 39daa18

File tree

6 files changed

+103
-48
lines changed

6 files changed

+103
-48
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
basename=xmlresolver
22

3-
resolverVersion=4.5.2
3+
resolverVersion=4.6.0
44

55
group=org.xmlresolver
66

src/main/java/org/xmlresolver/XMLResolverConfiguration.java

Lines changed: 64 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
import javax.xml.parsers.ParserConfigurationException;
1414
import javax.xml.parsers.SAXParser;
1515
import javax.xml.parsers.SAXParserFactory;
16+
import java.io.File;
1617
import java.io.IOException;
1718
import java.io.InputStream;
1819
import java.lang.reflect.Constructor;
1920
import java.lang.reflect.InvocationTargetException;
2021
import java.net.*;
22+
import java.nio.file.Path;
23+
import java.nio.file.Paths;
2124
import java.util.*;
2225
import java.util.function.Supplier;
2326

@@ -325,6 +328,19 @@ public XMLResolverConfiguration(XMLResolverConfiguration current) {
325328
accessExternalDocument = current.accessExternalDocument;
326329
saxParserFactoryClass = current.saxParserFactoryClass;
327330
xmlReaderSupplier = current.xmlReaderSupplier;
331+
fixWindowsSystemIdentifiers = current.fixWindowsSystemIdentifiers;
332+
}
333+
334+
private String getConfigProperty(String name) {
335+
String property = System.getProperty(name);
336+
if (property == null) {
337+
String env = name
338+
.replaceAll("\\.", "_")
339+
.replaceAll("([a-z])([A-Z])", "$1_$2")
340+
.toUpperCase();
341+
property = System.getenv(env);
342+
}
343+
return property;
328344
}
329345

330346
private void loadConfiguration(List<URL> propertyFiles, List<String> catalogFiles) {
@@ -333,10 +349,7 @@ private void loadConfiguration(List<URL> propertyFiles, List<String> catalogFile
333349
ArrayList<URL> propertyFilesList = new ArrayList<>();
334350
if (propertyFiles == null) {
335351
// Do default initialization
336-
String propfn = System.getProperty("xmlresolver.properties");
337-
if (propfn == null) {
338-
propfn = System.getenv("XMLRESOLVER_PROPERTIES");
339-
}
352+
String propfn = getConfigProperty("xmlresolver.properties");
340353

341354
// Hack: you can set the xmlresolver.properties to the empty string
342355
// to avoid loading the XMLRESOLVER_PROPERTIES environment. This is
@@ -406,7 +419,7 @@ private void loadConfiguration(List<URL> propertyFiles, List<String> catalogFile
406419
}
407420

408421
private void loadSystemPropertiesConfiguration() {
409-
String property = System.getProperty("xml.catalog.files");
422+
String property = getConfigProperty("xml.catalog.files");
410423
if (property != null) {
411424
StringTokenizer tokens = new StringTokenizer(property, ";");
412425
showConfigChange("Catalog list cleared");
@@ -420,7 +433,7 @@ private void loadSystemPropertiesConfiguration() {
420433
}
421434
}
422435

423-
property = System.getProperty("xml.catalog.additions");
436+
property = getConfigProperty("xml.catalog.additions");
424437
if (property != null) {
425438
StringTokenizer tokens = new StringTokenizer(property, ";");
426439
while (tokens.hasMoreTokens()) {
@@ -432,121 +445,121 @@ private void loadSystemPropertiesConfiguration() {
432445
}
433446
}
434447

435-
property = System.getProperty("xml.catalog.prefer");
448+
property = getConfigProperty("xml.catalog.prefer");
436449
if (property != null) {
437450
showConfigChange("Prefer public: %s", property);
438451
preferPublic = "public".equals(property);
439452
}
440453

441-
property = System.getProperty("xml.catalog.preferPropertyFile");
454+
property = getConfigProperty("xml.catalog.preferPropertyFile");
442455
if (property != null) {
443456
showConfigChange("Prefer propertyFile: %s", property);
444457
preferPropertyFile = isTrue(property);
445458
}
446459

447-
property = System.getProperty("xml.catalog.allowPI");
460+
property = getConfigProperty("xml.catalog.allowPI");
448461
if (property != null) {
449462
showConfigChange("Allow catalog PI: %s", property);
450463
allowCatalogPI = isTrue(property);
451464
}
452465

453-
property = System.getProperty("xml.catalog.cache");
466+
property = getConfigProperty("xml.catalog.cache");
454467
if (property != null) {
455468
showConfigChange("Cache directory: %s", property);
456469
cacheDirectory = property;
457470
}
458471

459-
property = System.getProperty("xml.catalog.cacheUnderHome");
472+
property = getConfigProperty("xml.catalog.cacheUnderHome");
460473
if (property != null) {
461474
showConfigChange("Cache under home: %s", property);
462475
cacheUnderHome = isTrue(property);
463476
}
464477

465-
property = System.getProperty("xml.catalog.cacheEnabled");
478+
property = getConfigProperty("xml.catalog.cacheEnabled");
466479
if (property != null) {
467480
showConfigChange("Cache enabled: %s", property);
468481
cacheEnabled = isTrue(property);
469482
}
470483

471-
property = System.getProperty("xml.catalog.uriForSystem");
484+
property = getConfigProperty("xml.catalog.uriForSystem");
472485
if (property != null) {
473486
showConfigChange("URI-for-system: %s", property);
474487
uriForSystem = isTrue(property);
475488
}
476489

477-
property = System.getProperty("xml.catalog.mergeHttps");
490+
property = getConfigProperty("xml.catalog.mergeHttps");
478491
if (property != null) {
479492
showConfigChange("Merge-https: %s", property);
480493
mergeHttps = isTrue(property);
481494
}
482495

483-
property = System.getProperty("xml.catalog.maskJarUris");
496+
property = getConfigProperty("xml.catalog.maskJarUris");
484497
if (property != null) {
485498
showConfigChange("Mask-jar-URIs: %s", property);
486499
maskJarUris = isTrue(property);
487500
}
488501

489-
property = System.getProperty("xml.catalog.catalogLoaderClass");
502+
property = getConfigProperty("xml.catalog.catalogLoaderClass");
490503
if (property != null) {
491504
showConfigChange("Catalog loader: %s", property);
492505
catalogLoader = property;
493506
}
494507

495-
property = System.getProperty("xml.catalog.parseRddl");
508+
property = getConfigProperty("xml.catalog.parseRddl");
496509
if (property != null) {
497510
showConfigChange("Use RDDL: %s", property);
498511
parseRddl = isTrue(property);
499512
}
500513

501-
property = System.getProperty("xml.catalog.classpathCatalogs");
514+
property = getConfigProperty("xml.catalog.classpathCatalogs");
502515
if (property != null) {
503516
showConfigChange("Classpath catalogs: %s", property);
504517
classpathCatalogs = isTrue(property);
505518
}
506519

507-
property = System.getProperty("xml.catalog.archivedCatalogs");
520+
property = getConfigProperty("xml.catalog.archivedCatalogs");
508521
if (property != null) {
509522
showConfigChange("Archived catalogs: %s", property);
510523
archivedCatalogs = isTrue(property);
511524
}
512525

513-
property = System.getProperty("xml.catalog.throwUriExceptions");
526+
property = getConfigProperty("xml.catalog.throwUriExceptions");
514527
if (property != null) {
515528
showConfigChange("Throw URI exceptions: %s", property);
516529
throwUriExceptions = isTrue(property);
517530
}
518531

519-
property = System.getProperty("xml.catalog.resolverLoggerClass");
532+
property = getConfigProperty("xml.catalog.resolverLoggerClass");
520533
if (property != null) {
521534
showConfigChange("Resolver logger class: %s", property);
522535
resolverLoggerClass = property;
523536
}
524537

525-
property = System.getProperty("xml.catalog.defaultLoggerLogLevel");
538+
property = getConfigProperty("xml.catalog.defaultLoggerLogLevel");
526539
if (property != null) {
527540
showConfigChange("Default logger log level: %s", property);
528541
defaultLoggerLogLevel = property;
529542
}
530543

531-
property = System.getProperty("xml.catalog.accessExternalEntity");
544+
property = getConfigProperty("xml.catalog.accessExternalEntity");
532545
if (property != null) {
533546
showConfigChange("Access external entity: %s", property);
534547
accessExternalEntity = property;
535548
}
536549

537-
property = System.getProperty("xml.catalog.accessExternalDocument");
550+
property = getConfigProperty("xml.catalog.accessExternalDocument");
538551
if (property != null) {
539552
showConfigChange("Access external document: %s", property);
540553
accessExternalDocument = property;
541554
}
542555

543-
property = System.getProperty("xml.catalog.saxParserFactoryClass");
556+
property = getConfigProperty("xml.catalog.saxParserFactoryClass");
544557
if (property != null) {
545558
showConfigChange("SAXParserFactory class: %s", property);
546559
saxParserFactoryClass = property;
547560
}
548561

549-
property = System.getProperty("xml.catalog.fixWindowsSystemIdentifiers");
562+
property = getConfigProperty("xml.catalog.fixWindowsSystemIdentifiers");
550563
if (property != null) {
551564
showConfigChange("Fix windows system identifiers: %s", property);
552565
fixWindowsSystemIdentifiers = isTrue(property);
@@ -556,7 +569,7 @@ private void loadSystemPropertiesConfiguration() {
556569
private void loadPropertiesConfiguration(URL propertiesURL, Properties properties) {
557570
// Bit of a hack here.
558571
String property = properties.getProperty("catalog-logging");
559-
if (property != null && System.getProperty("xml.catalog.logging") == null) {
572+
if (property != null && getConfigProperty("xml.catalog.logging") == null) {
560573
System.setProperty("xml.catalog.logging", property);
561574
}
562575

@@ -732,7 +745,7 @@ private void loadPropertiesConfiguration(URL propertiesURL, Properties propertie
732745
}
733746

734747
private void showConfig() {
735-
resolverLogger.log(AbstractLogger.CONFIG, "Logging: %s", System.getProperty("xml.catalog.logging"));
748+
resolverLogger.log(AbstractLogger.CONFIG, "Logging: %s", getConfigProperty("xml.catalog.logging"));
736749
resolverLogger.log(AbstractLogger.CONFIG, "Prefer public: %s", preferPublic);
737750
resolverLogger.log(AbstractLogger.CONFIG, "Prefer property file: %s", preferPropertyFile);
738751
resolverLogger.log(AbstractLogger.CONFIG, "Allow catalog PI: %s", allowCatalogPI);
@@ -1012,20 +1025,31 @@ private void showConfigChange(String message, Object value) {
10121025

10131026
private List<String> findClasspathCatalogFiles() {
10141027
if (classpathCatalogList == null) {
1015-
resolverLogger.log(AbstractLogger.CONFIG, "Searching for catalogs on classpath:");
1016-
try {
1017-
for (URL url : ((URLClassLoader) XMLResolverConfiguration.class.getClassLoader()).getURLs()) {
1018-
resolverLogger.log(AbstractLogger.CONFIG, " " + url.toString());
1028+
// Set to avoid duplicates
1029+
ArrayList<String> catalogs = new ArrayList<>();
1030+
1031+
// Starting in Java 9, the class loader is no longer a URLClassLoader, so the .getURLs()
1032+
// trick doesn't work. Instead, we're just going to assume that the java.class.path
1033+
// system property is correct. It seems to be.
1034+
String sep = System.getProperty("path.separator");
1035+
String cpath = System.getProperty("java.class.path");
1036+
if (sep != null && cpath != null) {
1037+
resolverLogger.log(AbstractLogger.CONFIG, "Searching for catalogs on classpath:");
1038+
for (String loc : cpath.split(sep)) {
1039+
File dir = new File(loc);
1040+
if (dir.exists() && dir.isDirectory()) {
1041+
Path path = Paths.get(loc, "catalog.xml");
1042+
if (path.toFile().exists()) {
1043+
catalogs.add(path.toString());
1044+
}
1045+
}
10191046
}
1020-
} catch (ClassCastException ex) {
1021-
resolverLogger.log(AbstractLogger.CONFIG, " Unknown: cannot determine class path in JDK9+");
10221047
}
1023-
ArrayList<String> catalogs = new ArrayList<>();
1048+
10241049
try {
10251050
Enumeration<URL> resources = XMLResolverConfiguration.class.getClassLoader().getResources("org/xmlresolver/catalog.xml");
10261051
while (resources.hasMoreElements()) {
10271052
URL catalog = resources.nextElement();
1028-
resolverLogger.log(AbstractLogger.CONFIG, "Catalog: " + catalog.toString());
10291053
catalogs.add(catalog.toString());
10301054
}
10311055
} catch (IOException ex) {
@@ -1171,8 +1195,10 @@ private static boolean isTrue(String aString) {
11711195

11721196
private static class FallbackLogger extends AbstractLogger {
11731197
private final ArrayList<Message> messages = new ArrayList<>();
1174-
private final String fallbackLogging = System.getProperty("xml.catalog.FallbackLoggerLogLevel");
1175-
1198+
private final String fallbackLogging
1199+
= System.getProperty("xml.catalog.FallbackLoggerLogLevel") != null
1200+
? System.getProperty("xml.catalog.FallbackLoggerLogLevel")
1201+
: System.getenv("XML_CATALOG_FALLBACK_LOGGER_LOG_LEVEL");
11761202

11771203
@Override
11781204
public void log(String cat, String message, Object... params) {

src/test/java/org/xmlresolver/CatalogResolverTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class CatalogResolverTest {
1919
public void setup() {
2020
config = new XMLResolverConfiguration(catalog);
2121
config.setFeature(ResolverFeature.CACHE_DIRECTORY, "/tmp/y/cache");
22+
config.addCatalog("build/resources/test/manual-catalog.xml");
2223
resolver = new Resolver(config);
2324

2425
// Make sure the Docker container is running where we expect.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.xmlresolver;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class ConfigTest {
7+
8+
private String renameProperty(String name) {
9+
String env = name.replaceAll("\\.", "_")
10+
.replaceAll("([a-z])([A-Z])", "$1_$2")
11+
.toUpperCase();
12+
return env;
13+
}
14+
15+
@Test
16+
public void testDot() {
17+
Assert.assertEquals("XML_RESOLVER", renameProperty("xml.resolver"));
18+
}
19+
20+
@Test
21+
public void testDots() {
22+
Assert.assertEquals("XML_RESOLVER_TEST", renameProperty("xml.resolver.test"));
23+
}
24+
25+
@Test
26+
public void testMixedCaseSingle() {
27+
Assert.assertEquals("XML_RESOLVER_TEST", renameProperty("xml.resolverTest"));
28+
}
29+
30+
@Test
31+
public void testMixedCaseMultiple() {
32+
Assert.assertEquals("XML_CATALOG_PREFER_PROPERTY_FILE", renameProperty("xml.catalog.preferPropertyFile"));
33+
}
34+
}

src/test/java/org/xmlresolver/LoaderTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class LoaderTest {
1616
@Test
1717
public void nonValidatingValidCatalog() {
1818
XMLResolverConfiguration config = new XMLResolverConfiguration(Collections.emptyList(), Collections.emptyList());
19-
config.setFeature(ResolverFeature.CATALOG_FILES, Collections.singletonList("classpath:/catalog.xml"));
19+
config.setFeature(ResolverFeature.CATALOG_FILES, Collections.singletonList("classpath:/manual-catalog.xml"));
2020
config.setFeature(ResolverFeature.CATALOG_LOADER_CLASS, "org.xmlresolver.loaders.XmlLoader");
2121
CatalogManager manager = config.getFeature(ResolverFeature.CATALOG_MANAGER);
2222
URI rsrc = manager.lookupSystem("https://xmlresolver.org/ns/sample/sample.dtd");
@@ -36,7 +36,7 @@ public void nonValidatingInvalidCatalog() {
3636
@Test
3737
public void validatingValidCatalog() {
3838
XMLResolverConfiguration config = new XMLResolverConfiguration(Collections.emptyList(), Collections.emptyList());
39-
config.setFeature(ResolverFeature.CATALOG_FILES, Collections.singletonList("classpath:/catalog.xml"));
39+
config.setFeature(ResolverFeature.CATALOG_FILES, Collections.singletonList("classpath:/manual-catalog.xml"));
4040
config.setFeature(ResolverFeature.CATALOG_LOADER_CLASS, "org.xmlresolver.loaders.ValidatingXmlLoader");
4141
config.setFeature(ResolverFeature.CLASSPATH_CATALOGS, false);
4242
CatalogManager manager = config.getFeature(ResolverFeature.CATALOG_MANAGER);
@@ -86,7 +86,7 @@ public void validatingInvalidCatalog() {
8686
public void validatingMissingCatalog() {
8787
// File not found isn't a validation error
8888
XMLResolverConfiguration config = new XMLResolverConfiguration(Collections.emptyList(), Collections.emptyList());
89-
config.setFeature(ResolverFeature.CATALOG_FILES, Arrays.asList("./not-a-catalog-in-sight.xml", "classpath:/catalog.xml"));
89+
config.setFeature(ResolverFeature.CATALOG_FILES, Arrays.asList("./not-a-catalog-in-sight.xml", "classpath:/manual-catalog.xml"));
9090
config.setFeature(ResolverFeature.CATALOG_LOADER_CLASS, "org.xmlresolver.loaders.ValidatingXmlLoader");
9191
config.setFeature(ResolverFeature.CLASSPATH_CATALOGS, false);
9292
CatalogManager manager = config.getFeature(ResolverFeature.CATALOG_MANAGER);

src/test/resources/catalog.xml renamed to src/test/resources/manual-catalog.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,4 @@
4141
<uri name="http://www.w3.org/XML/1998/namespace"
4242
uri="xmlns-xml.html"/>
4343

44-
<uri name="http://www.w3.org/2001/xml.xsd" uri="xml.xsd"/>
45-
46-
<!--
47-
<nextCatalog catalog="classpath:/org/xmlresolver/data/catalog.xml"/>
48-
-->
49-
5044
</catalog>

0 commit comments

Comments
 (0)