ServerRewritingStrategy

6.19.x
Gennady Azarenkov 2017-04-06 22:44:47 +03:00
parent 9c3c5336c3
commit 63baff63f3
8 changed files with 141 additions and 27 deletions

View File

@ -13,10 +13,11 @@ package org.eclipse.che.plugin.docker.machine;
import com.google.inject.assistedinject.Assisted;
import org.eclipse.che.api.core.model.machine.MachineConfig;
import org.eclipse.che.api.core.model.machine.OldMachineConfig;
import org.eclipse.che.api.core.model.machine.MachineRuntimeInfo;
import org.eclipse.che.api.core.model.machine.ServerConf;
import org.eclipse.che.api.machine.server.model.impl.ServerConfImpl;
import org.eclipse.che.api.core.model.machine.OldServerConf;
import org.eclipse.che.api.machine.server.model.impl.OldServerConfImpl;
//import org.eclipse.che.api.machine.server.model.impl.ServerConfImpl;
import org.eclipse.che.api.machine.server.model.impl.ServerImpl;
import org.eclipse.che.plugin.docker.client.json.ContainerConfig;
import org.eclipse.che.plugin.docker.client.json.ContainerInfo;
@ -74,21 +75,21 @@ public class DockerInstanceRuntimeInfo implements MachineRuntimeInfo {
*/
public static final String USER_TOKEN = "USER_TOKEN";
private final ContainerInfo info;
private final Map<String, ServerConfImpl> serversConf;
private final ContainerInfo info;
private final Map<String, OldServerConfImpl> serversConf;
private final String internalHost;
private final ServerEvaluationStrategyProvider provider;
@Inject
public DockerInstanceRuntimeInfo(@Assisted ContainerInfo containerInfo,
@Assisted MachineConfig machineConfig,
@Assisted OldMachineConfig machineConfig,
@Assisted String internalHost,
ServerEvaluationStrategyProvider provider,
@Named("machine.docker.dev_machine.machine_servers") Set<ServerConf> devMachineSystemServers,
@Named("machine.docker.machine_servers") Set<ServerConf> allMachinesSystemServers) {
@Named("machine.docker.dev_machine.machine_servers") Set<OldServerConf> devMachineSystemServers,
@Named("machine.docker.machine_servers") Set<OldServerConf> allMachinesSystemServers) {
this.info = containerInfo;
Stream<ServerConf> confStream = Stream.concat(machineConfig.getServers().stream(), allMachinesSystemServers.stream());
Stream<OldServerConf> confStream = Stream.concat(machineConfig.getServers().stream(), allMachinesSystemServers.stream());
if (machineConfig.isDev()) {
confStream = Stream.concat(confStream, devMachineSystemServers.stream());
}
@ -96,7 +97,7 @@ public class DockerInstanceRuntimeInfo implements MachineRuntimeInfo {
this.serversConf = confStream.collect(toMap(srvConf -> srvConf.getPort().contains("/") ?
srvConf.getPort() :
srvConf.getPort() + "/tcp",
ServerConfImpl::new));
OldServerConfImpl::new));
this.internalHost = internalHost;
this.provider = provider;

View File

@ -32,7 +32,7 @@ public class MachineImpl implements Machine {
}
// public MachineImpl(Machine machineRuntime) {
// this(machineRuntime.getEnvVariables(), machineRuntime.getProperties(), machineRuntime.getServers());
// this(machineRuntime.getProperties(), machineRuntime.getServers());
// }
public MachineImpl(Map<String, String> properties,

View File

@ -0,0 +1,75 @@
package org.eclipse.che.api.workspace.server;
import org.eclipse.che.api.core.model.workspace.Warning;
import org.eclipse.che.api.core.model.workspace.runtime.Server;
import org.eclipse.che.api.machine.server.model.impl.ServerImpl;
import org.eclipse.che.api.workspace.server.spi.RuntimeIdentity;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* A strategy for rewriting Server URLs
* @author gazarenkov
*/
public abstract class ServerRewritingStrategy {
public final Result rewrite(RuntimeIdentity identity, Map<String, ? extends Server> incoming) {
Map <String, Server> outgoing = new HashMap<>();
List<Warning> warnings = new ArrayList<>();
for(Map.Entry<String, ? extends Server> entry : incoming.entrySet()) {
String name = entry.getKey();
String strUrl = entry.getValue().getUrl();
try {
URL url = new URL(strUrl);
ServerImpl server = new ServerImpl(rewriteURL(identity, name, url).toString());
outgoing.put(name, server);
} catch (MalformedURLException e) {
warnings.add(new Warning() {
@Override
public int getCode() {
return 101;
}
@Override
public String getMessage() {
return "Malformed URL for " + name + " : " + e.getMessage();
}
});
}
}
return new Result(outgoing, warnings);
}
protected abstract URL rewriteURL(RuntimeIdentity identity, String name, URL url) throws MalformedURLException;
public static class Result {
private final Map <String, Server> servers;
private final List<Warning> warnings;
protected Result(Map<String, Server> servers, List<Warning> warnings) {
this.servers = servers;
this.warnings = warnings;
}
public Map<String, Server> getServers() {
return servers;
}
public List<Warning> getWarnings() {
return warnings;
}
}
}

View File

@ -22,7 +22,7 @@ import java.util.Objects;
/**
* @author Alexander Garagatyi
*/
@Entity(name = "OldServerConf")
@Entity(name = "ServerConf")
@Table(name = "serverconf")
public class ServerConfigImpl implements ServerConfig {

View File

@ -11,8 +11,15 @@
package org.eclipse.che.api.workspace.server.spi;
import org.eclipse.che.api.core.model.workspace.Runtime;
import org.eclipse.che.api.core.model.workspace.Warning;
import org.eclipse.che.api.core.model.workspace.runtime.Machine;
import org.eclipse.che.api.core.model.workspace.runtime.Server;
import org.eclipse.che.api.machine.server.model.impl.MachineImpl;
import org.eclipse.che.api.workspace.server.ServerRewritingStrategy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
@ -22,12 +29,19 @@ import java.util.Map;
*/
public abstract class InternalRuntime implements Runtime {
private final RuntimeContext context;
private final RuntimeContext context;
private final ServerRewritingStrategy serverRewritingStrategy;
private Map<String, Machine> cachedExternalMachines;
private final List<Warning> warnings = new ArrayList<>();
public InternalRuntime(RuntimeContext context) {
public InternalRuntime(RuntimeContext context, ServerRewritingStrategy serverRewritingStrategy) {
this.context = context;
this.serverRewritingStrategy = serverRewritingStrategy;
}
public abstract Map<String, ? extends Machine> getInternalMachines();
@Override
public String getActiveEnv() {
return context.getIdentity().getEnvName();
@ -39,7 +53,32 @@ public abstract class InternalRuntime implements Runtime {
}
@Override
public abstract Map<String, ? extends Machine> getMachines();
public List<? extends Warning> getWarnings() {
return warnings;
}
@Override
public Map<String, ? extends Machine> getMachines() {
if(cachedExternalMachines == null) {
cachedExternalMachines = new HashMap<>();
for(Map.Entry<String, ? extends Machine> entry : getInternalMachines().entrySet()) {
String key = entry.getKey();
Machine machine = entry.getValue();
ServerRewritingStrategy.Result result = serverRewritingStrategy.rewrite(context.getIdentity(), entry.getValue().getServers());
Map<String, Server> newServers = result.getServers();
MachineImpl newMachine = new MachineImpl(machine.getProperties(), newServers);
cachedExternalMachines.put(key, newMachine);
if(!result.getWarnings().isEmpty()) {
warnings.addAll(result.getWarnings());
}
}
}
return cachedExternalMachines;
}
/**
* @return some implementation specific properties if any
@ -53,5 +92,4 @@ public abstract class InternalRuntime implements Runtime {
return context;
}
}

View File

@ -25,13 +25,13 @@ import java.util.Map;
* Environment configuration transformed to view useful for Infrastructure to create Runtime
* @author gazarenkov
*/
public class InternalEnvironmentConfig {
public class InternalRuntimeConfig {
protected Map<String, InternalMachineConfig> internalMachines;
protected InternalRecipeConfig recipe;
protected EnvironmentImpl config;
public InternalEnvironmentConfig(Environment environment, URL registryEndpoint) throws ApiException, IOException {
public InternalRuntimeConfig(Environment environment, URL registryEndpoint) throws ApiException, IOException {
this.recipe = new InternalRecipeConfig(environment.getRecipe());

View File

@ -26,19 +26,19 @@ import java.util.Map;
*/
public abstract class RuntimeContext {
protected final Environment environment;
protected final InternalEnvironmentConfig internalEnv;
protected final RuntimeIdentity identity;
protected final RuntimeInfrastructure infrastructure;
protected final Environment environment;
protected final InternalRuntimeConfig internalRuntimeConfig;
protected final RuntimeIdentity identity;
protected final RuntimeInfrastructure infrastructure;
// TODO other than WorkspaceStatus impl
protected WorkspaceStatus state;
protected WorkspaceStatus state;
public RuntimeContext(Environment environment, RuntimeIdentity identity,
RuntimeInfrastructure infrastructure, URL registryEndpoint) throws ValidationException, ApiException, IOException {
this.environment = environment;
this.identity = identity;
this.infrastructure = infrastructure;
this.internalEnv = new InternalEnvironmentConfig(environment, registryEndpoint);
this.internalRuntimeConfig = new InternalRuntimeConfig(environment, registryEndpoint);
}

View File

@ -127,10 +127,10 @@ public class WorkspaceRuntimeIntegrationTest {
// when(instance.getId()).thenReturn("machineId");
// when(instance.getConfig()).thenReturn(machineConfig);
//
// CheServicesEnvironmentImpl internalEnv = new CheServicesEnvironmentImpl();
// internalEnv.getServices().put("service1", new CheServiceImpl().withId("machineId"));
// CheServicesEnvironmentImpl internalRuntimeConfig = new CheServicesEnvironmentImpl();
// internalRuntimeConfig.getServices().put("service1", new CheServiceImpl().withId("machineId"));
//
// when(environmentParser.parse(any(Environment.class))).thenReturn(internalEnv);
// when(environmentParser.parse(any(Environment.class))).thenReturn(internalRuntimeConfig);
// when(instanceProvider.startService(anyString(),
// anyString(),
// anyString(),