All Downloads are FREE. Search and download functionalities are using the official Maven repository.

editor.run.OutOfProcessRemote Maven / Gradle / Ivy

There is a newer version: 1.18.1
Show newest version
package editor.run;

import com.sun.jdi.Bootstrap;
import com.sun.jdi.VirtualMachineManager;
import com.sun.jdi.connect.AttachingConnector;
import com.sun.jdi.connect.Connector;
import com.sun.jdi.connect.ListeningConnector;

import java.util.List;
import java.util.Map;

/**
 */
public class OutOfProcessRemote extends AbstractOutOfProcessExecutor
{
  public OutOfProcessRemote()
  {
    super( RunState.Debug );
  }

  protected String exec() throws Exception
  {
    Connector conn = getConnector();
    Map args = applyArgs( conn, getRunConfig().getTransport() );
    printLabMessage( makeMessage( args ) );
    if( conn instanceof AttachingConnector )
    {
      AttachingConnector attachingConn = (AttachingConnector)conn;
      setVm( attachingConn.attach( args ) );
      getGosuPanel().makeDebugger( getVm() );
      return String.valueOf( "Attached to: " + getVm().name() );
    }
    else
    {
      ListeningConnector listeningConn = (ListeningConnector)conn;
      setVm( listeningConn.accept( args ) );
      getGosuPanel().makeDebugger( getVm() );
      return String.valueOf( "Accepted connection to: " + getVm().name() );
    }
  }

  private Connector getConnector()
  {
    VirtualMachineManager vmm = Bootstrap.virtualMachineManager();
    List attachingConnectors = vmm.attachingConnectors();
    for( AttachingConnector conn : attachingConnectors )
    {
      DebugTransport transport = getRunConfig().getTransport();
      if( conn.name().equals( transport.getFqn() ) )
      {
        return conn;
      }
    }
    List listeningConnectors = vmm.listeningConnectors();
    for( ListeningConnector conn : listeningConnectors )
    {
      DebugTransport transport = getRunConfig().getTransport();
      if( conn.name().equals( transport.getFqn() ) )
      {
        return conn;
      }
    }
    throw new IllegalStateException();
  }

  private Map applyArgs( Connector conn, DebugTransport transport )
  {
    Map args = conn.defaultArguments();
    switch( transport )
    {
      case AttachingSocket:
      case ListeningSocket:
        applySocketArgs( args );
        break;
      case AttachingMemory:
      case ListeningMemory:
        applySharedMemoryArgs( args );
        break;
    }
    return args;
  }

  private void applySharedMemoryArgs( Map args )
  {
    RemoteRunConfig runConfig = getRunConfig();
    Connector.Argument name = args.get( "name" );
    name.setValue( runConfig.getAddress() );
  }

  private void applySocketArgs( Map args )
  {
    RemoteRunConfig runConfig = getRunConfig();
    String host = runConfig.getHost();
    if( host != null && !host.isEmpty() )
    {
      Connector.Argument hostname = args.get( "hostname" );
      if( hostname != null )
      {
        hostname.setValue( host );
      }
    }
    Connector.Argument port = args.get( "port" );
    port.setValue( runConfig.getPort() );
  }

  private String makeMessage( Map args )
  {
    StringBuilder sb = new StringBuilder();
    for( Map.Entry entry: args.entrySet() )
    {
      sb.append( entry.getKey() ).append( ": " ).append( entry.getValue() ).append( "\n" );
    }
    sb.append( '\n' );
    sb.append( "Attaching: " ).append( getRunConfig().getName() ).append( "...\n" );
    return sb.toString();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy