summaryrefslogtreecommitdiff
path: root/java/remote-servers/impl/src/com/intellij/remoteServer/impl/runtime/deployment/debug/JavaDebuggerLauncherImpl.java
blob: 0ff284e054dd29e5bcb1646f1793e0f3c74c0c81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package com.intellij.remoteServer.impl.runtime.deployment.debug;

import com.intellij.debugger.DebugEnvironment;
import com.intellij.debugger.DebugUIEnvironment;
import com.intellij.debugger.DebuggerManager;
import com.intellij.debugger.engine.DebugProcess;
import com.intellij.debugger.engine.DebugProcessAdapter;
import com.intellij.debugger.engine.RemoteDebugProcessHandler;
import com.intellij.debugger.ui.DebuggerPanelsManager;
import com.intellij.diagnostic.logging.LogFilesManager;
import com.intellij.execution.*;
import com.intellij.execution.configurations.RemoteConnection;
import com.intellij.execution.configurations.RunProfile;
import com.intellij.execution.configurations.SearchScopeProvider;
import com.intellij.execution.executors.DefaultDebugExecutor;
import com.intellij.execution.impl.ConsoleViewImpl;
import com.intellij.execution.process.ProcessHandler;
import com.intellij.execution.runners.ExecutionEnvironment;
import com.intellij.execution.ui.RunContentDescriptor;
import com.intellij.execution.ui.actions.CloseAction;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.remoteServer.configuration.RemoteServer;
import com.intellij.remoteServer.runtime.deployment.debug.JavaDebugConnectionData;
import com.intellij.remoteServer.runtime.deployment.debug.JavaDebugServerModeHandler;
import com.intellij.remoteServer.runtime.deployment.debug.JavaDebuggerLauncher;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * @author nik
 */
public class JavaDebuggerLauncherImpl extends JavaDebuggerLauncher {
  private static final Logger LOG = Logger.getInstance(JavaDebuggerLauncherImpl.class);

  @Override
  public void startDebugSession(@NotNull JavaDebugConnectionData info, @NotNull ExecutionEnvironment executionEnvironment, @NotNull RemoteServer<?> server)
    throws ExecutionException {
    final Project project = executionEnvironment.getProject();
    Executor executor = DefaultDebugExecutor.getDebugExecutorInstance();
    final DebuggerPanelsManager manager = DebuggerPanelsManager.getInstance(project);
    final JavaDebugServerModeHandler serverModeHandler = info.getServerModeHandler();
    boolean serverMode = serverModeHandler != null;
    final RemoteConnection remoteConnection = new RemoteConnection(true, info.getHost(), String.valueOf(info.getPort()), serverMode);
    DebugEnvironment debugEnvironment = new RemoteServerDebugEnvironment(project, remoteConnection, executionEnvironment.getRunProfile());
    DebugUIEnvironment debugUIEnvironment = new RemoteServerDebugUIEnvironment(debugEnvironment, executionEnvironment);
    RunContentDescriptor debugContentDescriptor = manager.attachVirtualMachine(debugUIEnvironment);
    LOG.assertTrue(debugContentDescriptor != null);
    ProcessHandler processHandler = debugContentDescriptor.getProcessHandler();
    LOG.assertTrue(processHandler != null);
    processHandler.startNotify();
    ExecutionManager.getInstance(project).getContentManager().showRunContent(executor, debugContentDescriptor,
                                                                             executionEnvironment.getContentToReuse());
    if (serverMode) {
      serverModeHandler.attachRemote();
      DebuggerManager.getInstance(executionEnvironment.getProject())
        .addDebugProcessListener(processHandler, new DebugProcessAdapter() {
          public void processDetached(DebugProcess process, boolean closedByUser) {
            try {
              serverModeHandler.detachRemote();
            }
            catch (ExecutionException e) {
              LOG.info(e);
            }
          }
        });
    }
  }

  private static class RemoteServerDebugUIEnvironment implements DebugUIEnvironment {
    private final DebugEnvironment myEnvironment;
    private final ExecutionEnvironment myExecutionEnvironment;

    public RemoteServerDebugUIEnvironment(DebugEnvironment environment, ExecutionEnvironment executionEnvironment) {
      myEnvironment = environment;
      myExecutionEnvironment = executionEnvironment;
    }

    @Override
    public DebugEnvironment getEnvironment() {
      return myEnvironment;
    }

    @Nullable
    @Override
    public RunContentDescriptor getReuseContent() {
      return myExecutionEnvironment.getContentToReuse();
    }

    @Nullable
    @Override
    public Icon getIcon() {
      return myExecutionEnvironment.getRunProfile().getIcon();
    }

    @Override
    public void initLogs(RunContentDescriptor content, LogFilesManager logFilesManager) {
    }

    @Override
    public void initActions(RunContentDescriptor content, DefaultActionGroup actionGroup) {
      actionGroup.add(new CloseAction(myExecutionEnvironment.getExecutor(), content, myExecutionEnvironment.getProject()));
    }

    @Nullable
    @Override
    public RunProfile getRunProfile() {
      return myExecutionEnvironment.getRunProfile();
    }
  }

  private static class RemoteServerDebugEnvironment implements DebugEnvironment {
    private final Project myProject;
    private final GlobalSearchScope mySearchScope;
    private final RemoteConnection myRemoteConnection;
    private final RunProfile myRunProfile;

    public RemoteServerDebugEnvironment(Project project, RemoteConnection remoteConnection, RunProfile runProfile) {
      myProject = project;
      mySearchScope = SearchScopeProvider.createSearchScope(project, runProfile);
      myRemoteConnection = remoteConnection;
      myRunProfile = runProfile;
    }

    @Nullable
    @Override
    public ExecutionResult createExecutionResult() throws ExecutionException {
      ConsoleViewImpl consoleView = new ConsoleViewImpl(myProject, false);
      RemoteDebugProcessHandler process = new RemoteDebugProcessHandler(myProject);
      consoleView.attachToProcess(process);
      return new DefaultExecutionResult(consoleView, process);
    }

    @Override
    public GlobalSearchScope getSearchScope() {
      return mySearchScope;
    }

    @Override
    public boolean isRemote() {
      return true;
    }

    @Override
    public RemoteConnection getRemoteConnection() {
      return myRemoteConnection;
    }

    @Override
    public boolean isPollConnection() {
      return true;
    }

    @Override
    public String getSessionName() {
      return myRunProfile.getName();
    }
  }
}