/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.execution.configurations;

import org.jetbrains.annotations.NonNls;

public class RemoteConnection {
  private boolean myUseSockets;
  private boolean myServerMode;
  private String myHostName;
  private String myAddress;
  @NonNls public static final String ONTHROW = ",onthrow=<FQ exception class name>";
  @NonNls public static final String ONUNCAUGHT = ",onuncaught=<y/n>";

  public RemoteConnection(boolean useSockets, String hostName, String address, boolean serverMode) {
    myUseSockets = useSockets;
    myServerMode = serverMode;
    myHostName = hostName;
    myAddress = address;
  }

  public boolean isUseSockets() {
    return myUseSockets;
  }

  public boolean isServerMode() {
    return myServerMode;
  }

  public String getHostName() {
    return myHostName;
  }

  public String getAddress() {
    return myAddress;
  }


  public void setUseSockets(boolean useSockets) {
    myUseSockets = useSockets;
  }

  public void setServerMode(boolean serverMode) {
    myServerMode = serverMode;
  }

  public void setHostName(String hostName) {
    myHostName = hostName;
  }

  public void setAddress(String address) {
    myAddress = address;
  }

  public String getLaunchCommandLine() {
    final String address = getAddress();
    final boolean shmem = !isUseSockets();
    final boolean serverMode = isServerMode();

    @NonNls String result;
    if (shmem) {
      if (serverMode) {
        result = "-Xdebug -Xrunjdwp:transport=dt_shmem,server=n,address=" +
                 ((address.length() > 0)? address : "...") +
                 ONTHROW + ",suspend=y" + ONUNCAUGHT;
      }
      else {
        result = "-Xdebug -Xrunjdwp:transport=dt_shmem,server=y,suspend=n,address=" +
                 ((address.length() > 0)? address : "...");
      }
    }
    else { // socket transport
      int p = -1;
      try {
        p = Integer.parseInt(address);
      }
      catch (NumberFormatException e) {
      }
      if (serverMode) {
        result = "-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=" +
                 ((p == -1)? "..." : Integer.toString(p)) +
                 ONTHROW + ",suspend=y" + ONUNCAUGHT;
      }
      else {
        result = "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=" +
                 ((p == -1)? "..." : Integer.toString(p));
      }
    }
    return result;
  }
}

@@ -684,16 +684,84 @@
 onNls;%0A%0A
+import java.net.InetAddress;%0Aimport java.net.UnknownHostException;%0A%0A
 public c
@@ -2636,32 +2636,374 @@
  (serverMode) %7B%0A
+        String localHostName = %22%3Chost name%3E:%22;%0A        try %7B%0A          final InetAddress localAddress = InetAddress.getLocalHost();%0A          final String name = localAddress.getCanonicalHostName();%0A          if (name != null) %7B%0A            localHostName = name + %22:%22;%0A          %7D%0A        %7D%0A        catch (UnknownHostException e) %7B%0A        %7D%0A
         result =
@@ -3054,32 +3054,48 @@
 er=n,address=%22 +
+ localHostName +
 %0A               
@@ -3101,35 +3101,38 @@
    ((p == -1)? %22
-...
+%3Cport%3E
 %22 : Integer.toSt

include local host name in launching parameters for listening mode