Skip to content

Commit

Permalink
Refactor IDE code
Browse files Browse the repository at this point in the history
  • Loading branch information
azatsarynnyy committed May 24, 2017
1 parent 3ab6246 commit e847a68
Show file tree
Hide file tree
Showing 6 changed files with 294 additions and 256 deletions.
Expand Up @@ -27,7 +27,7 @@
import org.eclipse.che.ide.ui.loaders.LoaderPresenter; import org.eclipse.che.ide.ui.loaders.LoaderPresenter;
import org.eclipse.che.ide.util.loging.Log; import org.eclipse.che.ide.util.loging.Log;
import org.eclipse.che.ide.workspace.WorkspaceServiceClient; import org.eclipse.che.ide.workspace.WorkspaceServiceClient;
import org.eclipse.che.ide.workspace.events.WorkspaceStatusEventHandler; import org.eclipse.che.ide.workspace.WorkspaceStatusHandler;


import static org.eclipse.che.api.core.model.workspace.WorkspaceStatus.RUNNING; import static org.eclipse.che.api.core.model.workspace.WorkspaceStatus.RUNNING;
import static org.eclipse.che.api.core.model.workspace.WorkspaceStatus.STOPPED; import static org.eclipse.che.api.core.model.workspace.WorkspaceStatus.STOPPED;
Expand All @@ -53,7 +53,7 @@ public class CurrentWorkspaceManager {
private final Provider<NotificationManager> notificationManagerProvider; private final Provider<NotificationManager> notificationManagerProvider;
private final IdeInitializer ideInitializer; private final IdeInitializer ideInitializer;
private final CoreLocalizationConstant messages; private final CoreLocalizationConstant messages;
private final WorkspaceStatusEventHandler wsStatusEventHandler; private final WorkspaceStatusHandler wsStatusHandler;
private final AppContext appContext; private final AppContext appContext;


@Inject @Inject
Expand All @@ -64,7 +64,7 @@ public class CurrentWorkspaceManager {
Provider<NotificationManager> notificationManagerProvider, Provider<NotificationManager> notificationManagerProvider,
IdeInitializer ideInitializer, IdeInitializer ideInitializer,
CoreLocalizationConstant messages, CoreLocalizationConstant messages,
WorkspaceStatusEventHandler wsStatusEventHandler, WorkspaceStatusHandler wsStatusHandler,
AppContext appContext) { AppContext appContext) {
this.workspaceServiceClient = workspaceServiceClient; this.workspaceServiceClient = workspaceServiceClient;
this.browserAddress = browserAddress; this.browserAddress = browserAddress;
Expand All @@ -73,7 +73,7 @@ public class CurrentWorkspaceManager {
this.notificationManagerProvider = notificationManagerProvider; this.notificationManagerProvider = notificationManagerProvider;
this.ideInitializer = ideInitializer; this.ideInitializer = ideInitializer;
this.messages = messages; this.messages = messages;
this.wsStatusEventHandler = wsStatusEventHandler; this.wsStatusHandler = wsStatusHandler;
this.appContext = appContext; this.appContext = appContext;
} }


Expand Down Expand Up @@ -115,7 +115,7 @@ private void startWorkspace(Workspace workspace, boolean restoreFromSnapshot) {
final WorkspaceStatus workspaceStatus = workspace.getStatus(); final WorkspaceStatus workspaceStatus = workspace.getStatus();


if (workspaceStatus == RUNNING) { if (workspaceStatus == RUNNING) {
wsStatusEventHandler.handleWorkspaceStatusChanged(); wsStatusHandler.handleWorkspaceStatusChanged();
} else if (workspaceStatus == STOPPED || workspaceStatus == STOPPING) { } else if (workspaceStatus == STOPPED || workspaceStatus == STOPPING) {
wsStatusNotification.show(STARTING_WORKSPACE_RUNTIME); wsStatusNotification.show(STARTING_WORKSPACE_RUNTIME);


Expand Down
@@ -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);
}
}
}
@@ -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;
}
}
}

0 comments on commit e847a68

Please sign in to comment.