/
EnvironmentStatusHandler.java
123 lines (104 loc) · 5.01 KB
/
EnvironmentStatusHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*******************************************************************************
* Copyright (c) 2012-2017 Codenvy, S.A.
* 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:
* Codenvy, S.A. - initial API and implementation
*******************************************************************************/
package org.eclipse.che.ide.machine;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.web.bindery.event.shared.EventBus;
import org.eclipse.che.api.core.jsonrpc.commons.RequestTransmitter;
import org.eclipse.che.api.machine.shared.dto.event.MachineStatusEvent;
import org.eclipse.che.api.workspace.shared.dto.RuntimeDto;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.machine.ActiveRuntime;
import org.eclipse.che.ide.api.machine.MachineEntity;
import org.eclipse.che.ide.api.machine.events.MachineStateEvent;
import org.eclipse.che.ide.api.notification.NotificationManager;
import org.eclipse.che.ide.context.AppContextImpl;
import org.eclipse.che.ide.workspace.WorkspaceServiceClient;
import java.util.Optional;
import static com.google.common.base.Strings.isNullOrEmpty;
import static org.eclipse.che.ide.api.machine.events.MachineStateEvent.MachineAction.CREATING;
import static org.eclipse.che.ide.api.machine.events.MachineStateEvent.MachineAction.RUNNING;
import static org.eclipse.che.ide.api.notification.StatusNotification.DisplayMode.EMERGE_MODE;
import static org.eclipse.che.ide.api.notification.StatusNotification.Status.FAIL;
/**
* Handles changes in the workspace's environment and fires
* the corresponded events to notify all interested subscribers.
*/
@Singleton
public class EnvironmentStatusHandler {
private EventBus eventBus;
private AppContext appContext;
private WorkspaceServiceClient workspaceServiceClient;
private NotificationManager notificationManager;
private RequestTransmitter transmitter;
@Inject
EnvironmentStatusHandler(EventBus eventBus,
AppContext appContext,
WorkspaceServiceClient workspaceServiceClient,
NotificationManager notificationManager,
RequestTransmitter transmitter) {
this.eventBus = eventBus;
this.appContext = appContext;
this.workspaceServiceClient = workspaceServiceClient;
this.notificationManager = notificationManager;
this.transmitter = transmitter;
}
public void handleEnvironmentStatusChanged(MachineStatusEvent event) {
final String machineId = event.getMachineId();
final String workspaceId = event.getWorkspaceId();
workspaceServiceClient.getWorkspace(workspaceId).then(workspace -> {
RuntimeDto workspaceRuntime = workspace.getRuntime();
if (workspaceRuntime == null) {
return;
}
((AppContextImpl)appContext).setWorkspace(workspace);
switch (event.getEventType()) {
case CREATING:
handleMachineCreating(machineId);
break;
case RUNNING:
handleMachineRunning(machineId);
break;
case ERROR:
handleMachineError(event);
break;
}
});
}
private void handleMachineCreating(String machineName) {
final ActiveRuntime activeRuntime = appContext.getActiveRuntime();
final Optional<MachineEntity> machine = activeRuntime.getMachineByName(machineName);
if (machine.isPresent()) {
subscribeToMachineOutput(machineName);
eventBus.fireEvent(new MachineStateEvent(machine.get(), CREATING));
}
}
private void subscribeToMachineOutput(String machineName) {
final String endpointId = "ws-master";
final String subscribeByName = "event:environment-output:subscribe-by-machine-name";
final String workspaceIdPlusMachineName = appContext.getWorkspaceId() + "::" + machineName;
transmitter.newRequest()
.endpointId(endpointId)
.methodName(subscribeByName)
.paramsAsString(workspaceIdPlusMachineName)
.sendAndSkipResult();
}
private void handleMachineRunning(String machineName) {
final ActiveRuntime activeRuntime = appContext.getActiveRuntime();
final Optional<MachineEntity> machine = activeRuntime.getMachineByName(machineName);
machine.ifPresent(machineEntity -> eventBus.fireEvent(new MachineStateEvent(machineEntity, RUNNING)));
}
private void handleMachineError(MachineStatusEvent event) {
if (!isNullOrEmpty(event.getError())) {
notificationManager.notify(event.getError(), FAIL, EMERGE_MODE);
}
}
}