Skip to content

Commit 827b4bd

Browse files
authored
Improve EI Backend instances list configuration (#108)
1 parent 8cafd74 commit 827b4bd

22 files changed

+200
-115
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ before_install:
1111
- chmod +x pom.xml
1212

1313
script:
14-
- mvn test
14+
- mvn test

src/main/java/com/ericsson/ei/frontend/model/BackEndInformation.java

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
*/
1717
package com.ericsson.ei.frontend.model;
1818

19-
import org.springframework.beans.factory.annotation.Value;
2019
import org.springframework.stereotype.Component;
2120

21+
import com.ericsson.ei.frontend.utils.BackEndInstancesUtils;
2222
import com.fasterxml.jackson.annotation.JsonProperty;
2323
import com.google.gson.JsonObject;
2424
import com.google.gson.annotations.SerializedName;
@@ -36,26 +36,12 @@
3636
@NoArgsConstructor
3737
@AllArgsConstructor
3838
public class BackEndInformation {
39-
private static final String NAME = "name";
40-
private static final String HOST = "host";
41-
private static final String PORT = "port";
42-
private static final String PATH = "path";
43-
private static final String HTTPS = "https";
44-
private static final String DEFAULT = "defaultBackend";
4539

46-
@Value("${ei.backendServerName:#{null}}")
4740
private String name;
48-
49-
@Value("${ei.backendServerHost:#{null}}")
5041
private String host;
51-
52-
@Value("${ei.backendServerPort:#{null}}")
5342
private String port;
43+
private String contextPath;
5444

55-
@Value("${ei.backendContextPath:#{\"\"}}")
56-
private String path;
57-
58-
@Value("${ei.useSecureHttpBackend:#{false}}")
5945
@JsonProperty("https")
6046
@SerializedName("https")
6147
private boolean useSecureHttpBackend;
@@ -70,12 +56,12 @@ public class BackEndInformation {
7056
*/
7157
public JsonObject getAsJsonObject() {
7258
JsonObject instance = new JsonObject();
73-
instance.addProperty(NAME, name);
74-
instance.addProperty(HOST, host);
75-
instance.addProperty(PORT, Integer.valueOf(port));
76-
instance.addProperty(PATH, path);
77-
instance.addProperty(HTTPS, useSecureHttpBackend);
78-
instance.addProperty(DEFAULT, defaultBackend);
59+
instance.addProperty(BackEndInstancesUtils.NAME, name);
60+
instance.addProperty(BackEndInstancesUtils.HOST, host);
61+
instance.addProperty(BackEndInstancesUtils.PORT, Integer.valueOf(port));
62+
instance.addProperty(BackEndInstancesUtils.CONTEXT_PATH, contextPath);
63+
instance.addProperty(BackEndInstancesUtils.HTTPS, useSecureHttpBackend);
64+
instance.addProperty(BackEndInstancesUtils.DEFAULT, defaultBackend);
7965
return instance;
8066
}
8167

@@ -93,8 +79,8 @@ public String getUrlAsString() {
9379

9480
constructedUrl += "://" + host + ":" + port;
9581

96-
if (this.getPath() != null && !this.getPath().isEmpty()) {
97-
constructedUrl += ("/" + path).replaceAll("//", "/");
82+
if (this.getContextPath() != null && !this.getContextPath().isEmpty()) {
83+
constructedUrl += ("/" + contextPath).replaceAll("//", "/");
9884
}
9985

10086
return constructedUrl;

src/main/java/com/ericsson/ei/frontend/utils/BackEndInfoirmationControllerUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public ResponseEntity<String> handleRequestForInstances(HttpServletRequest reque
6868
} catch (Exception e) {
6969
LOG.error("ERROR!\n" + e.getMessage());
7070
return new ResponseEntity<>(
71-
"[{\"name\":\"Unable to load instances\",\"host\":\"NO HOST\",\"port\":\"NO PORT\",\"path\":\"/\"}]",
71+
"[{\"name\":\"Unable to load instances\",\"host\":\"NO HOST\",\"port\":\"NO PORT\",\"contextPath\":\"/\"}]",
7272
getHeaders(), HttpStatus.OK);
7373
}
7474
}

src/main/java/com/ericsson/ei/frontend/utils/BackEndInstanceFileUtils.java

Lines changed: 119 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,23 @@
33
import java.io.File;
44
import java.io.IOException;
55
import java.nio.file.Files;
6+
import java.nio.file.Path;
67
import java.nio.file.Paths;
78

89
import javax.annotation.PostConstruct;
910

1011
import org.slf4j.Logger;
1112
import org.slf4j.LoggerFactory;
13+
import org.springframework.beans.factory.annotation.Autowired;
1214
import org.springframework.beans.factory.annotation.Value;
1315
import org.springframework.stereotype.Component;
1416

17+
import com.ericsson.ei.frontend.model.BackEndInformation;
1518
import com.google.gson.JsonArray;
19+
import com.google.gson.JsonElement;
20+
import com.google.gson.JsonObject;
1621
import com.google.gson.JsonParser;
22+
import com.google.gson.JsonSyntaxException;
1723

1824
import lombok.Setter;
1925

@@ -28,40 +34,136 @@ public class BackEndInstanceFileUtils {
2834

2935
private String eiInstancesPath;
3036

37+
@Autowired
38+
BackEndInstancesUtils backendInstancesUtils;
39+
3140
@Value("${ei.backendInstancesFilePath:#{null}}")
3241
private String backendInstancesFilePath;
3342

43+
@Value("${ei.backendInstancesListJsonContent:#{null}}")
44+
private String backendInstancesListJsonContent;
45+
3446
@PostConstruct
3547
public void init() throws IOException {
3648
LOG.info("Initiating BackEndInstanceFileUtils.");
3749

3850
// Use home folder if a specific backendInstancesFilePath isn't provided
39-
if(backendInstancesFilePath == null || backendInstancesFilePath.isEmpty()) {
51+
if (backendInstancesFilePath == null || backendInstancesFilePath.isEmpty()) {
52+
4053
String homeFolder = System.getProperty("user.home");
4154
String eiHome = Paths.get(homeFolder, EI_HOME_DEFAULT_NAME).toString();
4255

4356
Boolean eiHomeExists = Files.isDirectory(Paths.get(eiHome));
4457
if (!eiHomeExists) {
4558
createEiHomeFolder(eiHome);
4659
}
60+
61+
Path eiInstancesListFilePath = Paths.get(eiHome, BACKEND_INSTANCES_DEFAULT_FILENAME);
62+
setEiInstancesPath(eiInstancesListFilePath.toString());
63+
File eiInstancesListFile = new File(eiInstancesListFilePath.toString());
64+
if (eiInstancesListFile.exists() && eiInstancesListFile.length() != 0) {
65+
LOG.info("EI Instances List file path is not provided, but found a file in path: " + eiInstancesPath +
66+
"\nWill use that EI Instances List file.");
67+
}
68+
else {
69+
if (eiInstancesListFile.exists() && eiInstancesListFile.length() == 0) {
70+
LOG.info("EI Instances List file path is not provided, but found a file in path: " + eiInstancesPath +
71+
"\nThat EI Instances List file is empty!. Will try to create a default EI Instances List in that file.");
72+
}
73+
else {
74+
LOG.info("EI Instances List file path is not provided! " +
75+
"Will create a default EI Instances List file at file path: " + eiInstancesPath);
76+
}
77+
parseAndSetEiInstancesList();
78+
}
4779

48-
setEiInstancesPath(Paths.get(eiHome, BACKEND_INSTANCES_DEFAULT_FILENAME).toString());
4980
} else {
5081
setEiInstancesPath(Paths.get(backendInstancesFilePath).toString());
82+
if (!(new File(eiInstancesPath).isFile())) {
83+
LOG.info("EI Instances List file don' exist! Creating file with given or "
84+
+ "default EI instances list content. File path: " + eiInstancesPath);
85+
createFileWithDirs();
86+
parseAndSetEiInstancesList();
87+
} else {
88+
LOG.info("EI-Backend instances list file that will be used: " + eiInstancesPath);
89+
}
90+
91+
}
92+
}
93+
94+
/**
95+
* Parses Ei Instances list from backendInstancesListJsonContent property and
96+
* sets default EI-Backend instance.
97+
*
98+
*/
99+
private void parseAndSetEiInstancesList() {
100+
JsonArray parsedBackendInstancesListJsonArray = null;
101+
parsedBackendInstancesListJsonArray = parseEiInstancesListJsonObject();
102+
dumpJsonArray(parsedBackendInstancesListJsonArray);
103+
try {
104+
ensureValidFile();
105+
} catch (IOException e) {
106+
LOG.error("Failed to validate EI Instances List json object." + "\nError message: " + e.getMessage()
107+
+ "\nErrors: " + e);
51108
}
109+
setDefaultEiBackendInstance(parsedBackendInstancesListJsonArray);
52110
}
53111

54112
/**
55-
*Gets the JSON-data from file and returns as a JsonArray
113+
* Parses Ei Instances list from backendInstancesListJsonContent property.
114+
*
115+
* @return JsonArray
116+
*/
117+
private JsonArray parseEiInstancesListJsonObject() {
118+
JsonArray backendInstancesListJsonArray = null;
119+
120+
if (backendInstancesListJsonContent == null || backendInstancesListJsonContent.isEmpty()) {
121+
LOG.error("EI backend instances list json object is empty, can't continue."
122+
+ "\nMake sure that EI Instances list flags is set, "
123+
+ " 'ei.backendInstancesFilePath' or 'ei.backendInstancesListJsonContent'");
124+
System.exit(1);
125+
}
126+
127+
try {
128+
backendInstancesListJsonArray = (JsonArray) new JsonParser()
129+
.parse(backendInstancesListJsonContent.toString());
130+
} catch (JsonSyntaxException e) {
131+
LOG.error("Failed to parse EI backend instances list json object."
132+
+ "\nMake sure ei.backendInstancesListJsonContent property is set with one or more EI Backend instances."
133+
+ "\nError message: " + e.getMessage() + "\nErrors: " + e);
134+
System.exit(1);
135+
}
136+
return backendInstancesListJsonArray;
137+
}
138+
139+
/**
140+
* Sets default EI Backend instance.
56141
*
57-
* @return
58-
* JsonArray
142+
*/
143+
private void setDefaultEiBackendInstance(JsonArray jArray) {
144+
for (JsonElement instanceJsonObj : jArray) {
145+
JsonObject jObject = instanceJsonObj.getAsJsonObject();
146+
if (Boolean.getBoolean(jObject.get("defaultBackend").toString())) {
147+
backendInstancesUtils.setDefaultBackEndInstance(jObject.get(BackEndInstancesUtils.NAME).toString(),
148+
jObject.get(BackEndInstancesUtils.HOST).toString(),
149+
Integer.parseInt(jObject.get(BackEndInstancesUtils.PORT).toString()),
150+
jObject.get(BackEndInstancesUtils.CONTEXT_PATH).toString(),
151+
Boolean.getBoolean(jObject.get(BackEndInstancesUtils.DEFAULT).toString()));
152+
}
153+
}
154+
}
155+
156+
/**
157+
* Gets the JSON-data from file and returns as a JsonArray
158+
*
159+
* @return JsonArray
59160
*/
60161
public JsonArray getInstancesFromFile() {
61162
try {
62163
ensureValidFile();
63164

64-
JsonArray inputBackEndInstances = new JsonParser().parse(new String(Files.readAllBytes(Paths.get(eiInstancesPath)))).getAsJsonArray();
165+
JsonArray inputBackEndInstances = new JsonParser()
166+
.parse(new String(Files.readAllBytes(Paths.get(eiInstancesPath)))).getAsJsonArray();
65167
return inputBackEndInstances;
66168
} catch (IOException e) {
67169
LOG.error("Failure when try to parse json file" + e.getMessage());
@@ -72,12 +174,10 @@ public JsonArray getInstancesFromFile() {
72174
/**
73175
* Saves the given JsonArray
74176
*
75-
* @param jsonArrayToDump
76-
* JsonArray
177+
* @param jsonArrayToDump JsonArray
77178
*/
78179
public void dumpJsonArray(JsonArray jsonArrayToDump) {
79180
try {
80-
ensureValidFile();
81181
Files.write(Paths.get(eiInstancesPath), jsonArrayToDump.toString().getBytes());
82182
} catch (IOException e) {
83183
LOG.error("Couldn't add instance to file " + e.getMessage());
@@ -93,12 +193,14 @@ private void ensureValidFile() throws IOException {
93193
}
94194

95195
if (!fileContainsJsonArray()) {
96-
LOG.error("File does not contain valid json! JSON:'" + new String(Files.readAllBytes(Paths.get(eiInstancesPath))) + "'.");
196+
LOG.error("File does not contain valid json! JSON:' "
197+
+ new String(Files.readAllBytes(Paths.get(eiInstancesPath))) + "'.");
97198
System.exit(-1);
98199
}
99-
} catch(Exception e) {
200+
} catch (Exception e) {
100201
String message = String.format(
101-
"Failed to read backendInstancesFilePath %s. Please check access rights or choose another backendInstancesFilePath in application.properties.", eiInstancesPath);
202+
"Failed to read backendInstancesFilePath %s. Please check access rights or choose another backendInstancesFilePath in application.properties.",
203+
eiInstancesPath);
102204
LOG.error(message);
103205
System.exit(-1);
104206
}
@@ -107,8 +209,9 @@ private void ensureValidFile() throws IOException {
107209
private void createFileWithDirs() throws IOException {
108210
File eiInstancesParentFolder = Paths.get(eiInstancesPath).getParent().toFile();
109211

110-
if (!(eiInstancesParentFolder.isDirectory())){
111-
LOG.info(String.format("Parentdir(s) for %s does not exist! Trying to create necessary parent dirs.", backendInstancesFilePath));
212+
if (!(eiInstancesParentFolder.isDirectory())) {
213+
LOG.info(String.format("Parentdir(s) for %s does not exist! Trying to create necessary parent dirs.",
214+
backendInstancesFilePath));
112215
eiInstancesParentFolder.mkdirs();
113216
}
114217

@@ -132,7 +235,8 @@ private void createEiHomeFolder(String eiHome) throws IOException {
132235

133236
if (!success) {
134237
String message = String.format(
135-
"Failed to create eiffel intelligence home folder in %s. Please check access rights or choose a specific backendInstancesFilePath in application.properties.", eiHome);
238+
"Failed to create eiffel intelligence home folder in %s. Please check access rights or choose a specific backendInstancesFilePath in application.properties.",
239+
eiHome);
136240
LOG.error(message);
137241
System.exit(-1);
138242
}

0 commit comments

Comments
 (0)