3
3
import java .io .File ;
4
4
import java .io .IOException ;
5
5
import java .nio .file .Files ;
6
+ import java .nio .file .Path ;
6
7
import java .nio .file .Paths ;
7
8
8
9
import javax .annotation .PostConstruct ;
9
10
10
11
import org .slf4j .Logger ;
11
12
import org .slf4j .LoggerFactory ;
13
+ import org .springframework .beans .factory .annotation .Autowired ;
12
14
import org .springframework .beans .factory .annotation .Value ;
13
15
import org .springframework .stereotype .Component ;
14
16
17
+ import com .ericsson .ei .frontend .model .BackEndInformation ;
15
18
import com .google .gson .JsonArray ;
19
+ import com .google .gson .JsonElement ;
20
+ import com .google .gson .JsonObject ;
16
21
import com .google .gson .JsonParser ;
22
+ import com .google .gson .JsonSyntaxException ;
17
23
18
24
import lombok .Setter ;
19
25
@@ -28,40 +34,136 @@ public class BackEndInstanceFileUtils {
28
34
29
35
private String eiInstancesPath ;
30
36
37
+ @ Autowired
38
+ BackEndInstancesUtils backendInstancesUtils ;
39
+
31
40
@ Value ("${ei.backendInstancesFilePath:#{null}}" )
32
41
private String backendInstancesFilePath ;
33
42
43
+ @ Value ("${ei.backendInstancesListJsonContent:#{null}}" )
44
+ private String backendInstancesListJsonContent ;
45
+
34
46
@ PostConstruct
35
47
public void init () throws IOException {
36
48
LOG .info ("Initiating BackEndInstanceFileUtils." );
37
49
38
50
// Use home folder if a specific backendInstancesFilePath isn't provided
39
- if (backendInstancesFilePath == null || backendInstancesFilePath .isEmpty ()) {
51
+ if (backendInstancesFilePath == null || backendInstancesFilePath .isEmpty ()) {
52
+
40
53
String homeFolder = System .getProperty ("user.home" );
41
54
String eiHome = Paths .get (homeFolder , EI_HOME_DEFAULT_NAME ).toString ();
42
55
43
56
Boolean eiHomeExists = Files .isDirectory (Paths .get (eiHome ));
44
57
if (!eiHomeExists ) {
45
58
createEiHomeFolder (eiHome );
46
59
}
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
+ "\n Will 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
+ "\n That 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
+ }
47
79
48
- setEiInstancesPath (Paths .get (eiHome , BACKEND_INSTANCES_DEFAULT_FILENAME ).toString ());
49
80
} else {
50
81
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." + "\n Error message: " + e .getMessage ()
107
+ + "\n Errors: " + e );
51
108
}
109
+ setDefaultEiBackendInstance (parsedBackendInstancesListJsonArray );
52
110
}
53
111
54
112
/**
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
+ + "\n Make 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
+ + "\n Make sure ei.backendInstancesListJsonContent property is set with one or more EI Backend instances."
133
+ + "\n Error message: " + e .getMessage () + "\n Errors: " + e );
134
+ System .exit (1 );
135
+ }
136
+ return backendInstancesListJsonArray ;
137
+ }
138
+
139
+ /**
140
+ * Sets default EI Backend instance.
56
141
*
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
59
160
*/
60
161
public JsonArray getInstancesFromFile () {
61
162
try {
62
163
ensureValidFile ();
63
164
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 ();
65
167
return inputBackEndInstances ;
66
168
} catch (IOException e ) {
67
169
LOG .error ("Failure when try to parse json file" + e .getMessage ());
@@ -72,12 +174,10 @@ public JsonArray getInstancesFromFile() {
72
174
/**
73
175
* Saves the given JsonArray
74
176
*
75
- * @param jsonArrayToDump
76
- * JsonArray
177
+ * @param jsonArrayToDump JsonArray
77
178
*/
78
179
public void dumpJsonArray (JsonArray jsonArrayToDump ) {
79
180
try {
80
- ensureValidFile ();
81
181
Files .write (Paths .get (eiInstancesPath ), jsonArrayToDump .toString ().getBytes ());
82
182
} catch (IOException e ) {
83
183
LOG .error ("Couldn't add instance to file " + e .getMessage ());
@@ -93,12 +193,14 @@ private void ensureValidFile() throws IOException {
93
193
}
94
194
95
195
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 ))) + "'." );
97
198
System .exit (-1 );
98
199
}
99
- } catch (Exception e ) {
200
+ } catch (Exception e ) {
100
201
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 );
102
204
LOG .error (message );
103
205
System .exit (-1 );
104
206
}
@@ -107,8 +209,9 @@ private void ensureValidFile() throws IOException {
107
209
private void createFileWithDirs () throws IOException {
108
210
File eiInstancesParentFolder = Paths .get (eiInstancesPath ).getParent ().toFile ();
109
211
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 ));
112
215
eiInstancesParentFolder .mkdirs ();
113
216
}
114
217
@@ -132,7 +235,8 @@ private void createEiHomeFolder(String eiHome) throws IOException {
132
235
133
236
if (!success ) {
134
237
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 );
136
240
LOG .error (message );
137
241
System .exit (-1 );
138
242
}
0 commit comments