From a0cd37c28661279ec173b8fbc18645739ae055aa Mon Sep 17 00:00:00 2001 From: Vitalii Parfonov Date: Mon, 16 Jan 2017 18:57:21 +0200 Subject: [PATCH 1/9] CHE-3620:Remove MachineServiceClient and related classes Signed-off-by: Vitalii Parfonov --- .../che/ide/api/machine/MachineManager.java | 58 --- .../ide/api/machine/MachineServiceClient.java | 49 --- .../api/machine/MachineServiceClientImpl.java | 71 ---- .../command/CommandProducerActionManager.java | 20 +- .../eclipse/che/ide/core/CoreGinModule.java | 3 - .../workspace/DefaultWorkspaceComponent.java | 4 - .../workspace/FactoryWorkspaceComponent.java | 4 - .../che/ide/workspace/WorkspaceComponent.java | 6 - .../ide/workspace/WorkspaceEventsHandler.java | 5 - .../workspace/WorkspaceEventsHandlerTest.java | 5 - .../client/command/CommandManagerImpl.java | 4 - .../client/inject/MachineGinModule.java | 7 - .../client/machine/MachineManagerImpl.java | 156 ------- .../create/CreateMachinePresenter.java | 147 ------- .../machine/create/CreateMachineView.java | 105 ----- .../machine/create/CreateMachineViewImpl.java | 387 ------------------ .../create/CreateMachineViewImpl.ui.xml | 81 ---- .../processes/ProcessesPresenter.java | 1 - .../docker/DockerCategoryPresenter.java | 76 +--- .../categories/ssh/SshCategoryPresenter.java | 61 +-- .../machine/MachineManagerImplTest.java | 169 -------- .../create/CreateMachinePresenterTest.java | 147 ------- .../processes/ProcessesPresenterTest.java | 1 - .../panel/ProcessesPanelPresenterTest.java | 4 - .../docker/DockerCategoryPresenterTest.java | 49 +-- .../ssh/SshCategoryPresenterTest.java | 41 -- ...ebuggerConfigurationPagePresenterTest.java | 3 - .../client/command/CommandManager.java | 9 +- 28 files changed, 19 insertions(+), 1654 deletions(-) delete mode 100644 ide/che-core-ide-api/src/main/java/org/eclipse/che/ide/api/machine/MachineManager.java delete mode 100644 ide/che-core-ide-api/src/main/java/org/eclipse/che/ide/api/machine/MachineServiceClient.java delete mode 100644 ide/che-core-ide-api/src/main/java/org/eclipse/che/ide/api/machine/MachineServiceClientImpl.java delete mode 100644 plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/MachineManagerImpl.java delete mode 100644 plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachinePresenter.java delete mode 100644 plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachineView.java delete mode 100644 plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachineViewImpl.java delete mode 100644 plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachineViewImpl.ui.xml delete mode 100644 plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/machine/MachineManagerImplTest.java delete mode 100644 plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachinePresenterTest.java diff --git a/ide/che-core-ide-api/src/main/java/org/eclipse/che/ide/api/machine/MachineManager.java b/ide/che-core-ide-api/src/main/java/org/eclipse/che/ide/api/machine/MachineManager.java deleted file mode 100644 index 6f74f6f5591..00000000000 --- a/ide/che-core-ide-api/src/main/java/org/eclipse/che/ide/api/machine/MachineManager.java +++ /dev/null @@ -1,58 +0,0 @@ -/******************************************************************************* - * 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.api.machine; - -import org.eclipse.che.api.promises.client.Promise; - -/** - * Manager for machine. - * - * @author Roman Nikitenko - */ -public interface MachineManager { - - /** - * Start new machine as dev-machine (bind workspace to running machine). - * - * @param recipeURL - * special recipe url to get docker image. - * @param displayName - * display name for machine - */ - void startDevMachine(String recipeURL, String displayName); - - /** - * Start new machine in workspace. - * - * @param recipeURL - * special recipe url to get docker image. - * @param displayName - * display name for machine - */ - void startMachine(String recipeURL, String displayName); - - /** - * Destroy machine. - * - * @param machine - * contains information about machine state - */ - Promise destroyMachine(MachineEntity machine); - - - /** - * Restart machine. - * - * @param machine - * contains information about machine state - */ - void restartMachine(final MachineEntity machine); -} diff --git a/ide/che-core-ide-api/src/main/java/org/eclipse/che/ide/api/machine/MachineServiceClient.java b/ide/che-core-ide-api/src/main/java/org/eclipse/che/ide/api/machine/MachineServiceClient.java deleted file mode 100644 index e19e13bab0e..00000000000 --- a/ide/che-core-ide-api/src/main/java/org/eclipse/che/ide/api/machine/MachineServiceClient.java +++ /dev/null @@ -1,49 +0,0 @@ -/******************************************************************************* - * 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.api.machine; - -import org.eclipse.che.api.core.model.machine.Command; -import org.eclipse.che.api.machine.shared.dto.MachineDto; -import org.eclipse.che.api.machine.shared.dto.MachineProcessDto; -import org.eclipse.che.api.promises.client.Promise; -import org.eclipse.che.commons.annotation.Nullable; - -import javax.validation.constraints.NotNull; -import java.util.List; - -/** - * Client for Machine API. - * - * @author Artem Zatsarynnyi - * @author Dmitry Shnurenko - */ -public interface MachineServiceClient { - - /** - * Returns list of machines which are bounded to the specified workspace. - * - * @param workspaceId - * workspace id - * @return a promise that will provide a list of {@link MachineDto}s for the given workspace ID, or rejects with an error - */ - Promise> getMachines(String workspaceId); - - /** - * Destroy machine with the specified ID. - * - * @param workspaceId - * ID of workspace - * @param machineId - * ID of machine that should be destroyed - * @return a promise that will resolve when the machine has been destroyed, or rejects with an error - */ - Promise destroyMachine(@NotNull String workspaceId, @NotNull String machineId); -} diff --git a/ide/che-core-ide-api/src/main/java/org/eclipse/che/ide/api/machine/MachineServiceClientImpl.java b/ide/che-core-ide-api/src/main/java/org/eclipse/che/ide/api/machine/MachineServiceClientImpl.java deleted file mode 100644 index 8866eef8448..00000000000 --- a/ide/che-core-ide-api/src/main/java/org/eclipse/che/ide/api/machine/MachineServiceClientImpl.java +++ /dev/null @@ -1,71 +0,0 @@ -/******************************************************************************* - * 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.api.machine; - -import com.google.inject.Inject; - -import org.eclipse.che.api.machine.shared.dto.MachineDto; -import org.eclipse.che.api.promises.client.Promise; -import org.eclipse.che.ide.api.app.AppContext; -import org.eclipse.che.ide.rest.AsyncRequestFactory; -import org.eclipse.che.ide.rest.DtoUnmarshallerFactory; -import org.eclipse.che.ide.ui.loaders.request.LoaderFactory; - -import javax.validation.constraints.NotNull; -import java.util.List; - -import static com.google.gwt.http.client.RequestBuilder.DELETE; -import static org.eclipse.che.ide.MimeType.APPLICATION_JSON; -import static org.eclipse.che.ide.rest.HTTPHeader.ACCEPT; - -/** - * Implementation for {@link MachineServiceClient}. - * - * @author Artem Zatsarynnyi - * @author Dmitry Shnurenko - */ -public class MachineServiceClientImpl implements MachineServiceClient { - private final DtoUnmarshallerFactory dtoUnmarshallerFactory; - private final AsyncRequestFactory asyncRequestFactory; - private final LoaderFactory loaderFactory; - private final String baseHttpUrl; - - @Inject - protected MachineServiceClientImpl(AppContext appContext, - DtoUnmarshallerFactory dtoUnmarshallerFactory, - AsyncRequestFactory asyncRequestFactory, - LoaderFactory loaderFactory) { - this.dtoUnmarshallerFactory = dtoUnmarshallerFactory; - this.asyncRequestFactory = asyncRequestFactory; - this.loaderFactory = loaderFactory; - this.baseHttpUrl = appContext.getMasterEndpoint() + "/workspace/"; - } - - @Override - public Promise> getMachines(String workspaceId) { - return asyncRequestFactory.createGetRequest(baseHttpUrl + workspaceId + "/machine") - .header(ACCEPT, APPLICATION_JSON) - .loader(loaderFactory.newLoader("Getting info about bound machines...")) - .send(dtoUnmarshallerFactory.newListUnmarshaller(MachineDto.class)); - } - - @Override - public Promise destroyMachine(@NotNull final String workspaceId, - @NotNull final String machineId) { - return asyncRequestFactory.createRequest(DELETE, - baseHttpUrl + workspaceId + - "/machine/" + machineId, - null, - false) - .loader(loaderFactory.newLoader("Destroying machine...")) - .send(); - } -} diff --git a/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/command/CommandProducerActionManager.java b/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/command/CommandProducerActionManager.java index 41594bfe0a6..cf962f7c7de 100644 --- a/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/command/CommandProducerActionManager.java +++ b/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/command/CommandProducerActionManager.java @@ -16,9 +16,6 @@ import com.google.web.bindery.event.shared.EventBus; import org.eclipse.che.api.core.model.machine.Machine; -import org.eclipse.che.api.machine.shared.dto.MachineDto; -import org.eclipse.che.api.promises.client.Operation; -import org.eclipse.che.api.promises.client.OperationException; import org.eclipse.che.ide.Resources; import org.eclipse.che.ide.api.action.Action; import org.eclipse.che.ide.api.action.ActionEvent; @@ -28,7 +25,7 @@ import org.eclipse.che.ide.api.command.CommandProducer; import org.eclipse.che.ide.api.component.Component; import org.eclipse.che.ide.api.constraints.Constraints; -import org.eclipse.che.ide.api.machine.MachineServiceClient; +import org.eclipse.che.ide.api.machine.ActiveRuntime; import org.eclipse.che.ide.api.machine.events.MachineStateEvent; import org.eclipse.che.ide.api.machine.events.WsAgentStateEvent; import org.eclipse.che.ide.api.machine.events.WsAgentStateHandler; @@ -61,7 +58,6 @@ public class CommandProducerActionManager implements MachineStateEvent.Handler, private final ActionManager actionManager; private final CommandProducerActionFactory commandProducerActionFactory; private final AppContext appContext; - private final MachineServiceClient machineServiceClient; private final Resources resources; private final List machines; @@ -77,12 +73,10 @@ public CommandProducerActionManager(EventBus eventBus, ActionManager actionManager, CommandProducerActionFactory commandProducerActionFactory, AppContext appContext, - MachineServiceClient machineServiceClient, Resources resources) { this.actionManager = actionManager; this.commandProducerActionFactory = commandProducerActionFactory; this.appContext = appContext; - this.machineServiceClient = machineServiceClient; this.resources = resources; machines = new ArrayList<>(); @@ -119,14 +113,12 @@ private void start(Set commandProducers) { @Override public void start(final Callback callback) { - machineServiceClient.getMachines(appContext.getWorkspaceId()).then(new Operation>() { - @Override - public void apply(List arg) throws OperationException { - machines.addAll(arg); + ActiveRuntime activeRuntime = appContext.getActiveRuntime(); + if (activeRuntime != null) { + machines.addAll(activeRuntime.getMachines()); + } - callback.onSuccess(CommandProducerActionManager.this); - } - }); + callback.onSuccess(this); } @Override diff --git a/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/core/CoreGinModule.java b/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/core/CoreGinModule.java index 086d69f5a1d..645107fe78b 100644 --- a/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/core/CoreGinModule.java +++ b/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/core/CoreGinModule.java @@ -31,8 +31,6 @@ import org.eclipse.che.ide.api.keybinding.KeyBindingAgent; import org.eclipse.che.ide.api.machine.ExecAgentCommandManager; import org.eclipse.che.ide.api.machine.ExecAgentEventManager; -import org.eclipse.che.ide.api.machine.MachineServiceClient; -import org.eclipse.che.ide.api.machine.MachineServiceClientImpl; import org.eclipse.che.ide.api.machine.RecipeServiceClient; import org.eclipse.che.ide.api.machine.RecipeServiceClientImpl; import org.eclipse.che.ide.api.machine.execagent.ConnectedEventHandler; @@ -138,7 +136,6 @@ protected void configure() { bind(GitServiceClient.class).to(GitServiceClientImpl.class).in(Singleton.class); bind(SshServiceClient.class).to(SshServiceClientImpl.class).in(Singleton.class); bind(RecipeServiceClient.class).to(RecipeServiceClientImpl.class).in(Singleton.class); - bind(MachineServiceClient.class).to(MachineServiceClientImpl.class).in(Singleton.class); // IDE agents bind(SelectionAgent.class).to(SelectionAgentImpl.class).asEagerSingleton(); diff --git a/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/DefaultWorkspaceComponent.java b/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/DefaultWorkspaceComponent.java index 2dca9ad8fd6..b8f9bb21441 100644 --- a/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/DefaultWorkspaceComponent.java +++ b/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/DefaultWorkspaceComponent.java @@ -12,7 +12,6 @@ import com.google.gwt.core.client.Callback; import com.google.inject.Inject; -import com.google.inject.Provider; import com.google.inject.Singleton; import com.google.web.bindery.event.shared.EventBus; @@ -24,7 +23,6 @@ import org.eclipse.che.ide.api.app.AppContext; import org.eclipse.che.ide.api.component.Component; import org.eclipse.che.ide.api.dialogs.DialogFactory; -import org.eclipse.che.ide.api.machine.MachineManager; import org.eclipse.che.ide.api.notification.NotificationManager; import org.eclipse.che.ide.api.preferences.PreferencesManager; import org.eclipse.che.ide.api.workspace.WorkspaceServiceClient; @@ -54,7 +52,6 @@ public DefaultWorkspaceComponent(WorkspaceServiceClient workspaceServiceClient, DtoUnmarshallerFactory dtoUnmarshallerFactory, EventBus eventBus, AppContext appContext, - Provider machineManagerProvider, NotificationManager notificationManager, MessageBusProvider messageBusProvider, BrowserQueryFieldRenderer browserQueryFieldRenderer, @@ -70,7 +67,6 @@ public DefaultWorkspaceComponent(WorkspaceServiceClient workspaceServiceClient, dtoUnmarshallerFactory, eventBus, appContext, - machineManagerProvider, notificationManager, messageBusProvider, browserQueryFieldRenderer, diff --git a/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/FactoryWorkspaceComponent.java b/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/FactoryWorkspaceComponent.java index 04ea1c9ddbd..80f71185c24 100644 --- a/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/FactoryWorkspaceComponent.java +++ b/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/FactoryWorkspaceComponent.java @@ -12,7 +12,6 @@ import com.google.gwt.core.client.Callback; import com.google.inject.Inject; -import com.google.inject.Provider; import com.google.inject.Singleton; import com.google.web.bindery.event.shared.EventBus; @@ -30,7 +29,6 @@ import org.eclipse.che.ide.api.component.Component; import org.eclipse.che.ide.api.dialogs.DialogFactory; import org.eclipse.che.ide.api.factory.FactoryServiceClient; -import org.eclipse.che.ide.api.machine.MachineManager; import org.eclipse.che.ide.api.notification.NotificationManager; import org.eclipse.che.ide.api.preferences.PreferencesManager; import org.eclipse.che.ide.api.workspace.WorkspaceServiceClient; @@ -74,7 +72,6 @@ public FactoryWorkspaceComponent(WorkspaceServiceClient workspaceServiceClient, DtoUnmarshallerFactory dtoUnmarshallerFactory, EventBus eventBus, AppContext appContext, - Provider machineManagerProvider, NotificationManager notificationManager, MessageBusProvider messageBusProvider, BrowserQueryFieldRenderer browserQueryFieldRenderer, @@ -90,7 +87,6 @@ public FactoryWorkspaceComponent(WorkspaceServiceClient workspaceServiceClient, dtoUnmarshallerFactory, eventBus, appContext, - machineManagerProvider, notificationManager, messageBusProvider, browserQueryFieldRenderer, diff --git a/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/WorkspaceComponent.java b/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/WorkspaceComponent.java index 52bcd1db4e2..bab1ec6b39c 100644 --- a/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/WorkspaceComponent.java +++ b/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/WorkspaceComponent.java @@ -12,7 +12,6 @@ import com.google.gwt.core.client.Callback; import com.google.gwt.core.client.Scheduler; -import com.google.inject.Provider; import com.google.web.bindery.event.shared.EventBus; import org.eclipse.che.api.core.model.workspace.Workspace; @@ -28,7 +27,6 @@ import org.eclipse.che.ide.api.dialogs.CancelCallback; import org.eclipse.che.ide.api.dialogs.ConfirmCallback; import org.eclipse.che.ide.api.dialogs.DialogFactory; -import org.eclipse.che.ide.api.machine.MachineManager; import org.eclipse.che.ide.api.machine.events.WsAgentStateEvent; import org.eclipse.che.ide.api.machine.events.WsAgentStateHandler; import org.eclipse.che.ide.api.notification.NotificationManager; @@ -73,7 +71,6 @@ public abstract class WorkspaceComponent implements Component, WsAgentStateHandl protected final StartWorkspacePresenter startWorkspacePresenter; private final EventBus eventBus; - private final Provider machineManagerProvider; private final MessageBusProvider messageBusProvider; private final WorkspaceEventsHandler workspaceEventsHandler; private final LoaderPresenter loader; @@ -89,7 +86,6 @@ public WorkspaceComponent(WorkspaceServiceClient workspaceServiceClient, DtoUnmarshallerFactory dtoUnmarshallerFactory, EventBus eventBus, AppContext appContext, - Provider machineManagerProvider, NotificationManager notificationManager, MessageBusProvider messageBusProvider, BrowserQueryFieldRenderer browserQueryFieldRenderer, @@ -105,7 +101,6 @@ public WorkspaceComponent(WorkspaceServiceClient workspaceServiceClient, this.dtoUnmarshallerFactory = dtoUnmarshallerFactory; this.eventBus = eventBus; this.appContext = appContext; - this.machineManagerProvider = machineManagerProvider; this.notificationManager = notificationManager; this.messageBusProvider = messageBusProvider; this.browserQueryFieldRenderer = browserQueryFieldRenderer; @@ -198,7 +193,6 @@ public void onOpen() { public void execute() { loader.setSuccess(STARTING_WORKSPACE_RUNTIME); eventBus.fireEvent(new WorkspaceStartedEvent(workspace)); - machineManagerProvider.get();//start instance of machine manager } }); break; diff --git a/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/WorkspaceEventsHandler.java b/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/WorkspaceEventsHandler.java index 140de8ac5f7..56779afc4fb 100644 --- a/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/WorkspaceEventsHandler.java +++ b/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/WorkspaceEventsHandler.java @@ -38,7 +38,6 @@ import org.eclipse.che.ide.api.dialogs.ConfirmCallback; import org.eclipse.che.ide.api.dialogs.DialogFactory; import org.eclipse.che.ide.api.machine.ExecAgentCommandManager; -import org.eclipse.che.ide.api.machine.MachineManager; import org.eclipse.che.ide.api.machine.OutputMessageUnmarshaller; import org.eclipse.che.ide.api.machine.events.WsAgentStateEvent; import org.eclipse.che.ide.api.notification.NotificationManager; @@ -90,7 +89,6 @@ public class WorkspaceEventsHandler { private final NotificationManager notificationManager; private final DialogFactory dialogFactory; private final DtoUnmarshallerFactory dtoUnmarshallerFactory; - private final Provider machineManagerProvider; private final Provider wsComponentProvider; private final AsyncRequestFactory asyncRequestFactory; private final WorkspaceSnapshotCreator snapshotCreator; @@ -125,7 +123,6 @@ public class WorkspaceEventsHandler { final DtoUnmarshallerFactory dtoUnmarshallerFactory, final NotificationManager notificationManager, final MessageBusProvider messageBusProvider, - final Provider machineManagerProvider, final WorkspaceSnapshotCreator snapshotCreator, final WorkspaceServiceClient workspaceServiceClient, final StartWorkspaceNotification startWorkspaceNotification, @@ -140,7 +137,6 @@ public class WorkspaceEventsHandler { this.notificationManager = notificationManager; this.dialogFactory = dialogFactory; this.dtoUnmarshallerFactory = dtoUnmarshallerFactory; - this.machineManagerProvider = machineManagerProvider; this.workspaceServiceClient = workspaceServiceClient; this.startWorkspaceNotification = startWorkspaceNotification; this.wsComponentProvider = wsComponentProvider; @@ -190,7 +186,6 @@ public void apply(WorkspaceDto workspace) throws OperationException { } workspaceComponent.setCurrentWorkspace(workspace); - machineManagerProvider.get(); loader.show(LoaderPresenter.Phase.STARTING_WORKSPACE_RUNTIME); eventBus.fireEvent(new WorkspaceStartingEvent(workspace)); diff --git a/ide/che-core-ide-app/src/test/java/org/eclipse/che/ide/workspace/WorkspaceEventsHandlerTest.java b/ide/che-core-ide-app/src/test/java/org/eclipse/che/ide/workspace/WorkspaceEventsHandlerTest.java index 4c1df0321c3..7a03c35fd7b 100644 --- a/ide/che-core-ide-app/src/test/java/org/eclipse/che/ide/workspace/WorkspaceEventsHandlerTest.java +++ b/ide/che-core-ide-app/src/test/java/org/eclipse/che/ide/workspace/WorkspaceEventsHandlerTest.java @@ -35,7 +35,6 @@ import org.eclipse.che.ide.api.dialogs.DialogFactory; import org.eclipse.che.ide.api.dialogs.MessageDialog; import org.eclipse.che.ide.api.machine.ExecAgentCommandManager; -import org.eclipse.che.ide.api.machine.MachineManager; import org.eclipse.che.ide.api.machine.events.WsAgentStateEvent; import org.eclipse.che.ide.api.notification.NotificationManager; import org.eclipse.che.ide.api.notification.StatusNotification; @@ -109,8 +108,6 @@ public class WorkspaceEventsHandlerTest { @Mock private DtoUnmarshallerFactory dtoUnmarshallerFactory; @Mock - private Provider machineManagerProvider; - @Mock private MessageLoader snapshotLoader; @Mock private Provider wsComponentProvider; @@ -173,7 +170,6 @@ public void setUp() { dtoUnmarshallerFactory, notificationManager, messageBusProvider, - machineManagerProvider, snapshotCreator, workspaceServiceClient, startWorkspaceNotification, @@ -252,7 +248,6 @@ public void onWorkspaceStartingTest() throws Exception { verify(workspaceServiceClient).getWorkspace(WORKSPACE_ID); verify(workspaceComponent).setCurrentWorkspace(workspace); - verify(machineManagerProvider).get(); verify(loader).show(eq(LoaderPresenter.Phase.STARTING_WORKSPACE_RUNTIME)); diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/command/CommandManagerImpl.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/command/CommandManagerImpl.java index 85364249e20..70da8ae8d42 100644 --- a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/command/CommandManagerImpl.java +++ b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/command/CommandManagerImpl.java @@ -15,7 +15,6 @@ import org.eclipse.che.api.core.model.machine.Command; import org.eclipse.che.api.core.model.machine.Machine; import org.eclipse.che.api.machine.shared.dto.CommandDto; -import org.eclipse.che.api.machine.shared.dto.MachineProcessDto; import org.eclipse.che.api.promises.client.Function; import org.eclipse.che.api.promises.client.FunctionException; import org.eclipse.che.api.promises.client.Operation; @@ -29,15 +28,12 @@ import org.eclipse.che.ide.api.command.CommandType; import org.eclipse.che.ide.api.command.CommandTypeRegistry; import org.eclipse.che.ide.api.machine.ExecAgentCommandManager; -import org.eclipse.che.ide.api.machine.MachineServiceClient; import org.eclipse.che.ide.api.macro.MacroProcessor; import org.eclipse.che.ide.api.workspace.WorkspaceServiceClient; import org.eclipse.che.ide.dto.DtoFactory; import org.eclipse.che.ide.extension.machine.client.outputspanel.console.CommandConsoleFactory; import org.eclipse.che.ide.extension.machine.client.outputspanel.console.CommandOutputConsole; import org.eclipse.che.ide.extension.machine.client.processes.panel.ProcessesPanelPresenter; -import org.eclipse.che.ide.util.UUID; -import org.eclipse.che.ide.util.loging.Log; import java.util.ArrayList; import java.util.Collections; diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/inject/MachineGinModule.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/inject/MachineGinModule.java index 8effb47d0fc..5bffcc4f043 100644 --- a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/inject/MachineGinModule.java +++ b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/inject/MachineGinModule.java @@ -21,7 +21,6 @@ import org.eclipse.che.ide.api.command.CommandType; import org.eclipse.che.ide.api.extension.ExtensionGinModule; import org.eclipse.che.ide.api.machine.MachineEntity; -import org.eclipse.che.ide.api.machine.MachineManager; import org.eclipse.che.ide.api.macro.Macro; import org.eclipse.che.ide.api.outputconsole.OutputConsole; import org.eclipse.che.ide.extension.machine.client.RecipeScriptDownloadServiceClient; @@ -37,9 +36,6 @@ import org.eclipse.che.ide.extension.machine.client.inject.factories.TerminalFactory; import org.eclipse.che.ide.extension.machine.client.inject.factories.WidgetsFactory; import org.eclipse.che.ide.extension.machine.client.machine.MachineItem; -import org.eclipse.che.ide.extension.machine.client.machine.MachineManagerImpl; -import org.eclipse.che.ide.extension.machine.client.machine.create.CreateMachineView; -import org.eclipse.che.ide.extension.machine.client.machine.create.CreateMachineViewImpl; import org.eclipse.che.ide.extension.machine.client.outputspanel.console.CommandConsoleFactory; import org.eclipse.che.ide.extension.machine.client.outputspanel.console.CommandOutputConsole; import org.eclipse.che.ide.extension.machine.client.outputspanel.console.CommandOutputConsolePresenter; @@ -82,7 +78,6 @@ public class MachineGinModule extends AbstractGinModule { @Override protected void configure() { - bind(CreateMachineView.class).to(CreateMachineViewImpl.class); bind(OutputConsoleView.class).to(OutputConsoleViewImpl.class); install(new GinFactoryModuleBuilder() .implement(CommandOutputConsole.class, Names.named("command"), CommandOutputConsolePresenter.class) @@ -111,8 +106,6 @@ protected void configure() { .build(EntityFactory.class)); install(new GinFactoryModuleBuilder().build(TerminalFactory.class)); - bind(MachineManager.class).to(MachineManagerImpl.class).in(Singleton.class); - bindConstant().annotatedWith(Names.named("machine.extension.api_port")).to(Constants.WS_AGENT_PORT); bind(SshView.class).to(SshViewImpl.class); diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/MachineManagerImpl.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/MachineManagerImpl.java deleted file mode 100644 index 12e8fc4aaaa..00000000000 --- a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/MachineManagerImpl.java +++ /dev/null @@ -1,156 +0,0 @@ -/******************************************************************************* - * 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.extension.machine.client.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.model.machine.MachineConfig; -import org.eclipse.che.api.core.model.machine.MachineSource; -import org.eclipse.che.api.machine.shared.dto.MachineConfigDto; -import org.eclipse.che.api.machine.shared.dto.MachineLimitsDto; -import org.eclipse.che.api.machine.shared.dto.MachineSourceDto; -import org.eclipse.che.api.promises.client.Operation; -import org.eclipse.che.api.promises.client.OperationException; -import org.eclipse.che.api.promises.client.Promise; -import org.eclipse.che.ide.api.app.AppContext; -import org.eclipse.che.ide.api.machine.MachineEntity; -import org.eclipse.che.ide.api.machine.MachineManager; -import org.eclipse.che.ide.api.machine.MachineServiceClient; -import org.eclipse.che.ide.api.machine.events.MachineStateEvent; -import org.eclipse.che.ide.api.workspace.WorkspaceServiceClient; -import org.eclipse.che.ide.dto.DtoFactory; - -import static org.eclipse.che.ide.api.machine.events.MachineStateEvent.MachineAction.DESTROYED; - -/** - * Manager for machine operations. - * - * @author Artem Zatsarynnyi - * @author Roman Nikitenko - */ -@Singleton -public class MachineManagerImpl implements MachineManager { - - private final MachineServiceClient machineServiceClient; - private final WorkspaceServiceClient workspaceServiceClient; - private final AppContext appContext; - private final DtoFactory dtoFactory; - private final EventBus eventBus; - - @Inject - public MachineManagerImpl(final MachineServiceClient machineServiceClient, - final WorkspaceServiceClient workspaceServiceClient, - final EventBus eventBus, - final AppContext appContext, - final DtoFactory dtoFactory) { - this.machineServiceClient = machineServiceClient; - this.workspaceServiceClient = workspaceServiceClient; - this.appContext = appContext; - this.dtoFactory = dtoFactory; - this.eventBus = eventBus; - } - - @Override - public void restartMachine(final MachineEntity machineState) { - destroyMachine(machineState).then(new Operation() { - @Override - public void apply(Void arg) throws OperationException { - final MachineConfig machineConfig = machineState.getConfig(); - final MachineSource machineSource = machineConfig.getSource(); - final String displayName = machineConfig.getName(); - final boolean isDev = machineConfig.isDev(); - - startMachine(asDto(machineSource), displayName, isDev, "docker"); - } - }); - } - - /** - * Converts {@link MachineSource} to {@link MachineSourceDto}. - */ - public MachineSourceDto asDto(MachineSource source) { - return this.dtoFactory.createDto(MachineSourceDto.class) - .withType(source.getType()) - .withLocation(source.getLocation()) - .withContent(source.getContent()); - } - - @Override - public void startMachine(String recipeURL, String displayName) { - startMachine(recipeURL, displayName, false, "dockerfile", "docker"); - } - - @Override - public void startDevMachine(String recipeURL, String displayName) { - startMachine(recipeURL, displayName, true, "dockerfile", "docker"); - } - - /** - * Start new machine in workspace. - * - * @param recipeURL - * special recipe url to get docker image. - * @param displayName - * display name for machine - * @param isDev - * @param sourceType - * "dockerfile" or "ssh-config" - * @param machineType - * "docker" or "ssh" - */ - private void startMachine(final String recipeURL, - final String displayName, - final boolean isDev, - final String sourceType, - final String machineType) { - MachineSourceDto sourceDto = dtoFactory.createDto(MachineSourceDto.class).withType(sourceType).withLocation(recipeURL); - startMachine(sourceDto, displayName, isDev, machineType); - } - - /** - * @param machineSourceDto - * @param displayName - * @param isDev - * @param machineType - * "docker" or "ssh" - */ - private void startMachine(final MachineSourceDto machineSourceDto, - final String displayName, - final boolean isDev, - final String machineType) { - - MachineLimitsDto limitsDto = dtoFactory.createDto(MachineLimitsDto.class).withRam(1024); - if (isDev) { - limitsDto.withRam(3072); - } - - MachineConfigDto configDto = dtoFactory.createDto(MachineConfigDto.class) - .withDev(isDev) - .withName(displayName) - .withSource(machineSourceDto) - .withLimits(limitsDto) - .withType(machineType); - workspaceServiceClient.createMachine(appContext.getWorkspaceId(), configDto); - } - - @Override - public Promise destroyMachine(final MachineEntity machineState) { - return machineServiceClient.destroyMachine(machineState.getWorkspaceId(), - machineState.getId()).then(new Operation() { - @Override - public void apply(Void arg) throws OperationException { - eventBus.fireEvent(new MachineStateEvent(machineState, DESTROYED)); - } - }); - } -} diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachinePresenter.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachinePresenter.java deleted file mode 100644 index 12ba65b38c3..00000000000 --- a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachinePresenter.java +++ /dev/null @@ -1,147 +0,0 @@ -/******************************************************************************* - * 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.extension.machine.client.machine.create; - -import com.google.gwt.regexp.shared.RegExp; -import com.google.inject.Inject; -import com.google.inject.Singleton; - -import org.eclipse.che.api.machine.shared.dto.recipe.RecipeDescriptor; -import org.eclipse.che.api.promises.client.Operation; -import org.eclipse.che.api.promises.client.OperationException; -import org.eclipse.che.ide.api.app.AppContext; -import org.eclipse.che.ide.api.machine.DevMachine; -import org.eclipse.che.ide.api.machine.MachineManager; -import org.eclipse.che.ide.api.machine.RecipeServiceClient; - -import java.util.Collections; -import java.util.List; - -/** - * Presenter for creating machine. - * - * @author Artem Zatsarynnyi - */ -@Singleton -public class CreateMachinePresenter implements CreateMachineView.ActionDelegate { - - private static final String URL_PATTERN = - "(https?|ftp)://(www\\.)?(((([a-zA-Z0-9.-]+\\.){1,}[a-zA-Z]{2,4}|localhost))|((\\d{1,3}\\.){3}(\\d{1,3})))(:(\\d+))?(/" + - "([a-zA-Z0-9-._~!$&'()*+,;=:@/]|%[0-9A-F]{2})*)?(\\?([a-zA-Z0-9-._~!$&'()*+,;=:/?@]|%[0-9A-F]{2})*)?(#([a-zA-Z0-9" + - "._-]|%[0-9A-F]{2})*)?"; - private static final RegExp URL = RegExp.compile(URL_PATTERN); - - private static final String RECIPE_TYPE = "docker"; - private static final int SKIP_COUNT = 0; - private static final int MAX_COUNT = 100; - - private final CreateMachineView view; - private final AppContext appContext; - private final MachineManager machineManager; - private final RecipeServiceClient recipeServiceClient; - - @Inject - public CreateMachinePresenter(CreateMachineView view, - AppContext appContext, - MachineManager machineManager, - RecipeServiceClient recipeServiceClient) { - this.view = view; - this.appContext = appContext; - this.machineManager = machineManager; - this.recipeServiceClient = recipeServiceClient; - - view.setDelegate(this); - } - - public void showDialog() { - view.show(); - - view.setCreateButtonState(false); - view.setReplaceButtonState(false); - view.setMachineName(""); - view.setErrorHint(false); - view.setNoRecipeHint(false); - view.setTags(""); - view.setRecipeURL(""); - } - - @Override - public void onNameChanged() { - checkButtons(); - } - - @Override - public void onRecipeUrlChanged() { - checkButtons(); - } - - @Override - public void onTagsChanged() { - if (view.getTags().isEmpty()) { - view.setRecipes(Collections.emptyList()); - view.setNoRecipeHint(false); - return; - } - - recipeServiceClient.searchRecipes(view.getTags(), RECIPE_TYPE, SKIP_COUNT, MAX_COUNT).then(new Operation>() { - @Override - public void apply(List arg) throws OperationException { - view.setRecipes(arg); - view.setNoRecipeHint(arg.isEmpty()); - } - }); - } - - @Override - public void onRecipeSelected(RecipeDescriptor recipe) { - view.setRecipeURL(recipe.getLink("get recipe script").getHref()); - } - - private void checkButtons() { - final String recipeURL = view.getRecipeURL(); - final boolean urlValid = URL.test(recipeURL); - - view.setErrorHint(!urlValid); - - final boolean allowCreation = urlValid && !view.getMachineName().isEmpty(); - - view.setCreateButtonState(allowCreation); - view.setReplaceButtonState(allowCreation); - } - - @Override - public void onCreateClicked() { - final String machineName = view.getMachineName(); - final String recipeURL = view.getRecipeURL(); - machineManager.startMachine(recipeURL, machineName); - - view.close(); - } - - @Override - public void onReplaceDevMachineClicked() { - final String machineName = view.getMachineName(); - final String recipeURL = view.getRecipeURL(); - final DevMachine devMachine = appContext.getDevMachine(); - - if (devMachine != null) { - machineManager.destroyMachine(devMachine); - } - - machineManager.startDevMachine(recipeURL, machineName); - view.close(); - } - - @Override - public void onCancelClicked() { - view.close(); - } -} diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachineView.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachineView.java deleted file mode 100644 index 6c40c047afe..00000000000 --- a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachineView.java +++ /dev/null @@ -1,105 +0,0 @@ -/******************************************************************************* - * 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.extension.machine.client.machine.create; - -import org.eclipse.che.api.machine.shared.dto.recipe.RecipeDescriptor; -import org.eclipse.che.ide.api.mvp.View; - -import java.util.List; - -/** - * The view of {@link CreateMachinePresenter}. - * - * @author Artem Zatsarynnyi - */ -public interface CreateMachineView extends View { - - /** Show view. */ - void show(); - - /** Close view. */ - void close(); - - /** Returns machine name. */ - String getMachineName(); - - /** Sets machine name. */ - void setMachineName(String name); - - /** Returns recipe URL. */ - String getRecipeURL(); - - /** Sets recipe URL. */ - void setRecipeURL(String url); - - /** Sets error hint visibility. */ - void setErrorHint(boolean show); - - /** Returns tags. */ - List getTags(); - - /** Sets tags. */ - void setTags(String tags); - - /** Sets 'no recipe' hint visibility. */ - void setNoRecipeHint(boolean show); - - /** Sets recipes corresponded to tags. */ - void setRecipes(List recipes); - - /** - * Sets whether 'Create' button is enabled. - * - * @param enabled - * true to enable the button, - * false to disable it - */ - void setCreateButtonState(boolean enabled); - - /** - * Sets whether 'Replace' button is enabled. - * - * @param enabled - * true to enable the button, - * false to disable it - */ - void setReplaceButtonState(boolean enabled); - - /** Action handler for the view actions/controls. */ - interface ActionDelegate { - - /** Called when machines name has been changed. */ - void onNameChanged(); - - /** Called when recipe URL has been changed. */ - void onRecipeUrlChanged(); - - /** Called when tags has been changed. */ - void onTagsChanged(); - - /** - * Called when recipe has been selected. - * - * @param recipe - * selected recipe - */ - void onRecipeSelected(RecipeDescriptor recipe); - - /** Called when 'Create' button has been clicked. */ - void onCreateClicked(); - - /** Called when 'Replace Dev Machine' button has been clicked. */ - void onReplaceDevMachineClicked(); - - /** Called when 'Cancel' button has been clicked. */ - void onCancelClicked(); - } -} diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachineViewImpl.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachineViewImpl.java deleted file mode 100644 index 3634aed0bd6..00000000000 --- a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachineViewImpl.java +++ /dev/null @@ -1,387 +0,0 @@ -/******************************************************************************* - * 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.extension.machine.client.machine.create; - -import elemental.dom.Element; -import elemental.dom.Node; -import elemental.html.TableCellElement; -import elemental.html.TableElement; - -import com.google.gwt.core.client.GWT; -import com.google.gwt.core.client.Scheduler; -import com.google.gwt.dom.client.Style; -import com.google.gwt.event.dom.client.ClickEvent; -import com.google.gwt.event.dom.client.ClickHandler; -import com.google.gwt.event.dom.client.KeyDownEvent; -import com.google.gwt.event.dom.client.KeyUpEvent; -import com.google.gwt.event.dom.client.KeyUpHandler; -import com.google.gwt.uibinder.client.UiBinder; -import com.google.gwt.uibinder.client.UiField; -import com.google.gwt.uibinder.client.UiHandler; -import com.google.gwt.user.client.Command; -import com.google.gwt.user.client.Timer; -import com.google.gwt.user.client.ui.Button; -import com.google.gwt.user.client.ui.HTML; -import com.google.gwt.user.client.ui.Label; -import com.google.gwt.user.client.ui.PopupPanel; -import com.google.gwt.user.client.ui.PopupPanel.PositionCallback; -import com.google.gwt.user.client.ui.TextBox; -import com.google.gwt.user.client.ui.Widget; -import com.google.inject.Inject; -import com.google.inject.Singleton; - -import org.eclipse.che.api.machine.shared.dto.recipe.RecipeDescriptor; -import org.eclipse.che.ide.api.autocomplete.AutoCompleteResources; -import org.eclipse.che.ide.extension.machine.client.MachineLocalizationConstant; -import org.eclipse.che.ide.extension.machine.client.MachineResources; -import org.eclipse.che.ide.ui.list.SimpleList; -import org.eclipse.che.ide.ui.toolbar.ToolbarResources; -import org.eclipse.che.ide.ui.window.Window; -import org.eclipse.che.ide.util.dom.Elements; -import org.vectomatic.dom.svg.ui.SVGImage; - -import java.util.ArrayList; -import java.util.List; - -import static com.google.gwt.dom.client.Style.Overflow.AUTO; -import static com.google.gwt.event.dom.client.KeyCodes.KEY_DOWN; -import static com.google.gwt.event.dom.client.KeyCodes.KEY_ENTER; -import static com.google.gwt.event.dom.client.KeyCodes.KEY_ESCAPE; -import static com.google.gwt.event.dom.client.KeyCodes.KEY_LEFT; -import static com.google.gwt.event.dom.client.KeyCodes.KEY_RIGHT; -import static com.google.gwt.event.dom.client.KeyCodes.KEY_UP; - -/** - * The implementation of {@link CreateMachineView}. - * - * @author Artem Zatsarynnyi - */ -@Singleton -public class CreateMachineViewImpl extends Window implements CreateMachineView { - - private static final CreateMachineViewImplUiBinder UI_BINDER = GWT.create(CreateMachineViewImplUiBinder.class); - - private final MachineResources machineResources; - private final org.eclipse.che.ide.Resources coreResources; - private final AutoCompleteResources.Css css; - private final SimpleList.ListItemRenderer listItemRenderer = - new SimpleList.ListItemRenderer() { - @Override - public void render(Element itemElement, RecipeDescriptor itemData) { - final TableCellElement icon = Elements.createTDElement(css.proposalIcon()); - final TableCellElement label = Elements.createTDElement(css.proposalLabel()); - final TableCellElement group = Elements.createTDElement(css.proposalGroup()); - - final SVGImage image = new SVGImage(machineResources.recipe()); - image.getElement().setAttribute("class", toolbarResources.toolbar().iconButtonIcon()); - image.getElement().getStyle().setMargin(0, Style.Unit.PX); - icon.appendChild((Node)image.getElement()); - - label.setInnerHTML(itemData.getName()); - group.setInnerHTML(itemData.getType()); - - itemElement.appendChild(icon); - itemElement.appendChild(label); - itemElement.appendChild(group); - } - - @Override - public Element createElement() { - return Elements.createTRElement(); - } - }; - - private final PopupPanel popupPanel; - - @UiField(provided = true) - MachineLocalizationConstant localizationConstant; - @UiField - TextBox machineName; - @UiField - TextBox recipeURL; - @UiField - Label errorHint; - @UiField - TextBox tags; - @UiField - Label noRecipeHint; - - private ToolbarResources toolbarResources; - - private SimpleList list; - private ActionDelegate delegate; - private final SimpleList.ListEventDelegate eventDelegate = new SimpleList.ListEventDelegate() { - @Override - public void onListItemClicked(Element listItemBase, RecipeDescriptor itemData) { - list.getSelectionModel().setSelectedItem(itemData); - } - - @Override - public void onListItemDoubleClicked(Element listItemBase, RecipeDescriptor itemData) { - delegate.onRecipeSelected(itemData); - popupPanel.hide(); - tags.setFocus(true); - } - }; - - private Button createButton; - private Button replaceButton; - private Button cancelButton; - - @Inject - public CreateMachineViewImpl(MachineLocalizationConstant localizationConstant, - MachineResources machineResources, - org.eclipse.che.ide.Resources coreResources, - AutoCompleteResources autoCompleteResources, - ToolbarResources toolbarResources) { - this.localizationConstant = localizationConstant; - this.machineResources = machineResources; - this.coreResources = coreResources; - this.toolbarResources = toolbarResources; - - css = autoCompleteResources.autocompleteComponentCss(); - popupPanel = new PopupPanel(); - - setWidget(UI_BINDER.createAndBindUi(this)); - setTitle(localizationConstant.viewCreateMachineTitle()); - - machineName.addKeyUpHandler(new KeyUpHandler() { - @Override - public void onKeyUp(KeyUpEvent event) { - delegate.onNameChanged(); - } - }); - - recipeURL.addKeyUpHandler(new KeyUpHandler() { - @Override - public void onKeyUp(KeyUpEvent event) { - delegate.onRecipeUrlChanged(); - } - }); - - createFooterButtons(); - } - - private void createFooterButtons() { - createButton = createButton(localizationConstant.viewCreateMachineButtonCreate(), "window-create-machine-create", - new ClickHandler() { - @Override - public void onClick(ClickEvent event) { - delegate.onCreateClicked(); - } - }); - - replaceButton = createButton(localizationConstant.viewCreateMachineButtonReplace(), "window-create-machine-replace", - new ClickHandler() { - @Override - public void onClick(ClickEvent event) { - delegate.onReplaceDevMachineClicked(); - } - }); - - cancelButton = createButton(localizationConstant.cancelButton(), "window-create-machine-cancel", - new ClickHandler() { - @Override - public void onClick(ClickEvent event) { - delegate.onCancelClicked(); - } - }); - - addButtonToFooter(createButton); - addButtonToFooter(replaceButton); - addButtonToFooter(cancelButton); - } - - @Override - public void setDelegate(ActionDelegate delegate) { - this.delegate = delegate; - } - - @Override - public void show() { - super.show(); - - new Timer() { - @Override - public void run() { - machineName.setFocus(true); - } - }.schedule(300); - } - - @Override - public void close() { - hide(); - } - - @Override - protected void onClose() { - super.onClose(); - popupPanel.hide(); - } - - @Override - public String getMachineName() { - return machineName.getValue(); - } - - @Override - public void setMachineName(String name) { - machineName.setValue(name); - delegate.onNameChanged(); - } - - @Override - public String getRecipeURL() { - return recipeURL.getValue(); - } - - @Override - public void setRecipeURL(String url) { - recipeURL.setValue(url); - recipeURL.setTitle(url); - - delegate.onRecipeUrlChanged(); - } - - @Override - public void setErrorHint(boolean show) { - errorHint.setVisible(show); - } - - @Override - public List getTags() { - final List tagList = new ArrayList<>(); - - for (String tag : tags.getValue().split(" ")) { - if (!tag.isEmpty()) { - tagList.add(tag.trim()); - } - } - - return tagList; - } - - @Override - public void setTags(String tags) { - this.tags.setValue(tags); - } - - @Override - public void setNoRecipeHint(boolean show) { - noRecipeHint.setVisible(show); - } - - @Override - public void setRecipes(List recipes) { - if (recipes.isEmpty()) { - popupPanel.hide(); - return; - } - - popupPanel.clear(); - - final TableElement itemHolder = Elements.createTableElement(); - itemHolder.setClassName(css.items()); - - final HTML html = new HTML(); - html.setStyleName(css.container()); - html.getElement().getStyle().setOverflow(AUTO); - html.getElement().appendChild(((com.google.gwt.dom.client.Element)itemHolder)); - ((Element)html.getElement()).getStyle().setProperty("max-height", "200px"); - - final HTML container = new HTML(); - container.getElement().appendChild(html.getElement()); - - list = SimpleList.create((SimpleList.View)container.getElement().cast(), - (Element)html.getElement(), - itemHolder, - coreResources.defaultSimpleListCss(), - listItemRenderer, - eventDelegate); - - list.render(recipes); - - popupPanel.add(container); - popupPanel.setWidth(tags.getOffsetWidth() - 10 + "px"); - popupPanel.setPopupPositionAndShow(new PositionCallback() { - @Override - public void setPosition(int offsetWidth, int offsetHeight) { - popupPanel.setPopupPosition(tags.getAbsoluteLeft(), tags.getAbsoluteTop() + tags.getOffsetHeight()); - } - }); - - list.getSelectionModel().setSelectedItem(0); - } - - @Override - public void setCreateButtonState(boolean enabled) { - createButton.setEnabled(enabled); - } - - @Override - public void setReplaceButtonState(boolean enabled) { - replaceButton.setEnabled(enabled); - } - - @UiHandler("tags") - void handleKeyDown(KeyDownEvent event) { - switch (event.getNativeKeyCode()) { - case KEY_UP: - if (popupPanel.isShowing()) { - event.preventDefault(); - - if (list.getSelectionModel().getSelectedIndex() == 0) { - list.getSelectionModel().setSelectedItem(list.getSelectionModel().size() - 1); - } else { - list.getSelectionModel().selectPrevious(); - } - } - break; - case KEY_DOWN: - if (popupPanel.isShowing()) { - event.preventDefault(); - - if (list.getSelectionModel().getSelectedIndex() == list.getSelectionModel().size() - 1) { - list.getSelectionModel().setSelectedItem(0); - } else { - list.getSelectionModel().selectNext(); - } - } - break; - case KEY_ENTER: - if (popupPanel.isShowing()) { - delegate.onRecipeSelected(list.getSelectionModel().getSelectedItem()); - popupPanel.hide(); - } - break; - case KEY_ESCAPE: - if (popupPanel.isShowing()) { - popupPanel.hide(); - } - break; - case KEY_LEFT: - case KEY_RIGHT: - break; - default: - Scheduler.get().scheduleDeferred(new Command() { - @Override - public void execute() { - delegate.onTagsChanged(); - } - }); - break; - } - } - - interface CreateMachineViewImplUiBinder extends UiBinder { - } -} diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachineViewImpl.ui.xml b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachineViewImpl.ui.xml deleted file mode 100644 index b083f64fdec..00000000000 --- a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachineViewImpl.ui.xml +++ /dev/null @@ -1,81 +0,0 @@ - - - - - - - .emptyBorder { - margin: 6px; - } - - .inputField { - color: #dbdbdb; - border: 1px solid #191c1e; - border-radius: 1px; - box-shadow: 0px 1px 2px 0px rgba(0, 0, 0, 0.19) inset, 0px 1px 0px 0px rgba(223, 223, 223, 0.4); - font-size: 12px; - padding-left: 8px; - height: 20px; - } - - .label { - line-height: 25px; - } - - .floatLeft { - float: left; - } - - .floatRight { - float: right; - } - - .errorHint { - color: red; - height: 12px; - margin: 10px; - } - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/perspective/widgets/machine/appliance/processes/ProcessesPresenter.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/perspective/widgets/machine/appliance/processes/ProcessesPresenter.java index a4aeef627b2..97f5ea2ce95 100644 --- a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/perspective/widgets/machine/appliance/processes/ProcessesPresenter.java +++ b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/perspective/widgets/machine/appliance/processes/ProcessesPresenter.java @@ -20,7 +20,6 @@ import org.eclipse.che.api.promises.client.Operation; import org.eclipse.che.api.promises.client.OperationException; import org.eclipse.che.ide.api.machine.ExecAgentCommandManager; -import org.eclipse.che.ide.api.machine.MachineServiceClient; import org.eclipse.che.ide.dto.DtoFactory; import org.eclipse.che.ide.extension.machine.client.perspective.widgets.tab.content.TabPresenter; diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/targets/categories/docker/DockerCategoryPresenter.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/targets/categories/docker/DockerCategoryPresenter.java index 0ef8b879dcb..1e0e1bc59b4 100644 --- a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/targets/categories/docker/DockerCategoryPresenter.java +++ b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/targets/categories/docker/DockerCategoryPresenter.java @@ -12,30 +12,14 @@ import com.google.gwt.user.client.ui.AcceptsOneWidget; import com.google.inject.Inject; -import com.google.web.bindery.event.shared.EventBus; - -import org.eclipse.che.api.promises.client.Operation; -import org.eclipse.che.api.promises.client.OperationException; -import org.eclipse.che.api.promises.client.PromiseError; -import org.eclipse.che.ide.api.dialogs.CancelCallback; -import org.eclipse.che.ide.api.dialogs.ConfirmCallback; -import org.eclipse.che.ide.api.dialogs.DialogFactory; + import org.eclipse.che.ide.api.machine.MachineEntity; -import org.eclipse.che.ide.api.machine.MachineServiceClient; -import org.eclipse.che.ide.api.notification.NotificationManager; import org.eclipse.che.ide.extension.machine.client.MachineLocalizationConstant; -import org.eclipse.che.ide.api.machine.events.MachineStateEvent; import org.eclipse.che.ide.extension.machine.client.targets.CategoryPage; import org.eclipse.che.ide.extension.machine.client.targets.Target; import org.eclipse.che.ide.extension.machine.client.targets.TargetManager; import org.eclipse.che.ide.extension.machine.client.targets.TargetsTreeManager; -import static org.eclipse.che.api.core.model.machine.MachineStatus.RUNNING; -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.api.notification.StatusNotification.Status.SUCCESS; -import static org.eclipse.che.ide.api.machine.events.MachineStateEvent.MachineAction.DESTROYED; - /** * Docker type page presenter. * @@ -43,29 +27,16 @@ */ public class DockerCategoryPresenter implements CategoryPage, TargetManager, DockerView.ActionDelegate { private final DockerView dockerView; - private final DialogFactory dialogFactory; - private final NotificationManager notificationManager; private final MachineLocalizationConstant machineLocale; - private final MachineServiceClient machineService; - private final EventBus eventBus; private DockerMachineTarget selectedTarget; private TargetsTreeManager targetsTreeManager; @Inject public DockerCategoryPresenter(DockerView dockerView, - DialogFactory dialogFactory, - NotificationManager notificationManager, - MachineLocalizationConstant machineLocale, - MachineServiceClient machineService, - EventBus eventBus) { + MachineLocalizationConstant machineLocale) { this.dockerView = dockerView; - this.dialogFactory = dialogFactory; - this.notificationManager = notificationManager; this.machineLocale = machineLocale; - this.machineService = machineService; - this.eventBus = eventBus; - dockerView.setDelegate(this); } @@ -93,13 +64,6 @@ private MachineEntity getMachineByName(String machineName) { return this.targetsTreeManager != null ? this.targetsTreeManager.getMachineByName(machineName) : null; } - private void updateTargets(String preselectTargetName) { - if (this.targetsTreeManager == null) { - return; - } - this.targetsTreeManager.updateTargets(preselectTargetName); - } - @Override public boolean onRestoreTargetFields(DockerMachineTarget target) { if (target == null) { @@ -122,44 +86,10 @@ public boolean onRestoreTargetFields(DockerMachineTarget target) { @Override public void onDeleteClicked(final Target target) { - dialogFactory.createConfirmDialog(machineLocale.targetsViewDeleteConfirmTitle(), - machineLocale.targetsViewDeleteConfirm(target.getName()), - new ConfirmCallback() { - @Override - public void accepted() { - destroyTargetMachine(target); - } - }, new CancelCallback() { - @Override - public void cancelled() { - updateTargets(null); - } - }).show(); + //unsupported operation } - private void destroyTargetMachine(final Target target) { - final MachineEntity machine = this.getMachineByName(target.getName()); - if (machine == null || machine.getStatus() != RUNNING) { - return; - } - - machineService.destroyMachine(machine.getWorkspaceId(), - machine.getId()).then(new Operation() { - @Override - public void apply(Void arg) throws OperationException { - eventBus.fireEvent(new MachineStateEvent(machine, DESTROYED)); - notificationManager.notify(machineLocale.targetsViewDisconnectSuccess(target.getName()), SUCCESS, FLOAT_MODE); - updateTargets(null); - } - }).catchError(new Operation() { - @Override - public void apply(PromiseError arg) throws OperationException { - notificationManager.notify(machineLocale.targetsViewDisconnectError(target.getName()), FAIL, FLOAT_MODE); - updateTargets(target.getName()); - } - }); - } @Override public void setCurrentSelection(Target selectedTarget) { diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/targets/categories/ssh/SshCategoryPresenter.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/targets/categories/ssh/SshCategoryPresenter.java index c632f0c7a5b..3c26813ddcd 100644 --- a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/targets/categories/ssh/SshCategoryPresenter.java +++ b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/targets/categories/ssh/SshCategoryPresenter.java @@ -32,12 +32,10 @@ import org.eclipse.che.api.workspace.shared.dto.WorkspaceDto; import org.eclipse.che.api.workspace.shared.dto.WorkspaceRuntimeDto; import org.eclipse.che.ide.api.app.AppContext; -import org.eclipse.che.ide.api.dialogs.CancelCallback; -import org.eclipse.che.ide.api.dialogs.ConfirmCallback; import org.eclipse.che.ide.api.dialogs.DialogFactory; import org.eclipse.che.ide.api.machine.MachineEntity; -import org.eclipse.che.ide.api.machine.MachineServiceClient; import org.eclipse.che.ide.api.machine.RecipeServiceClient; +import org.eclipse.che.ide.api.machine.events.MachineStateEvent; import org.eclipse.che.ide.api.notification.NotificationManager; import org.eclipse.che.ide.api.notification.StatusNotification; import org.eclipse.che.ide.api.workspace.WorkspaceServiceClient; @@ -45,7 +43,6 @@ import org.eclipse.che.ide.dto.DtoFactory; import org.eclipse.che.ide.extension.machine.client.MachineLocalizationConstant; import org.eclipse.che.ide.extension.machine.client.inject.factories.EntityFactory; -import org.eclipse.che.ide.api.machine.events.MachineStateEvent; import org.eclipse.che.ide.extension.machine.client.targets.CategoryPage; import org.eclipse.che.ide.extension.machine.client.targets.Target; import org.eclipse.che.ide.extension.machine.client.targets.TargetManager; @@ -55,11 +52,10 @@ import java.util.List; import static org.eclipse.che.api.core.model.machine.MachineStatus.RUNNING; +import static org.eclipse.che.ide.api.machine.events.MachineStateEvent.MachineAction.DESTROYED; 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.api.notification.StatusNotification.Status.PROGRESS; import static org.eclipse.che.ide.api.notification.StatusNotification.Status.SUCCESS; -import static org.eclipse.che.ide.api.machine.events.MachineStateEvent.MachineAction.DESTROYED; /** * SSH type page presenter. @@ -77,7 +73,6 @@ public class SshCategoryPresenter implements CategoryPage, TargetManager, SshVie private final NotificationManager notificationManager; private final MachineLocalizationConstant machineLocale; private final AppContext appContext; - private final MachineServiceClient machineService; private final EventBus eventBus; private final WorkspaceServiceClient workspaceServiceClient; @@ -100,7 +95,6 @@ public SshCategoryPresenter(SshView sshView, MachineLocalizationConstant machineLocale, WorkspaceServiceClient workspaceServiceClient, AppContext appContext, - MachineServiceClient machineService, EventBus eventBus) { this.sshView = sshView; this.recipeServiceClient = recipeServiceClient; @@ -111,7 +105,6 @@ public SshCategoryPresenter(SshView sshView, this.workspaceServiceClient = workspaceServiceClient; this.machineLocale = machineLocale; this.appContext = appContext; - this.machineService = machineService; this.eventBus = eventBus; sshView.setDelegate(this); @@ -471,40 +464,14 @@ private void disconnect(final MachineEntity machine) { return; } sshView.setConnectButtonText(null); - - machineService.destroyMachine(machine.getWorkspaceId(), - machine.getId()).then(new Operation() { - @Override - public void apply(Void arg) throws OperationException { - eventBus.fireEvent(new MachineStateEvent(machine, DESTROYED)); - - notificationManager.notify(machineLocale.targetsViewDisconnectSuccess(selectedTarget.getName()), SUCCESS, FLOAT_MODE); - updateTargets(null); - } - }).catchError(new Operation() { - @Override - public void apply(PromiseError arg) throws OperationException { - notificationManager.notify(machineLocale.targetsViewDisconnectError(selectedTarget.getName()), FAIL, FLOAT_MODE); - updateTargets(null); - } - }); + eventBus.fireEvent(new MachineStateEvent(machine, DESTROYED)); + notificationManager.notify(machineLocale.targetsViewDisconnectSuccess(selectedTarget.getName()), SUCCESS, FLOAT_MODE); + updateTargets(null); } @Override public void onDeleteClicked(final Target target) { - dialogFactory.createConfirmDialog(machineLocale.targetsViewDeleteConfirmTitle(), - machineLocale.targetsViewDeleteConfirm(target.getName()), - new ConfirmCallback() { - @Override - public void accepted() { - deleteTarget(target); - } - }, new CancelCallback() { - @Override - public void cancelled() { - updateTargets(null); - } - }).show(); + //unsupported operation } private void deleteTarget(final Target target) { @@ -519,22 +486,6 @@ private void deleteTarget(final Target target) { disconnect(machine); return; } - - machineService.destroyMachine(machine.getWorkspaceId(), - machine.getId()).then(new Operation() { - @Override - public void apply(Void arg) throws OperationException { - eventBus.fireEvent(new MachineStateEvent(machine, DESTROYED)); - notificationManager.notify(machineLocale.targetsViewDisconnectSuccess(target.getName()), SUCCESS, FLOAT_MODE); - deleteTargetRecipe(target); - } - }).catchError(new Operation() { - @Override - public void apply(PromiseError arg) throws OperationException { - notificationManager.notify(machineLocale.targetsViewDisconnectError(target.getName()), FAIL, FLOAT_MODE); - updateTargets(target.getName()); - } - }); } /** diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/machine/MachineManagerImplTest.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/machine/MachineManagerImplTest.java deleted file mode 100644 index da37fdbb8d9..00000000000 --- a/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/machine/MachineManagerImplTest.java +++ /dev/null @@ -1,169 +0,0 @@ -/******************************************************************************* - * 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.extension.machine.client.machine; - -import com.google.web.bindery.event.shared.EventBus; - -import org.eclipse.che.api.core.model.machine.MachineConfig; -import org.eclipse.che.api.core.model.machine.MachineSource; -import org.eclipse.che.api.machine.shared.dto.MachineLimitsDto; -import org.eclipse.che.api.machine.shared.dto.MachineConfigDto; -import org.eclipse.che.api.machine.shared.dto.MachineSourceDto; -import org.eclipse.che.api.promises.client.Operation; -import org.eclipse.che.api.promises.client.OperationException; -import org.eclipse.che.api.promises.client.Promise; -import org.eclipse.che.ide.api.app.AppContext; -import org.eclipse.che.ide.api.machine.DevMachine; -import org.eclipse.che.ide.api.machine.MachineEntity; -import org.eclipse.che.ide.api.machine.MachineServiceClient; -import org.eclipse.che.ide.api.machine.events.MachineStateEvent; -import org.eclipse.che.ide.api.parts.PerspectiveManager; -import org.eclipse.che.ide.api.workspace.WorkspaceServiceClient; -import org.eclipse.che.ide.dto.DtoFactory; -import org.eclipse.che.ide.rest.DtoUnmarshallerFactory; -import org.eclipse.che.ide.websocket.MessageBusProvider; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.ArgumentCaptor; -import org.mockito.Captor; -import org.mockito.InjectMocks; -import org.mockito.Matchers; -import org.mockito.Mock; -import org.mockito.runners.MockitoJUnitRunner; - -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyBoolean; -import static org.mockito.Matchers.anyInt; -import static org.mockito.Matchers.anyString; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -/** - * Check {@link MachineManagerImpl} - * - * @author Florent Benoit - */ -@RunWith(MockitoJUnitRunner.class) -public class MachineManagerImplTest { - - @Mock - private DtoUnmarshallerFactory dtoUnmarshallerFactor; - - @Mock - private MachineServiceClient machineServiceClient; - - @Mock - private WorkspaceServiceClient workspaceServiceClient; - - @Mock - private MachineStatusHandler machineStatusHandler; - - @Mock - private MessageBusProvider messageBusProvider; - - @Mock - private PerspectiveManager perspectiveManager; - - @Mock - private EventBus eventBus; - - @Mock - private AppContext appContext; - - @Mock - private DtoFactory dtoFactory; - - @Captor - private ArgumentCaptor startWorkspaceHandlerCaptor; - - @Captor - private ArgumentCaptor> operationArgumentCaptor; - - @Captor - private ArgumentCaptor machineConfigDtoArgumentCaptor; - - - @InjectMocks - private MachineManagerImpl machineManager; - - /** - * Check a valid source object is used on machine destroyed with restart flag - * - * @throws OperationException - * if restart fails - */ - @Test - public void checkUseValidSource() throws OperationException { - final String ID = "id"; - final String WORKSPACE_ID = "testWorkspaceId"; - final String DISPLAY_NAME = "my-display-name"; - final boolean IS_DEV = true; - - final String SOURCE_TYPE = "source-type"; - final String SOURCE_LOCATION = "source-location"; - final String SOURCE_CONTENT = "source-content"; - - MachineEntity machineState = mock(MachineEntity.class); - when(machineState.getId()).thenReturn(ID); - when(machineState.getWorkspaceId()).thenReturn(WORKSPACE_ID); - Promise promise = mock(Promise.class); - Promise promiseThen = mock(Promise.class); - when(machineServiceClient.destroyMachine(eq(WORKSPACE_ID), eq(ID))).thenReturn(promise); - when(promise.then(Matchers.>anyObject())).thenReturn(promiseThen); - - MachineSource machineSource = mock(MachineSource.class); - MachineConfig machineConfig = mock(MachineConfig.class); - when(machineState.getConfig()).thenReturn(machineConfig); - when(machineConfig.getSource()).thenReturn(machineSource); - when(machineConfig.getName()).thenReturn(DISPLAY_NAME); - when(machineConfig.isDev()).thenReturn(IS_DEV); - when(machineSource.getType()).thenReturn(SOURCE_TYPE); - when(machineSource.getLocation()).thenReturn(SOURCE_LOCATION); - when(machineSource.getContent()).thenReturn(SOURCE_CONTENT); - MachineSourceDto machineSourceDto = mock(MachineSourceDto.class); - when(machineSourceDto.withType(eq(SOURCE_TYPE))).thenReturn(machineSourceDto); - when(machineSourceDto.withLocation(eq(SOURCE_LOCATION))).thenReturn(machineSourceDto); - when(machineSourceDto.withContent(eq(SOURCE_CONTENT))).thenReturn(machineSourceDto); - when(dtoFactory.createDto(MachineSourceDto.class)).thenReturn(machineSourceDto); - MachineLimitsDto limitsDto = mock(MachineLimitsDto.class); - when(dtoFactory.createDto(MachineLimitsDto.class)).thenReturn(limitsDto); - when(limitsDto.withRam(anyInt())).thenReturn(limitsDto); - - MachineConfigDto machineConfigDto = mock(MachineConfigDto.class); - when(dtoFactory.createDto(MachineConfigDto.class)).thenReturn(machineConfigDto); - when(machineConfigDto.withDev(anyBoolean())).thenReturn(machineConfigDto); - when(machineConfigDto.withName(anyString())).thenReturn(machineConfigDto); - when(machineConfigDto.withSource(machineSourceDto)).thenReturn(machineConfigDto); - when(machineConfigDto.withLimits(limitsDto)).thenReturn(machineConfigDto); - when(machineConfigDto.withType(anyString())).thenReturn(machineConfigDto); - - when(appContext.getWorkspaceId()).thenReturn(ID); - DevMachine devMachine = mock(DevMachine.class); - when(appContext.getDevMachine()).thenReturn(devMachine); - when(devMachine.getId()).thenReturn(ID); - - Promise promiseEmpty = mock(Promise.class); - when(workspaceServiceClient.createMachine(anyString(), any(MachineConfigDto.class))).thenReturn(promiseEmpty); - - machineManager.restartMachine(machineState); - - verify(promiseThen).then(operationArgumentCaptor.capture()); - operationArgumentCaptor.getValue().apply(null); - - verify(workspaceServiceClient).createMachine(eq(ID), machineConfigDtoArgumentCaptor.capture()); - verify(machineSourceDto).withType(eq(SOURCE_TYPE)); - verify(machineSourceDto).withLocation(eq(SOURCE_LOCATION)); - verify(machineSourceDto).withContent(eq(SOURCE_CONTENT)); - } - -} diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachinePresenterTest.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachinePresenterTest.java deleted file mode 100644 index 383bfca31ff..00000000000 --- a/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachinePresenterTest.java +++ /dev/null @@ -1,147 +0,0 @@ -/******************************************************************************* - * 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.extension.machine.client.machine.create; - -import org.eclipse.che.ide.api.app.AppContext; -import org.eclipse.che.ide.api.machine.DevMachine; -import org.eclipse.che.ide.api.machine.MachineEntity; -import org.eclipse.che.ide.api.machine.MachineManager; -import org.eclipse.che.ide.api.project.ProjectTypeServiceClient; -import org.eclipse.che.ide.extension.machine.client.inject.factories.EntityFactory; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.runners.MockitoJUnitRunner; - -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -/** @author Artem Zatsarynny */ -@RunWith(MockitoJUnitRunner.class) -public class CreateMachinePresenterTest { - - private final static String RECIPE_URL = "http://www.host.com/recipe"; - private final static String MACHINE_NAME = "machine"; - private final static String WORKSPACE_ID = "testWorkspace123"; - - private final static String SOME_TEXT = "someText"; - - @Mock - private CreateMachineView view; - @Mock - private MachineManager machineManager; - @Mock - private AppContext appContext; - @Mock - private ProjectTypeServiceClient projectTypeServiceClient; - @Mock - private EntityFactory entityFactory; - - @InjectMocks - private CreateMachinePresenter presenter; - - @Before - public void setUp() { - when(view.getRecipeURL()).thenReturn(RECIPE_URL); - when(view.getMachineName()).thenReturn(MACHINE_NAME); - } - - @Test - public void shouldSetActionDelegate() throws Exception { - verify(view).setDelegate(presenter); - } - - @Test - public void viewShouldBeShown() throws Exception { - presenter.showDialog(); - - verify(view).show(); - verify(view).setCreateButtonState(false); - verify(view).setReplaceButtonState(false); - verify(view).setMachineName(""); - verify(view).setRecipeURL(""); - verify(view).setErrorHint(false); - verify(view).setTags(""); - } - - @Test - public void buttonsShouldBeDisabledWhenNameIsEmpty() throws Exception { - when(view.getMachineName()).thenReturn(""); - - presenter.onNameChanged(); - - verify(view).setCreateButtonState(eq(false)); - verify(view).setReplaceButtonState(eq(false)); - } - - @Test - public void buttonsShouldBeEnabledWhenNameIsNotEmpty() throws Exception { - presenter.onNameChanged(); - - verify(view).setCreateButtonState(eq(true)); - verify(view).setReplaceButtonState(eq(true)); - } - - @Test - public void shouldCreateMachine() throws Exception { - presenter.onCreateClicked(); - - verify(view).getRecipeURL(); - verify(view).getMachineName(); - verify(machineManager).startMachine(eq(RECIPE_URL), eq(MACHINE_NAME)); - verify(view).close(); - } - - @Test - public void shouldReplaceDevMachine() throws Exception { - DevMachine devMachine = mock(DevMachine.class); - when(appContext.getDevMachine()).thenReturn(devMachine); - when(devMachine.getId()).thenReturn(SOME_TEXT); - when(devMachine.getWorkspace()).thenReturn(WORKSPACE_ID); - - presenter.onReplaceDevMachineClicked(); - - verify(view).getMachineName(); - verify(view).getRecipeURL(); - verify(appContext).getDevMachine(); - verify(machineManager).destroyMachine(devMachine); - verify(machineManager).startDevMachine(eq(RECIPE_URL), eq(MACHINE_NAME)); - verify(view).close(); - } - - @Test - public void shouldStartNewDevMachine() throws Exception { - when(appContext.getDevMachine()).thenReturn(null); - - presenter.onReplaceDevMachineClicked(); - - verify(view).getMachineName(); - verify(view).getRecipeURL(); - verify(appContext).getDevMachine(); - verify(machineManager).startDevMachine(eq(RECIPE_URL), eq(MACHINE_NAME)); - verify(view).close(); - verify(machineManager, never()).destroyMachine(any(MachineEntity.class)); - verify(machineManager).startDevMachine(eq(RECIPE_URL), eq(MACHINE_NAME)); - } - - @Test - public void shouldCloseView() throws Exception { - presenter.onCancelClicked(); - - verify(view).close(); - } -} diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/perspective/widgets/machine/appliance/processes/ProcessesPresenterTest.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/perspective/widgets/machine/appliance/processes/ProcessesPresenterTest.java index b0a60a93df6..b9285383e38 100644 --- a/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/perspective/widgets/machine/appliance/processes/ProcessesPresenterTest.java +++ b/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/perspective/widgets/machine/appliance/processes/ProcessesPresenterTest.java @@ -17,7 +17,6 @@ import org.eclipse.che.api.promises.client.Operation; import org.eclipse.che.api.promises.client.Promise; import org.eclipse.che.ide.api.machine.ExecAgentCommandManager; -import org.eclipse.che.ide.api.machine.MachineServiceClient; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/processes/panel/ProcessesPanelPresenterTest.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/processes/panel/ProcessesPanelPresenterTest.java index 1a0fadb58c3..37ff2506597 100644 --- a/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/processes/panel/ProcessesPanelPresenterTest.java +++ b/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/processes/panel/ProcessesPanelPresenterTest.java @@ -33,8 +33,6 @@ import org.eclipse.che.ide.api.machine.DevMachine; import org.eclipse.che.ide.api.machine.ExecAgentCommandManager; import org.eclipse.che.ide.api.machine.MachineEntity; -import org.eclipse.che.ide.api.machine.MachineManager; -import org.eclipse.che.ide.api.machine.MachineServiceClient; import org.eclipse.che.ide.api.machine.events.MachineStateEvent; import org.eclipse.che.ide.api.machine.events.WsAgentStateEvent; import org.eclipse.che.ide.api.macro.MacroProcessor; @@ -126,8 +124,6 @@ public class ProcessesPanelPresenterTest { @Mock private OutputConsole outputConsole; @Mock - private MachineManager machineManager; - @Mock private EntityFactory entityFactory; @Mock private WorkspaceRuntimeDto workspaceRuntime; diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/targets/categories/docker/DockerCategoryPresenterTest.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/targets/categories/docker/DockerCategoryPresenterTest.java index be0fbefe981..5bdd4a6c8d3 100644 --- a/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/targets/categories/docker/DockerCategoryPresenterTest.java +++ b/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/targets/categories/docker/DockerCategoryPresenterTest.java @@ -19,11 +19,8 @@ import org.eclipse.che.ide.api.dialogs.ConfirmCallback; import org.eclipse.che.ide.api.dialogs.ConfirmDialog; import org.eclipse.che.ide.api.dialogs.DialogFactory; -import org.eclipse.che.ide.api.machine.MachineEntity; -import org.eclipse.che.ide.api.machine.MachineServiceClient; import org.eclipse.che.ide.api.notification.NotificationManager; import org.eclipse.che.ide.extension.machine.client.MachineLocalizationConstant; -import org.eclipse.che.ide.api.machine.events.MachineStateEvent; import org.eclipse.che.ide.extension.machine.client.targets.TargetsTreeManager; import org.junit.Before; import org.junit.Test; @@ -35,9 +32,6 @@ import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; -import static org.eclipse.che.api.core.model.machine.MachineStatus.RUNNING; -import static org.eclipse.che.ide.api.notification.StatusNotification.DisplayMode.FLOAT_MODE; -import static org.eclipse.che.ide.api.notification.StatusNotification.Status.SUCCESS; import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.verify; @@ -55,8 +49,6 @@ public class DockerCategoryPresenterTest { @Mock private MachineLocalizationConstant machineLocale; @Mock - private MachineServiceClient machineService; - @Mock private EventBus eventBus; @@ -88,51 +80,12 @@ public void setUp() { when(promise.then(operationSuccessCapture.capture())).thenReturn(promise); - arbitraryCategoryPresenter = new DockerCategoryPresenter(dockerView, - dialogFactory, - notificationManager, - machineLocale, - machineService, - eventBus); + arbitraryCategoryPresenter = new DockerCategoryPresenter(dockerView, machineLocale); arbitraryCategoryPresenter.setTargetsTreeManager(targetsTreeManager); } - @Test - public void testOnDeleteTarget() throws Exception { - final String deletingTargetName = "deletingTargetName"; - final String deleteProposal = "Are you sure you want to delete target " + deletingTargetName + " ?"; - final String deletingMachineId = "deletingMachineId"; - final String deleteSuccessMessage = "Successfully disconnected from machine " + deletingTargetName; - final DockerMachineTarget target = Mockito.mock(DockerMachineTarget.class); - final MachineEntity machine = Mockito.mock(MachineEntity.class); - when(machineLocale.targetsViewDisconnectSuccess(deletingTargetName)).thenReturn(deleteSuccessMessage); - when(target.getName()).thenReturn(deletingTargetName); - when(targetsTreeManager.getMachineByName(deletingTargetName)).thenReturn(machine); - when(machine.getId()).thenReturn(deletingMachineId); - when(machine.getWorkspaceId()).thenReturn("WS_ID"); - when(machine.getStatus()).thenReturn(RUNNING); - when(machineLocale.targetsViewDeleteConfirm(deletingTargetName)).thenReturn(deleteProposal); - when(machineService.destroyMachine("WS_ID", deletingMachineId)).thenReturn(promise); - - - arbitraryCategoryPresenter.onDeleteClicked(target); - - verify(dialogFactory).createConfirmDialog(anyString(), eq(deleteProposal), confirmCaptor.capture(), - Matchers.anyObject()); - - confirmCaptor.getValue().accepted(); - - verify(targetsTreeManager).getMachineByName(deletingTargetName); - verify(machineService).destroyMachine("WS_ID", deletingMachineId); - - operationSuccessCapture.getValue().apply(null); - - verify(eventBus).fireEvent(Matchers.anyObject()); - verify(notificationManager).notify(eq(deleteSuccessMessage), eq(SUCCESS), eq(FLOAT_MODE)); - verify(targetsTreeManager).updateTargets(null); - } @Test public void testGetCategory() throws Exception { diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/targets/categories/ssh/SshCategoryPresenterTest.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/targets/categories/ssh/SshCategoryPresenterTest.java index 6838e133809..ab60a431820 100644 --- a/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/targets/categories/ssh/SshCategoryPresenterTest.java +++ b/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/targets/categories/ssh/SshCategoryPresenterTest.java @@ -13,7 +13,6 @@ import com.google.gwt.user.client.ui.AcceptsOneWidget; import com.google.web.bindery.event.shared.EventBus; -import org.eclipse.che.api.machine.shared.dto.recipe.RecipeDescriptor; import org.eclipse.che.api.promises.client.Operation; import org.eclipse.che.api.promises.client.Promise; import org.eclipse.che.ide.api.app.AppContext; @@ -21,7 +20,6 @@ import org.eclipse.che.ide.api.dialogs.ConfirmCallback; import org.eclipse.che.ide.api.dialogs.ConfirmDialog; import org.eclipse.che.ide.api.dialogs.DialogFactory; -import org.eclipse.che.ide.api.machine.MachineServiceClient; import org.eclipse.che.ide.api.machine.RecipeServiceClient; import org.eclipse.che.ide.api.notification.NotificationManager; import org.eclipse.che.ide.api.workspace.WorkspaceServiceClient; @@ -39,8 +37,6 @@ import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; -import static org.eclipse.che.ide.api.notification.StatusNotification.DisplayMode.FLOAT_MODE; -import static org.eclipse.che.ide.api.notification.StatusNotification.Status.SUCCESS; import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.never; @@ -68,8 +64,6 @@ public class SshCategoryPresenterTest { @Mock private AppContext appContext; @Mock - private MachineServiceClient machineService; - @Mock private EventBus eventBus; @Mock private EntityFactory entityFactory; @@ -114,46 +108,11 @@ public void setUp() { machineLocale, workspaceServiceClient, appContext, - machineService, eventBus); arbitraryCategoryPresenter.setTargetsTreeManager(targetsTreeManager); arbitraryCategoryPresenter.setCurrentSelection(target); } - - @Test - public void testOnDeleteTarget() throws Exception { - final String deletingTargetName = "deletingTargetName"; - final String recipeId = "deletingTargetRecipeId"; - final String deleteProposal = "Are you sure you want to delete target " + deletingTargetName + " ?"; - final String deleteSuccessMessage = "Target recipe " + deletingTargetName + " successfully deleted"; - final SshMachineTarget target = Mockito.mock(SshMachineTarget.class); - final RecipeDescriptor recipe = Mockito.mock(RecipeDescriptor.class); - when(target.getName()).thenReturn(deletingTargetName); - when(target.getRecipe()).thenReturn(recipe); - when(recipe.getId()).thenReturn(recipeId); - when(recipeServiceClient.removeRecipe(recipeId)).thenReturn(promise); - when(machineLocale.targetsViewDeleteConfirm(deletingTargetName)).thenReturn(deleteProposal); - when(machineLocale.targetsRecipeDeleteSuccess(deletingTargetName)).thenReturn(deleteSuccessMessage); - - arbitraryCategoryPresenter.onDeleteClicked(target); - - verify(dialogFactory).createConfirmDialog(anyString(), eq(deleteProposal), confirmCaptor.capture(), - Matchers.anyObject()); - - confirmCaptor.getValue().accepted(); - - verify(recipeServiceClient).removeRecipe(recipeId); - verify(promise).then(operationSuccessCapture.capture()); - - operationSuccessCapture.getValue().apply(null); - - verify(notificationManager).notify(eq(deleteSuccessMessage), eq(SUCCESS), eq(FLOAT_MODE)); - verify(target).isConnected(); - verify(targetsTreeManager).updateTargets(null); - } - - @Test public void testOnCancelClicked() throws Exception { diff --git a/plugins/plugin-nodejs-debugger/che-plugin-nodejs-debugger-ide/src/test/java/org/eclipse/che/plugin/nodejsdbg/ide/configuration/NodeJsDebuggerConfigurationPagePresenterTest.java b/plugins/plugin-nodejs-debugger/che-plugin-nodejs-debugger-ide/src/test/java/org/eclipse/che/plugin/nodejsdbg/ide/configuration/NodeJsDebuggerConfigurationPagePresenterTest.java index 357d9d8c93b..61058609a4a 100644 --- a/plugins/plugin-nodejs-debugger/che-plugin-nodejs-debugger-ide/src/test/java/org/eclipse/che/plugin/nodejsdbg/ide/configuration/NodeJsDebuggerConfigurationPagePresenterTest.java +++ b/plugins/plugin-nodejs-debugger/che-plugin-nodejs-debugger-ide/src/test/java/org/eclipse/che/plugin/nodejsdbg/ide/configuration/NodeJsDebuggerConfigurationPagePresenterTest.java @@ -15,7 +15,6 @@ import org.eclipse.che.ide.api.app.AppContext; import org.eclipse.che.ide.api.debug.DebugConfiguration; import org.eclipse.che.ide.api.debug.DebugConfigurationPage; -import org.eclipse.che.ide.api.machine.MachineServiceClient; import org.eclipse.che.ide.api.machine.RecipeServiceClient; import org.eclipse.che.ide.extension.machine.client.command.macros.CurrentProjectPathMacro; import org.junit.Before; @@ -55,8 +54,6 @@ public class NodeJsDebuggerConfigurationPagePresenterTest { private AppContext appContext; @Mock private RecipeServiceClient recipeServiceClient; - @Mock - private MachineServiceClient machineServiceClient; @InjectMocks private NodeJsDebuggerConfigurationPagePresenter pagePresenter; diff --git a/samples/sample-plugin-nativeaccess/che-sample-plugin-nativeaccess-ide/src/main/java/org/eclipse/che/plugin/nativeaccessexample/machine/client/command/CommandManager.java b/samples/sample-plugin-nativeaccess/che-sample-plugin-nativeaccess-ide/src/main/java/org/eclipse/che/plugin/nativeaccessexample/machine/client/command/CommandManager.java index f680bc01484..eab9e02e00c 100644 --- a/samples/sample-plugin-nativeaccess/che-sample-plugin-nativeaccess-ide/src/main/java/org/eclipse/che/plugin/nativeaccessexample/machine/client/command/CommandManager.java +++ b/samples/sample-plugin-nativeaccess/che-sample-plugin-nativeaccess-ide/src/main/java/org/eclipse/che/plugin/nativeaccessexample/machine/client/command/CommandManager.java @@ -21,15 +21,13 @@ import org.eclipse.che.ide.api.app.AppContext; import org.eclipse.che.ide.api.machine.ExecAgentCommandManager; import org.eclipse.che.ide.api.machine.execagent.ExecAgentPromise; -import org.eclipse.che.ide.api.notification.NotificationManager; import org.eclipse.che.ide.dto.DtoFactory; -import org.eclipse.che.ide.util.UUID; import javax.validation.constraints.NotNull; /** * Simple command manager which allows to run native commands within the workspace Docker container. - * Please note that the actual call is delegated to the MachineServiceClient service. + * * * @author Mathias Schaefer */ @@ -38,15 +36,14 @@ public class CommandManager { private final DtoFactory dtoFactory; private final ExecAgentCommandManager commandManager; - private final NotificationManager notificationManager; private final AppContext appContext; @Inject - public CommandManager(DtoFactory dtoFactory, ExecAgentCommandManager commandManager, NotificationManager notificationManager, + public CommandManager(DtoFactory dtoFactory, + ExecAgentCommandManager commandManager, AppContext appContext) { this.dtoFactory = dtoFactory; this.commandManager = commandManager; - this.notificationManager = notificationManager; this.appContext = appContext; } From 5a682afd871083c81229419b0920bebdbe037617 Mon Sep 17 00:00:00 2001 From: Max Shaposhnik Date: Thu, 2 Mar 2017 16:05:15 +0200 Subject: [PATCH 2/9] Remove Machine API --- .../machine/local/LocalDockerModule.java | 5 +- .../environment/server/MachineService.java | 354 ------------------ .../server/MachineServiceLinksInjector.java | 193 ---------- .../server/WorkspaceServiceLinksInjector.java | 17 - .../MachineServiceLinksInjectorTest.java | 130 ------- .../server/MachineServiceTest.java | 145 ------- .../server/WorkspaceServiceTest.java | 3 +- 7 files changed, 2 insertions(+), 845 deletions(-) delete mode 100644 wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/environment/server/MachineService.java delete mode 100644 wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/environment/server/MachineServiceLinksInjector.java delete mode 100644 wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/environment/server/MachineServiceLinksInjectorTest.java delete mode 100644 wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/environment/server/MachineServiceTest.java diff --git a/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/local/LocalDockerModule.java b/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/local/LocalDockerModule.java index 12a6a3a06bb..f3391e6b480 100644 --- a/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/local/LocalDockerModule.java +++ b/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/local/LocalDockerModule.java @@ -17,7 +17,6 @@ import com.google.inject.multibindings.Multibinder; import com.google.inject.name.Names; -import org.eclipse.che.api.environment.server.MachineService; import org.eclipse.che.api.machine.server.spi.Instance; import org.eclipse.che.api.machine.server.spi.InstanceProcess; import org.eclipse.che.plugin.docker.client.DockerConnector; @@ -42,9 +41,7 @@ public class LocalDockerModule extends AbstractModule { @Override protected void configure() { - bind(MachineService.class); - - install(new FactoryModuleBuilder() + install(new FactoryModuleBuilder() .implement(Instance.class, DockerInstance.class) .implement(InstanceProcess.class, DockerProcess.class) .implement(DockerNode.class, LocalDockerNode.class) diff --git a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/environment/server/MachineService.java b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/environment/server/MachineService.java deleted file mode 100644 index b096c555c51..00000000000 --- a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/environment/server/MachineService.java +++ /dev/null @@ -1,354 +0,0 @@ -/******************************************************************************* - * 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.api.environment.server; - -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import io.swagger.annotations.ApiParam; -import io.swagger.annotations.ApiResponse; -import io.swagger.annotations.ApiResponses; - -import com.google.common.io.CharStreams; - -import org.eclipse.che.api.core.BadRequestException; -import org.eclipse.che.api.core.ConflictException; -import org.eclipse.che.api.core.ForbiddenException; -import org.eclipse.che.api.core.NotFoundException; -import org.eclipse.che.api.core.ServerException; -import org.eclipse.che.api.core.model.machine.Machine; -import org.eclipse.che.api.core.rest.Service; -import org.eclipse.che.api.machine.server.DtoConverter; -import org.eclipse.che.api.machine.server.exception.MachineException; -import org.eclipse.che.api.machine.shared.dto.CommandDto; -import org.eclipse.che.api.machine.shared.dto.MachineConfigDto; -import org.eclipse.che.api.machine.shared.dto.MachineDto; -import org.eclipse.che.api.machine.shared.dto.MachineProcessDto; -import org.eclipse.che.api.workspace.server.WorkspaceManager; -import org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl; - -import javax.inject.Inject; -import javax.servlet.http.HttpServletResponse; -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.MediaType; -import java.io.IOException; -import java.io.Reader; -import java.util.Collections; -import java.util.List; -import java.util.stream.Collectors; - -/** - * Machine API - * - * @author Alexander Garagatyi - * @author Anton Korneta - */ -@Api(value = "/machine", description = "Machine REST API") -@Path("/workspace/{workspaceId}/machine") -@Deprecated -public class MachineService extends Service { - private final MachineProcessManager machineProcessManager; - private final MachineServiceLinksInjector linksInjector; - private final WorkspaceManager workspaceManager; - private final CheEnvironmentValidator environmentValidator; - - @Inject - public MachineService(MachineProcessManager machineProcessManager, - MachineServiceLinksInjector linksInjector, - WorkspaceManager workspaceManager, - CheEnvironmentValidator environmentValidator) { - this.machineProcessManager = machineProcessManager; - this.linksInjector = linksInjector; - this.workspaceManager = workspaceManager; - this.environmentValidator = environmentValidator; - } - - @GET - @Path("/{machineId}") - @Produces(MediaType.APPLICATION_JSON) - @ApiOperation(value = "Get machine by ID") - @ApiResponses({@ApiResponse(code = 200, message = "The response contains requested machine entity"), - @ApiResponse(code = 404, message = "Machine with specified id does not exist"), - @ApiResponse(code = 500, message = "Internal server error occurred")}) - @Deprecated - public MachineDto getMachineById(@ApiParam(value = "Workspace ID") - @PathParam("workspaceId") - String workspaceId, - @ApiParam(value = "Machine ID") - @PathParam("machineId") - String machineId) - throws ServerException, - ForbiddenException, - NotFoundException { - - final Machine machine = workspaceManager.getMachineInstance(workspaceId, machineId); - return linksInjector.injectLinks(DtoConverter.asDto(machine), getServiceContext()); - } - - @GET - @Produces(MediaType.APPLICATION_JSON) - @ApiOperation(value = "Get all machines of workspace with specified ID", - response = MachineDto.class, - responseContainer = "List") - @ApiResponses({@ApiResponse(code = 200, message = "The response contains requested list of machine entities"), - @ApiResponse(code = 400, message = "Workspace ID is not specified"), - @ApiResponse(code = 500, message = "Internal server error occurred")}) - @Deprecated - public List getMachines(@ApiParam(value = "Workspace ID") - @PathParam("workspaceId") - String workspaceId) - throws ServerException, - BadRequestException, - NotFoundException { - - requiredNotNull(workspaceId, "Parameter workspace"); - - WorkspaceImpl workspace = workspaceManager.getWorkspace(workspaceId); - if (workspace.getRuntime() == null) { - return Collections.emptyList(); - } else { - return workspace.getRuntime() - .getMachines() - .stream() - .map(DtoConverter::asDto) - .map(machineDto -> linksInjector.injectLinks(machineDto, getServiceContext())) - .collect(Collectors.toList()); - } - } - - @POST - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - @ApiOperation(value = "Create a new machine based on the configuration", - notes = "This operation can be performed only by authorized user") - @ApiResponses({@ApiResponse(code = 204, message = "The machine successfully created"), - @ApiResponse(code = 400, message = "Missed required parameters, parameters are not valid"), - @ApiResponse(code = 403, message = "The user does not have access to create the new machine"), - @ApiResponse(code = 409, message = "Conflict error occurred during the machine creation" + - "(e.g. The machine with such name already exists)." + - "Workspace is not in RUNNING state"), - @ApiResponse(code = 500, message = "Internal server error occurred")}) - @Deprecated - public void startMachine(@ApiParam("The workspace id") - @PathParam("workspaceId") - String workspaceId, - @ApiParam(value = "The new machine configuration", required = true) - MachineConfigDto machineConfig) throws ForbiddenException, - NotFoundException, - ServerException, - ConflictException, - BadRequestException { - requiredNotNull(machineConfig, "Machine configuration"); - requiredNotNull(machineConfig.getType(), "Machine type"); - requiredNotNull(machineConfig.getSource(), "Machine source"); - requiredNotNull(machineConfig.getSource().getType(), "Machine source type"); - // definition of source should come either with a content or with location - requiredOnlyOneNotNull(machineConfig.getSource().getLocation(), machineConfig.getSource().getContent(), - "Machine source should provide either location or content"); - - try { - environmentValidator.validateMachine(machineConfig); - } catch (IllegalArgumentException e) { - throw new BadRequestException(e.getLocalizedMessage()); - } - - workspaceManager.startMachine(machineConfig, workspaceId); - } - - @DELETE - @Path("/{machineId}") - @Produces(MediaType.APPLICATION_JSON) - @ApiOperation(value = "Stop machine") - @ApiResponses({@ApiResponse(code = 204, message = "Machine was successfully stopped"), - @ApiResponse(code = 404, message = "Machine with specified id does not exist"), - @ApiResponse(code = 500, message = "Internal server error occurred")}) - @Deprecated - public void stopMachine(@ApiParam(value = "Workspace ID") - @PathParam("workspaceId") String workspaceId, - @ApiParam(value = "Machine ID") - @PathParam("machineId") String machineId) throws NotFoundException, - ServerException, - ForbiddenException, - ConflictException { - workspaceManager.stopMachine(workspaceId, machineId); - } - - @POST - @Path("/{machineId}/command") - @Consumes(MediaType.APPLICATION_JSON) - @ApiOperation(value = "Start specified command in machine") - @ApiResponses({@ApiResponse(code = 200, message = "The response contains entity of created machine process"), - @ApiResponse(code = 400, message = "Command entity is invalid"), - @ApiResponse(code = 404, message = "Machine with specified ID does not exist"), - @ApiResponse(code = 500, message = "Internal server error occurred")}) - @Deprecated - public MachineProcessDto executeCommandInMachine(@ApiParam(value = "Workspace ID") - @PathParam("workspaceId") - String workspaceId, - @ApiParam(value = "Machine ID") - @PathParam("machineId") - String machineId, - @ApiParam(value = "Command to execute", required = true) - final CommandDto command, - @ApiParam(value = "Channel for command output") - @QueryParam("outputChannel") - String outputChannel) - throws NotFoundException, - ServerException, - ForbiddenException, - BadRequestException { - - requiredNotNull(command, "Command description"); - requiredNotNull(command.getCommandLine(), "Commandline"); - return linksInjector.injectLinks(DtoConverter.asDto(machineProcessManager.exec(workspaceId, - machineId, - command, - outputChannel)), - workspaceId, - machineId, - getServiceContext()); - } - - @GET - @Path("/{machineId}/process") - @Produces(MediaType.APPLICATION_JSON) - @ApiOperation(value = "Get processes of machine", - response = MachineProcessDto.class, - responseContainer = "List") - @ApiResponses({@ApiResponse(code = 200, message = "The response contains machine process entities"), - @ApiResponse(code = 404, message = "Machine with specified ID does not exist"), - @ApiResponse(code = 500, message = "Internal server error occurred")}) - @Deprecated - public List getProcesses(@ApiParam(value = "Workspace ID") - @PathParam("workspaceId") - String workspaceId, - @ApiParam(value = "Machine ID") - @PathParam("machineId") - String machineId) - throws NotFoundException, - ServerException, - ForbiddenException { - - return machineProcessManager.getProcesses(workspaceId, machineId) - .stream() - .map(DtoConverter::asDto) - .map(machineProcess -> linksInjector.injectLinks(machineProcess, - workspaceId, - machineId, - getServiceContext())) - .collect(Collectors.toList()); - } - - @DELETE - @Path("/{machineId}/process/{processId}") - @ApiOperation(value = "Stop process in machine") - @ApiResponses({@ApiResponse(code = 204, message = "Process was successfully stopped"), - @ApiResponse(code = 404, message = "Machine with specified ID does not exist"), - @ApiResponse(code = 500, message = "Internal server error occurred")}) - @Deprecated - public void stopProcess(@ApiParam(value = "Workspace ID") - @PathParam("workspaceId") - String workspaceId, - @ApiParam(value = "Machine ID") - @PathParam("machineId") - String machineId, - @ApiParam(value = "Process ID") - @PathParam("processId") - int processId) - throws NotFoundException, - ForbiddenException, - ServerException { - - machineProcessManager.stopProcess(workspaceId, machineId, processId); - } - - @GET - @Path("/{machineId}/process/{pid}/logs") - @Produces(MediaType.TEXT_PLAIN) - @ApiOperation(value = "Get logs of machine process") - @ApiResponses({@ApiResponse(code = 200, message = "The response contains logs"), - @ApiResponse(code = 404, message = "Machine or process with specified ID does not exist"), - @ApiResponse(code = 500, message = "Internal server error occurred")}) - @Deprecated - public void getProcessLogs(@ApiParam(value = "Workspace ID") - @PathParam("workspaceId") - String workspaceId, - @ApiParam(value = "Machine ID") - @PathParam("machineId") - String machineId, - @ApiParam(value = "Process ID") - @PathParam("pid") - int pid, - @Context - HttpServletResponse httpServletResponse) - throws NotFoundException, - ForbiddenException, - ServerException, - IOException { - - addProcessLogsToResponse(machineId, pid, httpServletResponse); - } - - /** - * Checks only one of the given object reference is {@code null} - * - * @param object1 - * object reference to check - * @param object2 - * object reference to check - * @param subject - * used as subject of exception message "{subject} required" - * @throws BadRequestException - * when objects are both null or have both a value reference is {@code null} - */ - private void requiredOnlyOneNotNull(Object object1, Object object2, String subject) throws BadRequestException { - if (object1 == null && object2 == null) { - throw new BadRequestException(subject + " required"); - } - if (object1 != null && object2 != null) { - throw new BadRequestException(subject + " required"); - } - } - - private void addProcessLogsToResponse(String machineId, int pid, HttpServletResponse httpServletResponse) throws IOException, - NotFoundException, - MachineException { - try (Reader logsReader = machineProcessManager.getProcessLogReader(machineId, pid)) { - // Response is written directly to the servlet request stream - httpServletResponse.setContentType("text/plain"); - CharStreams.copy(logsReader, httpServletResponse.getWriter()); - httpServletResponse.getWriter().flush(); - } - } - - /** - * Checks object reference is not {@code null} - * - * @param object - * object reference to check - * @param subject - * used as subject of exception message "{subject} required" - * @throws BadRequestException - * when object reference is {@code null} - */ - private void requiredNotNull(Object object, String subject) throws BadRequestException { - if (object == null) { - throw new BadRequestException(subject + " required"); - } - } -} diff --git a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/environment/server/MachineServiceLinksInjector.java b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/environment/server/MachineServiceLinksInjector.java deleted file mode 100644 index 758e0e00aa0..00000000000 --- a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/environment/server/MachineServiceLinksInjector.java +++ /dev/null @@ -1,193 +0,0 @@ -/******************************************************************************* - * 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.api.environment.server; - -import com.google.common.collect.Lists; - -import org.eclipse.che.api.core.rest.ServiceContext; -import org.eclipse.che.api.core.rest.shared.dto.Link; -import org.eclipse.che.api.core.rest.shared.dto.LinkParameter; -import org.eclipse.che.api.machine.shared.Constants; -import org.eclipse.che.api.machine.shared.dto.MachineDto; -import org.eclipse.che.api.machine.shared.dto.MachineProcessDto; -import org.eclipse.che.api.machine.shared.dto.ServerDto; - -import javax.inject.Singleton; -import javax.ws.rs.HttpMethod; -import javax.ws.rs.core.UriBuilder; -import java.net.URI; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -import static java.lang.String.format; -import static java.util.Collections.singletonList; -import static javax.ws.rs.core.MediaType.APPLICATION_JSON; -import static javax.ws.rs.core.MediaType.TEXT_PLAIN; -import static org.eclipse.che.api.core.util.LinksHelper.createLink; -import static org.eclipse.che.api.machine.shared.Constants.ENVIRONMENT_OUTPUT_CHANNEL_TEMPLATE; -import static org.eclipse.che.api.machine.shared.Constants.ENVIRONMENT_STATUS_CHANNEL_TEMPLATE; -import static org.eclipse.che.api.machine.shared.Constants.EXEC_AGENT_REFERENCE; -import static org.eclipse.che.api.machine.shared.Constants.LINK_REL_ENVIRONMENT_OUTPUT_CHANNEL; -import static org.eclipse.che.api.machine.shared.Constants.TERMINAL_REFERENCE; -import static org.eclipse.che.dto.server.DtoFactory.cloneDto; -import static org.eclipse.che.dto.server.DtoFactory.newDto; - -/** - * Helps to inject {@link MachineService} related links. - * - * @author Anton Korneta - */ -@Singleton -public class MachineServiceLinksInjector { - - public MachineDto injectLinks(MachineDto machine, ServiceContext serviceContext) { - final UriBuilder uriBuilder = serviceContext.getServiceUriBuilder(); - final List links = new ArrayList<>(); - - links.add(createLink(HttpMethod.GET, - uriBuilder.clone() - .path(MachineService.class, "getMachineById") - .build(machine.getWorkspaceId(), machine.getId()) - .toString(), - APPLICATION_JSON, - "self link")); - links.add(createLink(HttpMethod.GET, - uriBuilder.clone() - .path(MachineService.class, "getMachines") - .build(machine.getWorkspaceId()) - .toString(), - null, - APPLICATION_JSON, - Constants.LINK_REL_GET_MACHINES)); - links.add(createLink(HttpMethod.DELETE, - uriBuilder.clone() - .path(MachineService.class, "stopMachine") - .build(machine.getWorkspaceId(), machine.getId()) - .toString(), - Constants.LINK_REL_DESTROY_MACHINE)); - links.add(createLink(HttpMethod.POST, - uriBuilder.clone() - .path(MachineService.class, "executeCommandInMachine") - .build(machine.getWorkspaceId(), machine.getId()) - .toString(), - APPLICATION_JSON, - APPLICATION_JSON, - Constants.LINK_REL_EXECUTE_COMMAND, - newDto(LinkParameter.class).withName("outputChannel") - .withRequired(false))); - URI getProcessesUri = uriBuilder.clone() - .path(MachineService.class, "getProcesses") - .build(machine.getWorkspaceId(), machine.getId()); - links.add(createLink(HttpMethod.GET, - getProcessesUri.toString(), - APPLICATION_JSON, - Constants.LINK_REL_GET_PROCESSES)); - - injectTerminalLink(machine, serviceContext, links); - injectExecAgentLink(machine, serviceContext, links); - - // add workspace channel links - final Link workspaceChannelLink = createLink("GET", - serviceContext.getBaseUriBuilder() - .path("ws") - .scheme("https".equals(getProcessesUri.getScheme()) ? "wss" : "ws") - .build() - .toString(), - null); - final LinkParameter channelParameter = newDto(LinkParameter.class).withName("channel") - .withRequired(true); - - links.add(cloneDto(workspaceChannelLink).withRel(LINK_REL_ENVIRONMENT_OUTPUT_CHANNEL) - .withParameters(singletonList(cloneDto(channelParameter) - .withDefaultValue(format(ENVIRONMENT_OUTPUT_CHANNEL_TEMPLATE, - machine.getWorkspaceId()))))); - - links.add(cloneDto(workspaceChannelLink).withRel(ENVIRONMENT_STATUS_CHANNEL_TEMPLATE) - .withParameters(singletonList(cloneDto(channelParameter) - .withDefaultValue(format(ENVIRONMENT_STATUS_CHANNEL_TEMPLATE, - machine.getWorkspaceId()))))); - - return machine.withLinks(links); - } - - protected void injectTerminalLink(MachineDto machine, ServiceContext serviceContext, List links) { - final String scheme = serviceContext.getBaseUriBuilder().build().getScheme(); - if (machine.getRuntime() != null) { - final Collection servers = machine.getRuntime().getServers().values(); - servers.stream() - .filter(server -> TERMINAL_REFERENCE.equals(server.getRef())) - .findAny() - .ifPresent(terminal -> links.add(createLink("GET", - UriBuilder.fromUri(terminal.getUrl()) - .scheme("https".equals(scheme) ? "wss" - : "ws") - .path("/pty") - .build() - .toString(), - TERMINAL_REFERENCE))); - } - } - - protected void injectExecAgentLink(MachineDto machine, ServiceContext serviceContext, List links) { - final String scheme = serviceContext.getBaseUriBuilder().build().getScheme(); - if (machine.getRuntime() != null) { - final Collection servers = machine.getRuntime().getServers().values(); - servers.stream() - .filter(server -> TERMINAL_REFERENCE.equals(server.getRef())) - .findAny() - .ifPresent(terminal -> - links.add(createLink("GET", - UriBuilder.fromUri(terminal.getUrl()) - .scheme("https".equals(scheme) ? "wss" : "ws") - .path("/connect") - .build() - .toString(), - EXEC_AGENT_REFERENCE))); - } - } - - public MachineProcessDto injectLinks(MachineProcessDto process, - String workspaceId, - String machineId, - ServiceContext serviceContext) { - final UriBuilder uriBuilder = serviceContext.getServiceUriBuilder(); - final List links = Lists.newArrayListWithExpectedSize(3); - - links.add(createLink(HttpMethod.DELETE, - uriBuilder.clone() - .path(MachineService.class, "stopProcess") - .build(workspaceId, - machineId, - process.getPid()) - .toString(), - Constants.LINK_REL_STOP_PROCESS)); - links.add(createLink(HttpMethod.GET, - uriBuilder.clone() - .path(MachineService.class, "getProcessLogs") - .build(workspaceId, - machineId, - process.getPid()) - .toString(), - TEXT_PLAIN, - Constants.LINK_REL_GET_PROCESS_LOGS)); - links.add(createLink(HttpMethod.GET, - uriBuilder.clone() - .path(MachineService.class, "getProcesses") - .build(workspaceId, - machineId) - .toString(), - APPLICATION_JSON, - Constants.LINK_REL_GET_PROCESSES)); - - return process.withLinks(links); - } -} diff --git a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceServiceLinksInjector.java b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceServiceLinksInjector.java index 1bf7870a637..7c3646eb920 100644 --- a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceServiceLinksInjector.java +++ b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceServiceLinksInjector.java @@ -13,14 +13,12 @@ import org.eclipse.che.api.core.rest.ServiceContext; import org.eclipse.che.api.core.rest.shared.dto.Link; import org.eclipse.che.api.core.rest.shared.dto.LinkParameter; -import org.eclipse.che.api.environment.server.MachineServiceLinksInjector; import org.eclipse.che.api.machine.shared.dto.MachineDto; import org.eclipse.che.api.machine.shared.dto.ServerDto; import org.eclipse.che.api.machine.shared.dto.SnapshotDto; import org.eclipse.che.api.workspace.shared.dto.WorkspaceDto; import org.eclipse.che.api.workspace.shared.dto.WorkspaceRuntimeDto; -import javax.inject.Inject; import javax.inject.Singleton; import javax.ws.rs.core.UriBuilder; import java.net.URI; @@ -63,15 +61,6 @@ @Singleton public class WorkspaceServiceLinksInjector { - //TODO: we need keep IDE context in some property to have possibility configure it because context is different in Che and Hosted packaging - //TODO: not good solution do it here but critical for this task https://jira.codenvycorp.com/browse/IDEX-3619 - private final MachineServiceLinksInjector machineLinksInjector; - - @Inject - public WorkspaceServiceLinksInjector(MachineServiceLinksInjector machineLinksInjector) { - this.machineLinksInjector = machineLinksInjector; - } - public WorkspaceDto injectLinks(WorkspaceDto workspace, ServiceContext serviceContext) { final UriBuilder uriBuilder = serviceContext.getServiceUriBuilder(); final List links = new ArrayList<>(); @@ -187,8 +176,6 @@ protected void injectRuntimeLinks(WorkspaceDto workspace, URI ideUri, UriBuilder .toString(), LINK_REL_STOP_WORKSPACE)); - runtime.getMachines().forEach(machine -> injectMachineLinks(machine, serviceContext)); - final MachineDto devMachine = runtime.getDevMachine(); if (devMachine != null) { final Collection servers = devMachine.getRuntime() @@ -245,8 +232,4 @@ protected void injectRuntimeLinks(WorkspaceDto workspace, URI ideUri, UriBuilder } } } - - public MachineDto injectMachineLinks(MachineDto machine, ServiceContext serviceContext) { - return machineLinksInjector.injectLinks(machine, serviceContext); - } } diff --git a/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/environment/server/MachineServiceLinksInjectorTest.java b/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/environment/server/MachineServiceLinksInjectorTest.java deleted file mode 100644 index 7d0c117a0e6..00000000000 --- a/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/environment/server/MachineServiceLinksInjectorTest.java +++ /dev/null @@ -1,130 +0,0 @@ -/******************************************************************************* - * 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.api.environment.server; - -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Sets; - -import org.eclipse.che.api.core.rest.ServiceContext; -import org.eclipse.che.api.machine.shared.dto.MachineConfigDto; -import org.eclipse.che.api.machine.shared.dto.MachineDto; -import org.eclipse.che.api.machine.shared.dto.MachineProcessDto; -import org.eclipse.che.api.machine.shared.dto.MachineRuntimeInfoDto; -import org.eclipse.che.api.machine.shared.dto.ServerDto; -import org.eclipse.che.commons.lang.Pair; -import org.eclipse.che.dto.server.DtoFactory; -import org.everrest.core.impl.uri.UriBuilderImpl; -import org.mockito.Mock; -import org.mockito.testng.MockitoTestNGListener; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Listeners; -import org.testng.annotations.Test; - -import javax.ws.rs.core.UriBuilder; -import java.util.HashSet; -import java.util.Set; -import java.util.stream.Collectors; - -import static java.util.Arrays.asList; -import static org.eclipse.che.api.machine.shared.Constants.ENVIRONMENT_STATUS_CHANNEL_TEMPLATE; -import static org.eclipse.che.api.machine.shared.Constants.EXEC_AGENT_REFERENCE; -import static org.eclipse.che.api.machine.shared.Constants.LINK_REL_DESTROY_MACHINE; -import static org.eclipse.che.api.machine.shared.Constants.LINK_REL_ENVIRONMENT_OUTPUT_CHANNEL; -import static org.eclipse.che.api.machine.shared.Constants.LINK_REL_EXECUTE_COMMAND; -import static org.eclipse.che.api.machine.shared.Constants.LINK_REL_GET_MACHINES; -import static org.eclipse.che.api.machine.shared.Constants.LINK_REL_GET_PROCESSES; -import static org.eclipse.che.api.machine.shared.Constants.LINK_REL_GET_PROCESS_LOGS; -import static org.eclipse.che.api.machine.shared.Constants.LINK_REL_SELF; -import static org.eclipse.che.api.machine.shared.Constants.LINK_REL_STOP_PROCESS; -import static org.eclipse.che.api.machine.shared.Constants.TERMINAL_REFERENCE; -import static org.mockito.Mockito.when; -import static org.testng.Assert.assertEquals; - -/** - * Tests for {@link MachineServiceLinksInjector}. - * - * @author Anton Korneta - */ -@Listeners(MockitoTestNGListener.class) -public class MachineServiceLinksInjectorTest { - private static final String URI_BASE = "http://localhost:8080"; - - @Mock - private ServiceContext serviceContextMock; - @Mock - private MachineRuntimeInfoDto machineRuntimeInfoDtoMock; - @Mock - private MachineConfigDto machineConfigDtoMock; - @Mock - private ServerDto terminalServerMock; - - @Mock - private ServerDto execAgentServerMock; - - - private MachineServiceLinksInjector machineLinksInjector; - - @BeforeMethod - public void setUp() throws Exception { - machineLinksInjector = new MachineServiceLinksInjector(); - final UriBuilder uriBuilder = new UriBuilderImpl(); - uriBuilder.uri(URI_BASE); - when(serviceContextMock.getServiceUriBuilder()).thenReturn(uriBuilder); - when(serviceContextMock.getBaseUriBuilder()).thenReturn(uriBuilder); - } - - @Test - public void shouldInjectLinksIntoMachineDto() { - when(terminalServerMock.getRef()).thenReturn(TERMINAL_REFERENCE); - when(terminalServerMock.getUrl()).thenReturn(URI_BASE + "/pty"); - when(execAgentServerMock.getRef()).thenReturn(EXEC_AGENT_REFERENCE); - when(execAgentServerMock.getUrl()).thenReturn(URI_BASE + "/connect"); - when(machineRuntimeInfoDtoMock.getServers()).thenReturn(ImmutableMap.of(TERMINAL_REFERENCE, terminalServerMock, - EXEC_AGENT_REFERENCE, execAgentServerMock)); - final MachineDto machineDto = DtoFactory.newDto(MachineDto.class) - .withId("id") - .withWorkspaceId("wsId") - .withConfig(machineConfigDtoMock) - .withRuntime(machineRuntimeInfoDtoMock); - machineLinksInjector.injectLinks(machineDto, serviceContextMock); - final Set> links = machineDto.getLinks() - .stream() - .map(link -> Pair.of(link.getMethod(), link.getRel())) - .collect(Collectors.toSet()); - final Set> expectedLinks = new HashSet<>(asList(Pair.of("GET", TERMINAL_REFERENCE), - Pair.of("GET", EXEC_AGENT_REFERENCE), - Pair.of("GET", LINK_REL_SELF), - Pair.of("GET", LINK_REL_GET_MACHINES), - Pair.of("POST", LINK_REL_EXECUTE_COMMAND), - Pair.of("DELETE", LINK_REL_DESTROY_MACHINE), - Pair.of("GET", LINK_REL_GET_PROCESSES), - Pair.of("GET", LINK_REL_ENVIRONMENT_OUTPUT_CHANNEL), - Pair.of("GET", ENVIRONMENT_STATUS_CHANNEL_TEMPLATE))); - - assertEquals(links, expectedLinks, "Difference " + Sets.symmetricDifference(links, expectedLinks) + "\n"); - } - - @Test - public void shouldInjectLinksIntoMachineProcessDto() { - final MachineProcessDto machineProcessDto = DtoFactory.newDto(MachineProcessDto.class); - machineLinksInjector.injectLinks(machineProcessDto, "workspaceId", "machineId", serviceContextMock); - final Set> links = machineProcessDto.getLinks() - .stream() - .map(link -> Pair.of(link.getMethod(), link.getRel())) - .collect(Collectors.toSet()); - final Set> expectedLinks = ImmutableSet.of(Pair.of("DELETE", LINK_REL_STOP_PROCESS), - Pair.of("GET", LINK_REL_GET_PROCESS_LOGS), - Pair.of("GET", LINK_REL_GET_PROCESSES)); - - assertEquals(links, expectedLinks, "Difference " + Sets.symmetricDifference(links, expectedLinks) + "\n"); - } -} diff --git a/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/environment/server/MachineServiceTest.java b/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/environment/server/MachineServiceTest.java deleted file mode 100644 index 1a150b4861a..00000000000 --- a/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/environment/server/MachineServiceTest.java +++ /dev/null @@ -1,145 +0,0 @@ -/******************************************************************************* - * 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.api.environment.server; - -import com.jayway.restassured.http.ContentType; -import com.jayway.restassured.response.Response; - -import org.eclipse.che.api.core.model.machine.MachineConfig; -import org.eclipse.che.api.core.rest.ApiExceptionMapper; -import org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl; -import org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl; -import org.eclipse.che.api.workspace.server.WorkspaceManager; -import org.eclipse.che.api.workspace.server.WorkspaceServiceTest; -import org.everrest.assured.EverrestJetty; -import org.mockito.Mock; -import org.mockito.testng.MockitoTestNGListener; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Listeners; -import org.testng.annotations.Test; - -import static com.jayway.restassured.RestAssured.given; -import static org.everrest.assured.JettyHttpServer.ADMIN_USER_NAME; -import static org.everrest.assured.JettyHttpServer.ADMIN_USER_PASSWORD; -import static org.everrest.assured.JettyHttpServer.SECURE_PATH; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyString; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.testng.Assert.assertEquals; - -/** - * @author Alexander Garagatyi - */ -@Listeners(value = {EverrestJetty.class, MockitoTestNGListener.class}) -public class MachineServiceTest { - @SuppressWarnings("unused") - static final ApiExceptionMapper MAPPER = new ApiExceptionMapper(); - @SuppressWarnings("unused") - static final WorkspaceServiceTest.EnvironmentFilter FILTER = new WorkspaceServiceTest.EnvironmentFilter(); - @Mock - WorkspaceManager wsManager; - @Mock - MachineProcessManager machineProcessManager; - @Mock - CheEnvironmentValidator environmentValidator; - - MachineService service; - - @BeforeMethod - public void setup() { - service = new MachineService(machineProcessManager, - new MachineServiceLinksInjector(), - wsManager, - environmentValidator); - } - - @Test(dataProvider = "illegalMachineConfigProvider") - public void shouldReturnErrorOnStartMachineIfBodyIsInvalid(MachineConfig machineConfig) throws Exception { - // given - String workspaceId = "wsId"; - - // when - final Response response = given().auth() - .basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD) - .when() - .body(machineConfig) - .contentType(ContentType.JSON) - .post(SECURE_PATH + "/workspace/" + workspaceId + "/machine"); - - // then - assertEquals(response.getStatusCode(), 400); - verify(wsManager, never()).startMachine(any(MachineConfig.class), anyString()); - } - - @DataProvider(name = "illegalMachineConfigProvider") - public static Object[][] illegalMachineConfigProvider() { - MachineConfigImpl.MachineConfigImplBuilder builder = - MachineConfigImpl.builder() - .setDev(false) - .setName("name") - .setType("type") - .setSource(new MachineSourceImpl("type1").setContent("content")); - return new Object[][] { - {builder.setType(null) - .build()}, - {builder.setSource(null) - .build()}, - {builder.setSource(new MachineSourceImpl((String)null).setContent("content")) - .build()}, - {builder.setSource(new MachineSourceImpl("type").setContent("content") - .setLocation("location")) - .build()}, - }; - } - - @Test - public void shouldStartMachine() throws Exception { - // given - String workspaceId = "wsId"; - MachineConfig machineConfig = MachineConfigImpl.builder() - .setDev(false) - .setName("name") - .setType("type") - .setSource(new MachineSourceImpl("type1").setContent("content")) - .build(); - - // when - final Response response = given().auth() - .basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD) - .when() - .body(machineConfig) - .contentType(ContentType.JSON) - .post(SECURE_PATH + "/workspace/" + workspaceId + "/machine"); - - // then - assertEquals(response.getStatusCode(), 204); - verify(wsManager).startMachine(any(MachineConfig.class), eq(workspaceId)); - } - - @Test - public void shouldStopMachine() throws Exception { - // given - String workspaceId = "wsId"; - String machineId = "mcId"; - - // when - final Response response = given().auth() - .basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD) - .when() - .delete(SECURE_PATH + "/workspace/" + workspaceId + "/machine/" + machineId); - - // then - assertEquals(response.getStatusCode(), 204); - } -} diff --git a/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/workspace/server/WorkspaceServiceTest.java b/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/workspace/server/WorkspaceServiceTest.java index 4f16a53916c..fb26706fc85 100644 --- a/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/workspace/server/WorkspaceServiceTest.java +++ b/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/workspace/server/WorkspaceServiceTest.java @@ -28,7 +28,6 @@ import org.eclipse.che.api.core.rest.shared.dto.Link; import org.eclipse.che.api.core.rest.shared.dto.ServiceError; import org.eclipse.che.api.environment.server.MachineProcessManager; -import org.eclipse.che.api.environment.server.MachineServiceLinksInjector; import org.eclipse.che.api.machine.server.model.impl.CommandImpl; import org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl; import org.eclipse.che.api.machine.server.model.impl.MachineImpl; @@ -151,7 +150,7 @@ public void setup() { wsManager, validator, wsAgentHealthChecker, - new WorkspaceServiceLinksInjector(new MachineServiceLinksInjector()), + new WorkspaceServiceLinksInjector(), true, false); } From 274c899606b67cd0e13f3f92ec21fb17f126282f Mon Sep 17 00:00:00 2001 From: Max Shaposhnik Date: Mon, 6 Mar 2017 16:18:05 +0200 Subject: [PATCH 3/9] Prevent NULL setting --- .../main/java/org/eclipse/che/ide/context/AppContextImpl.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/context/AppContextImpl.java b/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/context/AppContextImpl.java index 6c5b6c9d63f..2a2efce794f 100644 --- a/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/context/AppContextImpl.java +++ b/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/context/AppContextImpl.java @@ -130,7 +130,9 @@ public Workspace getWorkspace() { public void setWorkspace(Workspace workspace) { if (workspace != null) { usersWorkspace = workspace; - runtime = new ActiveRuntime(workspace.getRuntime()); + if (workspace.getRuntime() != null) { + runtime = new ActiveRuntime(workspace.getRuntime()); + } } else { usersWorkspace = null; runtime = null; From 301a0e218f00cf5d17636c18f9f5ead964eadd40 Mon Sep 17 00:00:00 2001 From: Max Shaposhnik Date: Mon, 6 Mar 2017 17:26:57 +0200 Subject: [PATCH 4/9] Revert machne links injector --- .../server/MachineLinksInjector.java | 111 ++++++++++++++++++ .../server/WorkspaceServiceLinksInjector.java | 17 +++ .../MachineServiceLinksInjectorTest.java | 101 ++++++++++++++++ .../server/WorkspaceServiceTest.java | 3 +- 4 files changed, 231 insertions(+), 1 deletion(-) create mode 100644 wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/environment/server/MachineLinksInjector.java create mode 100644 wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/environment/server/MachineServiceLinksInjectorTest.java diff --git a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/environment/server/MachineLinksInjector.java b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/environment/server/MachineLinksInjector.java new file mode 100644 index 00000000000..a54b5a21d4e --- /dev/null +++ b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/environment/server/MachineLinksInjector.java @@ -0,0 +1,111 @@ +/******************************************************************************* + * 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.api.environment.server; + +import org.eclipse.che.api.core.rest.ServiceContext; +import org.eclipse.che.api.core.rest.shared.dto.Link; +import org.eclipse.che.api.core.rest.shared.dto.LinkParameter; +import org.eclipse.che.api.machine.shared.dto.MachineDto; +import org.eclipse.che.api.machine.shared.dto.ServerDto; + +import javax.inject.Singleton; +import javax.ws.rs.core.UriBuilder; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import static java.lang.String.format; +import static java.util.Collections.singletonList; +import static org.eclipse.che.api.core.util.LinksHelper.createLink; +import static org.eclipse.che.api.machine.shared.Constants.ENVIRONMENT_OUTPUT_CHANNEL_TEMPLATE; +import static org.eclipse.che.api.machine.shared.Constants.ENVIRONMENT_STATUS_CHANNEL_TEMPLATE; +import static org.eclipse.che.api.machine.shared.Constants.EXEC_AGENT_REFERENCE; +import static org.eclipse.che.api.machine.shared.Constants.LINK_REL_ENVIRONMENT_OUTPUT_CHANNEL; +import static org.eclipse.che.api.machine.shared.Constants.TERMINAL_REFERENCE; +import static org.eclipse.che.dto.server.DtoFactory.cloneDto; +import static org.eclipse.che.dto.server.DtoFactory.newDto; + +/** + * Helps to inject Machine related links. + * + * @author Anton Korneta + */ +@Singleton +public class MachineLinksInjector { + + public MachineDto injectLinks(MachineDto machine, ServiceContext serviceContext) { + final List links = new ArrayList<>(); + + injectTerminalLink(machine, serviceContext, links); + injectExecAgentLink(machine, serviceContext, links); + + // add workspace channel links + final Link workspaceChannelLink = createLink("GET", + serviceContext.getBaseUriBuilder() + .path("ws") + .scheme("https".equals( + serviceContext.getBaseUriBuilder().build().getScheme()) ? "wss" + : "ws") + .build() + .toString(), + null); + final LinkParameter channelParameter = newDto(LinkParameter.class).withName("channel") + .withRequired(true); + + links.add(cloneDto(workspaceChannelLink).withRel(LINK_REL_ENVIRONMENT_OUTPUT_CHANNEL) + .withParameters(singletonList(cloneDto(channelParameter) + .withDefaultValue(format(ENVIRONMENT_OUTPUT_CHANNEL_TEMPLATE, + machine.getWorkspaceId()))))); + + links.add(cloneDto(workspaceChannelLink).withRel(ENVIRONMENT_STATUS_CHANNEL_TEMPLATE) + .withParameters(singletonList(cloneDto(channelParameter) + .withDefaultValue(format(ENVIRONMENT_STATUS_CHANNEL_TEMPLATE, + machine.getWorkspaceId()))))); + + return machine.withLinks(links); + } + + protected void injectTerminalLink(MachineDto machine, ServiceContext serviceContext, List links) { + final String scheme = serviceContext.getBaseUriBuilder().build().getScheme(); + if (machine.getRuntime() != null) { + final Collection servers = machine.getRuntime().getServers().values(); + servers.stream() + .filter(server -> TERMINAL_REFERENCE.equals(server.getRef())) + .findAny() + .ifPresent(terminal -> links.add(createLink("GET", + UriBuilder.fromUri(terminal.getUrl()) + .scheme("https".equals(scheme) ? "wss" + : "ws") + .path("/pty") + .build() + .toString(), + TERMINAL_REFERENCE))); + } + } + + protected void injectExecAgentLink(MachineDto machine, ServiceContext serviceContext, List links) { + final String scheme = serviceContext.getBaseUriBuilder().build().getScheme(); + if (machine.getRuntime() != null) { + final Collection servers = machine.getRuntime().getServers().values(); + servers.stream() + .filter(server -> TERMINAL_REFERENCE.equals(server.getRef())) + .findAny() + .ifPresent(terminal -> + links.add(createLink("GET", + UriBuilder.fromUri(terminal.getUrl()) + .scheme("https".equals(scheme) ? "wss" : "ws") + .path("/connect") + .build() + .toString(), + EXEC_AGENT_REFERENCE))); + } + } +} diff --git a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceServiceLinksInjector.java b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceServiceLinksInjector.java index 7c3646eb920..e2b99c4aab0 100644 --- a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceServiceLinksInjector.java +++ b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceServiceLinksInjector.java @@ -13,12 +13,14 @@ import org.eclipse.che.api.core.rest.ServiceContext; import org.eclipse.che.api.core.rest.shared.dto.Link; import org.eclipse.che.api.core.rest.shared.dto.LinkParameter; +import org.eclipse.che.api.environment.server.MachineLinksInjector; import org.eclipse.che.api.machine.shared.dto.MachineDto; import org.eclipse.che.api.machine.shared.dto.ServerDto; import org.eclipse.che.api.machine.shared.dto.SnapshotDto; import org.eclipse.che.api.workspace.shared.dto.WorkspaceDto; import org.eclipse.che.api.workspace.shared.dto.WorkspaceRuntimeDto; +import javax.inject.Inject; import javax.inject.Singleton; import javax.ws.rs.core.UriBuilder; import java.net.URI; @@ -61,6 +63,14 @@ @Singleton public class WorkspaceServiceLinksInjector { + private final MachineLinksInjector machineLinksInjector; + + @Inject + public WorkspaceServiceLinksInjector(MachineLinksInjector machineLinksInjector) { + this.machineLinksInjector = machineLinksInjector; + } + + public WorkspaceDto injectLinks(WorkspaceDto workspace, ServiceContext serviceContext) { final UriBuilder uriBuilder = serviceContext.getServiceUriBuilder(); final List links = new ArrayList<>(); @@ -176,6 +186,8 @@ protected void injectRuntimeLinks(WorkspaceDto workspace, URI ideUri, UriBuilder .toString(), LINK_REL_STOP_WORKSPACE)); + runtime.getMachines().forEach(machine -> injectMachineLinks(machine, serviceContext)); + final MachineDto devMachine = runtime.getDevMachine(); if (devMachine != null) { final Collection servers = devMachine.getRuntime() @@ -232,4 +244,9 @@ protected void injectRuntimeLinks(WorkspaceDto workspace, URI ideUri, UriBuilder } } } + + protected MachineDto injectMachineLinks(MachineDto machine, ServiceContext serviceContext) { + return machineLinksInjector.injectLinks(machine, serviceContext); + } + } diff --git a/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/environment/server/MachineServiceLinksInjectorTest.java b/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/environment/server/MachineServiceLinksInjectorTest.java new file mode 100644 index 00000000000..26829b7e43a --- /dev/null +++ b/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/environment/server/MachineServiceLinksInjectorTest.java @@ -0,0 +1,101 @@ +/******************************************************************************* + * 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.api.environment.server; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Sets; + +import org.eclipse.che.api.core.rest.ServiceContext; +import org.eclipse.che.api.machine.shared.dto.MachineConfigDto; +import org.eclipse.che.api.machine.shared.dto.MachineDto; +import org.eclipse.che.api.machine.shared.dto.MachineRuntimeInfoDto; +import org.eclipse.che.api.machine.shared.dto.ServerDto; +import org.eclipse.che.commons.lang.Pair; +import org.eclipse.che.dto.server.DtoFactory; +import org.everrest.core.impl.uri.UriBuilderImpl; +import org.mockito.Mock; +import org.mockito.testng.MockitoTestNGListener; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Listeners; +import org.testng.annotations.Test; + +import javax.ws.rs.core.UriBuilder; +import java.util.HashSet; +import java.util.Set; +import java.util.stream.Collectors; + +import static java.util.Arrays.asList; +import static org.eclipse.che.api.machine.shared.Constants.ENVIRONMENT_STATUS_CHANNEL_TEMPLATE; +import static org.eclipse.che.api.machine.shared.Constants.EXEC_AGENT_REFERENCE; +import static org.eclipse.che.api.machine.shared.Constants.LINK_REL_ENVIRONMENT_OUTPUT_CHANNEL; +import static org.eclipse.che.api.machine.shared.Constants.TERMINAL_REFERENCE; +import static org.mockito.Mockito.when; +import static org.testng.Assert.assertEquals; + +/** + * Tests for {@link MachineLinksInjector}. + * + * @author Anton Korneta + */ +@Listeners(MockitoTestNGListener.class) +public class MachineServiceLinksInjectorTest { + private static final String URI_BASE = "http://localhost:8080"; + + @Mock + private ServiceContext serviceContextMock; + @Mock + private MachineRuntimeInfoDto machineRuntimeInfoDtoMock; + @Mock + private MachineConfigDto machineConfigDtoMock; + @Mock + private ServerDto terminalServerMock; + + @Mock + private ServerDto execAgentServerMock; + + + private MachineLinksInjector machineLinksInjector; + + @BeforeMethod + public void setUp() throws Exception { + machineLinksInjector = new MachineLinksInjector(); + final UriBuilder uriBuilder = new UriBuilderImpl(); + uriBuilder.uri(URI_BASE); + when(serviceContextMock.getServiceUriBuilder()).thenReturn(uriBuilder); + when(serviceContextMock.getBaseUriBuilder()).thenReturn(uriBuilder); + } + + @Test + public void shouldInjectLinksIntoMachineDto() { + when(terminalServerMock.getRef()).thenReturn(TERMINAL_REFERENCE); + when(terminalServerMock.getUrl()).thenReturn(URI_BASE + "/pty"); + when(execAgentServerMock.getRef()).thenReturn(EXEC_AGENT_REFERENCE); + when(execAgentServerMock.getUrl()).thenReturn(URI_BASE + "/connect"); + when(machineRuntimeInfoDtoMock.getServers()).thenReturn(ImmutableMap.of(TERMINAL_REFERENCE, terminalServerMock, + EXEC_AGENT_REFERENCE, execAgentServerMock)); + final MachineDto machineDto = DtoFactory.newDto(MachineDto.class) + .withId("id") + .withWorkspaceId("wsId") + .withConfig(machineConfigDtoMock) + .withRuntime(machineRuntimeInfoDtoMock); + machineLinksInjector.injectLinks(machineDto, serviceContextMock); + final Set> links = machineDto.getLinks() + .stream() + .map(link -> Pair.of(link.getMethod(), link.getRel())) + .collect(Collectors.toSet()); + final Set> expectedLinks = new HashSet<>(asList(Pair.of("GET", TERMINAL_REFERENCE), + Pair.of("GET", EXEC_AGENT_REFERENCE), + Pair.of("GET", LINK_REL_ENVIRONMENT_OUTPUT_CHANNEL), + Pair.of("GET", ENVIRONMENT_STATUS_CHANNEL_TEMPLATE))); + + assertEquals(links, expectedLinks, "Difference " + Sets.symmetricDifference(links, expectedLinks) + "\n"); + } +} diff --git a/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/workspace/server/WorkspaceServiceTest.java b/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/workspace/server/WorkspaceServiceTest.java index fb26706fc85..c268067132b 100644 --- a/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/workspace/server/WorkspaceServiceTest.java +++ b/wsmaster/che-core-api-workspace/src/test/java/org/eclipse/che/api/workspace/server/WorkspaceServiceTest.java @@ -27,6 +27,7 @@ import org.eclipse.che.api.core.rest.ApiExceptionMapper; import org.eclipse.che.api.core.rest.shared.dto.Link; import org.eclipse.che.api.core.rest.shared.dto.ServiceError; +import org.eclipse.che.api.environment.server.MachineLinksInjector; import org.eclipse.che.api.environment.server.MachineProcessManager; import org.eclipse.che.api.machine.server.model.impl.CommandImpl; import org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl; @@ -150,7 +151,7 @@ public void setup() { wsManager, validator, wsAgentHealthChecker, - new WorkspaceServiceLinksInjector(), + new WorkspaceServiceLinksInjector(new MachineLinksInjector()), true, false); } From df8bf881d805f70e3646d6161c9ee02dfa52b848 Mon Sep 17 00:00:00 2001 From: Max Shaposhnik Date: Mon, 6 Mar 2017 18:00:31 +0200 Subject: [PATCH 5/9] Revert "Merge branch 'che#3620' into 3616" This reverts commit 52cf7bcdbe0c4b6ddc2056c4b5ec0abce7e1f7e6, reversing changes made to 85eebbaba164095cc37e7360378c1ce22936ea22. --- .../che/ide/api/machine/MachineManager.java | 58 +++ .../ide/api/machine/MachineServiceClient.java | 49 +++ .../api/machine/MachineServiceClientImpl.java | 71 ++++ .../command/CommandProducerActionManager.java | 20 +- .../eclipse/che/ide/core/CoreGinModule.java | 3 + .../workspace/DefaultWorkspaceComponent.java | 4 + .../workspace/FactoryWorkspaceComponent.java | 4 + .../che/ide/workspace/WorkspaceComponent.java | 6 + .../ide/workspace/WorkspaceEventsHandler.java | 5 + .../workspace/WorkspaceEventsHandlerTest.java | 5 + .../machine/client/MachineExtension.java | 6 +- .../client/command/CommandManagerImpl.java | 4 + .../client/inject/MachineGinModule.java | 7 + .../client/machine/MachineManagerImpl.java | 156 +++++++ .../create/CreateMachinePresenter.java | 147 +++++++ .../machine/create/CreateMachineView.java | 105 +++++ .../machine/create/CreateMachineViewImpl.java | 387 ++++++++++++++++++ .../create/CreateMachineViewImpl.ui.xml | 81 ++++ .../processes/ProcessesPresenter.java | 1 + .../docker/DockerCategoryPresenter.java | 76 +++- .../categories/ssh/SshCategoryPresenter.java | 61 ++- .../machine/MachineManagerImplTest.java | 169 ++++++++ .../create/CreateMachinePresenterTest.java | 147 +++++++ .../processes/ProcessesPresenterTest.java | 1 + .../panel/ProcessesPanelPresenterTest.java | 3 + .../docker/DockerCategoryPresenterTest.java | 49 ++- .../ssh/SshCategoryPresenterTest.java | 41 ++ ...ebuggerConfigurationPagePresenterTest.java | 3 + .../client/command/CommandManager.java | 9 +- 29 files changed, 1658 insertions(+), 20 deletions(-) create mode 100644 ide/che-core-ide-api/src/main/java/org/eclipse/che/ide/api/machine/MachineManager.java create mode 100644 ide/che-core-ide-api/src/main/java/org/eclipse/che/ide/api/machine/MachineServiceClient.java create mode 100644 ide/che-core-ide-api/src/main/java/org/eclipse/che/ide/api/machine/MachineServiceClientImpl.java create mode 100644 plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/MachineManagerImpl.java create mode 100644 plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachinePresenter.java create mode 100644 plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachineView.java create mode 100644 plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachineViewImpl.java create mode 100644 plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachineViewImpl.ui.xml create mode 100644 plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/machine/MachineManagerImplTest.java create mode 100644 plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachinePresenterTest.java diff --git a/ide/che-core-ide-api/src/main/java/org/eclipse/che/ide/api/machine/MachineManager.java b/ide/che-core-ide-api/src/main/java/org/eclipse/che/ide/api/machine/MachineManager.java new file mode 100644 index 00000000000..6f74f6f5591 --- /dev/null +++ b/ide/che-core-ide-api/src/main/java/org/eclipse/che/ide/api/machine/MachineManager.java @@ -0,0 +1,58 @@ +/******************************************************************************* + * 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.api.machine; + +import org.eclipse.che.api.promises.client.Promise; + +/** + * Manager for machine. + * + * @author Roman Nikitenko + */ +public interface MachineManager { + + /** + * Start new machine as dev-machine (bind workspace to running machine). + * + * @param recipeURL + * special recipe url to get docker image. + * @param displayName + * display name for machine + */ + void startDevMachine(String recipeURL, String displayName); + + /** + * Start new machine in workspace. + * + * @param recipeURL + * special recipe url to get docker image. + * @param displayName + * display name for machine + */ + void startMachine(String recipeURL, String displayName); + + /** + * Destroy machine. + * + * @param machine + * contains information about machine state + */ + Promise destroyMachine(MachineEntity machine); + + + /** + * Restart machine. + * + * @param machine + * contains information about machine state + */ + void restartMachine(final MachineEntity machine); +} diff --git a/ide/che-core-ide-api/src/main/java/org/eclipse/che/ide/api/machine/MachineServiceClient.java b/ide/che-core-ide-api/src/main/java/org/eclipse/che/ide/api/machine/MachineServiceClient.java new file mode 100644 index 00000000000..e19e13bab0e --- /dev/null +++ b/ide/che-core-ide-api/src/main/java/org/eclipse/che/ide/api/machine/MachineServiceClient.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * 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.api.machine; + +import org.eclipse.che.api.core.model.machine.Command; +import org.eclipse.che.api.machine.shared.dto.MachineDto; +import org.eclipse.che.api.machine.shared.dto.MachineProcessDto; +import org.eclipse.che.api.promises.client.Promise; +import org.eclipse.che.commons.annotation.Nullable; + +import javax.validation.constraints.NotNull; +import java.util.List; + +/** + * Client for Machine API. + * + * @author Artem Zatsarynnyi + * @author Dmitry Shnurenko + */ +public interface MachineServiceClient { + + /** + * Returns list of machines which are bounded to the specified workspace. + * + * @param workspaceId + * workspace id + * @return a promise that will provide a list of {@link MachineDto}s for the given workspace ID, or rejects with an error + */ + Promise> getMachines(String workspaceId); + + /** + * Destroy machine with the specified ID. + * + * @param workspaceId + * ID of workspace + * @param machineId + * ID of machine that should be destroyed + * @return a promise that will resolve when the machine has been destroyed, or rejects with an error + */ + Promise destroyMachine(@NotNull String workspaceId, @NotNull String machineId); +} diff --git a/ide/che-core-ide-api/src/main/java/org/eclipse/che/ide/api/machine/MachineServiceClientImpl.java b/ide/che-core-ide-api/src/main/java/org/eclipse/che/ide/api/machine/MachineServiceClientImpl.java new file mode 100644 index 00000000000..8866eef8448 --- /dev/null +++ b/ide/che-core-ide-api/src/main/java/org/eclipse/che/ide/api/machine/MachineServiceClientImpl.java @@ -0,0 +1,71 @@ +/******************************************************************************* + * 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.api.machine; + +import com.google.inject.Inject; + +import org.eclipse.che.api.machine.shared.dto.MachineDto; +import org.eclipse.che.api.promises.client.Promise; +import org.eclipse.che.ide.api.app.AppContext; +import org.eclipse.che.ide.rest.AsyncRequestFactory; +import org.eclipse.che.ide.rest.DtoUnmarshallerFactory; +import org.eclipse.che.ide.ui.loaders.request.LoaderFactory; + +import javax.validation.constraints.NotNull; +import java.util.List; + +import static com.google.gwt.http.client.RequestBuilder.DELETE; +import static org.eclipse.che.ide.MimeType.APPLICATION_JSON; +import static org.eclipse.che.ide.rest.HTTPHeader.ACCEPT; + +/** + * Implementation for {@link MachineServiceClient}. + * + * @author Artem Zatsarynnyi + * @author Dmitry Shnurenko + */ +public class MachineServiceClientImpl implements MachineServiceClient { + private final DtoUnmarshallerFactory dtoUnmarshallerFactory; + private final AsyncRequestFactory asyncRequestFactory; + private final LoaderFactory loaderFactory; + private final String baseHttpUrl; + + @Inject + protected MachineServiceClientImpl(AppContext appContext, + DtoUnmarshallerFactory dtoUnmarshallerFactory, + AsyncRequestFactory asyncRequestFactory, + LoaderFactory loaderFactory) { + this.dtoUnmarshallerFactory = dtoUnmarshallerFactory; + this.asyncRequestFactory = asyncRequestFactory; + this.loaderFactory = loaderFactory; + this.baseHttpUrl = appContext.getMasterEndpoint() + "/workspace/"; + } + + @Override + public Promise> getMachines(String workspaceId) { + return asyncRequestFactory.createGetRequest(baseHttpUrl + workspaceId + "/machine") + .header(ACCEPT, APPLICATION_JSON) + .loader(loaderFactory.newLoader("Getting info about bound machines...")) + .send(dtoUnmarshallerFactory.newListUnmarshaller(MachineDto.class)); + } + + @Override + public Promise destroyMachine(@NotNull final String workspaceId, + @NotNull final String machineId) { + return asyncRequestFactory.createRequest(DELETE, + baseHttpUrl + workspaceId + + "/machine/" + machineId, + null, + false) + .loader(loaderFactory.newLoader("Destroying machine...")) + .send(); + } +} diff --git a/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/command/CommandProducerActionManager.java b/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/command/CommandProducerActionManager.java index cf962f7c7de..41594bfe0a6 100644 --- a/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/command/CommandProducerActionManager.java +++ b/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/command/CommandProducerActionManager.java @@ -16,6 +16,9 @@ import com.google.web.bindery.event.shared.EventBus; import org.eclipse.che.api.core.model.machine.Machine; +import org.eclipse.che.api.machine.shared.dto.MachineDto; +import org.eclipse.che.api.promises.client.Operation; +import org.eclipse.che.api.promises.client.OperationException; import org.eclipse.che.ide.Resources; import org.eclipse.che.ide.api.action.Action; import org.eclipse.che.ide.api.action.ActionEvent; @@ -25,7 +28,7 @@ import org.eclipse.che.ide.api.command.CommandProducer; import org.eclipse.che.ide.api.component.Component; import org.eclipse.che.ide.api.constraints.Constraints; -import org.eclipse.che.ide.api.machine.ActiveRuntime; +import org.eclipse.che.ide.api.machine.MachineServiceClient; import org.eclipse.che.ide.api.machine.events.MachineStateEvent; import org.eclipse.che.ide.api.machine.events.WsAgentStateEvent; import org.eclipse.che.ide.api.machine.events.WsAgentStateHandler; @@ -58,6 +61,7 @@ public class CommandProducerActionManager implements MachineStateEvent.Handler, private final ActionManager actionManager; private final CommandProducerActionFactory commandProducerActionFactory; private final AppContext appContext; + private final MachineServiceClient machineServiceClient; private final Resources resources; private final List machines; @@ -73,10 +77,12 @@ public CommandProducerActionManager(EventBus eventBus, ActionManager actionManager, CommandProducerActionFactory commandProducerActionFactory, AppContext appContext, + MachineServiceClient machineServiceClient, Resources resources) { this.actionManager = actionManager; this.commandProducerActionFactory = commandProducerActionFactory; this.appContext = appContext; + this.machineServiceClient = machineServiceClient; this.resources = resources; machines = new ArrayList<>(); @@ -113,12 +119,14 @@ private void start(Set commandProducers) { @Override public void start(final Callback callback) { - ActiveRuntime activeRuntime = appContext.getActiveRuntime(); - if (activeRuntime != null) { - machines.addAll(activeRuntime.getMachines()); - } + machineServiceClient.getMachines(appContext.getWorkspaceId()).then(new Operation>() { + @Override + public void apply(List arg) throws OperationException { + machines.addAll(arg); - callback.onSuccess(this); + callback.onSuccess(CommandProducerActionManager.this); + } + }); } @Override diff --git a/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/core/CoreGinModule.java b/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/core/CoreGinModule.java index 645107fe78b..086d69f5a1d 100644 --- a/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/core/CoreGinModule.java +++ b/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/core/CoreGinModule.java @@ -31,6 +31,8 @@ import org.eclipse.che.ide.api.keybinding.KeyBindingAgent; import org.eclipse.che.ide.api.machine.ExecAgentCommandManager; import org.eclipse.che.ide.api.machine.ExecAgentEventManager; +import org.eclipse.che.ide.api.machine.MachineServiceClient; +import org.eclipse.che.ide.api.machine.MachineServiceClientImpl; import org.eclipse.che.ide.api.machine.RecipeServiceClient; import org.eclipse.che.ide.api.machine.RecipeServiceClientImpl; import org.eclipse.che.ide.api.machine.execagent.ConnectedEventHandler; @@ -136,6 +138,7 @@ protected void configure() { bind(GitServiceClient.class).to(GitServiceClientImpl.class).in(Singleton.class); bind(SshServiceClient.class).to(SshServiceClientImpl.class).in(Singleton.class); bind(RecipeServiceClient.class).to(RecipeServiceClientImpl.class).in(Singleton.class); + bind(MachineServiceClient.class).to(MachineServiceClientImpl.class).in(Singleton.class); // IDE agents bind(SelectionAgent.class).to(SelectionAgentImpl.class).asEagerSingleton(); diff --git a/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/DefaultWorkspaceComponent.java b/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/DefaultWorkspaceComponent.java index 7a1270c9387..628702dfed2 100644 --- a/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/DefaultWorkspaceComponent.java +++ b/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/DefaultWorkspaceComponent.java @@ -12,6 +12,7 @@ import com.google.gwt.core.client.Callback; import com.google.inject.Inject; +import com.google.inject.Provider; import com.google.inject.Singleton; import com.google.web.bindery.event.shared.EventBus; @@ -23,6 +24,7 @@ import org.eclipse.che.ide.api.app.AppContext; import org.eclipse.che.ide.api.component.Component; import org.eclipse.che.ide.api.dialogs.DialogFactory; +import org.eclipse.che.ide.api.machine.MachineManager; import org.eclipse.che.ide.api.notification.NotificationManager; import org.eclipse.che.ide.api.preferences.PreferencesManager; import org.eclipse.che.ide.api.workspace.WorkspaceServiceClient; @@ -51,6 +53,7 @@ public DefaultWorkspaceComponent(WorkspaceServiceClient workspaceServiceClient, DtoUnmarshallerFactory dtoUnmarshallerFactory, EventBus eventBus, AppContext appContext, + Provider machineManagerProvider, NotificationManager notificationManager, MessageBusProvider messageBusProvider, BrowserAddress browserAddress, @@ -66,6 +69,7 @@ public DefaultWorkspaceComponent(WorkspaceServiceClient workspaceServiceClient, dtoUnmarshallerFactory, eventBus, appContext, + machineManagerProvider, notificationManager, messageBusProvider, browserAddress, diff --git a/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/FactoryWorkspaceComponent.java b/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/FactoryWorkspaceComponent.java index cdb9a8c15f0..1baaf47d206 100644 --- a/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/FactoryWorkspaceComponent.java +++ b/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/FactoryWorkspaceComponent.java @@ -12,6 +12,7 @@ import com.google.gwt.core.client.Callback; import com.google.inject.Inject; +import com.google.inject.Provider; import com.google.inject.Singleton; import com.google.web.bindery.event.shared.EventBus; @@ -29,6 +30,7 @@ import org.eclipse.che.ide.api.component.Component; import org.eclipse.che.ide.api.dialogs.DialogFactory; import org.eclipse.che.ide.api.factory.FactoryServiceClient; +import org.eclipse.che.ide.api.machine.MachineManager; import org.eclipse.che.ide.api.notification.NotificationManager; import org.eclipse.che.ide.api.preferences.PreferencesManager; import org.eclipse.che.ide.api.workspace.WorkspaceServiceClient; @@ -72,6 +74,7 @@ public FactoryWorkspaceComponent(WorkspaceServiceClient workspaceServiceClient, DtoUnmarshallerFactory dtoUnmarshallerFactory, EventBus eventBus, AppContext appContext, + Provider machineManagerProvider, NotificationManager notificationManager, MessageBusProvider messageBusProvider, BrowserAddress browserAddress, @@ -88,6 +91,7 @@ public FactoryWorkspaceComponent(WorkspaceServiceClient workspaceServiceClient, dtoUnmarshallerFactory, eventBus, appContext, + machineManagerProvider, notificationManager, messageBusProvider, browserAddress, diff --git a/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/WorkspaceComponent.java b/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/WorkspaceComponent.java index 0a3ddf96ba4..b78df879a88 100644 --- a/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/WorkspaceComponent.java +++ b/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/WorkspaceComponent.java @@ -12,6 +12,7 @@ import com.google.gwt.core.client.Callback; import com.google.gwt.core.client.Scheduler; +import com.google.inject.Provider; import com.google.web.bindery.event.shared.EventBus; import org.eclipse.che.api.core.model.workspace.Workspace; @@ -27,6 +28,7 @@ import org.eclipse.che.ide.api.dialogs.CancelCallback; import org.eclipse.che.ide.api.dialogs.ConfirmCallback; import org.eclipse.che.ide.api.dialogs.DialogFactory; +import org.eclipse.che.ide.api.machine.MachineManager; import org.eclipse.che.ide.api.machine.events.WsAgentStateEvent; import org.eclipse.che.ide.api.machine.events.WsAgentStateHandler; import org.eclipse.che.ide.api.notification.NotificationManager; @@ -72,6 +74,7 @@ public abstract class WorkspaceComponent implements Component, WsAgentStateHandl protected final StartWorkspacePresenter startWorkspacePresenter; private final EventBus eventBus; + private final Provider machineManagerProvider; private final MessageBusProvider messageBusProvider; private final WorkspaceEventsHandler workspaceEventsHandler; private final LoaderPresenter loader; @@ -87,6 +90,7 @@ public WorkspaceComponent(WorkspaceServiceClient workspaceServiceClient, DtoUnmarshallerFactory dtoUnmarshallerFactory, EventBus eventBus, AppContext appContext, + Provider machineManagerProvider, NotificationManager notificationManager, MessageBusProvider messageBusProvider, BrowserAddress browserAddress, @@ -102,6 +106,7 @@ public WorkspaceComponent(WorkspaceServiceClient workspaceServiceClient, this.dtoUnmarshallerFactory = dtoUnmarshallerFactory; this.eventBus = eventBus; this.appContext = appContext; + this.machineManagerProvider = machineManagerProvider; this.notificationManager = notificationManager; this.messageBusProvider = messageBusProvider; this.browserAddress = browserAddress; @@ -197,6 +202,7 @@ public void onOpen() { public void execute() { loader.setSuccess(STARTING_WORKSPACE_RUNTIME); eventBus.fireEvent(new WorkspaceStartedEvent(workspace)); + machineManagerProvider.get();//start instance of machine manager } }); break; diff --git a/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/WorkspaceEventsHandler.java b/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/WorkspaceEventsHandler.java index 46e405d2761..7cc64a09de9 100644 --- a/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/WorkspaceEventsHandler.java +++ b/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/workspace/WorkspaceEventsHandler.java @@ -39,6 +39,7 @@ import org.eclipse.che.ide.api.dialogs.ConfirmCallback; import org.eclipse.che.ide.api.dialogs.DialogFactory; import org.eclipse.che.ide.api.machine.ExecAgentCommandManager; +import org.eclipse.che.ide.api.machine.MachineManager; import org.eclipse.che.ide.api.machine.OutputMessageUnmarshaller; import org.eclipse.che.ide.api.machine.events.WsAgentStateEvent; import org.eclipse.che.ide.api.notification.NotificationManager; @@ -90,6 +91,7 @@ public class WorkspaceEventsHandler { private final NotificationManager notificationManager; private final DialogFactory dialogFactory; private final DtoUnmarshallerFactory dtoUnmarshallerFactory; + private final Provider machineManagerProvider; private final Provider wsComponentProvider; private final AsyncRequestFactory asyncRequestFactory; private final WorkspaceSnapshotCreator snapshotCreator; @@ -124,6 +126,7 @@ public class WorkspaceEventsHandler { final DtoUnmarshallerFactory dtoUnmarshallerFactory, final NotificationManager notificationManager, final MessageBusProvider messageBusProvider, + final Provider machineManagerProvider, final WorkspaceSnapshotCreator snapshotCreator, final WorkspaceServiceClient workspaceServiceClient, final StartWorkspaceNotification startWorkspaceNotification, @@ -138,6 +141,7 @@ public class WorkspaceEventsHandler { this.notificationManager = notificationManager; this.dialogFactory = dialogFactory; this.dtoUnmarshallerFactory = dtoUnmarshallerFactory; + this.machineManagerProvider = machineManagerProvider; this.workspaceServiceClient = workspaceServiceClient; this.startWorkspaceNotification = startWorkspaceNotification; this.wsComponentProvider = wsComponentProvider; @@ -187,6 +191,7 @@ public void apply(WorkspaceDto workspace) throws OperationException { } workspaceComponent.setCurrentWorkspace(workspace); + machineManagerProvider.get(); loader.show(LoaderPresenter.Phase.STARTING_WORKSPACE_RUNTIME); eventBus.fireEvent(new WorkspaceStartingEvent(workspace)); diff --git a/ide/che-core-ide-app/src/test/java/org/eclipse/che/ide/workspace/WorkspaceEventsHandlerTest.java b/ide/che-core-ide-app/src/test/java/org/eclipse/che/ide/workspace/WorkspaceEventsHandlerTest.java index 54eef633330..9f337001652 100644 --- a/ide/che-core-ide-app/src/test/java/org/eclipse/che/ide/workspace/WorkspaceEventsHandlerTest.java +++ b/ide/che-core-ide-app/src/test/java/org/eclipse/che/ide/workspace/WorkspaceEventsHandlerTest.java @@ -36,6 +36,7 @@ import org.eclipse.che.ide.api.dialogs.DialogFactory; import org.eclipse.che.ide.api.dialogs.MessageDialog; import org.eclipse.che.ide.api.machine.ExecAgentCommandManager; +import org.eclipse.che.ide.api.machine.MachineManager; import org.eclipse.che.ide.api.machine.events.WsAgentStateEvent; import org.eclipse.che.ide.api.notification.NotificationManager; import org.eclipse.che.ide.api.notification.StatusNotification; @@ -109,6 +110,8 @@ public class WorkspaceEventsHandlerTest { @Mock private DtoUnmarshallerFactory dtoUnmarshallerFactory; @Mock + private Provider machineManagerProvider; + @Mock private MessageLoader snapshotLoader; @Mock private Provider wsComponentProvider; @@ -171,6 +174,7 @@ public void setUp() { dtoUnmarshallerFactory, notificationManager, messageBusProvider, + machineManagerProvider, snapshotCreator, workspaceServiceClient, startWorkspaceNotification, @@ -249,6 +253,7 @@ public void onWorkspaceStartingTest() throws Exception { verify(workspaceServiceClient).getWorkspace(WORKSPACE_ID); verify(workspaceComponent).setCurrentWorkspace(workspace); + verify(machineManagerProvider).get(); verify(loader).show(eq(LoaderPresenter.Phase.STARTING_WORKSPACE_RUNTIME)); diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/MachineExtension.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/MachineExtension.java index dc16d99faaf..1694add5250 100644 --- a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/MachineExtension.java +++ b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/MachineExtension.java @@ -27,6 +27,7 @@ import org.eclipse.che.ide.api.action.DefaultActionGroup; import org.eclipse.che.ide.api.action.IdeActions; import org.eclipse.che.ide.api.app.AppContext; +import org.eclipse.che.ide.api.constraints.Constraints; import org.eclipse.che.ide.api.extension.Extension; import org.eclipse.che.ide.api.icon.Icon; import org.eclipse.che.ide.api.icon.IconRegistry; @@ -52,16 +53,19 @@ import org.eclipse.che.ide.extension.machine.client.processes.actions.ReRunProcessAction; import org.eclipse.che.ide.extension.machine.client.processes.actions.StopProcessAction; import org.eclipse.che.ide.extension.machine.client.targets.EditTargetsAction; +import org.eclipse.che.plugin.requirejs.ide.RequireJsLoader; import org.eclipse.che.ide.terminal.client.TerminalResources; import org.eclipse.che.ide.util.input.KeyCodeMap; -import org.eclipse.che.plugin.requirejs.ide.RequireJsLoader; import javax.inject.Named; import static org.eclipse.che.ide.api.action.IdeActions.GROUP_CENTER_TOOLBAR; import static org.eclipse.che.ide.api.action.IdeActions.GROUP_CONSOLES_TREE_CONTEXT_MENU; +import static org.eclipse.che.ide.api.action.IdeActions.GROUP_MAIN_MENU; +import static org.eclipse.che.ide.api.action.IdeActions.GROUP_RIGHT_TOOLBAR; import static org.eclipse.che.ide.api.action.IdeActions.GROUP_RUN; import static org.eclipse.che.ide.api.action.IdeActions.GROUP_WORKSPACE; +import static org.eclipse.che.ide.api.constraints.Anchor.AFTER; import static org.eclipse.che.ide.api.constraints.Constraints.FIRST; /** diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/command/CommandManagerImpl.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/command/CommandManagerImpl.java index 70da8ae8d42..85364249e20 100644 --- a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/command/CommandManagerImpl.java +++ b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/command/CommandManagerImpl.java @@ -15,6 +15,7 @@ import org.eclipse.che.api.core.model.machine.Command; import org.eclipse.che.api.core.model.machine.Machine; import org.eclipse.che.api.machine.shared.dto.CommandDto; +import org.eclipse.che.api.machine.shared.dto.MachineProcessDto; import org.eclipse.che.api.promises.client.Function; import org.eclipse.che.api.promises.client.FunctionException; import org.eclipse.che.api.promises.client.Operation; @@ -28,12 +29,15 @@ import org.eclipse.che.ide.api.command.CommandType; import org.eclipse.che.ide.api.command.CommandTypeRegistry; import org.eclipse.che.ide.api.machine.ExecAgentCommandManager; +import org.eclipse.che.ide.api.machine.MachineServiceClient; import org.eclipse.che.ide.api.macro.MacroProcessor; import org.eclipse.che.ide.api.workspace.WorkspaceServiceClient; import org.eclipse.che.ide.dto.DtoFactory; import org.eclipse.che.ide.extension.machine.client.outputspanel.console.CommandConsoleFactory; import org.eclipse.che.ide.extension.machine.client.outputspanel.console.CommandOutputConsole; import org.eclipse.che.ide.extension.machine.client.processes.panel.ProcessesPanelPresenter; +import org.eclipse.che.ide.util.UUID; +import org.eclipse.che.ide.util.loging.Log; import java.util.ArrayList; import java.util.Collections; diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/inject/MachineGinModule.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/inject/MachineGinModule.java index 5bffcc4f043..8effb47d0fc 100644 --- a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/inject/MachineGinModule.java +++ b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/inject/MachineGinModule.java @@ -21,6 +21,7 @@ import org.eclipse.che.ide.api.command.CommandType; import org.eclipse.che.ide.api.extension.ExtensionGinModule; import org.eclipse.che.ide.api.machine.MachineEntity; +import org.eclipse.che.ide.api.machine.MachineManager; import org.eclipse.che.ide.api.macro.Macro; import org.eclipse.che.ide.api.outputconsole.OutputConsole; import org.eclipse.che.ide.extension.machine.client.RecipeScriptDownloadServiceClient; @@ -36,6 +37,9 @@ import org.eclipse.che.ide.extension.machine.client.inject.factories.TerminalFactory; import org.eclipse.che.ide.extension.machine.client.inject.factories.WidgetsFactory; import org.eclipse.che.ide.extension.machine.client.machine.MachineItem; +import org.eclipse.che.ide.extension.machine.client.machine.MachineManagerImpl; +import org.eclipse.che.ide.extension.machine.client.machine.create.CreateMachineView; +import org.eclipse.che.ide.extension.machine.client.machine.create.CreateMachineViewImpl; import org.eclipse.che.ide.extension.machine.client.outputspanel.console.CommandConsoleFactory; import org.eclipse.che.ide.extension.machine.client.outputspanel.console.CommandOutputConsole; import org.eclipse.che.ide.extension.machine.client.outputspanel.console.CommandOutputConsolePresenter; @@ -78,6 +82,7 @@ public class MachineGinModule extends AbstractGinModule { @Override protected void configure() { + bind(CreateMachineView.class).to(CreateMachineViewImpl.class); bind(OutputConsoleView.class).to(OutputConsoleViewImpl.class); install(new GinFactoryModuleBuilder() .implement(CommandOutputConsole.class, Names.named("command"), CommandOutputConsolePresenter.class) @@ -106,6 +111,8 @@ protected void configure() { .build(EntityFactory.class)); install(new GinFactoryModuleBuilder().build(TerminalFactory.class)); + bind(MachineManager.class).to(MachineManagerImpl.class).in(Singleton.class); + bindConstant().annotatedWith(Names.named("machine.extension.api_port")).to(Constants.WS_AGENT_PORT); bind(SshView.class).to(SshViewImpl.class); diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/MachineManagerImpl.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/MachineManagerImpl.java new file mode 100644 index 00000000000..12e8fc4aaaa --- /dev/null +++ b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/MachineManagerImpl.java @@ -0,0 +1,156 @@ +/******************************************************************************* + * 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.extension.machine.client.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.model.machine.MachineConfig; +import org.eclipse.che.api.core.model.machine.MachineSource; +import org.eclipse.che.api.machine.shared.dto.MachineConfigDto; +import org.eclipse.che.api.machine.shared.dto.MachineLimitsDto; +import org.eclipse.che.api.machine.shared.dto.MachineSourceDto; +import org.eclipse.che.api.promises.client.Operation; +import org.eclipse.che.api.promises.client.OperationException; +import org.eclipse.che.api.promises.client.Promise; +import org.eclipse.che.ide.api.app.AppContext; +import org.eclipse.che.ide.api.machine.MachineEntity; +import org.eclipse.che.ide.api.machine.MachineManager; +import org.eclipse.che.ide.api.machine.MachineServiceClient; +import org.eclipse.che.ide.api.machine.events.MachineStateEvent; +import org.eclipse.che.ide.api.workspace.WorkspaceServiceClient; +import org.eclipse.che.ide.dto.DtoFactory; + +import static org.eclipse.che.ide.api.machine.events.MachineStateEvent.MachineAction.DESTROYED; + +/** + * Manager for machine operations. + * + * @author Artem Zatsarynnyi + * @author Roman Nikitenko + */ +@Singleton +public class MachineManagerImpl implements MachineManager { + + private final MachineServiceClient machineServiceClient; + private final WorkspaceServiceClient workspaceServiceClient; + private final AppContext appContext; + private final DtoFactory dtoFactory; + private final EventBus eventBus; + + @Inject + public MachineManagerImpl(final MachineServiceClient machineServiceClient, + final WorkspaceServiceClient workspaceServiceClient, + final EventBus eventBus, + final AppContext appContext, + final DtoFactory dtoFactory) { + this.machineServiceClient = machineServiceClient; + this.workspaceServiceClient = workspaceServiceClient; + this.appContext = appContext; + this.dtoFactory = dtoFactory; + this.eventBus = eventBus; + } + + @Override + public void restartMachine(final MachineEntity machineState) { + destroyMachine(machineState).then(new Operation() { + @Override + public void apply(Void arg) throws OperationException { + final MachineConfig machineConfig = machineState.getConfig(); + final MachineSource machineSource = machineConfig.getSource(); + final String displayName = machineConfig.getName(); + final boolean isDev = machineConfig.isDev(); + + startMachine(asDto(machineSource), displayName, isDev, "docker"); + } + }); + } + + /** + * Converts {@link MachineSource} to {@link MachineSourceDto}. + */ + public MachineSourceDto asDto(MachineSource source) { + return this.dtoFactory.createDto(MachineSourceDto.class) + .withType(source.getType()) + .withLocation(source.getLocation()) + .withContent(source.getContent()); + } + + @Override + public void startMachine(String recipeURL, String displayName) { + startMachine(recipeURL, displayName, false, "dockerfile", "docker"); + } + + @Override + public void startDevMachine(String recipeURL, String displayName) { + startMachine(recipeURL, displayName, true, "dockerfile", "docker"); + } + + /** + * Start new machine in workspace. + * + * @param recipeURL + * special recipe url to get docker image. + * @param displayName + * display name for machine + * @param isDev + * @param sourceType + * "dockerfile" or "ssh-config" + * @param machineType + * "docker" or "ssh" + */ + private void startMachine(final String recipeURL, + final String displayName, + final boolean isDev, + final String sourceType, + final String machineType) { + MachineSourceDto sourceDto = dtoFactory.createDto(MachineSourceDto.class).withType(sourceType).withLocation(recipeURL); + startMachine(sourceDto, displayName, isDev, machineType); + } + + /** + * @param machineSourceDto + * @param displayName + * @param isDev + * @param machineType + * "docker" or "ssh" + */ + private void startMachine(final MachineSourceDto machineSourceDto, + final String displayName, + final boolean isDev, + final String machineType) { + + MachineLimitsDto limitsDto = dtoFactory.createDto(MachineLimitsDto.class).withRam(1024); + if (isDev) { + limitsDto.withRam(3072); + } + + MachineConfigDto configDto = dtoFactory.createDto(MachineConfigDto.class) + .withDev(isDev) + .withName(displayName) + .withSource(machineSourceDto) + .withLimits(limitsDto) + .withType(machineType); + workspaceServiceClient.createMachine(appContext.getWorkspaceId(), configDto); + } + + @Override + public Promise destroyMachine(final MachineEntity machineState) { + return machineServiceClient.destroyMachine(machineState.getWorkspaceId(), + machineState.getId()).then(new Operation() { + @Override + public void apply(Void arg) throws OperationException { + eventBus.fireEvent(new MachineStateEvent(machineState, DESTROYED)); + } + }); + } +} diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachinePresenter.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachinePresenter.java new file mode 100644 index 00000000000..12ba65b38c3 --- /dev/null +++ b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachinePresenter.java @@ -0,0 +1,147 @@ +/******************************************************************************* + * 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.extension.machine.client.machine.create; + +import com.google.gwt.regexp.shared.RegExp; +import com.google.inject.Inject; +import com.google.inject.Singleton; + +import org.eclipse.che.api.machine.shared.dto.recipe.RecipeDescriptor; +import org.eclipse.che.api.promises.client.Operation; +import org.eclipse.che.api.promises.client.OperationException; +import org.eclipse.che.ide.api.app.AppContext; +import org.eclipse.che.ide.api.machine.DevMachine; +import org.eclipse.che.ide.api.machine.MachineManager; +import org.eclipse.che.ide.api.machine.RecipeServiceClient; + +import java.util.Collections; +import java.util.List; + +/** + * Presenter for creating machine. + * + * @author Artem Zatsarynnyi + */ +@Singleton +public class CreateMachinePresenter implements CreateMachineView.ActionDelegate { + + private static final String URL_PATTERN = + "(https?|ftp)://(www\\.)?(((([a-zA-Z0-9.-]+\\.){1,}[a-zA-Z]{2,4}|localhost))|((\\d{1,3}\\.){3}(\\d{1,3})))(:(\\d+))?(/" + + "([a-zA-Z0-9-._~!$&'()*+,;=:@/]|%[0-9A-F]{2})*)?(\\?([a-zA-Z0-9-._~!$&'()*+,;=:/?@]|%[0-9A-F]{2})*)?(#([a-zA-Z0-9" + + "._-]|%[0-9A-F]{2})*)?"; + private static final RegExp URL = RegExp.compile(URL_PATTERN); + + private static final String RECIPE_TYPE = "docker"; + private static final int SKIP_COUNT = 0; + private static final int MAX_COUNT = 100; + + private final CreateMachineView view; + private final AppContext appContext; + private final MachineManager machineManager; + private final RecipeServiceClient recipeServiceClient; + + @Inject + public CreateMachinePresenter(CreateMachineView view, + AppContext appContext, + MachineManager machineManager, + RecipeServiceClient recipeServiceClient) { + this.view = view; + this.appContext = appContext; + this.machineManager = machineManager; + this.recipeServiceClient = recipeServiceClient; + + view.setDelegate(this); + } + + public void showDialog() { + view.show(); + + view.setCreateButtonState(false); + view.setReplaceButtonState(false); + view.setMachineName(""); + view.setErrorHint(false); + view.setNoRecipeHint(false); + view.setTags(""); + view.setRecipeURL(""); + } + + @Override + public void onNameChanged() { + checkButtons(); + } + + @Override + public void onRecipeUrlChanged() { + checkButtons(); + } + + @Override + public void onTagsChanged() { + if (view.getTags().isEmpty()) { + view.setRecipes(Collections.emptyList()); + view.setNoRecipeHint(false); + return; + } + + recipeServiceClient.searchRecipes(view.getTags(), RECIPE_TYPE, SKIP_COUNT, MAX_COUNT).then(new Operation>() { + @Override + public void apply(List arg) throws OperationException { + view.setRecipes(arg); + view.setNoRecipeHint(arg.isEmpty()); + } + }); + } + + @Override + public void onRecipeSelected(RecipeDescriptor recipe) { + view.setRecipeURL(recipe.getLink("get recipe script").getHref()); + } + + private void checkButtons() { + final String recipeURL = view.getRecipeURL(); + final boolean urlValid = URL.test(recipeURL); + + view.setErrorHint(!urlValid); + + final boolean allowCreation = urlValid && !view.getMachineName().isEmpty(); + + view.setCreateButtonState(allowCreation); + view.setReplaceButtonState(allowCreation); + } + + @Override + public void onCreateClicked() { + final String machineName = view.getMachineName(); + final String recipeURL = view.getRecipeURL(); + machineManager.startMachine(recipeURL, machineName); + + view.close(); + } + + @Override + public void onReplaceDevMachineClicked() { + final String machineName = view.getMachineName(); + final String recipeURL = view.getRecipeURL(); + final DevMachine devMachine = appContext.getDevMachine(); + + if (devMachine != null) { + machineManager.destroyMachine(devMachine); + } + + machineManager.startDevMachine(recipeURL, machineName); + view.close(); + } + + @Override + public void onCancelClicked() { + view.close(); + } +} diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachineView.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachineView.java new file mode 100644 index 00000000000..6c40c047afe --- /dev/null +++ b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachineView.java @@ -0,0 +1,105 @@ +/******************************************************************************* + * 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.extension.machine.client.machine.create; + +import org.eclipse.che.api.machine.shared.dto.recipe.RecipeDescriptor; +import org.eclipse.che.ide.api.mvp.View; + +import java.util.List; + +/** + * The view of {@link CreateMachinePresenter}. + * + * @author Artem Zatsarynnyi + */ +public interface CreateMachineView extends View { + + /** Show view. */ + void show(); + + /** Close view. */ + void close(); + + /** Returns machine name. */ + String getMachineName(); + + /** Sets machine name. */ + void setMachineName(String name); + + /** Returns recipe URL. */ + String getRecipeURL(); + + /** Sets recipe URL. */ + void setRecipeURL(String url); + + /** Sets error hint visibility. */ + void setErrorHint(boolean show); + + /** Returns tags. */ + List getTags(); + + /** Sets tags. */ + void setTags(String tags); + + /** Sets 'no recipe' hint visibility. */ + void setNoRecipeHint(boolean show); + + /** Sets recipes corresponded to tags. */ + void setRecipes(List recipes); + + /** + * Sets whether 'Create' button is enabled. + * + * @param enabled + * true to enable the button, + * false to disable it + */ + void setCreateButtonState(boolean enabled); + + /** + * Sets whether 'Replace' button is enabled. + * + * @param enabled + * true to enable the button, + * false to disable it + */ + void setReplaceButtonState(boolean enabled); + + /** Action handler for the view actions/controls. */ + interface ActionDelegate { + + /** Called when machines name has been changed. */ + void onNameChanged(); + + /** Called when recipe URL has been changed. */ + void onRecipeUrlChanged(); + + /** Called when tags has been changed. */ + void onTagsChanged(); + + /** + * Called when recipe has been selected. + * + * @param recipe + * selected recipe + */ + void onRecipeSelected(RecipeDescriptor recipe); + + /** Called when 'Create' button has been clicked. */ + void onCreateClicked(); + + /** Called when 'Replace Dev Machine' button has been clicked. */ + void onReplaceDevMachineClicked(); + + /** Called when 'Cancel' button has been clicked. */ + void onCancelClicked(); + } +} diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachineViewImpl.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachineViewImpl.java new file mode 100644 index 00000000000..3634aed0bd6 --- /dev/null +++ b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachineViewImpl.java @@ -0,0 +1,387 @@ +/******************************************************************************* + * 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.extension.machine.client.machine.create; + +import elemental.dom.Element; +import elemental.dom.Node; +import elemental.html.TableCellElement; +import elemental.html.TableElement; + +import com.google.gwt.core.client.GWT; +import com.google.gwt.core.client.Scheduler; +import com.google.gwt.dom.client.Style; +import com.google.gwt.event.dom.client.ClickEvent; +import com.google.gwt.event.dom.client.ClickHandler; +import com.google.gwt.event.dom.client.KeyDownEvent; +import com.google.gwt.event.dom.client.KeyUpEvent; +import com.google.gwt.event.dom.client.KeyUpHandler; +import com.google.gwt.uibinder.client.UiBinder; +import com.google.gwt.uibinder.client.UiField; +import com.google.gwt.uibinder.client.UiHandler; +import com.google.gwt.user.client.Command; +import com.google.gwt.user.client.Timer; +import com.google.gwt.user.client.ui.Button; +import com.google.gwt.user.client.ui.HTML; +import com.google.gwt.user.client.ui.Label; +import com.google.gwt.user.client.ui.PopupPanel; +import com.google.gwt.user.client.ui.PopupPanel.PositionCallback; +import com.google.gwt.user.client.ui.TextBox; +import com.google.gwt.user.client.ui.Widget; +import com.google.inject.Inject; +import com.google.inject.Singleton; + +import org.eclipse.che.api.machine.shared.dto.recipe.RecipeDescriptor; +import org.eclipse.che.ide.api.autocomplete.AutoCompleteResources; +import org.eclipse.che.ide.extension.machine.client.MachineLocalizationConstant; +import org.eclipse.che.ide.extension.machine.client.MachineResources; +import org.eclipse.che.ide.ui.list.SimpleList; +import org.eclipse.che.ide.ui.toolbar.ToolbarResources; +import org.eclipse.che.ide.ui.window.Window; +import org.eclipse.che.ide.util.dom.Elements; +import org.vectomatic.dom.svg.ui.SVGImage; + +import java.util.ArrayList; +import java.util.List; + +import static com.google.gwt.dom.client.Style.Overflow.AUTO; +import static com.google.gwt.event.dom.client.KeyCodes.KEY_DOWN; +import static com.google.gwt.event.dom.client.KeyCodes.KEY_ENTER; +import static com.google.gwt.event.dom.client.KeyCodes.KEY_ESCAPE; +import static com.google.gwt.event.dom.client.KeyCodes.KEY_LEFT; +import static com.google.gwt.event.dom.client.KeyCodes.KEY_RIGHT; +import static com.google.gwt.event.dom.client.KeyCodes.KEY_UP; + +/** + * The implementation of {@link CreateMachineView}. + * + * @author Artem Zatsarynnyi + */ +@Singleton +public class CreateMachineViewImpl extends Window implements CreateMachineView { + + private static final CreateMachineViewImplUiBinder UI_BINDER = GWT.create(CreateMachineViewImplUiBinder.class); + + private final MachineResources machineResources; + private final org.eclipse.che.ide.Resources coreResources; + private final AutoCompleteResources.Css css; + private final SimpleList.ListItemRenderer listItemRenderer = + new SimpleList.ListItemRenderer() { + @Override + public void render(Element itemElement, RecipeDescriptor itemData) { + final TableCellElement icon = Elements.createTDElement(css.proposalIcon()); + final TableCellElement label = Elements.createTDElement(css.proposalLabel()); + final TableCellElement group = Elements.createTDElement(css.proposalGroup()); + + final SVGImage image = new SVGImage(machineResources.recipe()); + image.getElement().setAttribute("class", toolbarResources.toolbar().iconButtonIcon()); + image.getElement().getStyle().setMargin(0, Style.Unit.PX); + icon.appendChild((Node)image.getElement()); + + label.setInnerHTML(itemData.getName()); + group.setInnerHTML(itemData.getType()); + + itemElement.appendChild(icon); + itemElement.appendChild(label); + itemElement.appendChild(group); + } + + @Override + public Element createElement() { + return Elements.createTRElement(); + } + }; + + private final PopupPanel popupPanel; + + @UiField(provided = true) + MachineLocalizationConstant localizationConstant; + @UiField + TextBox machineName; + @UiField + TextBox recipeURL; + @UiField + Label errorHint; + @UiField + TextBox tags; + @UiField + Label noRecipeHint; + + private ToolbarResources toolbarResources; + + private SimpleList list; + private ActionDelegate delegate; + private final SimpleList.ListEventDelegate eventDelegate = new SimpleList.ListEventDelegate() { + @Override + public void onListItemClicked(Element listItemBase, RecipeDescriptor itemData) { + list.getSelectionModel().setSelectedItem(itemData); + } + + @Override + public void onListItemDoubleClicked(Element listItemBase, RecipeDescriptor itemData) { + delegate.onRecipeSelected(itemData); + popupPanel.hide(); + tags.setFocus(true); + } + }; + + private Button createButton; + private Button replaceButton; + private Button cancelButton; + + @Inject + public CreateMachineViewImpl(MachineLocalizationConstant localizationConstant, + MachineResources machineResources, + org.eclipse.che.ide.Resources coreResources, + AutoCompleteResources autoCompleteResources, + ToolbarResources toolbarResources) { + this.localizationConstant = localizationConstant; + this.machineResources = machineResources; + this.coreResources = coreResources; + this.toolbarResources = toolbarResources; + + css = autoCompleteResources.autocompleteComponentCss(); + popupPanel = new PopupPanel(); + + setWidget(UI_BINDER.createAndBindUi(this)); + setTitle(localizationConstant.viewCreateMachineTitle()); + + machineName.addKeyUpHandler(new KeyUpHandler() { + @Override + public void onKeyUp(KeyUpEvent event) { + delegate.onNameChanged(); + } + }); + + recipeURL.addKeyUpHandler(new KeyUpHandler() { + @Override + public void onKeyUp(KeyUpEvent event) { + delegate.onRecipeUrlChanged(); + } + }); + + createFooterButtons(); + } + + private void createFooterButtons() { + createButton = createButton(localizationConstant.viewCreateMachineButtonCreate(), "window-create-machine-create", + new ClickHandler() { + @Override + public void onClick(ClickEvent event) { + delegate.onCreateClicked(); + } + }); + + replaceButton = createButton(localizationConstant.viewCreateMachineButtonReplace(), "window-create-machine-replace", + new ClickHandler() { + @Override + public void onClick(ClickEvent event) { + delegate.onReplaceDevMachineClicked(); + } + }); + + cancelButton = createButton(localizationConstant.cancelButton(), "window-create-machine-cancel", + new ClickHandler() { + @Override + public void onClick(ClickEvent event) { + delegate.onCancelClicked(); + } + }); + + addButtonToFooter(createButton); + addButtonToFooter(replaceButton); + addButtonToFooter(cancelButton); + } + + @Override + public void setDelegate(ActionDelegate delegate) { + this.delegate = delegate; + } + + @Override + public void show() { + super.show(); + + new Timer() { + @Override + public void run() { + machineName.setFocus(true); + } + }.schedule(300); + } + + @Override + public void close() { + hide(); + } + + @Override + protected void onClose() { + super.onClose(); + popupPanel.hide(); + } + + @Override + public String getMachineName() { + return machineName.getValue(); + } + + @Override + public void setMachineName(String name) { + machineName.setValue(name); + delegate.onNameChanged(); + } + + @Override + public String getRecipeURL() { + return recipeURL.getValue(); + } + + @Override + public void setRecipeURL(String url) { + recipeURL.setValue(url); + recipeURL.setTitle(url); + + delegate.onRecipeUrlChanged(); + } + + @Override + public void setErrorHint(boolean show) { + errorHint.setVisible(show); + } + + @Override + public List getTags() { + final List tagList = new ArrayList<>(); + + for (String tag : tags.getValue().split(" ")) { + if (!tag.isEmpty()) { + tagList.add(tag.trim()); + } + } + + return tagList; + } + + @Override + public void setTags(String tags) { + this.tags.setValue(tags); + } + + @Override + public void setNoRecipeHint(boolean show) { + noRecipeHint.setVisible(show); + } + + @Override + public void setRecipes(List recipes) { + if (recipes.isEmpty()) { + popupPanel.hide(); + return; + } + + popupPanel.clear(); + + final TableElement itemHolder = Elements.createTableElement(); + itemHolder.setClassName(css.items()); + + final HTML html = new HTML(); + html.setStyleName(css.container()); + html.getElement().getStyle().setOverflow(AUTO); + html.getElement().appendChild(((com.google.gwt.dom.client.Element)itemHolder)); + ((Element)html.getElement()).getStyle().setProperty("max-height", "200px"); + + final HTML container = new HTML(); + container.getElement().appendChild(html.getElement()); + + list = SimpleList.create((SimpleList.View)container.getElement().cast(), + (Element)html.getElement(), + itemHolder, + coreResources.defaultSimpleListCss(), + listItemRenderer, + eventDelegate); + + list.render(recipes); + + popupPanel.add(container); + popupPanel.setWidth(tags.getOffsetWidth() - 10 + "px"); + popupPanel.setPopupPositionAndShow(new PositionCallback() { + @Override + public void setPosition(int offsetWidth, int offsetHeight) { + popupPanel.setPopupPosition(tags.getAbsoluteLeft(), tags.getAbsoluteTop() + tags.getOffsetHeight()); + } + }); + + list.getSelectionModel().setSelectedItem(0); + } + + @Override + public void setCreateButtonState(boolean enabled) { + createButton.setEnabled(enabled); + } + + @Override + public void setReplaceButtonState(boolean enabled) { + replaceButton.setEnabled(enabled); + } + + @UiHandler("tags") + void handleKeyDown(KeyDownEvent event) { + switch (event.getNativeKeyCode()) { + case KEY_UP: + if (popupPanel.isShowing()) { + event.preventDefault(); + + if (list.getSelectionModel().getSelectedIndex() == 0) { + list.getSelectionModel().setSelectedItem(list.getSelectionModel().size() - 1); + } else { + list.getSelectionModel().selectPrevious(); + } + } + break; + case KEY_DOWN: + if (popupPanel.isShowing()) { + event.preventDefault(); + + if (list.getSelectionModel().getSelectedIndex() == list.getSelectionModel().size() - 1) { + list.getSelectionModel().setSelectedItem(0); + } else { + list.getSelectionModel().selectNext(); + } + } + break; + case KEY_ENTER: + if (popupPanel.isShowing()) { + delegate.onRecipeSelected(list.getSelectionModel().getSelectedItem()); + popupPanel.hide(); + } + break; + case KEY_ESCAPE: + if (popupPanel.isShowing()) { + popupPanel.hide(); + } + break; + case KEY_LEFT: + case KEY_RIGHT: + break; + default: + Scheduler.get().scheduleDeferred(new Command() { + @Override + public void execute() { + delegate.onTagsChanged(); + } + }); + break; + } + } + + interface CreateMachineViewImplUiBinder extends UiBinder { + } +} diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachineViewImpl.ui.xml b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachineViewImpl.ui.xml new file mode 100644 index 00000000000..b083f64fdec --- /dev/null +++ b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachineViewImpl.ui.xml @@ -0,0 +1,81 @@ + + + + + + + .emptyBorder { + margin: 6px; + } + + .inputField { + color: #dbdbdb; + border: 1px solid #191c1e; + border-radius: 1px; + box-shadow: 0px 1px 2px 0px rgba(0, 0, 0, 0.19) inset, 0px 1px 0px 0px rgba(223, 223, 223, 0.4); + font-size: 12px; + padding-left: 8px; + height: 20px; + } + + .label { + line-height: 25px; + } + + .floatLeft { + float: left; + } + + .floatRight { + float: right; + } + + .errorHint { + color: red; + height: 12px; + margin: 10px; + } + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/perspective/widgets/machine/appliance/processes/ProcessesPresenter.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/perspective/widgets/machine/appliance/processes/ProcessesPresenter.java index 97f5ea2ce95..a4aeef627b2 100644 --- a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/perspective/widgets/machine/appliance/processes/ProcessesPresenter.java +++ b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/perspective/widgets/machine/appliance/processes/ProcessesPresenter.java @@ -20,6 +20,7 @@ import org.eclipse.che.api.promises.client.Operation; import org.eclipse.che.api.promises.client.OperationException; import org.eclipse.che.ide.api.machine.ExecAgentCommandManager; +import org.eclipse.che.ide.api.machine.MachineServiceClient; import org.eclipse.che.ide.dto.DtoFactory; import org.eclipse.che.ide.extension.machine.client.perspective.widgets.tab.content.TabPresenter; diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/targets/categories/docker/DockerCategoryPresenter.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/targets/categories/docker/DockerCategoryPresenter.java index 1e0e1bc59b4..0ef8b879dcb 100644 --- a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/targets/categories/docker/DockerCategoryPresenter.java +++ b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/targets/categories/docker/DockerCategoryPresenter.java @@ -12,14 +12,30 @@ import com.google.gwt.user.client.ui.AcceptsOneWidget; import com.google.inject.Inject; - +import com.google.web.bindery.event.shared.EventBus; + +import org.eclipse.che.api.promises.client.Operation; +import org.eclipse.che.api.promises.client.OperationException; +import org.eclipse.che.api.promises.client.PromiseError; +import org.eclipse.che.ide.api.dialogs.CancelCallback; +import org.eclipse.che.ide.api.dialogs.ConfirmCallback; +import org.eclipse.che.ide.api.dialogs.DialogFactory; import org.eclipse.che.ide.api.machine.MachineEntity; +import org.eclipse.che.ide.api.machine.MachineServiceClient; +import org.eclipse.che.ide.api.notification.NotificationManager; import org.eclipse.che.ide.extension.machine.client.MachineLocalizationConstant; +import org.eclipse.che.ide.api.machine.events.MachineStateEvent; import org.eclipse.che.ide.extension.machine.client.targets.CategoryPage; import org.eclipse.che.ide.extension.machine.client.targets.Target; import org.eclipse.che.ide.extension.machine.client.targets.TargetManager; import org.eclipse.che.ide.extension.machine.client.targets.TargetsTreeManager; +import static org.eclipse.che.api.core.model.machine.MachineStatus.RUNNING; +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.api.notification.StatusNotification.Status.SUCCESS; +import static org.eclipse.che.ide.api.machine.events.MachineStateEvent.MachineAction.DESTROYED; + /** * Docker type page presenter. * @@ -27,16 +43,29 @@ */ public class DockerCategoryPresenter implements CategoryPage, TargetManager, DockerView.ActionDelegate { private final DockerView dockerView; + private final DialogFactory dialogFactory; + private final NotificationManager notificationManager; private final MachineLocalizationConstant machineLocale; + private final MachineServiceClient machineService; + private final EventBus eventBus; private DockerMachineTarget selectedTarget; private TargetsTreeManager targetsTreeManager; @Inject public DockerCategoryPresenter(DockerView dockerView, - MachineLocalizationConstant machineLocale) { + DialogFactory dialogFactory, + NotificationManager notificationManager, + MachineLocalizationConstant machineLocale, + MachineServiceClient machineService, + EventBus eventBus) { this.dockerView = dockerView; + this.dialogFactory = dialogFactory; + this.notificationManager = notificationManager; this.machineLocale = machineLocale; + this.machineService = machineService; + this.eventBus = eventBus; + dockerView.setDelegate(this); } @@ -64,6 +93,13 @@ private MachineEntity getMachineByName(String machineName) { return this.targetsTreeManager != null ? this.targetsTreeManager.getMachineByName(machineName) : null; } + private void updateTargets(String preselectTargetName) { + if (this.targetsTreeManager == null) { + return; + } + this.targetsTreeManager.updateTargets(preselectTargetName); + } + @Override public boolean onRestoreTargetFields(DockerMachineTarget target) { if (target == null) { @@ -86,10 +122,44 @@ public boolean onRestoreTargetFields(DockerMachineTarget target) { @Override public void onDeleteClicked(final Target target) { - //unsupported operation + dialogFactory.createConfirmDialog(machineLocale.targetsViewDeleteConfirmTitle(), + machineLocale.targetsViewDeleteConfirm(target.getName()), + new ConfirmCallback() { + @Override + public void accepted() { + destroyTargetMachine(target); + } + }, new CancelCallback() { + @Override + public void cancelled() { + updateTargets(null); + } + }).show(); } + private void destroyTargetMachine(final Target target) { + final MachineEntity machine = this.getMachineByName(target.getName()); + if (machine == null || machine.getStatus() != RUNNING) { + return; + } + + machineService.destroyMachine(machine.getWorkspaceId(), + machine.getId()).then(new Operation() { + @Override + public void apply(Void arg) throws OperationException { + eventBus.fireEvent(new MachineStateEvent(machine, DESTROYED)); + notificationManager.notify(machineLocale.targetsViewDisconnectSuccess(target.getName()), SUCCESS, FLOAT_MODE); + updateTargets(null); + } + }).catchError(new Operation() { + @Override + public void apply(PromiseError arg) throws OperationException { + notificationManager.notify(machineLocale.targetsViewDisconnectError(target.getName()), FAIL, FLOAT_MODE); + updateTargets(target.getName()); + } + }); + } @Override public void setCurrentSelection(Target selectedTarget) { diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/targets/categories/ssh/SshCategoryPresenter.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/targets/categories/ssh/SshCategoryPresenter.java index 3c26813ddcd..c632f0c7a5b 100644 --- a/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/targets/categories/ssh/SshCategoryPresenter.java +++ b/plugins/plugin-machine/che-plugin-machine-ext-client/src/main/java/org/eclipse/che/ide/extension/machine/client/targets/categories/ssh/SshCategoryPresenter.java @@ -32,10 +32,12 @@ import org.eclipse.che.api.workspace.shared.dto.WorkspaceDto; import org.eclipse.che.api.workspace.shared.dto.WorkspaceRuntimeDto; import org.eclipse.che.ide.api.app.AppContext; +import org.eclipse.che.ide.api.dialogs.CancelCallback; +import org.eclipse.che.ide.api.dialogs.ConfirmCallback; import org.eclipse.che.ide.api.dialogs.DialogFactory; import org.eclipse.che.ide.api.machine.MachineEntity; +import org.eclipse.che.ide.api.machine.MachineServiceClient; import org.eclipse.che.ide.api.machine.RecipeServiceClient; -import org.eclipse.che.ide.api.machine.events.MachineStateEvent; import org.eclipse.che.ide.api.notification.NotificationManager; import org.eclipse.che.ide.api.notification.StatusNotification; import org.eclipse.che.ide.api.workspace.WorkspaceServiceClient; @@ -43,6 +45,7 @@ import org.eclipse.che.ide.dto.DtoFactory; import org.eclipse.che.ide.extension.machine.client.MachineLocalizationConstant; import org.eclipse.che.ide.extension.machine.client.inject.factories.EntityFactory; +import org.eclipse.che.ide.api.machine.events.MachineStateEvent; import org.eclipse.che.ide.extension.machine.client.targets.CategoryPage; import org.eclipse.che.ide.extension.machine.client.targets.Target; import org.eclipse.che.ide.extension.machine.client.targets.TargetManager; @@ -52,10 +55,11 @@ import java.util.List; import static org.eclipse.che.api.core.model.machine.MachineStatus.RUNNING; -import static org.eclipse.che.ide.api.machine.events.MachineStateEvent.MachineAction.DESTROYED; 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.api.notification.StatusNotification.Status.PROGRESS; import static org.eclipse.che.ide.api.notification.StatusNotification.Status.SUCCESS; +import static org.eclipse.che.ide.api.machine.events.MachineStateEvent.MachineAction.DESTROYED; /** * SSH type page presenter. @@ -73,6 +77,7 @@ public class SshCategoryPresenter implements CategoryPage, TargetManager, SshVie private final NotificationManager notificationManager; private final MachineLocalizationConstant machineLocale; private final AppContext appContext; + private final MachineServiceClient machineService; private final EventBus eventBus; private final WorkspaceServiceClient workspaceServiceClient; @@ -95,6 +100,7 @@ public SshCategoryPresenter(SshView sshView, MachineLocalizationConstant machineLocale, WorkspaceServiceClient workspaceServiceClient, AppContext appContext, + MachineServiceClient machineService, EventBus eventBus) { this.sshView = sshView; this.recipeServiceClient = recipeServiceClient; @@ -105,6 +111,7 @@ public SshCategoryPresenter(SshView sshView, this.workspaceServiceClient = workspaceServiceClient; this.machineLocale = machineLocale; this.appContext = appContext; + this.machineService = machineService; this.eventBus = eventBus; sshView.setDelegate(this); @@ -464,14 +471,40 @@ private void disconnect(final MachineEntity machine) { return; } sshView.setConnectButtonText(null); - eventBus.fireEvent(new MachineStateEvent(machine, DESTROYED)); - notificationManager.notify(machineLocale.targetsViewDisconnectSuccess(selectedTarget.getName()), SUCCESS, FLOAT_MODE); - updateTargets(null); + + machineService.destroyMachine(machine.getWorkspaceId(), + machine.getId()).then(new Operation() { + @Override + public void apply(Void arg) throws OperationException { + eventBus.fireEvent(new MachineStateEvent(machine, DESTROYED)); + + notificationManager.notify(machineLocale.targetsViewDisconnectSuccess(selectedTarget.getName()), SUCCESS, FLOAT_MODE); + updateTargets(null); + } + }).catchError(new Operation() { + @Override + public void apply(PromiseError arg) throws OperationException { + notificationManager.notify(machineLocale.targetsViewDisconnectError(selectedTarget.getName()), FAIL, FLOAT_MODE); + updateTargets(null); + } + }); } @Override public void onDeleteClicked(final Target target) { - //unsupported operation + dialogFactory.createConfirmDialog(machineLocale.targetsViewDeleteConfirmTitle(), + machineLocale.targetsViewDeleteConfirm(target.getName()), + new ConfirmCallback() { + @Override + public void accepted() { + deleteTarget(target); + } + }, new CancelCallback() { + @Override + public void cancelled() { + updateTargets(null); + } + }).show(); } private void deleteTarget(final Target target) { @@ -486,6 +519,22 @@ private void deleteTarget(final Target target) { disconnect(machine); return; } + + machineService.destroyMachine(machine.getWorkspaceId(), + machine.getId()).then(new Operation() { + @Override + public void apply(Void arg) throws OperationException { + eventBus.fireEvent(new MachineStateEvent(machine, DESTROYED)); + notificationManager.notify(machineLocale.targetsViewDisconnectSuccess(target.getName()), SUCCESS, FLOAT_MODE); + deleteTargetRecipe(target); + } + }).catchError(new Operation() { + @Override + public void apply(PromiseError arg) throws OperationException { + notificationManager.notify(machineLocale.targetsViewDisconnectError(target.getName()), FAIL, FLOAT_MODE); + updateTargets(target.getName()); + } + }); } /** diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/machine/MachineManagerImplTest.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/machine/MachineManagerImplTest.java new file mode 100644 index 00000000000..da37fdbb8d9 --- /dev/null +++ b/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/machine/MachineManagerImplTest.java @@ -0,0 +1,169 @@ +/******************************************************************************* + * 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.extension.machine.client.machine; + +import com.google.web.bindery.event.shared.EventBus; + +import org.eclipse.che.api.core.model.machine.MachineConfig; +import org.eclipse.che.api.core.model.machine.MachineSource; +import org.eclipse.che.api.machine.shared.dto.MachineLimitsDto; +import org.eclipse.che.api.machine.shared.dto.MachineConfigDto; +import org.eclipse.che.api.machine.shared.dto.MachineSourceDto; +import org.eclipse.che.api.promises.client.Operation; +import org.eclipse.che.api.promises.client.OperationException; +import org.eclipse.che.api.promises.client.Promise; +import org.eclipse.che.ide.api.app.AppContext; +import org.eclipse.che.ide.api.machine.DevMachine; +import org.eclipse.che.ide.api.machine.MachineEntity; +import org.eclipse.che.ide.api.machine.MachineServiceClient; +import org.eclipse.che.ide.api.machine.events.MachineStateEvent; +import org.eclipse.che.ide.api.parts.PerspectiveManager; +import org.eclipse.che.ide.api.workspace.WorkspaceServiceClient; +import org.eclipse.che.ide.dto.DtoFactory; +import org.eclipse.che.ide.rest.DtoUnmarshallerFactory; +import org.eclipse.che.ide.websocket.MessageBusProvider; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.InjectMocks; +import org.mockito.Matchers; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyBoolean; +import static org.mockito.Matchers.anyInt; +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Check {@link MachineManagerImpl} + * + * @author Florent Benoit + */ +@RunWith(MockitoJUnitRunner.class) +public class MachineManagerImplTest { + + @Mock + private DtoUnmarshallerFactory dtoUnmarshallerFactor; + + @Mock + private MachineServiceClient machineServiceClient; + + @Mock + private WorkspaceServiceClient workspaceServiceClient; + + @Mock + private MachineStatusHandler machineStatusHandler; + + @Mock + private MessageBusProvider messageBusProvider; + + @Mock + private PerspectiveManager perspectiveManager; + + @Mock + private EventBus eventBus; + + @Mock + private AppContext appContext; + + @Mock + private DtoFactory dtoFactory; + + @Captor + private ArgumentCaptor startWorkspaceHandlerCaptor; + + @Captor + private ArgumentCaptor> operationArgumentCaptor; + + @Captor + private ArgumentCaptor machineConfigDtoArgumentCaptor; + + + @InjectMocks + private MachineManagerImpl machineManager; + + /** + * Check a valid source object is used on machine destroyed with restart flag + * + * @throws OperationException + * if restart fails + */ + @Test + public void checkUseValidSource() throws OperationException { + final String ID = "id"; + final String WORKSPACE_ID = "testWorkspaceId"; + final String DISPLAY_NAME = "my-display-name"; + final boolean IS_DEV = true; + + final String SOURCE_TYPE = "source-type"; + final String SOURCE_LOCATION = "source-location"; + final String SOURCE_CONTENT = "source-content"; + + MachineEntity machineState = mock(MachineEntity.class); + when(machineState.getId()).thenReturn(ID); + when(machineState.getWorkspaceId()).thenReturn(WORKSPACE_ID); + Promise promise = mock(Promise.class); + Promise promiseThen = mock(Promise.class); + when(machineServiceClient.destroyMachine(eq(WORKSPACE_ID), eq(ID))).thenReturn(promise); + when(promise.then(Matchers.>anyObject())).thenReturn(promiseThen); + + MachineSource machineSource = mock(MachineSource.class); + MachineConfig machineConfig = mock(MachineConfig.class); + when(machineState.getConfig()).thenReturn(machineConfig); + when(machineConfig.getSource()).thenReturn(machineSource); + when(machineConfig.getName()).thenReturn(DISPLAY_NAME); + when(machineConfig.isDev()).thenReturn(IS_DEV); + when(machineSource.getType()).thenReturn(SOURCE_TYPE); + when(machineSource.getLocation()).thenReturn(SOURCE_LOCATION); + when(machineSource.getContent()).thenReturn(SOURCE_CONTENT); + MachineSourceDto machineSourceDto = mock(MachineSourceDto.class); + when(machineSourceDto.withType(eq(SOURCE_TYPE))).thenReturn(machineSourceDto); + when(machineSourceDto.withLocation(eq(SOURCE_LOCATION))).thenReturn(machineSourceDto); + when(machineSourceDto.withContent(eq(SOURCE_CONTENT))).thenReturn(machineSourceDto); + when(dtoFactory.createDto(MachineSourceDto.class)).thenReturn(machineSourceDto); + MachineLimitsDto limitsDto = mock(MachineLimitsDto.class); + when(dtoFactory.createDto(MachineLimitsDto.class)).thenReturn(limitsDto); + when(limitsDto.withRam(anyInt())).thenReturn(limitsDto); + + MachineConfigDto machineConfigDto = mock(MachineConfigDto.class); + when(dtoFactory.createDto(MachineConfigDto.class)).thenReturn(machineConfigDto); + when(machineConfigDto.withDev(anyBoolean())).thenReturn(machineConfigDto); + when(machineConfigDto.withName(anyString())).thenReturn(machineConfigDto); + when(machineConfigDto.withSource(machineSourceDto)).thenReturn(machineConfigDto); + when(machineConfigDto.withLimits(limitsDto)).thenReturn(machineConfigDto); + when(machineConfigDto.withType(anyString())).thenReturn(machineConfigDto); + + when(appContext.getWorkspaceId()).thenReturn(ID); + DevMachine devMachine = mock(DevMachine.class); + when(appContext.getDevMachine()).thenReturn(devMachine); + when(devMachine.getId()).thenReturn(ID); + + Promise promiseEmpty = mock(Promise.class); + when(workspaceServiceClient.createMachine(anyString(), any(MachineConfigDto.class))).thenReturn(promiseEmpty); + + machineManager.restartMachine(machineState); + + verify(promiseThen).then(operationArgumentCaptor.capture()); + operationArgumentCaptor.getValue().apply(null); + + verify(workspaceServiceClient).createMachine(eq(ID), machineConfigDtoArgumentCaptor.capture()); + verify(machineSourceDto).withType(eq(SOURCE_TYPE)); + verify(machineSourceDto).withLocation(eq(SOURCE_LOCATION)); + verify(machineSourceDto).withContent(eq(SOURCE_CONTENT)); + } + +} diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachinePresenterTest.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachinePresenterTest.java new file mode 100644 index 00000000000..383bfca31ff --- /dev/null +++ b/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/machine/create/CreateMachinePresenterTest.java @@ -0,0 +1,147 @@ +/******************************************************************************* + * 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.extension.machine.client.machine.create; + +import org.eclipse.che.ide.api.app.AppContext; +import org.eclipse.che.ide.api.machine.DevMachine; +import org.eclipse.che.ide.api.machine.MachineEntity; +import org.eclipse.che.ide.api.machine.MachineManager; +import org.eclipse.che.ide.api.project.ProjectTypeServiceClient; +import org.eclipse.che.ide.extension.machine.client.inject.factories.EntityFactory; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** @author Artem Zatsarynny */ +@RunWith(MockitoJUnitRunner.class) +public class CreateMachinePresenterTest { + + private final static String RECIPE_URL = "http://www.host.com/recipe"; + private final static String MACHINE_NAME = "machine"; + private final static String WORKSPACE_ID = "testWorkspace123"; + + private final static String SOME_TEXT = "someText"; + + @Mock + private CreateMachineView view; + @Mock + private MachineManager machineManager; + @Mock + private AppContext appContext; + @Mock + private ProjectTypeServiceClient projectTypeServiceClient; + @Mock + private EntityFactory entityFactory; + + @InjectMocks + private CreateMachinePresenter presenter; + + @Before + public void setUp() { + when(view.getRecipeURL()).thenReturn(RECIPE_URL); + when(view.getMachineName()).thenReturn(MACHINE_NAME); + } + + @Test + public void shouldSetActionDelegate() throws Exception { + verify(view).setDelegate(presenter); + } + + @Test + public void viewShouldBeShown() throws Exception { + presenter.showDialog(); + + verify(view).show(); + verify(view).setCreateButtonState(false); + verify(view).setReplaceButtonState(false); + verify(view).setMachineName(""); + verify(view).setRecipeURL(""); + verify(view).setErrorHint(false); + verify(view).setTags(""); + } + + @Test + public void buttonsShouldBeDisabledWhenNameIsEmpty() throws Exception { + when(view.getMachineName()).thenReturn(""); + + presenter.onNameChanged(); + + verify(view).setCreateButtonState(eq(false)); + verify(view).setReplaceButtonState(eq(false)); + } + + @Test + public void buttonsShouldBeEnabledWhenNameIsNotEmpty() throws Exception { + presenter.onNameChanged(); + + verify(view).setCreateButtonState(eq(true)); + verify(view).setReplaceButtonState(eq(true)); + } + + @Test + public void shouldCreateMachine() throws Exception { + presenter.onCreateClicked(); + + verify(view).getRecipeURL(); + verify(view).getMachineName(); + verify(machineManager).startMachine(eq(RECIPE_URL), eq(MACHINE_NAME)); + verify(view).close(); + } + + @Test + public void shouldReplaceDevMachine() throws Exception { + DevMachine devMachine = mock(DevMachine.class); + when(appContext.getDevMachine()).thenReturn(devMachine); + when(devMachine.getId()).thenReturn(SOME_TEXT); + when(devMachine.getWorkspace()).thenReturn(WORKSPACE_ID); + + presenter.onReplaceDevMachineClicked(); + + verify(view).getMachineName(); + verify(view).getRecipeURL(); + verify(appContext).getDevMachine(); + verify(machineManager).destroyMachine(devMachine); + verify(machineManager).startDevMachine(eq(RECIPE_URL), eq(MACHINE_NAME)); + verify(view).close(); + } + + @Test + public void shouldStartNewDevMachine() throws Exception { + when(appContext.getDevMachine()).thenReturn(null); + + presenter.onReplaceDevMachineClicked(); + + verify(view).getMachineName(); + verify(view).getRecipeURL(); + verify(appContext).getDevMachine(); + verify(machineManager).startDevMachine(eq(RECIPE_URL), eq(MACHINE_NAME)); + verify(view).close(); + verify(machineManager, never()).destroyMachine(any(MachineEntity.class)); + verify(machineManager).startDevMachine(eq(RECIPE_URL), eq(MACHINE_NAME)); + } + + @Test + public void shouldCloseView() throws Exception { + presenter.onCancelClicked(); + + verify(view).close(); + } +} diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/perspective/widgets/machine/appliance/processes/ProcessesPresenterTest.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/perspective/widgets/machine/appliance/processes/ProcessesPresenterTest.java index b9285383e38..b0a60a93df6 100644 --- a/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/perspective/widgets/machine/appliance/processes/ProcessesPresenterTest.java +++ b/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/perspective/widgets/machine/appliance/processes/ProcessesPresenterTest.java @@ -17,6 +17,7 @@ import org.eclipse.che.api.promises.client.Operation; import org.eclipse.che.api.promises.client.Promise; import org.eclipse.che.ide.api.machine.ExecAgentCommandManager; +import org.eclipse.che.ide.api.machine.MachineServiceClient; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/processes/panel/ProcessesPanelPresenterTest.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/processes/panel/ProcessesPanelPresenterTest.java index 46124b43d91..d201fc2036c 100644 --- a/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/processes/panel/ProcessesPanelPresenterTest.java +++ b/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/processes/panel/ProcessesPanelPresenterTest.java @@ -34,6 +34,7 @@ import org.eclipse.che.ide.api.machine.DevMachine; import org.eclipse.che.ide.api.machine.ExecAgentCommandManager; import org.eclipse.che.ide.api.machine.MachineEntity; +import org.eclipse.che.ide.api.machine.MachineManager; import org.eclipse.che.ide.api.machine.events.MachineStateEvent; import org.eclipse.che.ide.api.machine.events.WsAgentStateEvent; import org.eclipse.che.ide.api.macro.MacroProcessor; @@ -125,6 +126,8 @@ public class ProcessesPanelPresenterTest { @Mock private OutputConsole outputConsole; @Mock + private MachineManager machineManager; + @Mock private EntityFactory entityFactory; @Mock private WorkspaceRuntimeDto workspaceRuntime; diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/targets/categories/docker/DockerCategoryPresenterTest.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/targets/categories/docker/DockerCategoryPresenterTest.java index 5bdd4a6c8d3..be0fbefe981 100644 --- a/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/targets/categories/docker/DockerCategoryPresenterTest.java +++ b/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/targets/categories/docker/DockerCategoryPresenterTest.java @@ -19,8 +19,11 @@ import org.eclipse.che.ide.api.dialogs.ConfirmCallback; import org.eclipse.che.ide.api.dialogs.ConfirmDialog; import org.eclipse.che.ide.api.dialogs.DialogFactory; +import org.eclipse.che.ide.api.machine.MachineEntity; +import org.eclipse.che.ide.api.machine.MachineServiceClient; import org.eclipse.che.ide.api.notification.NotificationManager; import org.eclipse.che.ide.extension.machine.client.MachineLocalizationConstant; +import org.eclipse.che.ide.api.machine.events.MachineStateEvent; import org.eclipse.che.ide.extension.machine.client.targets.TargetsTreeManager; import org.junit.Before; import org.junit.Test; @@ -32,6 +35,9 @@ import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; +import static org.eclipse.che.api.core.model.machine.MachineStatus.RUNNING; +import static org.eclipse.che.ide.api.notification.StatusNotification.DisplayMode.FLOAT_MODE; +import static org.eclipse.che.ide.api.notification.StatusNotification.Status.SUCCESS; import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.verify; @@ -49,6 +55,8 @@ public class DockerCategoryPresenterTest { @Mock private MachineLocalizationConstant machineLocale; @Mock + private MachineServiceClient machineService; + @Mock private EventBus eventBus; @@ -80,12 +88,51 @@ public void setUp() { when(promise.then(operationSuccessCapture.capture())).thenReturn(promise); - arbitraryCategoryPresenter = new DockerCategoryPresenter(dockerView, machineLocale); + arbitraryCategoryPresenter = new DockerCategoryPresenter(dockerView, + dialogFactory, + notificationManager, + machineLocale, + machineService, + eventBus); arbitraryCategoryPresenter.setTargetsTreeManager(targetsTreeManager); } + @Test + public void testOnDeleteTarget() throws Exception { + final String deletingTargetName = "deletingTargetName"; + final String deleteProposal = "Are you sure you want to delete target " + deletingTargetName + " ?"; + final String deletingMachineId = "deletingMachineId"; + final String deleteSuccessMessage = "Successfully disconnected from machine " + deletingTargetName; + final DockerMachineTarget target = Mockito.mock(DockerMachineTarget.class); + final MachineEntity machine = Mockito.mock(MachineEntity.class); + when(machineLocale.targetsViewDisconnectSuccess(deletingTargetName)).thenReturn(deleteSuccessMessage); + when(target.getName()).thenReturn(deletingTargetName); + when(targetsTreeManager.getMachineByName(deletingTargetName)).thenReturn(machine); + when(machine.getId()).thenReturn(deletingMachineId); + when(machine.getWorkspaceId()).thenReturn("WS_ID"); + when(machine.getStatus()).thenReturn(RUNNING); + when(machineLocale.targetsViewDeleteConfirm(deletingTargetName)).thenReturn(deleteProposal); + when(machineService.destroyMachine("WS_ID", deletingMachineId)).thenReturn(promise); + + + arbitraryCategoryPresenter.onDeleteClicked(target); + + verify(dialogFactory).createConfirmDialog(anyString(), eq(deleteProposal), confirmCaptor.capture(), + Matchers.anyObject()); + + confirmCaptor.getValue().accepted(); + + verify(targetsTreeManager).getMachineByName(deletingTargetName); + verify(machineService).destroyMachine("WS_ID", deletingMachineId); + + operationSuccessCapture.getValue().apply(null); + + verify(eventBus).fireEvent(Matchers.anyObject()); + verify(notificationManager).notify(eq(deleteSuccessMessage), eq(SUCCESS), eq(FLOAT_MODE)); + verify(targetsTreeManager).updateTargets(null); + } @Test public void testGetCategory() throws Exception { diff --git a/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/targets/categories/ssh/SshCategoryPresenterTest.java b/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/targets/categories/ssh/SshCategoryPresenterTest.java index ab60a431820..6838e133809 100644 --- a/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/targets/categories/ssh/SshCategoryPresenterTest.java +++ b/plugins/plugin-machine/che-plugin-machine-ext-client/src/test/java/org/eclipse/che/ide/extension/machine/client/targets/categories/ssh/SshCategoryPresenterTest.java @@ -13,6 +13,7 @@ import com.google.gwt.user.client.ui.AcceptsOneWidget; import com.google.web.bindery.event.shared.EventBus; +import org.eclipse.che.api.machine.shared.dto.recipe.RecipeDescriptor; import org.eclipse.che.api.promises.client.Operation; import org.eclipse.che.api.promises.client.Promise; import org.eclipse.che.ide.api.app.AppContext; @@ -20,6 +21,7 @@ import org.eclipse.che.ide.api.dialogs.ConfirmCallback; import org.eclipse.che.ide.api.dialogs.ConfirmDialog; import org.eclipse.che.ide.api.dialogs.DialogFactory; +import org.eclipse.che.ide.api.machine.MachineServiceClient; import org.eclipse.che.ide.api.machine.RecipeServiceClient; import org.eclipse.che.ide.api.notification.NotificationManager; import org.eclipse.che.ide.api.workspace.WorkspaceServiceClient; @@ -37,6 +39,8 @@ import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; +import static org.eclipse.che.ide.api.notification.StatusNotification.DisplayMode.FLOAT_MODE; +import static org.eclipse.che.ide.api.notification.StatusNotification.Status.SUCCESS; import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.never; @@ -64,6 +68,8 @@ public class SshCategoryPresenterTest { @Mock private AppContext appContext; @Mock + private MachineServiceClient machineService; + @Mock private EventBus eventBus; @Mock private EntityFactory entityFactory; @@ -108,11 +114,46 @@ public void setUp() { machineLocale, workspaceServiceClient, appContext, + machineService, eventBus); arbitraryCategoryPresenter.setTargetsTreeManager(targetsTreeManager); arbitraryCategoryPresenter.setCurrentSelection(target); } + + @Test + public void testOnDeleteTarget() throws Exception { + final String deletingTargetName = "deletingTargetName"; + final String recipeId = "deletingTargetRecipeId"; + final String deleteProposal = "Are you sure you want to delete target " + deletingTargetName + " ?"; + final String deleteSuccessMessage = "Target recipe " + deletingTargetName + " successfully deleted"; + final SshMachineTarget target = Mockito.mock(SshMachineTarget.class); + final RecipeDescriptor recipe = Mockito.mock(RecipeDescriptor.class); + when(target.getName()).thenReturn(deletingTargetName); + when(target.getRecipe()).thenReturn(recipe); + when(recipe.getId()).thenReturn(recipeId); + when(recipeServiceClient.removeRecipe(recipeId)).thenReturn(promise); + when(machineLocale.targetsViewDeleteConfirm(deletingTargetName)).thenReturn(deleteProposal); + when(machineLocale.targetsRecipeDeleteSuccess(deletingTargetName)).thenReturn(deleteSuccessMessage); + + arbitraryCategoryPresenter.onDeleteClicked(target); + + verify(dialogFactory).createConfirmDialog(anyString(), eq(deleteProposal), confirmCaptor.capture(), + Matchers.anyObject()); + + confirmCaptor.getValue().accepted(); + + verify(recipeServiceClient).removeRecipe(recipeId); + verify(promise).then(operationSuccessCapture.capture()); + + operationSuccessCapture.getValue().apply(null); + + verify(notificationManager).notify(eq(deleteSuccessMessage), eq(SUCCESS), eq(FLOAT_MODE)); + verify(target).isConnected(); + verify(targetsTreeManager).updateTargets(null); + } + + @Test public void testOnCancelClicked() throws Exception { diff --git a/plugins/plugin-nodejs-debugger/che-plugin-nodejs-debugger-ide/src/test/java/org/eclipse/che/plugin/nodejsdbg/ide/configuration/NodeJsDebuggerConfigurationPagePresenterTest.java b/plugins/plugin-nodejs-debugger/che-plugin-nodejs-debugger-ide/src/test/java/org/eclipse/che/plugin/nodejsdbg/ide/configuration/NodeJsDebuggerConfigurationPagePresenterTest.java index 61058609a4a..357d9d8c93b 100644 --- a/plugins/plugin-nodejs-debugger/che-plugin-nodejs-debugger-ide/src/test/java/org/eclipse/che/plugin/nodejsdbg/ide/configuration/NodeJsDebuggerConfigurationPagePresenterTest.java +++ b/plugins/plugin-nodejs-debugger/che-plugin-nodejs-debugger-ide/src/test/java/org/eclipse/che/plugin/nodejsdbg/ide/configuration/NodeJsDebuggerConfigurationPagePresenterTest.java @@ -15,6 +15,7 @@ import org.eclipse.che.ide.api.app.AppContext; import org.eclipse.che.ide.api.debug.DebugConfiguration; import org.eclipse.che.ide.api.debug.DebugConfigurationPage; +import org.eclipse.che.ide.api.machine.MachineServiceClient; import org.eclipse.che.ide.api.machine.RecipeServiceClient; import org.eclipse.che.ide.extension.machine.client.command.macros.CurrentProjectPathMacro; import org.junit.Before; @@ -54,6 +55,8 @@ public class NodeJsDebuggerConfigurationPagePresenterTest { private AppContext appContext; @Mock private RecipeServiceClient recipeServiceClient; + @Mock + private MachineServiceClient machineServiceClient; @InjectMocks private NodeJsDebuggerConfigurationPagePresenter pagePresenter; diff --git a/samples/sample-plugin-nativeaccess/che-sample-plugin-nativeaccess-ide/src/main/java/org/eclipse/che/plugin/nativeaccessexample/machine/client/command/CommandManager.java b/samples/sample-plugin-nativeaccess/che-sample-plugin-nativeaccess-ide/src/main/java/org/eclipse/che/plugin/nativeaccessexample/machine/client/command/CommandManager.java index eab9e02e00c..f680bc01484 100644 --- a/samples/sample-plugin-nativeaccess/che-sample-plugin-nativeaccess-ide/src/main/java/org/eclipse/che/plugin/nativeaccessexample/machine/client/command/CommandManager.java +++ b/samples/sample-plugin-nativeaccess/che-sample-plugin-nativeaccess-ide/src/main/java/org/eclipse/che/plugin/nativeaccessexample/machine/client/command/CommandManager.java @@ -21,13 +21,15 @@ import org.eclipse.che.ide.api.app.AppContext; import org.eclipse.che.ide.api.machine.ExecAgentCommandManager; import org.eclipse.che.ide.api.machine.execagent.ExecAgentPromise; +import org.eclipse.che.ide.api.notification.NotificationManager; import org.eclipse.che.ide.dto.DtoFactory; +import org.eclipse.che.ide.util.UUID; import javax.validation.constraints.NotNull; /** * Simple command manager which allows to run native commands within the workspace Docker container. - * + * Please note that the actual call is delegated to the MachineServiceClient service. * * @author Mathias Schaefer */ @@ -36,14 +38,15 @@ public class CommandManager { private final DtoFactory dtoFactory; private final ExecAgentCommandManager commandManager; + private final NotificationManager notificationManager; private final AppContext appContext; @Inject - public CommandManager(DtoFactory dtoFactory, - ExecAgentCommandManager commandManager, + public CommandManager(DtoFactory dtoFactory, ExecAgentCommandManager commandManager, NotificationManager notificationManager, AppContext appContext) { this.dtoFactory = dtoFactory; this.commandManager = commandManager; + this.notificationManager = notificationManager; this.appContext = appContext; } From 36bbe869cbb74252c076cc2e4c77dbeb13facc46 Mon Sep 17 00:00:00 2001 From: Max Shaposhnik Date: Mon, 6 Mar 2017 18:01:13 +0200 Subject: [PATCH 6/9] Revert "Merge branch 'che#3620' into 3616" This reverts commit a41f88dfd8466fd567819a9f58aa9a1b278e239c, reversing changes made to 52cf7bcdbe0c4b6ddc2056c4b5ec0abce7e1f7e6. --- .../main/java/org/eclipse/che/ide/context/AppContextImpl.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/context/AppContextImpl.java b/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/context/AppContextImpl.java index 2a2efce794f..6c5b6c9d63f 100644 --- a/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/context/AppContextImpl.java +++ b/ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/context/AppContextImpl.java @@ -130,9 +130,7 @@ public Workspace getWorkspace() { public void setWorkspace(Workspace workspace) { if (workspace != null) { usersWorkspace = workspace; - if (workspace.getRuntime() != null) { - runtime = new ActiveRuntime(workspace.getRuntime()); - } + runtime = new ActiveRuntime(workspace.getRuntime()); } else { usersWorkspace = null; runtime = null; From 8b8d21c40b9066b0fc7bb6dc6a40f56ec44eb979 Mon Sep 17 00:00:00 2001 From: Max Shaposhnik Date: Mon, 6 Mar 2017 18:05:07 +0200 Subject: [PATCH 7/9] Fix indent --- .../plugin/docker/machine/local/LocalDockerModule.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/local/LocalDockerModule.java b/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/local/LocalDockerModule.java index f3391e6b480..79b6faa0a01 100644 --- a/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/local/LocalDockerModule.java +++ b/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/local/LocalDockerModule.java @@ -41,7 +41,7 @@ public class LocalDockerModule extends AbstractModule { @Override protected void configure() { - install(new FactoryModuleBuilder() + install(new FactoryModuleBuilder() .implement(Instance.class, DockerInstance.class) .implement(InstanceProcess.class, DockerProcess.class) .implement(DockerNode.class, LocalDockerNode.class) @@ -82,12 +82,14 @@ protected void configure() { devMachineVolumes.addBinding().toProvider(org.eclipse.che.plugin.docker.machine.ext.provider.ExtraVolumeProvider.class); Multibinder> networks = Multibinder.newSetBinder(binder(), - new TypeLiteral>() {}, + new TypeLiteral>() { + }, Names.named("machine.docker.networks")); networks.addBinding().toProvider(org.eclipse.che.plugin.docker.machine.CheInContainerNetworkProvider.class); Multibinder> extraHosts = Multibinder.newSetBinder(binder(), - new TypeLiteral>() {}, + new TypeLiteral>() { + }, Names.named("che.docker.extra_hosts")) .permitDuplicates(); extraHosts.addBinding() From 58a2179d2bd08f4216e6a4a82c98f926b4807e0e Mon Sep 17 00:00:00 2001 From: Max Shaposhnik Date: Tue, 7 Mar 2017 11:23:59 +0200 Subject: [PATCH 8/9] fix formatting --- .../che/plugin/docker/machine/local/LocalDockerModule.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/local/LocalDockerModule.java b/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/local/LocalDockerModule.java index 79b6faa0a01..263c8ee0849 100644 --- a/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/local/LocalDockerModule.java +++ b/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/local/LocalDockerModule.java @@ -82,14 +82,12 @@ protected void configure() { devMachineVolumes.addBinding().toProvider(org.eclipse.che.plugin.docker.machine.ext.provider.ExtraVolumeProvider.class); Multibinder> networks = Multibinder.newSetBinder(binder(), - new TypeLiteral>() { - }, + new TypeLiteral>() {}, Names.named("machine.docker.networks")); networks.addBinding().toProvider(org.eclipse.che.plugin.docker.machine.CheInContainerNetworkProvider.class); Multibinder> extraHosts = Multibinder.newSetBinder(binder(), - new TypeLiteral>() { - }, + new TypeLiteral>() {}, Names.named("che.docker.extra_hosts")) .permitDuplicates(); extraHosts.addBinding() From 5154ba2caa6e3bb734d43de5bac5f8e5dd6c89ac Mon Sep 17 00:00:00 2001 From: Max Shaposhnik Date: Tue, 7 Mar 2017 11:34:49 +0200 Subject: [PATCH 9/9] fix formatting --- .../api/workspace/server/WorkspaceServiceLinksInjector.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceServiceLinksInjector.java b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceServiceLinksInjector.java index e2b99c4aab0..75724fb5579 100644 --- a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceServiceLinksInjector.java +++ b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceServiceLinksInjector.java @@ -67,10 +67,9 @@ public class WorkspaceServiceLinksInjector { @Inject public WorkspaceServiceLinksInjector(MachineLinksInjector machineLinksInjector) { - this.machineLinksInjector = machineLinksInjector; + this.machineLinksInjector = machineLinksInjector; } - public WorkspaceDto injectLinks(WorkspaceDto workspace, ServiceContext serviceContext) { final UriBuilder uriBuilder = serviceContext.getServiceUriBuilder(); final List links = new ArrayList<>(); @@ -248,5 +247,4 @@ protected void injectRuntimeLinks(WorkspaceDto workspace, URI ideUri, UriBuilder protected MachineDto injectMachineLinks(MachineDto machine, ServiceContext serviceContext) { return machineLinksInjector.injectLinks(machine, serviceContext); } - }