18
18
19
19
import com .ericsson .ei .frontend .model .BackEndInformation ;
20
20
import com .fasterxml .jackson .databind .ObjectMapper ;
21
+ import com .google .gson .JsonArray ;
22
+ import com .google .gson .JsonElement ;
23
+ import com .google .gson .JsonObject ;
24
+ import com .google .gson .JsonParser ;
21
25
import lombok .Getter ;
22
26
import lombok .Setter ;
23
- import org .json .JSONArray ;
24
- import org .json .JSONObject ;
25
27
import org .slf4j .Logger ;
26
28
import org .slf4j .LoggerFactory ;
27
29
import org .springframework .beans .factory .annotation .Autowired ;
@@ -63,28 +65,28 @@ public class BackEndInstancesUtils {
63
65
private BackEndInformation backEndInformation ;
64
66
65
67
private List <BackEndInformation > information = new ArrayList <>();
66
- private JSONArray instances = new JSONArray ();
68
+ private JsonArray instances = new JsonArray ();
67
69
68
70
@ PostConstruct
69
71
public void init () {
70
72
parseBackEndInstancesFile ();
71
73
if (!checkIfInstanceAlreadyExist (getCurrentInstance ())) {
72
- instances .put (getCurrentInstance ());
74
+ instances .add (getCurrentInstance ());
73
75
}
74
76
if (eiInstancesPath .equals ("" )) {
75
77
setEiInstancesPath (PATH );
76
78
}
77
79
writeIntoFile ();
78
80
}
79
81
80
- private JSONObject getCurrentInstance () {
81
- JSONObject instance = new JSONObject ();
82
- instance .put ("name" , "core" );
83
- instance .put ("host" , host );
84
- instance .put ("port" , port );
85
- instance .put ("path" , path );
86
- instance .put ("https" , https );
87
- instance .put ("active" , true );
82
+ private JsonObject getCurrentInstance () {
83
+ JsonObject instance = new JsonObject ();
84
+ instance .addProperty ("name" , "core" );
85
+ instance .addProperty ("host" , host );
86
+ instance .addProperty ("port" , port );
87
+ instance .addProperty ("path" , path );
88
+ instance .addProperty ("https" , https );
89
+ instance .addProperty ("active" , true );
88
90
return instance ;
89
91
}
90
92
@@ -96,11 +98,11 @@ public void setBackEndProperties(BackEndInformation properties) {
96
98
backEndInformation .setHttps (properties .isHttps ());
97
99
}
98
100
99
- public boolean checkIfInstanceAlreadyExist (JSONObject instance ) {
100
- for (int i = 0 ; i < instances . length (); i ++ ) {
101
- if (instances . getJSONObject ( i ).get ("host" ).equals (instance .get ("host" )) &&
102
- instances . getJSONObject ( i ).get ("port" ).equals (instance .get ("port" )) &&
103
- instances . getJSONObject ( i ).get ("path" ).equals (instance .get ("path" ))) {
101
+ public boolean checkIfInstanceAlreadyExist (JsonObject instance ) {
102
+ for (JsonElement element : instances ) {
103
+ if (element . getAsJsonObject ( ).get ("host" ).equals (instance .get ("host" )) &&
104
+ element . getAsJsonObject ( ).get ("port" ).equals (instance .get ("port" )) &&
105
+ element . getAsJsonObject ( ).get ("path" ).equals (instance .get ("path" ))) {
104
106
return true ;
105
107
}
106
108
}
@@ -123,12 +125,11 @@ public void parseBackEndInstancesFile() {
123
125
if (eiInstancesPath != null ) {
124
126
try {
125
127
information .clear ();
126
- instances = new JSONArray ();
127
- JSONArray inputBackEndInstances = new JSONArray (new String (Files .readAllBytes (Paths .get (eiInstancesPath ))));
128
- for (Object o : inputBackEndInstances ) {
129
- JSONObject instance = (JSONObject ) o ;
130
- information .add (new ObjectMapper ().readValue (instance .toString (), BackEndInformation .class ));
131
- instances .put (instance );
128
+ instances = new JsonArray ();
129
+ JsonArray inputBackEndInstances = new JsonParser ().parse (new String (Files .readAllBytes (Paths .get (eiInstancesPath )))).getAsJsonArray ();
130
+ for (JsonElement element : inputBackEndInstances ) {
131
+ information .add (new ObjectMapper ().readValue (element .toString (), BackEndInformation .class ));
132
+ instances .add (element );
132
133
}
133
134
} catch (IOException e ) {
134
135
LOG .error ("Failure when try to parse json file" + e .getMessage ());
0 commit comments