1
1
/**
2
- * (C) Copyright IBM Corporation 2017, 2023 .
2
+ * (C) Copyright IBM Corporation 2017, 2024 .
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
53
53
// Moved from ci.maven/liberty-maven-plugin/src/main/java/net/wasdev/wlp/maven/plugins/ServerConfigDocument.java
54
54
public class ServerConfigDocument {
55
55
56
- private static ServerConfigDocument instance ;
56
+ private CommonLoggerI log ;
57
57
58
- private static CommonLoggerI log ;
58
+ private File configDirectory ;
59
+ private File serverXMLFile ;
59
60
60
- private static DocumentBuilder docBuilder ;
61
-
62
- private static File configDirectory ;
63
- private static File serverXMLFile ;
64
-
65
- private static Set <String > names ;
66
- private static Set <String > namelessLocations ;
67
- private static Set <String > locations ;
68
- private static HashMap <String , String > locationsAndNames ;
69
- private static Properties props ;
70
- private static Properties defaultProps ;
71
- private static Map <String , File > libertyDirectoryPropertyToFile = null ;
61
+ private Set <String > names ;
62
+ private Set <String > namelessLocations ;
63
+ private Set <String > locations ;
64
+ private HashMap <String , String > locationsAndNames ;
65
+ private Properties props ;
66
+ private Properties defaultProps ;
67
+ private Map <String , File > libertyDirectoryPropertyToFile = null ;
72
68
73
69
private static final XPathExpression XPATH_SERVER_APPLICATION ;
74
70
private static final XPathExpression XPATH_SERVER_WEB_APPLICATION ;
@@ -104,86 +100,49 @@ public Set<String> getNamelessLocations() {
104
100
return namelessLocations ;
105
101
}
106
102
107
- public static Properties getProperties () {
103
+ public Properties getProperties () {
108
104
return props ;
109
105
}
110
106
111
- public static Map <String , File > getLibertyDirPropertyFiles () {
107
+ public Map <String , File > getLibertyDirPropertyFiles () {
112
108
return libertyDirectoryPropertyToFile ;
113
109
}
114
110
115
- public static Properties getDefaultProperties () {
111
+ public Properties getDefaultProperties () {
116
112
return defaultProps ;
117
113
}
118
114
119
- private static File getServerXML () {
115
+ public File getServerXML () {
120
116
return serverXMLFile ;
121
117
}
122
118
123
- public ServerConfigDocument (CommonLoggerI log , File serverXML , File configDir , File bootstrapFile ,
124
- Map <String , String > bootstrapProp , File serverEnvFile ) {
125
- this (log , serverXML , configDir , bootstrapFile , bootstrapProp , serverEnvFile , true , null );
126
- }
127
-
128
- public ServerConfigDocument (CommonLoggerI log , File serverXML , File configDir , File bootstrapFile ,
129
- Map <String , String > bootstrapProp , File serverEnvFile , boolean giveConfigDirPrecedence ) {
130
- this (log , serverXML , configDir , bootstrapFile , bootstrapProp , serverEnvFile , giveConfigDirPrecedence , null );
131
- }
132
-
133
119
public ServerConfigDocument (CommonLoggerI log , File serverXML , File configDir , File bootstrapFile ,
134
120
Map <String , String > bootstrapProp , File serverEnvFile , boolean giveConfigDirPrecedence , Map <String , File > libertyDirPropertyFiles ) {
135
121
initializeAppsLocation (log , serverXML , configDir , bootstrapFile , bootstrapProp , serverEnvFile , giveConfigDirPrecedence , libertyDirPropertyFiles );
136
122
}
137
123
138
- private static DocumentBuilder getDocumentBuilder () {
139
- if (docBuilder == null ) {
140
- DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory .newInstance ();
141
- docBuilderFactory .setIgnoringComments (true );
142
- docBuilderFactory .setCoalescing (true );
143
- docBuilderFactory .setIgnoringElementContentWhitespace (true );
144
- docBuilderFactory .setValidating (false );
145
- try {
146
- docBuilder = docBuilderFactory .newDocumentBuilder ();
147
- } catch (ParserConfigurationException e ) {
148
- // fail catastrophically if we can't create a document builder
149
- throw new RuntimeException (e );
150
- }
151
- }
152
- return docBuilder ;
153
- }
154
-
155
- /**
156
- * Nulls out cached instance so a new one will be created next time a getInstance() is done.
157
- * Not thread-safe.
158
- */
159
- public static void markInstanceStale () {
160
- instance = null ;
161
- }
162
-
163
- public static ServerConfigDocument getInstance (CommonLoggerI log , File serverXML , File configDir , File bootstrapFile ,
164
- Map <String , String > bootstrapProp , File serverEnvFile ) throws IOException {
165
- return getInstance (log , serverXML , configDir , bootstrapFile , bootstrapProp , serverEnvFile , true , null );
166
- }
167
-
168
- public static ServerConfigDocument getInstance (CommonLoggerI log , File serverXML , File configDir , File bootstrapFile ,
169
- Map <String , String > bootstrapProp , File serverEnvFile , boolean giveConfigDirPrecedence ) throws IOException {
170
- return getInstance (log , serverXML , configDir , bootstrapFile , bootstrapProp , serverEnvFile , giveConfigDirPrecedence , null );
171
- }
124
+ private DocumentBuilder getDocumentBuilder () {
125
+ DocumentBuilder docBuilder ;
172
126
173
- public static ServerConfigDocument getInstance (CommonLoggerI log , File serverXML , File configDir , File bootstrapFile ,
174
- Map <String , String > bootstrapProp , File serverEnvFile , boolean giveConfigDirPrecedence , Map <String , File > libertyDirPropertyFiles ) throws IOException {
175
- // Initialize if instance is not created yet, or source server xml file
176
- // location has been changed.
177
- if (instance == null || !serverXML .getCanonicalPath ().equals (getServerXML ().getCanonicalPath ())) {
178
- instance = new ServerConfigDocument (log , serverXML , configDir , bootstrapFile , bootstrapProp , serverEnvFile , giveConfigDirPrecedence , libertyDirPropertyFiles );
127
+ DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory .newInstance ();
128
+ docBuilderFactory .setIgnoringComments (true );
129
+ docBuilderFactory .setCoalescing (true );
130
+ docBuilderFactory .setIgnoringElementContentWhitespace (true );
131
+ docBuilderFactory .setValidating (false );
132
+ try {
133
+ docBuilder = docBuilderFactory .newDocumentBuilder ();
134
+ } catch (ParserConfigurationException e ) {
135
+ // fail catastrophically if we can't create a document builder
136
+ throw new RuntimeException (e );
179
137
}
180
- return instance ;
138
+
139
+ return docBuilder ;
181
140
}
182
141
183
- private static void initializeAppsLocation (CommonLoggerI log , File serverXML , File configDir , File bootstrapFile ,
142
+ private void initializeAppsLocation (CommonLoggerI log , File serverXML , File configDir , File bootstrapFile ,
184
143
Map <String , String > bootstrapProp , File serverEnvFile , boolean giveConfigDirPrecedence , Map <String , File > libertyDirPropertyFiles ) {
185
144
try {
186
- ServerConfigDocument .log = log ;
145
+ this .log = log ;
187
146
serverXMLFile = serverXML ;
188
147
configDirectory = configDir ;
189
148
if (libertyDirPropertyFiles != null ) {
@@ -268,7 +227,7 @@ private static void initializeAppsLocation(CommonLoggerI log, File serverXML, Fi
268
227
}
269
228
270
229
//Checks for application names in the document. Will add locations without names to a Set
271
- private static void parseNames (Document doc , String expression ) throws XPathExpressionException , IOException , SAXException {
230
+ private void parseNames (Document doc , String expression ) throws XPathExpressionException , IOException , SAXException {
272
231
// parse input document
273
232
XPath xPath = XPathFactory .newInstance ().newXPath ();
274
233
NodeList nodeList = (NodeList ) xPath .compile (expression ).evaluate (doc , XPathConstants .NODESET );
@@ -315,7 +274,7 @@ private static void parseNames(Document doc, String expression) throws XPathExpr
315
274
}
316
275
}
317
276
318
- public static String findNameForLocation (String location ) {
277
+ public String findNameForLocation (String location ) {
319
278
String appName = locationsAndNames .get (location );
320
279
321
280
if (appName == null || appName .isEmpty ()) {
@@ -325,7 +284,7 @@ public static String findNameForLocation(String location) {
325
284
return appName ;
326
285
}
327
286
328
- private static void parseApplication (Document doc , XPathExpression expression ) throws XPathExpressionException {
287
+ private void parseApplication (Document doc , XPathExpression expression ) throws XPathExpressionException {
329
288
330
289
NodeList nodeList = (NodeList ) expression .evaluate (doc , XPathConstants .NODESET );
331
290
@@ -349,7 +308,7 @@ private static void parseApplication(Document doc, XPathExpression expression) t
349
308
}
350
309
}
351
310
352
- private static void parseInclude (Document doc ) throws XPathExpressionException , IOException , SAXException {
311
+ private void parseInclude (Document doc ) throws XPathExpressionException , IOException , SAXException {
353
312
// parse include document in source server xml
354
313
NodeList nodeList = (NodeList ) XPATH_SERVER_INCLUDE .evaluate (doc , XPathConstants .NODESET );
355
314
@@ -378,7 +337,7 @@ private static void parseInclude(Document doc) throws XPathExpressionException,
378
337
}
379
338
}
380
339
381
- private static void parseConfigDropinsDir () throws XPathExpressionException , IOException , SAXException {
340
+ private void parseConfigDropinsDir () throws XPathExpressionException , IOException , SAXException {
382
341
File configDropins = getConfigDropinsDir ();
383
342
384
343
if (configDropins == null || !configDropins .exists ()) {
@@ -396,7 +355,7 @@ private static void parseConfigDropinsDir() throws XPathExpressionException, IOE
396
355
}
397
356
}
398
357
399
- private static void parseDropinsFiles (File [] files ) throws XPathExpressionException , IOException , SAXException {
358
+ private void parseDropinsFiles (File [] files ) throws XPathExpressionException , IOException , SAXException {
400
359
Arrays .sort (files , NameFileComparator .NAME_INSENSITIVE_COMPARATOR );
401
360
for (File file : files ) {
402
361
if (file .isFile ()) {
@@ -405,7 +364,7 @@ private static void parseDropinsFiles(File[] files) throws XPathExpressionExcept
405
364
}
406
365
}
407
366
408
- private static void parseDropinsFile (File file ) throws IOException , XPathExpressionException , SAXException {
367
+ private void parseDropinsFile (File file ) throws IOException , XPathExpressionException , SAXException {
409
368
// get input XML Document
410
369
Document doc = parseDocument (file );
411
370
if (doc != null ) {
@@ -416,7 +375,7 @@ private static void parseDropinsFile(File file) throws IOException, XPathExpress
416
375
}
417
376
}
418
377
419
- private static ArrayList <Document > getIncludeDocs (String loc ) throws IOException , SAXException {
378
+ private ArrayList <Document > getIncludeDocs (String loc ) throws IOException , SAXException {
420
379
ArrayList <Document > docs = new ArrayList <Document >();
421
380
Document doc = null ;
422
381
File locFile = null ;
@@ -467,7 +426,7 @@ private static ArrayList<Document> getIncludeDocs(String loc) throws IOException
467
426
* @throws IOException
468
427
* @throws SAXException
469
428
*/
470
- private static void parseDocumentFromFileOrDirectory (File f , String locationString , ArrayList <Document > docs ) throws FileNotFoundException , IOException , SAXException {
429
+ private void parseDocumentFromFileOrDirectory (File f , String locationString , ArrayList <Document > docs ) throws FileNotFoundException , IOException , SAXException {
471
430
Document doc = null ;
472
431
// Earlier call to VariableUtility.resolveVariables() already converts all \ to /
473
432
boolean isLibertyDirectory = locationString .endsWith ("/" ); // Liberty uses this to determine if directory.
@@ -499,7 +458,7 @@ private static void parseDocumentFromFileOrDirectory(File f, String locationStri
499
458
* @param docs - ArrayList to store parsed Documents.
500
459
* @throws IOException
501
460
*/
502
- private static void parseDocumentsInDirectory (File directory , ArrayList <Document > docs ) {
461
+ private void parseDocumentsInDirectory (File directory , ArrayList <Document > docs ) {
503
462
// OpenLiberty reference code for behavior: https://github.com/OpenLiberty/open-liberty
504
463
// ServerXMLConfiguration.java:parseDirectoryFiles() and XMLConfigParser.java:parseInclude()
505
464
File [] files = directory .listFiles ();
@@ -521,7 +480,7 @@ private static void parseDocumentsInDirectory(File directory, ArrayList<Document
521
480
* @throws IOException
522
481
* @throws SAXException
523
482
*/
524
- private static Document parseDocument (File file ) throws FileNotFoundException , IOException {
483
+ private Document parseDocument (File file ) throws FileNotFoundException , IOException {
525
484
try (FileInputStream is = new FileInputStream (file )) {
526
485
return parseDocument (is );
527
486
} catch (SAXException ex ) {
@@ -532,20 +491,20 @@ private static Document parseDocument(File file) throws FileNotFoundException, I
532
491
}
533
492
}
534
493
535
- private static Document parseDocument (URL url ) throws IOException , SAXException {
494
+ private Document parseDocument (URL url ) throws IOException , SAXException {
536
495
URLConnection connection = url .openConnection ();
537
496
try (InputStream is = connection .getInputStream ()) {
538
497
return parseDocument (is );
539
498
}
540
499
}
541
500
542
- private static Document parseDocument (InputStream in ) throws SAXException , IOException {
501
+ private Document parseDocument (InputStream in ) throws SAXException , IOException {
543
502
try (InputStream ins = in ) { // ins will be auto-closed
544
503
return getDocumentBuilder ().parse (ins );
545
504
}
546
505
}
547
506
548
- private static void parseProperties (InputStream ins ) throws Exception {
507
+ private void parseProperties (InputStream ins ) throws Exception {
549
508
try {
550
509
props .load (ins );
551
510
} catch (Exception e ) {
@@ -557,7 +516,7 @@ private static void parseProperties(InputStream ins) throws Exception {
557
516
}
558
517
}
559
518
560
- private static boolean isValidURL (String url ) {
519
+ private boolean isValidURL (String url ) {
561
520
try {
562
521
URL testURL = new URL (url );
563
522
testURL .toURI ();
@@ -568,19 +527,19 @@ private static boolean isValidURL(String url) {
568
527
}
569
528
570
529
571
- private static void parseVariablesForDefaultValues (Document doc ) throws XPathExpressionException {
530
+ private void parseVariablesForDefaultValues (Document doc ) throws XPathExpressionException {
572
531
parseVariables (doc , true , false , false );
573
532
}
574
533
575
- private static void parseVariablesForValues (Document doc ) throws XPathExpressionException {
534
+ private void parseVariablesForValues (Document doc ) throws XPathExpressionException {
576
535
parseVariables (doc , false , true , false );
577
536
}
578
537
579
- private static void parseVariablesForBothValues (Document doc ) throws XPathExpressionException {
538
+ private void parseVariablesForBothValues (Document doc ) throws XPathExpressionException {
580
539
parseVariables (doc , false , false , true );
581
540
}
582
541
583
- private static void parseVariables (Document doc , boolean defaultValues , boolean values , boolean both ) throws XPathExpressionException {
542
+ private void parseVariables (Document doc , boolean defaultValues , boolean values , boolean both ) throws XPathExpressionException {
584
543
// parse input document
585
544
NodeList nodeList = (NodeList ) XPATH_SERVER_VARIABLE .evaluate (doc , XPathConstants .NODESET );
586
545
@@ -605,7 +564,7 @@ private static void parseVariables(Document doc, boolean defaultValues, boolean
605
564
}
606
565
}
607
566
608
- private static String getValue (NamedNodeMap attr , String nodeName ) {
567
+ private String getValue (NamedNodeMap attr , String nodeName ) {
609
568
String value = null ;
610
569
Node valueNode = attr .getNamedItem (nodeName );
611
570
if (valueNode != null ) {
@@ -614,7 +573,7 @@ private static String getValue(NamedNodeMap attr, String nodeName) {
614
573
return value ;
615
574
}
616
575
617
- private static void parseIncludeVariables (Document doc ) throws XPathExpressionException , IOException , SAXException {
576
+ private void parseIncludeVariables (Document doc ) throws XPathExpressionException , IOException , SAXException {
618
577
// parse include document in source server xml
619
578
NodeList nodeList = (NodeList ) XPATH_SERVER_INCLUDE .evaluate (doc , XPathConstants .NODESET );
620
579
@@ -641,7 +600,7 @@ private static void parseIncludeVariables(Document doc) throws XPathExpressionEx
641
600
}
642
601
}
643
602
644
- private static File getConfigDropinsDir () {
603
+ private File getConfigDropinsDir () {
645
604
File configDropins = null ;
646
605
647
606
// if configDirectory exists and contains configDropins directory,
@@ -656,7 +615,7 @@ private static File getConfigDropinsDir() {
656
615
return configDropins ;
657
616
}
658
617
659
- private static void parseConfigDropinsDirVariables (String inDir )
618
+ private void parseConfigDropinsDirVariables (String inDir )
660
619
throws XPathExpressionException , SAXException , IOException {
661
620
File configDropins = getConfigDropinsDir ();
662
621
if (configDropins == null || !configDropins .exists ()) {
@@ -677,7 +636,7 @@ private static void parseConfigDropinsDirVariables(String inDir)
677
636
}
678
637
}
679
638
680
- private static void parseDropinsFilesVariables (File file )
639
+ private void parseDropinsFilesVariables (File file )
681
640
throws SAXException , IOException , XPathExpressionException {
682
641
// get input XML Document
683
642
Document doc = parseDocument (file );
@@ -693,7 +652,7 @@ private static void parseDropinsFilesVariables(File file)
693
652
* If giveConfigDirPrecedence is set to false, return specificFile if it exists;
694
653
* otherwise return the file from the configDirectory if it exists, or null if not.
695
654
*/
696
- private static File findConfigFile (String fileName , File specificFile , boolean giveConfigDirPrecedence ) {
655
+ private File findConfigFile (String fileName , File specificFile , boolean giveConfigDirPrecedence ) {
697
656
File f = new File (configDirectory , fileName );
698
657
699
658
if (giveConfigDirPrecedence ) {
@@ -718,7 +677,7 @@ private static File findConfigFile(String fileName, File specificFile, boolean g
718
677
/*
719
678
* Get the file from configDrectory if it exists, or null if not
720
679
*/
721
- private static File getFileFromConfigDirectory (String file ) {
680
+ private File getFileFromConfigDirectory (String file ) {
722
681
File f = new File (configDirectory , file );
723
682
if (configDirectory != null && f .exists ()) {
724
683
return f ;
0 commit comments