/*
 * Copyright (c) 2012-2017 Red Hat, Inc.
 * 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:
 *   Red Hat, Inc. - initial API and implementation
 */
package org.eclipse.che.workspace.infrastructure.docker;

import java.io.IOException;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import org.eclipse.che.api.core.model.workspace.config.ServerConfig;
import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
import org.eclipse.che.commons.annotation.Nullable;
import org.eclipse.che.infrastructure.docker.client.DockerConnector;
import org.eclipse.che.infrastructure.docker.client.json.ContainerInfo;
import org.eclipse.che.infrastructure.docker.client.json.ContainerListEntry;
import org.eclipse.che.infrastructure.docker.client.json.NetworkSettings;
import org.eclipse.che.workspace.infrastructure.docker.monit.DockerMachineStopDetector;
import org.eclipse.che.workspace.infrastructure.docker.server.mapping.ServersMapper;

/** Helps to create {@link DockerMachine} instances. */
@Singleton
public class DockerMachineCreator {

  private final DockerConnector docker;
  private final String registry;
  private final boolean snapshotUseRegistry;
  private final String registryNamespace;
  private final DockerMachineStopDetector dockerMachineStopDetector;

  @Inject
  public DockerMachineCreator(
      DockerConnector docker,
      @Named("che.docker.registry") String registry,
      @Named("che.docker.registry_for_snapshots") boolean snapshotUseRegistry,
      @Named("che.docker.namespace") @Nullable String registryNamespace,
      DockerMachineStopDetector dockerMachineStopDetector) {
    this.docker = docker;
    this.registry = registry;
    this.snapshotUseRegistry = snapshotUseRegistry;
    this.registryNamespace = registryNamespace;
    this.dockerMachineStopDetector = dockerMachineStopDetector;
  }

  /** Creates new docker machine instance from the short container description. */
  public DockerMachine create(ContainerListEntry container) throws InfrastructureException {
    try {
      return create(docker.inspectContainer(container.getId()));
    } catch (IOException x) {
      throw new InfrastructureException(x.getMessage(), x);
    }
  }

  /** Creates new docker machine instance from the full container description. */
  public DockerMachine create(ContainerInfo container) {
    NetworkSettings networkSettings = container.getNetworkSettings();
    String hostname = networkSettings.getGateway();
    Map<String, ServerConfig> configs =
        Labels.newDeserializer(container.getConfig().getLabels()).servers();

    return new DockerMachine(
        container.getId(),
        container.getConfig().getImage(),
        docker,
        new ServersMapper(hostname).map(networkSettings.getPorts(), configs),
        registry,
        snapshotUseRegistry,
        registryNamespace,
        dockerMachineStopDetector);
  }
}

@@ -1477,16 +1477,57 @@
 espace;%0A
+  private final String internalDockerIP;%0A
   privat
@@ -1587,16 +1587,16 @@
 tector;%0A
-
 %0A  @Inje
@@ -1860,24 +1860,89 @@
 yNamespace,%0A
+      @Named(%22che.docker.ip%22) @Nullable String internalDockerIP,%0A
       Docker
@@ -2146,16 +2146,62 @@
 espace;%0A
+    this.internalDockerIP = internalDockerIP;%0A
     this
@@ -2833,16 +2833,116 @@
  String 
+hostname;%0A    if (internalDockerIP != null) %7B%0A      hostname = internalDockerIP;%0A    %7D else %7B%0A      
 hostname
@@ -2970,24 +2970,30 @@
 tGateway();%0A
+    %7D%0A
     Map%3CStri

Fix servers host evaluation on Docker infra