Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ab6246
commit e847a68
Showing
6 changed files
with
294 additions
and
256 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
123 changes: 123 additions & 0 deletions
123
ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/machine/EnvironmentStatusHandler.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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,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); | |||
} | |||
} | |||
} |
150 changes: 150 additions & 0 deletions
150
ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/WorkspaceStatusHandler.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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,150 @@ | |||
/******************************************************************************* | |||
* 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.workspace; | |||
|
|||
import com.google.inject.Inject; | |||
import com.google.inject.Singleton; | |||
import com.google.web.bindery.event.shared.EventBus; | |||
|
|||
import org.eclipse.che.api.workspace.shared.dto.event.WorkspaceStatusEvent; | |||
import org.eclipse.che.commons.annotation.Nullable; | |||
import org.eclipse.che.ide.CoreLocalizationConstant; | |||
import org.eclipse.che.ide.actions.WorkspaceSnapshotNotifier; | |||
import org.eclipse.che.ide.api.app.AppContext; | |||
import org.eclipse.che.ide.api.machine.WsAgentStateController; | |||
import org.eclipse.che.ide.api.machine.WsAgentURLModifier; | |||
import org.eclipse.che.ide.api.notification.NotificationManager; | |||
import org.eclipse.che.ide.api.workspace.event.WorkspaceStartedEvent; | |||
import org.eclipse.che.ide.api.workspace.event.WorkspaceStartingEvent; | |||
import org.eclipse.che.ide.api.workspace.event.WorkspaceStoppedEvent; | |||
import org.eclipse.che.ide.context.AppContextImpl; | |||
import org.eclipse.che.ide.resource.Path; | |||
import org.eclipse.che.ide.ui.loaders.LoaderPresenter; | |||
import org.eclipse.che.ide.workspace.start.StartWorkspaceNotification; | |||
|
|||
import static org.eclipse.che.api.core.model.workspace.WorkspaceStatus.RUNNING; | |||
import static org.eclipse.che.api.core.model.workspace.WorkspaceStatus.STARTING; | |||
import static org.eclipse.che.api.core.model.workspace.WorkspaceStatus.STOPPED; | |||
import static org.eclipse.che.ide.api.notification.StatusNotification.DisplayMode.FLOAT_MODE; | |||
import static org.eclipse.che.ide.api.notification.StatusNotification.Status.FAIL; | |||
import static org.eclipse.che.ide.ui.loaders.LoaderPresenter.Phase.CREATING_WORKSPACE_SNAPSHOT; | |||
import static org.eclipse.che.ide.ui.loaders.LoaderPresenter.Phase.STARTING_WORKSPACE_RUNTIME; | |||
import static org.eclipse.che.ide.ui.loaders.LoaderPresenter.Phase.STOPPING_WORKSPACE; | |||
|
|||
/** | |||
* Handles changes of the workspace status and fires | |||
* the corresponded events to notify all interested subscribers. | |||
*/ | |||
@Singleton | |||
public class WorkspaceStatusHandler { | |||
|
|||
private final WorkspaceServiceClient workspaceServiceClient; | |||
private final AppContext appContext; | |||
private final StartWorkspaceNotification startWorkspaceNotificationProvider; | |||
private final NotificationManager notificationManagerProvider; | |||
private final WorkspaceSnapshotNotifier snapshotNotifierProvider; | |||
private final LoaderPresenter wsStatusNotification; | |||
private final WsAgentStateController wsAgentStateController; | |||
private final WsAgentURLModifier wsAgentURLModifier; | |||
private final EventBus eventBus; | |||
private final CoreLocalizationConstant messages; | |||
|
|||
@Inject | |||
WorkspaceStatusHandler(WorkspaceServiceClient workspaceServiceClient, | |||
AppContext appContext, | |||
StartWorkspaceNotification startWorkspaceNotification, | |||
NotificationManager notificationManager, | |||
WorkspaceSnapshotNotifier snapshotNotifier, | |||
LoaderPresenter wsStatusNotification, | |||
WsAgentStateController wsAgentStateController, | |||
WsAgentURLModifier wsAgentURLModifier, | |||
EventBus eventBus, | |||
CoreLocalizationConstant messages) { | |||
this.workspaceServiceClient = workspaceServiceClient; | |||
this.appContext = appContext; | |||
this.startWorkspaceNotificationProvider = startWorkspaceNotification; | |||
this.notificationManagerProvider = notificationManager; | |||
this.snapshotNotifierProvider = snapshotNotifier; | |||
this.wsStatusNotification = wsStatusNotification; | |||
this.wsAgentStateController = wsAgentStateController; | |||
this.wsAgentURLModifier = wsAgentURLModifier; | |||
this.eventBus = eventBus; | |||
this.messages = messages; | |||
} | |||
|
|||
public void handleWorkspaceStatusChanged() { | |||
handleWorkspaceStatusChanged(null); | |||
} | |||
|
|||
public void handleWorkspaceStatusChanged(@Nullable WorkspaceStatusEvent serverEvent) { | |||
workspaceServiceClient.getWorkspace(appContext.getWorkspaceId()).then(workspace -> { | |||
((AppContextImpl)appContext).setWorkspace(workspace); | |||
|
|||
// FIXME: spi | |||
// should be set on server `ws-agent` has been started | |||
((AppContextImpl)appContext).setProjectsRoot(Path.valueOf("/projects")); | |||
|
|||
if (workspace.getStatus() == RUNNING) { | |||
wsStatusNotification.setSuccess(STARTING_WORKSPACE_RUNTIME); | |||
wsAgentStateController.initialize(appContext.getDevMachine()); | |||
wsAgentURLModifier.initialize(appContext.getDevMachine()); | |||
|
|||
eventBus.fireEvent(new WorkspaceStartedEvent(workspace)); | |||
} else if (workspace.getStatus() == STARTING) { | |||
eventBus.fireEvent(new WorkspaceStartingEvent(workspace)); | |||
} else if (workspace.getStatus() == STOPPED) { | |||
eventBus.fireEvent(new WorkspaceStoppedEvent(workspace)); | |||
} | |||
|
|||
if (serverEvent != null) { | |||
notify(serverEvent); | |||
} | |||
}); | |||
} | |||
|
|||
// TODO: move to the separate component that should listen appropriate events | |||
private void notify(WorkspaceStatusEvent event) { | |||
switch (event.getEventType()) { | |||
case STARTING: | |||
wsStatusNotification.setSuccess(STARTING_WORKSPACE_RUNTIME); | |||
break; | |||
case RUNNING: | |||
startWorkspaceNotificationProvider.hide(); | |||
wsStatusNotification.setSuccess(STARTING_WORKSPACE_RUNTIME); | |||
break; | |||
case STOPPING: | |||
wsStatusNotification.show(STOPPING_WORKSPACE); | |||
break; | |||
case STOPPED: | |||
wsStatusNotification.setSuccess(STOPPING_WORKSPACE); | |||
startWorkspaceNotificationProvider.show(); | |||
break; | |||
case ERROR: | |||
notificationManagerProvider.notify(messages.workspaceStartFailed(), FAIL, FLOAT_MODE); | |||
startWorkspaceNotificationProvider.show(); | |||
break; | |||
case SNAPSHOT_CREATING: | |||
wsStatusNotification.show(CREATING_WORKSPACE_SNAPSHOT); | |||
snapshotNotifierProvider.creationStarted(); | |||
break; | |||
case SNAPSHOT_CREATED: | |||
wsStatusNotification.setSuccess(CREATING_WORKSPACE_SNAPSHOT); | |||
snapshotNotifierProvider.successfullyCreated(); | |||
break; | |||
case SNAPSHOT_CREATION_ERROR: | |||
wsStatusNotification.setError(CREATING_WORKSPACE_SNAPSHOT); | |||
snapshotNotifierProvider.creationError("Snapshot creation error: " + event.getError()); | |||
break; | |||
default: | |||
break; | |||
} | |||
} | |||
} |
Oops, something went wrong.