Search in sources :

Example 11 with Debugger

use of com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudDebugProcessStateController method deleteBreakpoint.

/**
 * Called from the {@link CloudBreakpointHandler} to remove breakpoints from the server.
 *
 * @param breakpointId the {@link Breakpoint} Id to delete
 */
@SuppressWarnings("FutureReturnValueIgnored")
private void deleteBreakpoint(@NotNull final String breakpointId, boolean performAsync) {
    if (state == null) {
        throw new IllegalStateException();
    }
    final Debugger client = CloudDebuggerClient.getLongTimeoutClient(state);
    if (client == null) {
        LOG.warn("no client available attempting to setBreakpoint");
        Messages.showErrorDialog(state.getProject(), GctBundle.getString("clouddebug.bad.login.message"), GctBundle.getString("clouddebug.message.title"));
        return;
    }
    final String debuggeeId = state.getDebuggeeId();
    assert debuggeeId != null;
    Runnable performDelete = () -> {
        try {
            client.debuggees().breakpoints().delete(debuggeeId, breakpointId).setClientVersion(ServiceManager.getService(PluginInfoService.class).getClientVersionForCloudDebugger()).execute();
        } catch (IOException ex) {
            LOG.warn("exception deleting breakpoint " + breakpointId, ex);
        }
    };
    if (performAsync) {
        ApplicationManager.getApplication().executeOnPooledThread(performDelete);
    } else {
        performDelete.run();
    }
}
Also used : Debugger(com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger) IOException(java.io.IOException)

Example 12 with Debugger

use of com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudDebugProcessStateController method resolveBreakpointAsync.

/**
 * Returns a fully realized {@link Breakpoint} with all results possibly asynchronously.
 */
@SuppressWarnings("FutureReturnValueIgnored")
public void resolveBreakpointAsync(@NotNull final String id, @NotNull final ResolveBreakpointHandler handler) {
    if (fullFinalBreakpoints.containsKey(id)) {
        handler.onSuccess(fullFinalBreakpoints.get(id));
        return;
    }
    if (state == null) {
        handler.onError(GctBundle.getString("clouddebug.invalid.state"));
        return;
    }
    final Debugger client = CloudDebuggerClient.getLongTimeoutClient(state);
    if (client == null) {
        LOG.warn("no client available attempting to resolveBreakpointAsync");
        handler.onError(GctBundle.getString("clouddebug.bad.login.message"));
        return;
    }
    List<Breakpoint> currentList = state.getCurrentServerBreakpointList();
    for (Breakpoint serverBreakpointCandidate : currentList) {
        if (serverBreakpointCandidate.getId().equals(id) && !Boolean.TRUE.equals(serverBreakpointCandidate.getIsFinalState())) {
            handler.onSuccess(serverBreakpointCandidate);
            return;
        }
    }
    ApplicationManager.getApplication().executeOnPooledThread(() -> {
        // At this point, the user has selected a final state breakpoint which is not yet
        // hydrated.
        // So we query the server to get this final on a worker thread and then run the
        // runnable
        // back on ui
        GetBreakpointResponse response;
        try {
            response = client.debuggees().breakpoints().get(state.getDebuggeeId(), id).setClientVersion(ServiceManager.getService(PluginInfoService.class).getClientVersionForCloudDebugger()).execute();
            Breakpoint result = response.getBreakpoint();
            if (result != null) {
                fullFinalBreakpoints.put(id, result);
                handler.onSuccess(result);
            } else {
                handler.onError(GctBundle.getString("clouddebug.no.response"));
            }
        } catch (IOException ex) {
            LOG.warn("IOException hydrating a snapshot.  User may have deleted the snapshot", ex);
            handler.onError(ex.toString());
        }
    });
}
Also used : Debugger(com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger) Breakpoint(com.google.api.services.clouddebugger.v2.model.Breakpoint) IOException(java.io.IOException) GetBreakpointResponse(com.google.api.services.clouddebugger.v2.model.GetBreakpointResponse)

Example 13 with Debugger

use of com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudDebugProcessStateController method queryServerForBreakpoints.

