-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CHE-5338. Clean up app state preference when workspace is removed
Signed-off-by: Roman Nikitenko <[email protected]>
- Loading branch information
1 parent
0157ffc
commit 030d3f6
Showing
4 changed files
with
168 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...c/main/java/org/eclipse/che/plugin/docker/machine/cleaner/AppStatesPreferenceCleaner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright (c) 2012-2017 Red Hat, Inc. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.che.plugin.docker.machine.cleaner; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
import com.google.inject.Inject; | ||
import com.google.inject.Singleton; | ||
import java.util.Map; | ||
import javax.annotation.PostConstruct; | ||
import org.eclipse.che.api.core.NotFoundException; | ||
import org.eclipse.che.api.core.ServerException; | ||
import org.eclipse.che.api.core.model.user.User; | ||
import org.eclipse.che.api.core.model.workspace.Workspace; | ||
import org.eclipse.che.api.core.notification.EventService; | ||
import org.eclipse.che.api.core.notification.EventSubscriber; | ||
import org.eclipse.che.api.user.server.PreferenceManager; | ||
import org.eclipse.che.api.user.server.UserManager; | ||
import org.eclipse.che.api.workspace.server.event.WorkspaceRemovedEvent; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** Handler for clean up app state preference when workspace is removed. */ | ||
@Singleton | ||
public class AppStatesPreferenceCleaner implements EventSubscriber<WorkspaceRemovedEvent> { | ||
private static final Logger LOG = LoggerFactory.getLogger(AppStatesPreferenceCleaner.class); | ||
|
||
/** The name of the property for the mappings in user preferences. */ | ||
public static final String APP_STATES_PREFERENCE_PROPERTY = "IdeAppStates"; | ||
|
||
private JsonParser jsonParser; | ||
private EventService eventService; | ||
private UserManager userManager; | ||
private PreferenceManager preferenceManager; | ||
|
||
@Inject | ||
public AppStatesPreferenceCleaner( | ||
JsonParser jsonParser, | ||
EventService eventService, | ||
UserManager userManager, | ||
PreferenceManager preferenceManager) { | ||
this.jsonParser = jsonParser; | ||
this.eventService = eventService; | ||
this.userManager = userManager; | ||
this.preferenceManager = preferenceManager; | ||
} | ||
|
||
@PostConstruct | ||
public void subscribe() { | ||
eventService.subscribe(this); | ||
} | ||
|
||
@Override | ||
public void onEvent(WorkspaceRemovedEvent workspaceRemovedEvent) { | ||
try { | ||
Workspace workspace = workspaceRemovedEvent.getWorkspace(); | ||
User user = userManager.getByName(workspace.getNamespace()); | ||
if (user == null) { | ||
return; | ||
} | ||
|
||
String userId = user.getId(); | ||
Map<String, String> preferences = preferenceManager.find(userId); | ||
String appStates = preferences.get(APP_STATES_PREFERENCE_PROPERTY); | ||
if (appStates == null) { | ||
return; | ||
} | ||
|
||
JsonObject workspaces = jsonParser.parse(appStates).getAsJsonObject(); | ||
JsonElement removedWorkspacePreferences = workspaces.remove(workspace.getId()); | ||
if (removedWorkspacePreferences != null) { | ||
preferences.put(APP_STATES_PREFERENCE_PROPERTY, workspaces.toString()); | ||
preferenceManager.save(userId, preferences); | ||
} | ||
} catch (NotFoundException | ServerException e) { | ||
Workspace workspace = workspaceRemovedEvent.getWorkspace(); | ||
LOG.error( | ||
"Unable to clean up preferences for owner of the workspace {} with namespace {}", | ||
workspace.getId(), | ||
workspace.getNamespace()); | ||
} | ||
} | ||
} |