Skip to content

Commit

Permalink
Merge pull request #2461 from eclipse/CHE-1929
Browse files Browse the repository at this point in the history
CHE-1929. Do not use machine API for machine handling
  • Loading branch information
RomanNikitenko authored Sep 29, 2016
2 parents 27b4f17 + 1a958ea commit 47193dc
Show file tree
Hide file tree
Showing 75 changed files with 1,260 additions and 1,124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

import com.google.common.base.Strings;

import org.eclipse.che.api.core.model.machine.MachineConfig;
import org.eclipse.che.api.core.model.machine.MachineRuntimeInfo;
import org.eclipse.che.api.core.model.machine.MachineStatus;
import org.eclipse.che.api.core.rest.shared.dto.Hyperlinks;
import org.eclipse.che.api.core.rest.shared.dto.Link;
import org.eclipse.che.api.core.model.machine.Machine;
Expand All @@ -31,9 +34,10 @@
*
* @author Vitalii Parfonov
*/
public class DevMachine {
public class DevMachine implements MachineEntity {

private final Machine devMachineDescriptor;
private final Machine devMachineDescriptor;
private final MachineConfig machineConfig;

private final Map<String, DevMachineServer> servers;
private final Map<String, String> runtimeProperties;
Expand All @@ -42,29 +46,43 @@ public class DevMachine {

public DevMachine(@NotNull Machine devMachineDescriptor) {
this.devMachineDescriptor = devMachineDescriptor;
this.machineConfig = devMachineDescriptor.getConfig();
this.devMachineLinks = devMachineDescriptor instanceof Hyperlinks ? ((Hyperlinks)devMachineDescriptor).getLinks() : null;

Map<String, ? extends Server> serverDtoMap = devMachineDescriptor.getRuntime().getServers();
servers = new HashMap<>(serverDtoMap.size());
for (String s : serverDtoMap.keySet()) {
servers.put(s, new DevMachineServer(serverDtoMap.get(s)));
}
runtimeProperties = devMachineDescriptor.getRuntime().getProperties();
envVariables = devMachineDescriptor.getRuntime().getEnvVariables();

MachineRuntimeInfo machineRuntime = devMachineDescriptor.getRuntime();
runtimeProperties = machineRuntime != null ? machineRuntime.getProperties() : null;
envVariables = machineRuntime != null ? machineRuntime.getEnvVariables() : null;
}

public Map<String, String> getEnvVariables() {
return envVariables;
}

public Map<String, String> getRuntimeProperties() {
return runtimeProperties;
@Override
public boolean isDev() {
return true;
}

public String getType() {
return devMachineDescriptor.getConfig().getType();
}

@Override
public String getDisplayName() {
return machineConfig.getName();
}

@Override
public Map<String, String> getProperties() {
return runtimeProperties;
}

public String getWsAgentWebSocketUrl() {
for (Link link : devMachineLinks) {
if (Constants.WSAGENT_WEBSOCKET_REFERENCE.equals(link.getRel())) {
Expand Down Expand Up @@ -128,10 +146,40 @@ public String getWorkspace() {
return devMachineDescriptor.getWorkspaceId();
}

@Override
public MachineConfig getConfig() {
return machineConfig;
}

public String getId() {
return devMachineDescriptor.getId();
}

@Override
public String getWorkspaceId() {
return devMachineDescriptor.getWorkspaceId();
}

@Override
public String getEnvName() {
return devMachineDescriptor.getEnvName();
}

@Override
public String getOwner() {
return devMachineDescriptor.getOwner();
}

@Override
public MachineStatus getStatus() {
return devMachineDescriptor.getStatus();
}

@Override
public MachineRuntimeInfo getRuntime() {
return devMachineDescriptor.getRuntime();
}

public List<Link> getDevMachineLinks() {
return devMachineLinks;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*******************************************************************************
* Copyright (c) 2012-2016 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.Machine;

import java.util.Map;

/**
* Defines machine entity on client side.
*
* @author Roman Nikitenko
*/
public interface MachineEntity extends Machine {

/** Returns {@code true} when the machine entity is development machine and {@code false} otherwise */
boolean isDev();

/** Machine type (i.e. "docker"). */
String getType();

/** Returns current machine's display name */
String getDisplayName();

/** Returns machine specific properties. */
Map<String, String> getProperties();

/** Returns url to connects to special WebSocket which allows get information from terminal on server side. */
String getTerminalUrl();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*******************************************************************************/
package org.eclipse.che.ide.api.machine;

import org.eclipse.che.api.core.model.machine.Machine;
import org.eclipse.che.api.promises.client.Promise;

/**
Expand Down Expand Up @@ -46,7 +45,7 @@ public interface MachineManager {
* @param machine
* contains information about machine state
*/
Promise<Void> destroyMachine(Machine machine);
Promise<Void> destroyMachine(MachineEntity machine);


/**
Expand All @@ -55,6 +54,5 @@ public interface MachineManager {
* @param machine
* contains information about machine state
*/
void restartMachine(final Machine machine);

void restartMachine(final MachineEntity machine);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
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;
Expand All @@ -27,26 +26,6 @@
*/
public interface MachineServiceClient {

/**
* Get machine information by it's id.
*
* @param workspaceId
* ID of workspace
* @param machineId
* ID of the machine
* @return a promise that resolves to the {@link MachineDto}, or rejects with an error
*/
Promise<MachineDto> getMachine(@NotNull String workspaceId, @NotNull String machineId);

/**
* 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<List<MachineDto>> getMachines(String workspaceId);

/**
* Destroy machine with the specified ID.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.google.inject.Inject;

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;
Expand Down Expand Up @@ -52,24 +51,6 @@ protected MachineServiceClientImpl(@RestContext String restContext,
this.baseHttpUrl = restContext + "/workspace/";
}

@Override
public Promise<MachineDto> getMachine(@NotNull final String workspaceId,
@NotNull final String machineId) {
return asyncRequestFactory.createGetRequest(baseHttpUrl + workspaceId +
"/machine/" + machineId)
.header(ACCEPT, APPLICATION_JSON)
.loader(loaderFactory.newLoader("Getting info about machine..."))
.send(dtoUnmarshallerFactory.newUnmarshaller(MachineDto.class));
}

@Override
public Promise<List<MachineDto>> getMachines(@NotNull 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<Void> destroyMachine(@NotNull final String workspaceId,
@NotNull final String machineId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.eclipse.che.ide.api.resources.VirtualFile;
import org.eclipse.che.ide.api.selection.Selection;
import org.eclipse.che.ide.api.workspace.WorkspaceReadyEvent;
import org.eclipse.che.ide.api.workspace.event.WorkspaceStartedEvent;
import org.eclipse.che.ide.api.workspace.event.WorkspaceStoppedEvent;
import org.eclipse.che.ide.project.node.SyntheticNode;
import org.eclipse.che.ide.resource.Path;
Expand Down Expand Up @@ -77,6 +78,7 @@ public class AppContextImpl implements AppContext,
SelectionChangedHandler,
ResourceChangedHandler,
WindowActionHandler,
WorkspaceStartedEvent.Handler,
WorkspaceStoppedEvent.Handler {

private final BrowserQueryFieldRenderer browserQueryFieldRenderer;
Expand Down Expand Up @@ -431,6 +433,11 @@ public void onWindowClosing(WindowActionEvent event) {
appStateManager.get().persistWorkspaceState(getWorkspaceId());
}

@Override
public void onWorkspaceStarted(WorkspaceStartedEvent event) {
setWorkspace(event.getWorkspace());
}

@Override
public void onWorkspaceStopped(WorkspaceStoppedEvent event) {
appStateManager.get().persistWorkspaceState(getWorkspaceId()).then(new Operation<Void>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import com.google.inject.Inject;
import com.google.inject.Singleton;

import org.eclipse.che.api.core.model.machine.Machine;
import org.eclipse.che.api.core.model.workspace.Workspace;
import org.eclipse.che.api.machine.shared.dto.MachineDto;
import org.eclipse.che.api.machine.shared.dto.recipe.RecipeDescriptor;
import org.eclipse.che.api.promises.client.Operation;
Expand All @@ -24,16 +26,19 @@
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.dto.DtoFactory;
import org.eclipse.che.ide.extension.machine.client.command.valueproviders.CurrentProjectPathProvider;
import org.eclipse.che.ide.extension.machine.client.inject.factories.EntityFactory;
import org.eclipse.che.ide.json.JsonHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.util.Collections.emptyList;

/**
* Page allows to edit GDB debug configuration.
*
Expand All @@ -46,8 +51,8 @@ public class GdbConfigurationPagePresenter implements GdbConfigurationPageView.A
public static final String DEFAULT_EXECUTABLE_TARGET_NAME = "a.out";

private final GdbConfigurationPageView view;
private final MachineServiceClient machineServiceClient;
private final AppContext appContext;
private final EntityFactory entityFactory;
private final RecipeServiceClient recipeServiceClient;
private final DtoFactory dtoFactory;
private final CurrentProjectPathProvider currentProjectPathProvider;
Expand All @@ -60,14 +65,14 @@ public class GdbConfigurationPagePresenter implements GdbConfigurationPageView.A

@Inject
public GdbConfigurationPagePresenter(GdbConfigurationPageView view,
MachineServiceClient machineServiceClient,
AppContext appContext,
DtoFactory dtoFactory,
EntityFactory entityFactory,
RecipeServiceClient recipeServiceClient,
CurrentProjectPathProvider currentProjectPathProvider) {
this.view = view;
this.machineServiceClient = machineServiceClient;
this.appContext = appContext;
this.entityFactory = entityFactory;
this.recipeServiceClient = recipeServiceClient;
this.dtoFactory = dtoFactory;
this.currentProjectPathProvider = currentProjectPathProvider;
Expand Down Expand Up @@ -112,28 +117,43 @@ public void go(AcceptsOneWidget container) {
view.setPortEnableState(!devHost);
view.setHostEnableState(!devHost);

setHostsList();
List<Machine> machines = getMachines();
if (!machines.isEmpty()) {
setHosts(machines);
}
}

private void setHostsList() {
machineServiceClient.getMachines(appContext.getWorkspaceId()).then(new Operation<List<MachineDto>>() {
@Override
public void apply(List<MachineDto> machines) throws OperationException {
@SuppressWarnings("unchecked")
Promise<RecipeDescriptor>[] recipePromises = (Promise<RecipeDescriptor>[])new Promise[machines.size()];

for (int i = 0; i < machines.size(); i++) {
String location = machines.get(i).getConfig().getSource().getLocation();
String recipeId = getRecipeId(location);
recipePromises[i] = recipeServiceClient.getRecipe(recipeId);
}
private void setHosts(List<Machine> machines) {
List<Promise<RecipeDescriptor>> recipePromises = new ArrayList<>(machines.size());
for (Machine machine : machines) {
String location = machine.getConfig().getSource().getLocation();
String recipeId = getRecipeId(location);
recipePromises.add(recipeServiceClient.getRecipe(recipeId));
}

setHostsList(recipePromises, machines);
@SuppressWarnings("unchecked")
final Promise<RecipeDescriptor>[] recipePromisesArray = (Promise<RecipeDescriptor>[])recipePromises.toArray();
setHostsList(recipePromisesArray, machines);
}

private List<Machine> getMachines() {
Workspace workspace = appContext.getWorkspace();
if (workspace == null || workspace.getRuntime() == null) {
return emptyList();
}

List<? extends Machine> runtimeMachines = workspace.getRuntime().getMachines();
List<Machine> machines = new ArrayList<>(runtimeMachines.size());
for (Machine currentMachine : runtimeMachines) {
if (currentMachine instanceof MachineDto) {
Machine machine = entityFactory.createMachine((MachineDto)currentMachine);
machines.add(machine);
}
});
}
return machines;
}

private void setHostsList(final Promise<RecipeDescriptor>[] recipePromises, final List<MachineDto> machines) {
private void setHostsList(final Promise<RecipeDescriptor>[] recipePromises, final List<Machine> machines) {
Promises.all(recipePromises).then(new Operation<JsArrayMixed>() {
@Override
public void apply(JsArrayMixed recipes) throws OperationException {
Expand Down
Loading

0 comments on commit 47193dc

Please sign in to comment.