-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathGXServices.java
More file actions
189 lines (164 loc) · 5.67 KB
/
GXServices.java
File metadata and controls
189 lines (164 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package com.genexus.util;
import java.io.File;
import java.util.Hashtable;
import com.genexus.ApplicationContext;
import com.genexus.ModelContext;
import com.genexus.diagnostics.core.ILogger;
import com.genexus.diagnostics.core.LogManager;
import com.genexus.internet.HttpContext;
import com.genexus.webpanels.HttpContextWeb;
import com.genexus.xml.XMLReader;
public class GXServices {
private static final ILogger logger = LogManager.getLogger(GXServices.class);
public static final String WEBNOTIFICATIONS_SERVICE = "WebNotifications";
public static final String STORAGE_SERVICE = "Storage";
public static final String STORAGE_APISERVICE = "StorageAPI";
public static final String CACHE_SERVICE = "Cache";
public static final String DATA_ACCESS_SERVICE = "DataAccess";
private static final String SERVICES_FILE = "CloudServices.config";
private static final String SERVICES_DEV_FILE = "CloudServices.dev.config";
private static final String STORAGE_DEFAULT_NAME = "STORAGE_DEFAULT_NAME";
private static final String STORAGE_DEFAULT_CLASSNAME = "STORAGE_DEFAULT_CLASSNAME";
private static GXServices instance;
private Hashtable<String, GXService> services = new Hashtable<String, GXService>();
public GXServices() {
readServices("");
}
public GXServices(String basePath) {
readServices(basePath);
}
public static GXServices getInstance() {
if (instance == null)
instance = new GXServices();
return instance;
}
public static GXServices getInstance(String basePath) {
if (instance == null)
instance = new GXServices(basePath);
return instance;
}
public static void endGXServices() {
instance = null;
}
public static void loadFromFile(String basePath, String fileName, GXServices services){
if (basePath.equals("")) {
basePath = services.configBaseDirectory();
}
String fullPath = basePath + fileName;
XMLReader reader = new XMLReader();
reader.open(fullPath);
reader.readType(1, "Services");
reader.read();
if (reader.getErrCode() == 0) {
while (!reader.getName().equals("Services")) {
services.processService(reader);
reader.read();
if (reader.getName().equals("Service") && reader.getNodeType() == 2) //</Service>
reader.read();
}
reader.close();
}
else
{
if (!ApplicationContext.getInstance().getReorganization())
{
logger.debug("GXServices - Could not load Services Config: " + fullPath + " - " + reader.getErrDescription());
}
}
}
public String configBaseDirectory() {
if (ApplicationContext.getInstance().isSpringBootApp())
return "";
String baseDir = "";
String envVariable = System.getenv("LAMBDA_TASK_ROOT");
if (envVariable != null && envVariable.length() > 0)
return envVariable + File.separator;
if (ModelContext.getModelContext() != null) {
HttpContext webContext = (HttpContext) ModelContext.getModelContext().getHttpContext();
if ((webContext != null) && (webContext instanceof HttpContextWeb)) {
baseDir = com.genexus.ModelContext.getModelContext()
.getHttpContext().getDefaultPath()
+ File.separator + "WEB-INF" + File.separatorChar;
}
}
if (baseDir.equals("")) {
String servletPath = com.genexus.ApplicationContext.getInstance().getServletEngineDefaultPath();
if (servletPath != null && !servletPath.equals(""))
{
baseDir = servletPath + File.separator + "WEB-INF" + File.separatorChar;
}
}
return baseDir;
}
private void readServices(String basePath) {
if (basePath.equals(""))
basePath = configBaseDirectory();
if (ApplicationContext.getInstance().checkIfResourceExist(basePath + SERVICES_DEV_FILE)){
loadFromFile(basePath, SERVICES_DEV_FILE, this);
}
if (ApplicationContext.getInstance().checkIfResourceExist(basePath + SERVICES_FILE)){
loadFromFile(basePath, SERVICES_FILE, this);
}
}
private void processService(XMLReader reader) {
short result;
result = reader.readType(1, "Name");
String name = new String(reader.getValue());
result = reader.readType(1, "Type");
String type = new String(reader.getValue());
result = reader.readType(1, "ClassName");
String className = new String(reader.getValue());
if (type.equals(STORAGE_SERVICE)) {
name = getEnvValue(STORAGE_DEFAULT_NAME, name);
className = getEnvValue(STORAGE_DEFAULT_CLASSNAME, className);
}
boolean allowMultiple = false;
reader.read();
if (reader.getName() == "AllowMultiple")
{
allowMultiple = Boolean.parseBoolean(reader.getValue());
reader.read();
}
GXProperties properties = processProperties(type, name, reader);
GXService service = new GXService();
service.setName(name);
service.setType(type);
service.setClassName(className);
service.setAllowMultiple(allowMultiple);
service.setProperties(properties);
if (service.getAllowMultiple()){
services.put(service.getType() + ":" + service.getName(), service);
}
else{
services.put(type, service);
}
}
private GXProperties processProperties(String serviceType, String serviceName, XMLReader reader) {
short result;
GXProperties properties = new GXProperties();
reader.read();
while (reader.getName().equals("Property")) {
result = reader.readType(1, "Name");
String name = new String(reader.getValue());
result = reader.readType(1, "Value");
String value = new String(reader.getValue());
String envValue = EnvVarReader.getEnvironmentValue(serviceType, serviceName, name);
if (envValue != null)
value = envValue;
properties.add(name, value);
reader.read();
reader.read();
}
return properties;
}
public GXService get(String name) {
return services.get(name);
}
private String getEnvValue(String envVar, String defaultValue) {
String value = System.getenv(envVar);
if (value == null){
value = defaultValue;
}
return value;
}
}