Skip to content

Commit 823979b

Browse files
ggallottiGonzalo Gallotti Vazquez
and
Gonzalo Gallotti Vazquez
authored
Support Reading CloudServices.config from File System and Classpath (#944)
* Support reading the CloudService.config file from filesystem and classLoader. * Support reading the CloudService.config file from filesystem and classLoader. --------- Co-authored-by: Gonzalo Gallotti Vazquez <g.gallotti@globant.com>
1 parent 2487aa3 commit 823979b

File tree

2 files changed

+87
-33
lines changed

2 files changed

+87
-33
lines changed

common/src/main/java/com/genexus/xml/XMLReader.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,27 @@ public void openResource(String url)
814814
}
815815
}
816816

817-
817+
public void openFromInputStream(InputStream inputStream)
818+
{
819+
reset();
820+
try
821+
{
822+
streamToClose = inputStream;
823+
if (documentEncoding.length() > 0)
824+
inputSource = new XMLInputSource(null, null, null, inputStream, documentEncoding);
825+
else
826+
inputSource = new XMLInputSource(null, null, null, inputStream, null);
827+
828+
parserConfiguration.setInputSource(inputSource);
829+
}
830+
catch (IOException e)
831+
{
832+
errCode = ERROR_IO;
833+
errDescription = e.getMessage();
834+
}
835+
}
836+
837+
818838
public void openFromString(String s)
819839
{
820840
reset();

java/src/main/java/com/genexus/util/GXServices.java

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.genexus.util;
22

33
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
47
import java.util.Hashtable;
58

69
import com.genexus.ApplicationContext;
@@ -47,32 +50,6 @@ public static void endGXServices() {
4750
instance = null;
4851
}
4952

50-
public static void loadFromFile(String basePath, String fileName, GXServices services){
51-
if (basePath.equals("")) {
52-
basePath = services.configBaseDirectory();
53-
}
54-
String fullPath = basePath + fileName;
55-
XMLReader reader = new XMLReader();
56-
reader.open(fullPath);
57-
reader.readType(1, "Services");
58-
reader.read();
59-
if (reader.getErrCode() == 0) {
60-
while (!reader.getName().equals("Services")) {
61-
services.processService(reader);
62-
reader.read();
63-
if (reader.getName().equals("Service") && reader.getNodeType() == 2) //</Service>
64-
reader.read();
65-
}
66-
reader.close();
67-
}
68-
else
69-
{
70-
if (!ApplicationContext.getInstance().getReorganization())
71-
{
72-
logger.debug("GXServices - Could not load Services Config: " + fullPath + " - " + reader.getErrDescription());
73-
}
74-
}
75-
}
7653

7754
private String configBaseDirectory() {
7855
String baseDir = "";
@@ -99,14 +76,71 @@ private String configBaseDirectory() {
9976
}
10077

10178
private void readServices(String basePath) {
102-
103-
if (basePath.equals(""))
79+
if (basePath.equals("")) {
10480
basePath = configBaseDirectory();
105-
if (new File(basePath + SERVICES_DEV_FILE).exists()){
106-
loadFromFile(basePath, SERVICES_DEV_FILE, this);
10781
}
108-
if (new File(basePath + SERVICES_FILE).exists()){
109-
loadFromFile(basePath, SERVICES_FILE, this);
82+
83+
if (!readFromFileSystem(basePath, SERVICES_DEV_FILE)) {
84+
readFromClasspath(SERVICES_DEV_FILE);
85+
}
86+
87+
if (!readFromFileSystem(basePath, SERVICES_FILE)) {
88+
readFromClasspath(SERVICES_FILE);
89+
}
90+
}
91+
92+
private boolean readFromFileSystem(String basePath, String fileName) {
93+
File file = new File(basePath + fileName);
94+
if (file.exists()) {
95+
loadFromFile(basePath, fileName, this);
96+
return true;
97+
}
98+
return false;
99+
}
100+
101+
private boolean readFromClasspath(String fileName) {
102+
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(fileName);
103+
if (inputStream == null) {
104+
return false;
105+
}
106+
try {
107+
loadFromStream(inputStream, fileName, this);
108+
return true;
109+
} catch (IOException e) {
110+
logger.debug("GXServices - Could not load Services Config from classpath: " + fileName + " - " + e.getMessage());
111+
}
112+
return false;
113+
}
114+
115+
public static void loadFromFile(String basePath, String fileName, GXServices services) {
116+
if (basePath.equals("")) {
117+
basePath = services.configBaseDirectory();
118+
}
119+
String fullPath = basePath + fileName;
120+
try (InputStream inputStream = new FileInputStream(fullPath)) {
121+
loadFromStream(inputStream, fullPath, services);
122+
} catch (IOException e) {
123+
logger.debug("GXServices - Could not load Services Config from file: " + fullPath + " - " + e.getMessage());
124+
}
125+
}
126+
127+
public static void loadFromStream(InputStream inputStream, String source, GXServices services) throws IOException {
128+
XMLReader reader = new XMLReader();
129+
reader.openFromInputStream(inputStream);
130+
reader.readType(1, "Services");
131+
reader.read();
132+
if (reader.getErrCode() == 0) {
133+
while (!reader.getName().equals("Services")) {
134+
services.processService(reader);
135+
reader.read();
136+
if (reader.getName().equals("Service") && reader.getNodeType() == 2) //</Service>
137+
reader.read();
138+
}
139+
reader.close();
140+
} else {
141+
if (!ApplicationContext.getInstance().getReorganization()) {
142+
logger.debug("GXServices - Could not load Services Config: " + source + " - " + reader.getErrDescription());
143+
}
110144
}
111145
}
112146

0 commit comments

Comments
 (0)