gt:         for (ServerConf serverConf : allMachinesServers) {
context: /*******************************************************************************
   * 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.plugin.docker.machine;
  
  import com.google.common.base.MoreObjects;
  import com.google.common.base.Strings;
  import com.google.common.util.concurrent.ThreadFactoryBuilder;
  
  import org.eclipse.che.api.core.NotFoundException;
  import org.eclipse.che.api.core.ServerException;
  import org.eclipse.che.api.core.model.machine.MachineStatus;
  import org.eclipse.che.api.core.model.machine.ServerConf;
  import org.eclipse.che.api.core.util.FileCleaner;
  import org.eclipse.che.api.core.util.LineConsumer;
  import org.eclipse.che.api.core.util.SystemInfo;
  import org.eclipse.che.api.environment.server.MachineInstanceProvider;
  import org.eclipse.che.api.environment.server.model.CheServiceImpl;
  import org.eclipse.che.api.machine.server.exception.MachineException;
  import org.eclipse.che.api.machine.server.exception.SourceNotFoundException;
  import org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl;
  import org.eclipse.che.api.machine.server.model.impl.MachineImpl;
  import org.eclipse.che.api.machine.server.model.impl.MachineLimitsImpl;
  import org.eclipse.che.api.machine.server.model.impl.MachineSourceImpl;
  import org.eclipse.che.api.machine.server.spi.Instance;
  import org.eclipse.che.commons.annotation.Nullable;
  import org.eclipse.che.commons.env.EnvironmentContext;
  import org.eclipse.che.commons.lang.Size;
  import org.eclipse.che.commons.lang.concurrent.LoggingUncaughtExceptionHandler;
  import org.eclipse.che.commons.lang.os.WindowsPathEscaper;
  import org.eclipse.che.plugin.docker.client.DockerConnector;
  import org.eclipse.che.plugin.docker.client.ProgressLineFormatterImpl;
  import org.eclipse.che.plugin.docker.client.ProgressMonitor;
  import org.eclipse.che.plugin.docker.client.UserSpecificDockerRegistryCredentialsProvider;
  import org.eclipse.che.plugin.docker.client.exception.ContainerNotFoundException;
  import org.eclipse.che.plugin.docker.client.exception.ImageNotFoundException;
  import org.eclipse.che.plugin.docker.client.exception.NetworkNotFoundException;
  import org.eclipse.che.plugin.docker.client.json.ContainerConfig;
  import org.eclipse.che.plugin.docker.client.json.HostConfig;
  import org.eclipse.che.plugin.docker.client.json.PortBinding;
  import org.eclipse.che.plugin.docker.client.json.container.NetworkingConfig;
  import org.eclipse.che.plugin.docker.client.json.network.ConnectContainer;
  import org.eclipse.che.plugin.docker.client.json.network.EndpointConfig;
  import org.eclipse.che.plugin.docker.client.json.network.NewNetwork;
  import org.eclipse.che.plugin.docker.client.params.BuildImageParams;
  import org.eclipse.che.plugin.docker.client.params.CreateContainerParams;
  import org.eclipse.che.plugin.docker.client.params.GetContainerLogsParams;
  import org.eclipse.che.plugin.docker.client.params.PullParams;
  import org.eclipse.che.plugin.docker.client.params.RemoveContainerParams;
  import org.eclipse.che.plugin.docker.client.params.RemoveImageParams;
  import org.eclipse.che.plugin.docker.client.params.RemoveNetworkParams;
  import org.eclipse.che.plugin.docker.client.params.StartContainerParams;
  import org.eclipse.che.plugin.docker.client.params.TagParams;
  import org.eclipse.che.plugin.docker.client.params.network.ConnectContainerToNetworkParams;
  import org.eclipse.che.plugin.docker.client.params.network.CreateNetworkParams;
  import org.eclipse.che.plugin.docker.machine.node.DockerNode;
  import org.slf4j.Logger;
  
  import javax.inject.Inject;
  import javax.inject.Named;
  import java.io.File;
  import java.io.FileWriter;
  import java.io.IOException;
  import java.net.SocketTimeoutException;
  import java.nio.file.Files;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
  import java.util.function.Function;
  import java.util.regex.Pattern;
  
  import static java.lang.String.format;
  import static java.lang.Thread.sleep;
  import static java.util.Collections.emptyMap;
  import static java.util.Collections.singletonMap;
  import static java.util.stream.Collectors.toMap;
  import static java.util.stream.Collectors.toSet;
  import static org.eclipse.che.plugin.docker.machine.DockerInstance.LATEST_TAG;
  import static org.slf4j.LoggerFactory.getLogger;
  
  /**
   * Creates/destroys docker networks and creates docker compose based {@link Instance}.
   *
   * @author Alexander Garagatyi
   */
  public class MachineProviderImpl implements MachineInstanceProvider {
      private static final Logger LOG = getLogger(MachineProviderImpl.class);
  
      /**
       * Prefix of image repository, used to identify that the image is a machine saved to snapshot.
       */
      public static final String MACHINE_SNAPSHOT_PREFIX = "machine_snapshot_";
  
      public static final Pattern SNAPSHOT_LOCATION_PATTERN = Pattern.compile("(.+/)?" + MACHINE_SNAPSHOT_PREFIX + ".+");
  
      private final DockerConnector                               docker;
      private final UserSpecificDockerRegistryCredentialsProvider dockerCredentials;
      private final ExecutorService                               executor;
      private final DockerInstanceStopDetector                    dockerInstanceStopDetector;
      private final boolean                                       doForcePullOnBuild;
      private final boolean                                       privilegedMode;
      private final int                                           pidsLimit;
      private final DockerMachineFactory                          dockerMachineFactory;
      private final List<String>                                  devMachinePortsToExpose;
      private final List<String>                                  commonMachinePortsToExpose;
      private final List<String>                                  devMachineSystemVolumes;
      private final List<String>                                  commonMachineSystemVolumes;
      private final Map<String, String>                           devMachineEnvVariables;
      private final Map<String, String>                           commonMachineEnvVariables;
      private final String[]                                      allMachinesExtraHosts;
      private final boolean                                       snapshotUseRegistry;
      private final double                                        memorySwapMultiplier;
      private final String                                        networkDriver;
      private final Set<String>                                   additionalNetworks;
      private final String                                        parentCgroup;
      private final String                                        cpusetCpus;
      private final long                                          cpuPeriod;
      private final long                                          cpuQuota;
      private final WindowsPathEscaper                            windowsPathEscaper;
  
      @Inject
      public MachineProviderImpl(DockerConnector docker,
                                 UserSpecificDockerRegistryCredentialsProvider dockerCredentials,
                                 DockerMachineFactory dockerMachineFactory,
                                 DockerInstanceStopDetector dockerInstanceStopDetector,
                                 @Named("machine.docker.dev_machine.machine_servers") Set<ServerConf> devMachineServers,
                                 @Named("machine.docker.machine_servers") Set<ServerConf> allMachinesServers,
                                 @Named("machine.docker.dev_machine.machine_volumes") Set<String> devMachineSystemVolumes,
                                 @Named("machine.docker.machine_volumes") Set<String> allMachinesSystemVolumes,
                                 @Named("che.docker.always_pull_image") boolean doForcePullOnBuild,
                                 @Named("che.docker.privileged") boolean privilegedMode,
                                 @Named("che.docker.pids_limit") int pidsLimit,
                                 @Named("machine.docker.dev_machine.machine_env") Set<String> devMachineEnvVariables,
                                 @Named("machine.docker.machine_env") Set<String> allMachinesEnvVariables,
                                 @Named("che.docker.registry_for_snapshots") boolean snapshotUseRegistry,
                                 @Named("che.docker.swap") double memorySwapMultiplier,
                                 @Named("machine.docker.networks") Set<Set<String>> additionalNetworks,
                                 @Nullable @Named("che.docker.network_driver") String networkDriver,
                                 @Nullable @Named("che.docker.parent_cgroup") String parentCgroup,
                                 @Nullable @Named("che.docker.cpuset_cpus") String cpusetCpus,
                                 @Named("che.docker.cpu_period") long cpuPeriod,
                                 @Named("che.docker.cpu_quota") long cpuQuota,
                                 WindowsPathEscaper windowsPathEscaper,
                                 @Named("che.docker.extra_hosts") Set<Set<String>> additionalHosts)
              throws IOException {
          this.docker = docker;
          this.dockerCredentials = dockerCredentials;
          this.dockerMachineFactory = dockerMachineFactory;
          this.dockerInstanceStopDetector = dockerInstanceStopDetector;
          this.doForcePullOnBuild = doForcePullOnBuild;
          this.privilegedMode = privilegedMode;
          this.snapshotUseRegistry = snapshotUseRegistry;
          // use-cases:
          //  -1  enable unlimited swap
          //  0   disable swap
          //  0.5 enable swap with size equal to half of current memory size
          //  1   enable swap with size equal to current memory size
          //
          //  according to docker docs field  memorySwap should be equal to memory+swap
          //  we calculate this field as memorySwap=memory * (1 + multiplier) so we just add 1 to multiplier
          this.memorySwapMultiplier = memorySwapMultiplier == -1 ? -1 : memorySwapMultiplier + 1;
          this.networkDriver = networkDriver;
          this.parentCgroup = parentCgroup;
          this.cpusetCpus = cpusetCpus;
          this.cpuPeriod = cpuPeriod;
          this.cpuQuota = cpuQuota;
          this.windowsPathEscaper = windowsPathEscaper;
          this.pidsLimit = pidsLimit;
  
          allMachinesSystemVolumes = removeEmptyAndNullValues(allMachinesSystemVolumes);
          devMachineSystemVolumes = removeEmptyAndNullValues(devMachineSystemVolumes);
  
          allMachinesSystemVolumes = allMachinesSystemVolumes.stream()
                                                             .map(line -> line.split(";"))
                                                             .flatMap(Arrays::stream)
                                                             .distinct()
                                                             .collect(toSet());
  
          devMachineSystemVolumes = devMachineSystemVolumes.stream()
                                                           .map(line -> line.split(";"))
                                                           .flatMap(Arrays::stream)
                                                           .distinct()
                                                           .collect(toSet());
  
          if (SystemInfo.isWindows()) {
              allMachinesSystemVolumes = escapePaths(allMachinesSystemVolumes);
              devMachineSystemVolumes = escapePaths(devMachineSystemVolumes);
          }
          this.commonMachineSystemVolumes = new ArrayList<>(allMachinesSystemVolumes);
          List<String> devMachineVolumes = new ArrayList<>(allMachinesSystemVolumes.size()
                                                           + devMachineSystemVolumes.size());
          devMachineVolumes.addAll(allMachinesSystemVolumes);
          devMachineVolumes.addAll(devMachineSystemVolumes);
          this.devMachineSystemVolumes = devMachineVolumes;
  
          this.devMachinePortsToExpose = new ArrayList<>(allMachinesServers.size() + devMachineServers.size());
          this.commonMachinePortsToExpose = new ArrayList<>(allMachinesServers.size());
          for (ServerConf serverConf : devMachineServers) {
              devMachinePortsToExpose.add(serverConf.getPort());
          }
  