private List<Breakpoint> queryServerForBreakpoints(CloudDebugProcessState state, Debugger client, String tokenToSend) throws IOException {
    List<Breakpoint> currentList = null;
    String responseWaitToken = tokenToSend;
    while (tokenToSend == null || tokenToSend.equals(responseWaitToken)) {
        if (tokenToSend != null && !isBackgroundListening()) {
            return null;
        }
        ListBreakpointsResponse response = client.debuggees().breakpoints().list(state.getDebuggeeId()).setIncludeInactive(Boolean.TRUE).setActionValue("CAPTURE").setStripResults(Boolean.TRUE).setWaitToken(CloudDebugConfigType.useWaitToken() ? tokenToSend : null).setClientVersion(ServiceManager.getService(PluginInfoService.class).getClientVersionForCloudDebugger()).execute();
        // up the background watcher.
        if (tokenToSend != null && !isBackgroundListening()) {
            return null;
        }
        currentList = response.getBreakpoints();
        responseWaitToken = response.getNextWaitToken();
        if (tokenToSend == null) {
            break;
        }
        if (!CloudDebugConfigType.useWaitToken() && tokenToSend.equals(responseWaitToken)) {
            try {
                // our fallback polling mode has a 1 second loop.
                Thread.currentThread().sleep(1000);
            } catch (InterruptedException ex) {
                return null;
            }
        }
    }
    state.setWaitToken(responseWaitToken);
    if (currentList != null) {
        Collections.sort(currentList, BreakpointComparer.getDefaultInstance());
    }
    state.setCurrentServerBreakpointList(currentList != null ? ContainerUtil.immutableList(currentList) : ContainerUtil.immutableList(new ArrayList<>()));
    return currentList;
}
Also used : Breakpoint(com.google.api.services.clouddebugger.v2.model.Breakpoint) ListBreakpointsResponse(com.google.api.services.clouddebugger.v2.model.ListBreakpointsResponse) PluginInfoService(com.google.cloud.tools.intellij.service.PluginInfoService)

Example 14 with Debugger

use of com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudDebuggerClient method getClient.

/**
 * Returns a cloud debugger connection given a user email to indicate the credentials to use. The
 * function may return null if the user is not logged in.
 */
@Nullable
private static Debugger getClient(@Nullable final String userEmail, final int timeout) {
    if (Strings.isNullOrEmpty(userEmail)) {
        LOG.warn("unexpected null email in controller initialize.");
        return null;
    }
    final String hashkey = userEmail + timeout;
    Debugger cloudDebuggerClient = debuggerClientsFromUserEmail.get(hashkey);
    if (cloudDebuggerClient == null) {
        try {
            final CredentialedUser user = Services.getLoginService().getAllUsers().get(userEmail);
            final Credential credential = (user != null ? user.getCredential() : null);
            if (credential != null) {
                user.getGoogleLoginState().addLoginListener(new LoginListener() {

                    @Override
                    public void statusChanged(boolean login) {
                        if (!login) {
                            // aggressively remove the cached item on any status change.
                            debuggerClientsFromUserEmail.remove(hashkey);
                        } else {
                        // NOPMD
                        // user logged in, should we do something?
                        }
                    }
                });
                HttpRequestInitializer initializer = new HttpRequestInitializer() {

                    @Override
                    public void initialize(HttpRequest httpRequest) throws IOException {
                        HttpHeaders headers = new HttpHeaders();
                        httpRequest.setConnectTimeout(timeout);
                        httpRequest.setReadTimeout(timeout);
                        httpRequest.setHeaders(headers);
                        credential.initialize(httpRequest);
                    }
                };
                HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
                String userAgent = ServiceManager.getService(PluginInfoService.class).getUserAgent();
                cloudDebuggerClient = new Builder(httpTransport, JSON_FACTORY, initializer).setRootUrl(ROOT_URL).setApplicationName(userAgent).build().debugger();
            }
        } catch (IOException ex) {
            LOG.warn("Error connecting to Cloud Debugger API", ex);
        } catch (GeneralSecurityException ex) {
            LOG.warn("Error connecting to Cloud Debugger API", ex);
        }
        if (cloudDebuggerClient != null) {
            debuggerClientsFromUserEmail.put(hashkey, cloudDebuggerClient);
        }
    }
    return cloudDebuggerClient;
}
Also used : Debugger(com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger) HttpRequest(com.google.api.client.http.HttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) Credential(com.google.api.client.auth.oauth2.Credential) Builder(com.google.api.services.clouddebugger.v2.Clouddebugger.Builder) GeneralSecurityException(java.security.GeneralSecurityException) PluginInfoService(com.google.cloud.tools.intellij.service.PluginInfoService) IOException(java.io.IOException) HttpTransport(com.google.api.client.http.HttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) LoginListener(com.google.gdt.eclipse.login.common.LoginListener) CredentialedUser(com.google.cloud.tools.intellij.login.CredentialedUser) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer) Nullable(org.jetbrains.annotations.Nullable)

