Skip to content

Commit ca14a06

Browse files
authored
Merge pull request #435 from cherylking/makeThreadSafe
Remove static vars from ServerConfigDocument
2 parents 3397fe0 + 4aeb5d8 commit ca14a06

File tree

3 files changed

+61
-103
lines changed

3 files changed

+61
-103
lines changed

src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigDocument.java

Lines changed: 57 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* (C) Copyright IBM Corporation 2017, 2023.
2+
* (C) Copyright IBM Corporation 2017, 2024.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -53,22 +53,18 @@
5353
// Moved from ci.maven/liberty-maven-plugin/src/main/java/net/wasdev/wlp/maven/plugins/ServerConfigDocument.java
5454
public class ServerConfigDocument {
5555

56-
private static ServerConfigDocument instance;
56+
private CommonLoggerI log;
5757

58-
private static CommonLoggerI log;
58+
private File configDirectory;
59+
private File serverXMLFile;
5960

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;
7268

7369
private static final XPathExpression XPATH_SERVER_APPLICATION;
7470
private static final XPathExpression XPATH_SERVER_WEB_APPLICATION;
@@ -104,86 +100,49 @@ public Set<String> getNamelessLocations() {
104100
return namelessLocations;
105101
}
106102

107-
public static Properties getProperties() {
103+
public Properties getProperties() {
108104
return props;
109105
}
110106

111-
public static Map<String, File> getLibertyDirPropertyFiles() {
107+
public Map<String, File> getLibertyDirPropertyFiles() {
112108
return libertyDirectoryPropertyToFile;
113109
}
114110

115-
public static Properties getDefaultProperties() {
111+
public Properties getDefaultProperties() {
116112
return defaultProps;
117113
}
118114

119-
private static File getServerXML() {
115+
public File getServerXML() {
120116
return serverXMLFile;
121117
}
122118

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-
133119
public ServerConfigDocument(CommonLoggerI log, File serverXML, File configDir, File bootstrapFile,
134120
Map<String, String> bootstrapProp, File serverEnvFile, boolean giveConfigDirPrecedence, Map<String, File> libertyDirPropertyFiles) {
135121
initializeAppsLocation(log, serverXML, configDir, bootstrapFile, bootstrapProp, serverEnvFile, giveConfigDirPrecedence, libertyDirPropertyFiles);
136122
}
137123

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;
172126

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);
179137
}
180-
return instance;
138+
139+
return docBuilder;
181140
}
182141

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,
184143
Map<String, String> bootstrapProp, File serverEnvFile, boolean giveConfigDirPrecedence, Map<String, File> libertyDirPropertyFiles) {
185144
try {
186-
ServerConfigDocument.log = log;
145+
this.log = log;
187146
serverXMLFile = serverXML;
188147
configDirectory = configDir;
189148
if (libertyDirPropertyFiles != null) {
@@ -268,7 +227,7 @@ private static void initializeAppsLocation(CommonLoggerI log, File serverXML, Fi
268227
}
269228

270229
//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 {
272231
// parse input document
273232
XPath xPath = XPathFactory.newInstance().newXPath();
274233
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET);
@@ -315,7 +274,7 @@ private static void parseNames(Document doc, String expression) throws XPathExpr
315274
}
316275
}
317276

318-
public static String findNameForLocation(String location) {
277+
public String findNameForLocation(String location) {
319278
String appName = locationsAndNames.get(location);
320279

321280
if (appName == null || appName.isEmpty()) {
@@ -325,7 +284,7 @@ public static String findNameForLocation(String location) {
325284
return appName;
326285
}
327286

328-
private static void parseApplication(Document doc, XPathExpression expression) throws XPathExpressionException {
287+
private void parseApplication(Document doc, XPathExpression expression) throws XPathExpressionException {
329288

330289
NodeList nodeList = (NodeList) expression.evaluate(doc, XPathConstants.NODESET);
331290

@@ -349,7 +308,7 @@ private static void parseApplication(Document doc, XPathExpression expression) t
349308
}
350309
}
351310

352-
private static void parseInclude(Document doc) throws XPathExpressionException, IOException, SAXException {
311+
private void parseInclude(Document doc) throws XPathExpressionException, IOException, SAXException {
353312
// parse include document in source server xml
354313
NodeList nodeList = (NodeList) XPATH_SERVER_INCLUDE.evaluate(doc, XPathConstants.NODESET);
355314

@@ -378,7 +337,7 @@ private static void parseInclude(Document doc) throws XPathExpressionException,
378337
}
379338
}
380339

381-
private static void parseConfigDropinsDir() throws XPathExpressionException, IOException, SAXException {
340+
private void parseConfigDropinsDir() throws XPathExpressionException, IOException, SAXException {
382341
File configDropins = getConfigDropinsDir();
383342

384343
if (configDropins == null || !configDropins.exists()) {
@@ -396,7 +355,7 @@ private static void parseConfigDropinsDir() throws XPathExpressionException, IOE
396355
}
397356
}
398357

399-
private static void parseDropinsFiles(File[] files) throws XPathExpressionException, IOException, SAXException {
358+
private void parseDropinsFiles(File[] files) throws XPathExpressionException, IOException, SAXException {
400359
Arrays.sort(files, NameFileComparator.NAME_INSENSITIVE_COMPARATOR);
401360
for (File file : files) {
402361
if (file.isFile()) {
@@ -405,7 +364,7 @@ private static void parseDropinsFiles(File[] files) throws XPathExpressionExcept
405364
}
406365
}
407366

408-
private static void parseDropinsFile(File file) throws IOException, XPathExpressionException, SAXException {
367+
private void parseDropinsFile(File file) throws IOException, XPathExpressionException, SAXException {
409368
// get input XML Document
410369
Document doc = parseDocument(file);
411370
if (doc != null) {
@@ -416,7 +375,7 @@ private static void parseDropinsFile(File file) throws IOException, XPathExpress
416375
}
417376
}
418377

419-
private static ArrayList<Document> getIncludeDocs(String loc) throws IOException, SAXException {
378+
private ArrayList<Document> getIncludeDocs(String loc) throws IOException, SAXException {
420379
ArrayList<Document> docs = new ArrayList<Document>();
421380
Document doc = null;
422381
File locFile = null;
@@ -467,7 +426,7 @@ private static ArrayList<Document> getIncludeDocs(String loc) throws IOException
467426
* @throws IOException
468427
* @throws SAXException
469428
*/
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 {
471430
Document doc = null;
472431
// Earlier call to VariableUtility.resolveVariables() already converts all \ to /
473432
boolean isLibertyDirectory = locationString.endsWith("/"); // Liberty uses this to determine if directory.
@@ -499,7 +458,7 @@ private static void parseDocumentFromFileOrDirectory(File f, String locationStri
499458
* @param docs - ArrayList to store parsed Documents.
500459
* @throws IOException
501460
*/
502-
private static void parseDocumentsInDirectory(File directory, ArrayList<Document> docs) {
461+
private void parseDocumentsInDirectory(File directory, ArrayList<Document> docs) {
503462
// OpenLiberty reference code for behavior: https://github.com/OpenLiberty/open-liberty
504463
// ServerXMLConfiguration.java:parseDirectoryFiles() and XMLConfigParser.java:parseInclude()
505464
File[] files = directory.listFiles();
@@ -521,7 +480,7 @@ private static void parseDocumentsInDirectory(File directory, ArrayList<Document
521480
* @throws IOException
522481
* @throws SAXException
523482
*/
524-
private static Document parseDocument(File file) throws FileNotFoundException, IOException {
483+
private Document parseDocument(File file) throws FileNotFoundException, IOException {
525484
try (FileInputStream is = new FileInputStream(file)) {
526485
return parseDocument(is);
527486
} catch (SAXException ex) {
@@ -532,20 +491,20 @@ private static Document parseDocument(File file) throws FileNotFoundException, I
532491
}
533492
}
534493

535-
private static Document parseDocument(URL url) throws IOException, SAXException {
494+
private Document parseDocument(URL url) throws IOException, SAXException {
536495
URLConnection connection = url.openConnection();
537496
try (InputStream is = connection.getInputStream()) {
538497
return parseDocument(is);
539498
}
540499
}
541500

542-
private static Document parseDocument(InputStream in) throws SAXException, IOException {
501+
private Document parseDocument(InputStream in) throws SAXException, IOException {
543502
try (InputStream ins = in) { // ins will be auto-closed
544503
return getDocumentBuilder().parse(ins);
545504
}
546505
}
547506

548-
private static void parseProperties(InputStream ins) throws Exception {
507+
private void parseProperties(InputStream ins) throws Exception {
549508
try {
550509
props.load(ins);
551510
} catch (Exception e) {
@@ -557,7 +516,7 @@ private static void parseProperties(InputStream ins) throws Exception {
557516
}
558517
}
559518

560-
private static boolean isValidURL(String url) {
519+
private boolean isValidURL(String url) {
561520
try {
562521
URL testURL = new URL(url);
563522
testURL.toURI();
@@ -568,19 +527,19 @@ private static boolean isValidURL(String url) {
568527
}
569528

570529

571-
private static void parseVariablesForDefaultValues(Document doc) throws XPathExpressionException {
530+
private void parseVariablesForDefaultValues(Document doc) throws XPathExpressionException {
572531
parseVariables(doc, true, false, false);
573532
}
574533

575-
private static void parseVariablesForValues(Document doc) throws XPathExpressionException {
534+
private void parseVariablesForValues(Document doc) throws XPathExpressionException {
576535
parseVariables(doc, false, true, false);
577536
}
578537

579-
private static void parseVariablesForBothValues(Document doc) throws XPathExpressionException {
538+
private void parseVariablesForBothValues(Document doc) throws XPathExpressionException {
580539
parseVariables(doc, false, false, true);
581540
}
582541

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 {
584543
// parse input document
585544
NodeList nodeList = (NodeList) XPATH_SERVER_VARIABLE.evaluate(doc, XPathConstants.NODESET);
586545

@@ -605,7 +564,7 @@ private static void parseVariables(Document doc, boolean defaultValues, boolean
605564
}
606565
}
607566

608-
private static String getValue(NamedNodeMap attr, String nodeName) {
567+
private String getValue(NamedNodeMap attr, String nodeName) {
609568
String value = null;
610569
Node valueNode = attr.getNamedItem(nodeName);
611570
if (valueNode != null) {
@@ -614,7 +573,7 @@ private static String getValue(NamedNodeMap attr, String nodeName) {
614573
return value;
615574
}
616575

617-
private static void parseIncludeVariables(Document doc) throws XPathExpressionException, IOException, SAXException {
576+
private void parseIncludeVariables(Document doc) throws XPathExpressionException, IOException, SAXException {
618577
// parse include document in source server xml
619578
NodeList nodeList = (NodeList) XPATH_SERVER_INCLUDE.evaluate(doc, XPathConstants.NODESET);
620579

@@ -641,7 +600,7 @@ private static void parseIncludeVariables(Document doc) throws XPathExpressionEx
641600
}
642601
}
643602

644-
private static File getConfigDropinsDir() {
603+
private File getConfigDropinsDir() {
645604
File configDropins = null;
646605

647606
// if configDirectory exists and contains configDropins directory,
@@ -656,7 +615,7 @@ private static File getConfigDropinsDir() {
656615
return configDropins;
657616
}
658617

659-
private static void parseConfigDropinsDirVariables(String inDir)
618+
private void parseConfigDropinsDirVariables(String inDir)
660619
throws XPathExpressionException, SAXException, IOException {
661620
File configDropins = getConfigDropinsDir();
662621
if (configDropins == null || !configDropins.exists()) {
@@ -677,7 +636,7 @@ private static void parseConfigDropinsDirVariables(String inDir)
677636
}
678637
}
679638

680-
private static void parseDropinsFilesVariables(File file)
639+
private void parseDropinsFilesVariables(File file)
681640
throws SAXException, IOException, XPathExpressionException {
682641
// get input XML Document
683642
Document doc = parseDocument(file);
@@ -693,7 +652,7 @@ private static void parseDropinsFilesVariables(File file)
693652
* If giveConfigDirPrecedence is set to false, return specificFile if it exists;
694653
* otherwise return the file from the configDirectory if it exists, or null if not.
695654
*/
696-
private static File findConfigFile(String fileName, File specificFile, boolean giveConfigDirPrecedence) {
655+
private File findConfigFile(String fileName, File specificFile, boolean giveConfigDirPrecedence) {
697656
File f = new File(configDirectory, fileName);
698657

699658
if (giveConfigDirPrecedence) {
@@ -718,7 +677,7 @@ private static File findConfigFile(String fileName, File specificFile, boolean g
718677
/*
719678
* Get the file from configDrectory if it exists, or null if not
720679
*/
721-
private static File getFileFromConfigDirectory(String file) {
680+
private File getFileFromConfigDirectory(String file) {
722681
File f = new File(configDirectory, file);
723682
if (configDirectory != null && f.exists()) {
724683
return f;

src/main/java/io/openliberty/tools/common/plugins/util/ServerFeatureUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,9 +682,9 @@ protected Map<String, Object> createMapBasedInstallKernelInstance(String bundle,
682682
mapBasedInstallKernel.put("target.user.directory", usrDir);
683683
if (isDebugEnabled()) {
684684
mapBasedInstallKernel.put("debug", Level.FINEST);
685-
} /* else { removed due to "java.lang.ClassNotFoundException: com.ibm.websphere.ras.DataFormatHelper" reported in ci.gradle issue 867
685+
} else { //removed due to "java.lang.ClassNotFoundException: com.ibm.websphere.ras.DataFormatHelper" reported in ci.gradle issue 867
686686
mapBasedInstallKernel.put("debug", Level.INFO);
687-
} */
687+
}
688688
return mapBasedInstallKernel;
689689
}
690690

0 commit comments

Comments
 (0)