Skip to content

Commit 2e6a2c0

Browse files
committed
Issue #42: remove projects that are not in the list on a refresh
1 parent 2348fe5 commit 2e6a2c0

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

dev/com.ibm.microclimate.core/src/com/ibm/microclimate/core/internal/MicroclimateApplicationFactory.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2018 IBM Corporation and others.
2+
* Copyright (c) 2018, 2019 IBM Corporation and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v2.0
55
* which accompanies this distribution, and is available at
@@ -11,6 +11,9 @@
1111

1212
package com.ibm.microclimate.core.internal;
1313

14+
import java.util.HashSet;
15+
import java.util.Set;
16+
1417
import org.json.JSONArray;
1518
import org.json.JSONException;
1619
import org.json.JSONObject;
@@ -38,11 +41,13 @@ public static void getAppsFromProjectsJson(MicroclimateConnection mcConnection,
3841
try {
3942
MCLogger.log(projectsJson);
4043
JSONArray appArray = new JSONArray(projectsJson);
44+
Set<String> idSet = new HashSet<String>();
4145

4246
for(int i = 0; i < appArray.length(); i++) {
4347
JSONObject appJso = appArray.getJSONObject(i);
4448
try {
4549
String id = appJso.getString(MCConstants.KEY_PROJECT_ID);
50+
idSet.add(id);
4651
// If a project id was passed in then only process the JSON object for that project
4752
if (projectID == null || projectID.equals(id)) {
4853
synchronized(MicroclimateApplicationFactory.class) {
@@ -65,6 +70,17 @@ public static void getAppsFromProjectsJson(MicroclimateConnection mcConnection,
6570
MCLogger.logError("Error parsing project json: " + appJso, e); //$NON-NLS-1$
6671
}
6772
}
73+
74+
// If refreshing all of the projects, remove any projects that are not in the list returned by Microclimate.
75+
// This will only happen if something goes wrong and no delete event is received from Microclimate for a
76+
// project.
77+
if (projectID == null) {
78+
for (String id : mcConnection.getAppIds()) {
79+
if (!idSet.contains(id)) {
80+
mcConnection.removeApp(id);
81+
}
82+
}
83+
}
6884
} catch (Exception e) {
6985
MCLogger.logError("Error parsing json for project array.", e); //$NON-NLS-1$
7086
}

dev/com.ibm.microclimate.core/src/com/ibm/microclimate/core/internal/connection/MicroclimateConnection.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
import java.net.URISyntaxException;
1818
import java.net.URL;
1919
import java.util.ArrayList;
20+
import java.util.HashSet;
2021
import java.util.LinkedHashMap;
2122
import java.util.List;
2223
import java.util.Map;
24+
import java.util.Set;
2325
import java.util.regex.Matcher;
2426
import java.util.regex.Pattern;
2527

@@ -285,6 +287,12 @@ public List<MicroclimateApplication> getApps() {
285287
return new ArrayList<MicroclimateApplication>(appMap.values());
286288
}
287289
}
290+
291+
public Set<String> getAppIds() {
292+
synchronized(appMap) {
293+
return new HashSet<String>(appMap.keySet());
294+
}
295+
}
288296

289297
public MicroclimateApplication removeApp(String projectID) {
290298
synchronized(appMap) {

0 commit comments

Comments
 (0)