Example 15 with Debugger

use of com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudDebugProcessStateTest method createMockClient.

private Debugger createMockClient(final List<Breakpoint> returnedBreakpoints) throws IOException {
    Debugger client = Mockito.mock(Debugger.class);
    Debugger.Debuggees debuggees = Mockito.mock(Debugger.Debuggees.class);
    Debugger.Debuggees.Breakpoints breakpoints = Mockito.mock(Debugger.Debuggees.Breakpoints.class);
    Debugger.Debuggees.Breakpoints.List list = Mockito.mock(Debugger.Debuggees.Breakpoints.List.class);
    when(client.debuggees()).thenReturn(debuggees);
    when(debuggees.breakpoints()).thenReturn(breakpoints);
    when(breakpoints.list(DEBUGEE_ID)).thenReturn(list);
    when(list.setIncludeInactive(Boolean.TRUE)).thenReturn(list);
    when(list.setActionValue("CAPTURE")).thenReturn(list);
    when(list.setStripResults(Boolean.TRUE)).thenReturn(list);
    when(list.setWaitToken(null)).thenReturn(list);
    when(list.setClientVersion(ServiceManager.getService(PluginInfoService.class).getClientVersionForCloudDebugger())).thenReturn(list);
    when(list.execute()).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            ListBreakpointsResponse response = new ListBreakpointsResponse();
            List<Breakpoint> copy = new ArrayList<Breakpoint>(returnedBreakpoints);
            response.setBreakpoints(copy);
            return response;
        }
    });
    return client;
}
Also used : Debugger(com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger) Breakpoint(com.google.api.services.clouddebugger.v2.model.Breakpoint) ListBreakpointsResponse(com.google.api.services.clouddebugger.v2.model.ListBreakpointsResponse) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Debugger (com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger)12 IOException (java.io.IOException)10 Breakpoint (com.google.api.services.clouddebugger.v2.model.Breakpoint)9 PluginInfoService (com.google.cloud.tools.intellij.service.PluginInfoService)5 Debuggee (com.google.api.services.clouddebugger.v2.model.Debuggee)4 ListBreakpointsResponse (com.google.api.services.clouddebugger.v2.model.ListBreakpointsResponse)3 ArrayList (java.util.ArrayList)3 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)2 HttpHeaders (com.google.api.client.http.HttpHeaders)2 HttpRequest (com.google.api.client.http.HttpRequest)2 Debuggees (com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger.Debuggees)2 Breakpoints (com.google.api.services.clouddebugger.v2.Clouddebugger.Debugger.Debuggees.Breakpoints)2 ListDebuggeesResponse (com.google.api.services.clouddebugger.v2.model.ListDebuggeesResponse)2 RegisterDebuggeeRequest (com.google.api.services.clouddebugger.v2.model.RegisterDebuggeeRequest)2 RegisterDebuggeeResponse (com.google.api.services.clouddebugger.v2.model.RegisterDebuggeeResponse)2 SourceLocation (com.google.api.services.clouddebugger.v2.model.SourceLocation)2 SocketTimeoutException (java.net.SocketTimeoutException)2 Test (org.junit.Test)2 Credential (com.google.api.client.auth.oauth2.Credential)1 GoogleNetHttpTransport (com.google.api.client.googleapis.javanet.GoogleNetHttpTransport)1