Skip to content

Commit 41307bf

Browse files
authored
Change behaviour of the EI backendInstancesinformation file (#99)
- EI will now use user.home as default place to create ".eiffel-intelligent-home" folder with EIBackendInstancesInformation.json as default name. If it doesnt exist it it will try to create it and if it cannot create it, it will exit. If backendInstancesFilePath is given in application.properties that will be used instead, if it doesnt exist it will be created. If it can't be created it will exit.
1 parent 59009e4 commit 41307bf

File tree

2 files changed

+59
-13
lines changed

2 files changed

+59
-13
lines changed

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

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,32 @@
2222
public class BackEndInstanceFileUtils {
2323

2424
private static final Logger LOG = LoggerFactory.getLogger(BackEndInstanceFileUtils.class);
25-
private static final String PATH_TO_WRITE = "src/main/resources/EIBackendInstancesInformation.json";
2625

27-
@Value("${ei.backendInstancesPath:#{null}}")
26+
private static final String BACKEND_INSTANCES_DEFAULT_FILENAME = "EIBackendInstancesInformation.json";
27+
private static final String EI_HOME_DEFAULT_NAME = ".eiffel-intelligence-frontend";
28+
2829
private String eiInstancesPath;
2930

31+
@Value("${ei.backendInstancesFilePath:#{null}}")
32+
private String backendInstancesFilePath;
33+
3034
@PostConstruct
31-
public void init() {
35+
public void init() throws IOException {
3236
LOG.info("Initiating BackEndInstanceFileUtils.");
33-
if (eiInstancesPath == null || eiInstancesPath.isEmpty()) {
34-
setEiInstancesPath(PATH_TO_WRITE);
37+
38+
// Use home folder if a specific backendInstancesFilePath isn't provided
39+
if(backendInstancesFilePath == null || backendInstancesFilePath.isEmpty()) {
40+
String homeFolder = System.getProperty("user.home");
41+
String eiHome = Paths.get(homeFolder, EI_HOME_DEFAULT_NAME).toString();
42+
43+
Boolean eiHomeExists = Files.isDirectory(Paths.get(eiHome));
44+
if (!eiHomeExists) {
45+
createEiHomeFolder(eiHome);
46+
}
47+
48+
setEiInstancesPath(Paths.get(eiHome, BACKEND_INSTANCES_DEFAULT_FILENAME).toString());
49+
} else {
50+
setEiInstancesPath(Paths.get(backendInstancesFilePath).toString());
3551
}
3652
}
3753

@@ -70,16 +86,35 @@ public void dumpJsonArray(JsonArray jsonArrayToDump) {
7086
}
7187

7288
private void ensureValidFile() throws IOException {
73-
if (!(new File(eiInstancesPath).isFile())) {
74-
LOG.error("File does not exist! Trying to creat file.");
75-
Files.createFile(Paths.get(eiInstancesPath));
76-
Files.write(Paths.get(eiInstancesPath), "[]".getBytes());
77-
return;
89+
try {
90+
if (!(new File(eiInstancesPath).isFile())) {
91+
createFileWithDirs();
92+
return;
93+
}
94+
95+
if (!fileContainsJsonArray()) {
96+
LOG.error("File does not contain valid json! JSON:'" + new String(Files.readAllBytes(Paths.get(eiInstancesPath))) + "'.");
97+
System.exit(-1);
98+
}
99+
} catch(Exception e) {
100+
String message = String.format(
101+
"Failed to read backendInstancesFilePath %s. Please check access rights or choose another backendInstancesFilePath in application.properties.", eiInstancesPath);
102+
LOG.error(message);
103+
System.exit(-1);
78104
}
105+
}
79106

80-
if (!fileContainsJsonArray()) {
81-
LOG.error("File does not contain valid json! JSON:'" + new String(Files.readAllBytes(Paths.get(eiInstancesPath))) + "'.");
107+
private void createFileWithDirs() throws IOException {
108+
File eiInstancesParentFolder = Paths.get(eiInstancesPath).getParent().toFile();
109+
110+
if (!(eiInstancesParentFolder.isDirectory())){
111+
LOG.info(String.format("Parentdir(s) for %s does not exist! Trying to create necessary parent dirs.", backendInstancesFilePath));
112+
eiInstancesParentFolder.mkdirs();
82113
}
114+
115+
LOG.info("File does not exist! Trying to create file.");
116+
Files.createFile(Paths.get(eiInstancesPath));
117+
Files.write(Paths.get(eiInstancesPath), "[]".getBytes());
83118
}
84119

85120
private boolean fileContainsJsonArray() {
@@ -92,4 +127,15 @@ private boolean fileContainsJsonArray() {
92127
}
93128
}
94129

130+
private void createEiHomeFolder(String eiHome) throws IOException {
131+
Boolean success = (new File(eiHome)).mkdirs();
132+
133+
if (!success) {
134+
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);
136+
LOG.error(message);
137+
System.exit(-1);
138+
}
139+
}
140+
95141
}

src/main/resources/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ ei.backendServerHost=localhost
2222
ei.backendServerPort=8090
2323
ei.backendContextPath=
2424
ei.useSecureHttpBackend=false
25-
ei.backendInstancesPath=
25+
ei.backendInstancesFilePath=
2626

2727
###### EI Documentation Link Url ##########
2828
ei.eiffelDocumentationUrls={ "EI Frontend GitHub": "https://github.com/eiffel-community/eiffel-intelligence-frontend",\

0 commit comments

Comments
 (0)