Skip to content

Commit 7be7e99

Browse files
author
sgomez
committed
Removed XMLReader from config reading (use deserialization instead)
1 parent 9c89cb3 commit 7be7e99

File tree

10 files changed

+249
-81
lines changed

10 files changed

+249
-81
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ target/
99
# Mac OS
1010
.DS_Store
1111

12+
.vscode/
13+
1214
.project
1315
.factorypath
1416
.classpath

common/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
<artifactId>simple-xml</artifactId>
3535
<version>2.7.1</version>
3636
</dependency>
37+
<dependency>
38+
<groupId>com.fasterxml.jackson.dataformat</groupId>
39+
<artifactId>jackson-dataformat-xml</artifactId>
40+
<version>2.9.9</version>
41+
</dependency>
3742
</dependencies>
3843

3944
<build>

common/src/main/java/com/genexus/util/GXService.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public class GXService
55
private String name;
66
private String type;
77
private String className;
8+
private boolean allowMultiple;
89
private GXProperties properties;
910

1011
public String getName()
@@ -36,6 +37,16 @@ public void setClassName(String className)
3637
{
3738
this.className = className;
3839
}
40+
41+
public boolean getAllowMultiple()
42+
{
43+
return allowMultiple;
44+
}
45+
46+
public void setAllowMultiple(boolean allowMultiple)
47+
{
48+
this.allowMultiple = allowMultiple;
49+
}
3950

4051
public GXProperties getProperties()
4152
{

docker-run.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Start a docker container with these files in the /usr/src folder
22
# Follow the README file to compile (mvn compile) or package (mvn package)
33

4-
$containerName = "java-maven"
4+
$containerName = (Get-Item -Path .\).Name
55
$containers = docker ps --all --format='{{json .Names}}'
66

77
if ($containers -Contains "`"$($containerName)`""){

java/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@
115115
<artifactId>jackson-databind</artifactId>
116116
<version>2.9.9</version>
117117
</dependency>
118+
<dependency>
119+
<groupId>com.fasterxml.jackson.dataformat</groupId>
120+
<artifactId>jackson-dataformat-xml</artifactId>
121+
<version>2.9.9</version>
122+
</dependency>
118123
<dependency>
119124
<groupId>com.google.apis</groupId>
120125
<artifactId>google-api-services-androidpublisher</artifactId>
Lines changed: 59 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
package com.genexus.util;
22

33
import java.io.File;
4+
import java.io.IOException;
45
import java.util.Hashtable;
56

6-
import com.genexus.ApplicationContext;
7+
import com.fasterxml.jackson.core.JsonParseException;
8+
import com.fasterxml.jackson.databind.JsonMappingException;
9+
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
10+
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
711
import com.genexus.ModelContext;
812
import com.genexus.internet.HttpContext;
13+
import com.genexus.util.cloudservice.Property;
14+
import com.genexus.util.cloudservice.Service;
15+
import com.genexus.util.cloudservice.Services;
916
import com.genexus.webpanels.HttpContextWeb;
10-
import com.genexus.xml.XMLReader;
17+
import com.genexus.diagnostics.core.ILogger;
18+
import com.genexus.diagnostics.core.LogManager;
1119

1220
public class GXServices {
21+
22+
public static final ILogger logger = LogManager.getLogger(GXServices.class);
23+
1324
private static final boolean DEBUG = com.genexus.DebugFlag.DEBUG;
14-
public static final String WEBNOTIFICATIONS_SERVICE = "WebNotifications";
25+
public static final String WEBNOTIFICATIONS_SERVICE = "WebNotifications";
1526
public static final String STORAGE_SERVICE = "Storage";
1627
public static final String STORAGE_APISERVICE = "StorageAPI";
1728
public static final String CACHE_SERVICE = "Cache";
@@ -23,7 +34,7 @@ public class GXServices {
2334
public GXServices() {
2435
readServices("");
2536
}
26-
37+
2738
public GXServices(String basePath) {
2839
readServices(basePath);
2940
}
@@ -33,7 +44,7 @@ public static GXServices getInstance() {
3344
instance = new GXServices();
3445
return instance;
3546
}
36-
47+
3748
public static GXServices getInstance(String basePath) {
3849
if (instance == null)
3950
instance = new GXServices(basePath);
@@ -44,29 +55,46 @@ public static void endGXServices() {
4455
instance = null;
4556
}
4657

47-
public static void loadFromFile(String basePath, String fileName, GXServices services){
58+
public static void loadFromFile(String basePath, String fileName, GXServices services) {
4859
if (basePath.equals("")) {
4960
basePath = services.configBaseDirectory();
5061
}
5162
String fullPath = basePath + fileName;
52-
XMLReader reader = new XMLReader();
53-
reader.open(fullPath);
54-
reader.readType(1, "Services");
55-
reader.read();
56-
if (reader.getErrCode() == 0) {
57-
while (!reader.getName().equals("Services")) {
58-
services.processService(reader);
59-
reader.read();
60-
if (reader.getName().equals("Service") && reader.getNodeType() == 2) //</Service>
61-
reader.read();
62-
}
63-
reader.close();
63+
XmlMapper xmlMapper = new XmlMapper();
64+
xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
65+
Services xmlServices = null;
66+
try {
67+
xmlServices = xmlMapper.readValue(new File(fullPath), Services.class);
68+
} catch (JsonParseException e) {
69+
logger.error(e.getMessage(), e);
70+
e.printStackTrace();
71+
} catch (JsonMappingException e) {
72+
logger.error(e.getMessage(), e);
73+
e.printStackTrace();
74+
} catch (IOException e) {
75+
logger.error(e.getMessage(), e);
76+
e.printStackTrace();
6477
}
65-
else
66-
{
67-
if (!ApplicationContext.getInstance().getReorganization() && DEBUG)
68-
{
69-
System.out.println("GXServices - Could not load Services Config: " + fullPath + " - " + reader.getErrDescription());
78+
79+
if (services == null)
80+
services = new GXServices();
81+
82+
for (Service xmlService : xmlServices.getService()) {
83+
GXService service = new GXService();
84+
service.setName(xmlService.getName());
85+
service.setType(xmlService.getType());
86+
service.setClassName(xmlService.getClassName());
87+
service.setAllowMultiple(xmlService.getAllowMultiple());
88+
GXProperties ptys = new GXProperties();
89+
for (Property xmlPty : xmlService.getProperties().getProperty()) {
90+
ptys.add(xmlPty.getName(), xmlPty.getValue());
91+
}
92+
service.setProperties(ptys);
93+
94+
if (service.getAllowMultiple()) {
95+
services.services.put(service.getType() + ":" + service.getName(), service);
96+
} else {
97+
services.services.put(service.getType(), service);
7098
}
7199
}
72100
}
@@ -76,19 +104,17 @@ private String configBaseDirectory() {
76104
String envVariable = System.getenv("LAMBDA_TASK_ROOT");
77105
if (envVariable != null && envVariable.length() > 0)
78106
return envVariable + File.separator;
79-
107+
80108
if (ModelContext.getModelContext() != null) {
81109
HttpContext webContext = (HttpContext) ModelContext.getModelContext().getHttpContext();
82110
if ((webContext != null) && (webContext instanceof HttpContextWeb)) {
83-
baseDir = com.genexus.ModelContext.getModelContext()
84-
.getHttpContext().getDefaultPath()
85-
+ File.separator + "WEB-INF" + File.separatorChar;
86-
}
111+
baseDir = com.genexus.ModelContext.getModelContext().getHttpContext().getDefaultPath() + File.separator
112+
+ "WEB-INF" + File.separatorChar;
113+
}
87114
}
88115
if (baseDir.equals("")) {
89-
String servletPath = com.genexus.ApplicationContext.getInstance().getServletEngineDefaultPath();
90-
if (servletPath != null && !servletPath.equals(""))
91-
{
116+
String servletPath = com.genexus.ApplicationContext.getInstance().getServletEngineDefaultPath();
117+
if (servletPath != null && !servletPath.equals("")) {
92118
baseDir = servletPath + File.separator + "WEB-INF" + File.separatorChar;
93119
}
94120
}
@@ -99,62 +125,15 @@ private void readServices(String basePath) {
99125

100126
if (basePath.equals(""))
101127
basePath = configBaseDirectory();
102-
if (new File(basePath + SERVICES_DEV_FILE).exists()){
128+
if (new File(basePath + SERVICES_DEV_FILE).exists()) {
103129
loadFromFile(basePath, SERVICES_DEV_FILE, this);
104130
}
105-
if (new File(basePath + SERVICES_FILE).exists()){
131+
if (new File(basePath + SERVICES_FILE).exists()) {
106132
loadFromFile(basePath, SERVICES_FILE, this);
107133
}
108134
}
109135

110-
private void processService(XMLReader reader) {
111-
short result;
112-
result = reader.readType(1, "Name");
113-
String name = new String(reader.getValue());
114-
115-
result = reader.readType(1, "Type");
116-
String type = new String(reader.getValue());
117-
118-
result = reader.readType(1, "ClassName");
119-
String className = new String(reader.getValue());
120-
121-
GXProperties properties = processProperties(reader);
122-
123-
GXService service = new GXService();
124-
service.setName(name);
125-
service.setType(type);
126-
service.setClassName(className);
127-
service.setProperties(properties);
128-
if (services.containsKey(type)){
129-
GXService prev = services.get(type);
130-
services.remove(type);
131-
services.put(prev.getType() + ":" + prev.getName(), prev);
132-
services.put(service.getType() + ":" + service.getName(), service);
133-
}
134-
else{
135-
services.put(type, service);
136-
}
137-
}
138-
139-
private GXProperties processProperties(XMLReader reader) {
140-
short result;
141-
GXProperties properties = new GXProperties();
142-
reader.readType(1, "Properties");
143-
reader.read();
144-
while (reader.getName().equals("Property")) {
145-
result = reader.readType(1, "Name");
146-
String name = new String(reader.getValue());
147-
result = reader.readType(1, "Value");
148-
String value = new String(reader.getValue());
149-
properties.add(name, value);
150-
reader.read();
151-
reader.read();
152-
}
153-
return properties;
154-
}
155-
156136
public GXService get(String name) {
157137
return services.get(name);
158138
}
159-
160139
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.genexus.util.cloudservice;
2+
3+
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
4+
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
5+
6+
public class Properties
7+
{
8+
@JacksonXmlElementWrapper(useWrapping = false)
9+
@JacksonXmlProperty(localName = "Property")
10+
private Property[] property;
11+
12+
public Property[] getProperty ()
13+
{
14+
return property;
15+
}
16+
17+
public void setProperty (Property[] property)
18+
{
19+
this.property = property;
20+
}
21+
22+
@Override
23+
public String toString()
24+
{
25+
return "ClassPojo [Property = "+property+"]";
26+
}
27+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.genexus.util.cloudservice;
2+
3+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
4+
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
5+
6+
@JsonPropertyOrder({ "Name", "Value" })
7+
public class Property
8+
{
9+
10+
@JacksonXmlProperty(localName = "Value")
11+
private String value;
12+
13+
@JacksonXmlProperty(localName = "Name")
14+
private String name;
15+
16+
public String getValue ()
17+
{
18+
return value;
19+
}
20+
21+
public void setValue (final String value) {
22+
this.value = value;
23+
}
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public void setName(final String name)
30+
{
31+
this.name = name;
32+
}
33+
34+
@Override
35+
public String toString()
36+
{
37+
return "ClassPojo [Value = "+value+", Name = "+name+"]";
38+
}
39+
}

0 commit comments

Comments
 (0)