Skip to content

Commit 5e46ecd

Browse files
PR feedback
1 parent bffb584 commit 5e46ecd

27 files changed

+180
-205
lines changed

backend/core/src/main/java/org/sonarsource/sonarlint/core/ServerApiProvider.java renamed to backend/core/src/main/java/org/sonarsource/sonarlint/core/ConnectionManager.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
*/
2020
package org.sonarsource.sonarlint.core;
2121

22-
import com.google.common.annotations.VisibleForTesting;
2322
import java.net.URI;
2423
import java.util.Optional;
2524
import java.util.function.Consumer;
@@ -30,7 +29,6 @@
3029
import org.eclipse.lsp4j.jsonrpc.ResponseErrorException;
3130
import org.eclipse.lsp4j.jsonrpc.messages.ResponseError;
3231
import org.sonarsource.sonarlint.core.commons.log.SonarLintLogger;
33-
import org.sonarsource.sonarlint.core.connection.ConnectionManager;
3432
import org.sonarsource.sonarlint.core.connection.ServerConnection;
3533
import org.sonarsource.sonarlint.core.commons.progress.SonarLintCancelMonitor;
3634
import org.sonarsource.sonarlint.core.http.ConnectionAwareHttpClientProvider;
@@ -53,7 +51,7 @@
5351

5452
@Named
5553
@Singleton
56-
public class ServerApiProvider implements ConnectionManager {
54+
public class ConnectionManager {
5755

5856
private static final SonarLintLogger LOG = SonarLintLogger.get();
5957
private final ConnectionConfigurationRepository connectionRepository;
@@ -62,8 +60,8 @@ public class ServerApiProvider implements ConnectionManager {
6260
private final SonarLintRpcClient client;
6361
private final URI sonarCloudUri;
6462

65-
public ServerApiProvider(ConnectionConfigurationRepository connectionRepository, ConnectionAwareHttpClientProvider awareHttpClientProvider, HttpClientProvider httpClientProvider,
66-
SonarCloudActiveEnvironment sonarCloudActiveEnvironment, SonarLintRpcClient client) {
63+
public ConnectionManager(ConnectionConfigurationRepository connectionRepository, ConnectionAwareHttpClientProvider awareHttpClientProvider, HttpClientProvider httpClientProvider,
64+
SonarCloudActiveEnvironment sonarCloudActiveEnvironment, SonarLintRpcClient client) {
6765
this.connectionRepository = connectionRepository;
6866
this.awareHttpClientProvider = awareHttpClientProvider;
6967
this.httpClientProvider = httpClientProvider;
@@ -145,7 +143,6 @@ private HttpClient getClientFor(EndpointParams params, Either<TokenDto, Username
145143
userPass -> httpClientProvider.getHttpClientWithPreemptiveAuth(userPass.getUsername(), userPass.getPassword()));
146144
}
147145

148-
@Override
149146
public ServerConnection getConnectionOrThrow(String connectionId) {
150147
var serverApi = getServerApiOrThrow(connectionId);
151148
return new ServerConnection(connectionId, serverApi, client);
@@ -161,28 +158,27 @@ public Optional<ServerConnection> tryGetConnectionWithoutCredentials(String conn
161158
.map(serverApi -> new ServerConnection(connectionId, serverApi, client));
162159
}
163160

164-
@Override
165161
public ServerApi getTransientConnection(String token,@Nullable String organization, String baseUrl) {
166162
return getServerApi(baseUrl, organization, token);
167163
}
168164

169-
@Override
170165
public void withValidConnection(String connectionId, Consumer<ServerApi> serverApiConsumer) {
171166
getValidConnection(connectionId).ifPresent(connection -> connection.withClientApi(serverApiConsumer));
172167
}
173168

174-
@Override
175169
public <T> Optional<T> withValidConnectionAndReturn(String connectionId, Function<ServerApi, T> serverApiConsumer) {
176170
return getValidConnection(connectionId).map(connection -> connection.withClientApiAndReturn(serverApiConsumer));
177171
}
178172

179-
@Override
180173
public <T> Optional<T> withValidConnectionFlatMapOptionalAndReturn(String connectionId, Function<ServerApi, Optional<T>> serverApiConsumer) {
181174
return getValidConnection(connectionId).map(connection -> connection.withClientApiAndReturn(serverApiConsumer)).flatMap(Function.identity());
182175
}
183176

184-
@VisibleForTesting
185-
public Optional<ServerConnection> getValidConnection(String connectionId) {
186-
return tryGetConnection(connectionId).filter(ServerConnection::isValid);
177+
private Optional<ServerConnection> getValidConnection(String connectionId) {
178+
return tryGetConnection(connectionId).filter(ServerConnection::isValid)
179+
.or(() -> {
180+
LOG.debug("Connection '{}' is invalid", connectionId);
181+
return Optional.empty();
182+
});
187183
}
188184
}

backend/core/src/main/java/org/sonarsource/sonarlint/core/ConnectionService.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,23 @@ public class ConnectionService {
6363
private final ApplicationEventPublisher applicationEventPublisher;
6464
private final ConnectionConfigurationRepository repository;
6565
private final URI sonarCloudUri;
66-
private final ServerApiProvider serverApiProvider;
66+
private final ConnectionManager connectionManager;
6767
private final TokenGeneratorHelper tokenGeneratorHelper;
6868

6969
@Inject
7070
public ConnectionService(ApplicationEventPublisher applicationEventPublisher, ConnectionConfigurationRepository repository, InitializeParams params,
71-
SonarCloudActiveEnvironment sonarCloudActiveEnvironment, TokenGeneratorHelper tokenGeneratorHelper, ServerApiProvider serverApiProvider) {
72-
this(applicationEventPublisher, repository, params.getSonarQubeConnections(), params.getSonarCloudConnections(), sonarCloudActiveEnvironment, serverApiProvider,
71+
SonarCloudActiveEnvironment sonarCloudActiveEnvironment, TokenGeneratorHelper tokenGeneratorHelper, ConnectionManager connectionManager) {
72+
this(applicationEventPublisher, repository, params.getSonarQubeConnections(), params.getSonarCloudConnections(), sonarCloudActiveEnvironment, connectionManager,
7373
tokenGeneratorHelper);
7474
}
7575

7676
ConnectionService(ApplicationEventPublisher applicationEventPublisher, ConnectionConfigurationRepository repository,
7777
@Nullable List<SonarQubeConnectionConfigurationDto> initSonarQubeConnections, @Nullable List<SonarCloudConnectionConfigurationDto> initSonarCloudConnections,
78-
SonarCloudActiveEnvironment sonarCloudActiveEnvironment, ServerApiProvider serverApiProvider, TokenGeneratorHelper tokenGeneratorHelper) {
78+
SonarCloudActiveEnvironment sonarCloudActiveEnvironment, ConnectionManager connectionManager, TokenGeneratorHelper tokenGeneratorHelper) {
7979
this.applicationEventPublisher = applicationEventPublisher;
8080
this.repository = repository;
8181
this.sonarCloudUri = sonarCloudActiveEnvironment.getUri();
82-
this.serverApiProvider = serverApiProvider;
82+
this.connectionManager = connectionManager;
8383
this.tokenGeneratorHelper = tokenGeneratorHelper;
8484
if (initSonarQubeConnections != null) {
8585
initSonarQubeConnections.forEach(c -> repository.addOrReplace(adapt(c)));
@@ -157,7 +157,7 @@ private void updateConnection(AbstractConnectionConfiguration connectionConfigur
157157

158158
public ValidateConnectionResponse validateConnection(Either<TransientSonarQubeConnectionDto, TransientSonarCloudConnectionDto> transientConnection,
159159
SonarLintCancelMonitor cancelMonitor) {
160-
var serverApi = serverApiProvider.getForTransientConnection(transientConnection);
160+
var serverApi = connectionManager.getForTransientConnection(transientConnection);
161161
var serverChecker = new ServerVersionAndStatusChecker(serverApi);
162162
try {
163163
serverChecker.checkVersionAndStatus(cancelMonitor);
@@ -179,7 +179,7 @@ public ValidateConnectionResponse validateConnection(Either<TransientSonarQubeCo
179179

180180
public boolean checkSmartNotificationsSupported(Either<TransientSonarQubeConnectionDto, TransientSonarCloudConnectionDto> transientConnection,
181181
SonarLintCancelMonitor cancelMonitor) {
182-
var serverApi = serverApiProvider.getForTransientConnection(transientConnection);
182+
var serverApi = connectionManager.getForTransientConnection(transientConnection);
183183
var developersApi = serverApi.developers();
184184
return developersApi.isSupported(cancelMonitor);
185185
}
@@ -189,15 +189,15 @@ public HelpGenerateUserTokenResponse helpGenerateUserToken(String serverUrl, Son
189189
}
190190

191191
public List<SonarProjectDto> getAllProjects(Either<TransientSonarQubeConnectionDto, TransientSonarCloudConnectionDto> transientConnection, SonarLintCancelMonitor cancelMonitor) {
192-
var serverApi = serverApiProvider.getForTransientConnection(transientConnection);
192+
var serverApi = connectionManager.getForTransientConnection(transientConnection);
193193
return serverApi.component().getAllProjects(cancelMonitor)
194194
.stream().map(serverProject -> new SonarProjectDto(serverProject.getKey(), serverProject.getName()))
195195
.collect(Collectors.toList());
196196
}
197197

198198
public Map<String, String> getProjectNamesByKey(Either<TransientSonarQubeConnectionDto, TransientSonarCloudConnectionDto> transientConnection,
199199
List<String> projectKeys, SonarLintCancelMonitor cancelMonitor) {
200-
var serverApi = serverApiProvider.getForTransientConnection(transientConnection);
200+
var serverApi = connectionManager.getForTransientConnection(transientConnection);
201201
var projectNamesByKey = new HashMap<String, String>();
202202
projectKeys.forEach(key -> {
203203
var projectName = serverApi.component().getProject(key, cancelMonitor).map(ServerProject::getName).orElse(null);

backend/core/src/main/java/org/sonarsource/sonarlint/core/OrganizationsCache.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@
4747
public class OrganizationsCache {
4848

4949
private static final SonarLintLogger LOG = SonarLintLogger.get();
50-
private final ServerApiProvider serverApiProvider;
50+
private final ConnectionManager connectionManager;
5151

5252
private final Cache<Either<TokenDto, UsernamePasswordDto>, TextSearchIndex<OrganizationDto>> textSearchIndexCacheByCredentials = CacheBuilder.newBuilder()
5353
.expireAfterWrite(5, TimeUnit.MINUTES)
5454
.build();
5555

56-
public OrganizationsCache(ServerApiProvider serverApiProvider) {
57-
this.serverApiProvider = serverApiProvider;
56+
public OrganizationsCache(ConnectionManager connectionManager) {
57+
this.connectionManager = connectionManager;
5858
}
5959

6060
public List<OrganizationDto> fuzzySearchOrganizations(Either<TokenDto, UsernamePasswordDto> credentials, String searchText, SonarLintCancelMonitor cancelMonitor) {
@@ -74,7 +74,7 @@ public TextSearchIndex<OrganizationDto> getTextSearchIndex(Either<TokenDto, User
7474
LOG.debug("Load user organizations...");
7575
List<OrganizationDto> orgs;
7676
try {
77-
var serverApi = serverApiProvider.getForSonarCloudNoOrg(credentials);
77+
var serverApi = connectionManager.getForSonarCloudNoOrg(credentials);
7878
var serverOrganizations = serverApi.organization().listUserOrganizations(cancelMonitor);
7979
orgs = serverOrganizations.stream().map(o -> new OrganizationDto(o.getKey(), o.getName(), o.getDescription())).collect(Collectors.toList());
8080
} catch (Exception e) {
@@ -103,7 +103,7 @@ public List<OrganizationDto> listUserOrganizations(Either<TokenDto, UsernamePass
103103

104104
@CheckForNull
105105
public OrganizationDto getOrganization(Either<TokenDto, UsernamePasswordDto> credentials, String organizationKey, SonarLintCancelMonitor cancelMonitor) {
106-
var helper = serverApiProvider.getForSonarCloudNoOrg(credentials);
106+
var helper = connectionManager.getForSonarCloudNoOrg(credentials);
107107
var serverOrganization = helper.organization().getOrganization(organizationKey, cancelMonitor);
108108
return serverOrganization.map(o -> new OrganizationDto(o.getKey(), o.getName(), o.getDescription())).orElse(null);
109109
}

backend/core/src/main/java/org/sonarsource/sonarlint/core/SonarProjectsCache.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
public class SonarProjectsCache {
4747

4848
private static final SonarLintLogger LOG = SonarLintLogger.get();
49-
private final ServerApiProvider serverApiProvider;
49+
private final ConnectionManager connectionManager;
5050

5151
private final Cache<String, TextSearchIndex<ServerProject>> textSearchIndexCacheByConnectionId = CacheBuilder.newBuilder()
5252
.expireAfterWrite(1, TimeUnit.HOURS)
@@ -94,8 +94,8 @@ public int hashCode() {
9494
}
9595
}
9696

97-
public SonarProjectsCache(ServerApiProvider serverApiProvider) {
98-
this.serverApiProvider = serverApiProvider;
97+
public SonarProjectsCache(ConnectionManager connectionManager) {
98+
this.connectionManager = connectionManager;
9999
}
100100

101101
@EventListener
@@ -120,7 +120,7 @@ public Optional<ServerProject> getSonarProject(String connectionId, String sonar
120120
return singleProjectsCache.get(new SonarProjectKey(connectionId, sonarProjectKey), () -> {
121121
LOG.debug("Query project '{}' on connection '{}'...", sonarProjectKey, connectionId);
122122
try {
123-
return serverApiProvider.withValidConnectionAndReturn(connectionId,
123+
return connectionManager.withValidConnectionAndReturn(connectionId,
124124
s -> s.component().getProject(sonarProjectKey, cancelMonitor)).orElse(Optional.empty());
125125
} catch (Exception e) {
126126
LOG.error("Error while querying project '{}' from connection '{}'", sonarProjectKey, connectionId, e);
@@ -138,7 +138,7 @@ public TextSearchIndex<ServerProject> getTextSearchIndex(String connectionId, So
138138
LOG.debug("Load projects from connection '{}'...", connectionId);
139139
List<ServerProject> projects;
140140
try {
141-
projects = serverApiProvider.withValidConnectionAndReturn(connectionId,
141+
projects = connectionManager.withValidConnectionAndReturn(connectionId,
142142
s -> s.component().getAllProjects(cancelMonitor))
143143
.orElse(List.of());
144144
} catch (Exception e) {

backend/core/src/main/java/org/sonarsource/sonarlint/core/VersionSoonUnsupportedHelper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,17 @@ public class VersionSoonUnsupportedHelper {
5656
private final SonarLintRpcClient client;
5757
private final ConfigurationRepository configRepository;
5858
private final ConnectionConfigurationRepository connectionRepository;
59-
private final ServerApiProvider serverApiProvider;
59+
private final ConnectionManager connectionManager;
6060
private final SynchronizationService synchronizationService;
6161
private final Map<String, Version> cacheConnectionIdPerVersion = new ConcurrentHashMap<>();
6262
private final ExecutorServiceShutdownWatchable<?> executorService;
6363

64-
public VersionSoonUnsupportedHelper(SonarLintRpcClient client, ConfigurationRepository configRepository, ServerApiProvider serverApiProvider,
64+
public VersionSoonUnsupportedHelper(SonarLintRpcClient client, ConfigurationRepository configRepository, ConnectionManager connectionManager,
6565
ConnectionConfigurationRepository connectionRepository, SynchronizationService synchronizationService) {
6666
this.client = client;
6767
this.configRepository = configRepository;
6868
this.connectionRepository = connectionRepository;
69-
this.serverApiProvider = serverApiProvider;
69+
this.connectionManager = connectionManager;
7070
this.synchronizationService = synchronizationService;
7171
this.executorService = new ExecutorServiceShutdownWatchable<>(new ThreadPoolExecutor(0, 1, 10L, TimeUnit.SECONDS,
7272
new LinkedBlockingQueue<>(), r -> new Thread(r, "Version Soon Unsupported Helper")));
@@ -107,7 +107,7 @@ private void queueCheckIfSoonUnsupported(String connectionId, String configScope
107107
try {
108108
var connection = connectionRepository.getConnectionById(connectionId);
109109
if (connection != null && connection.getKind() == ConnectionKind.SONARQUBE) {
110-
serverApiProvider.tryGetConnectionWithoutCredentials(connectionId)
110+
connectionManager.tryGetConnectionWithoutCredentials(connectionId)
111111
.ifPresent(serverConnection -> serverConnection.withClientApi(serverApi -> {
112112
var version = synchronizationService.readOrSynchronizeServerVersion(connectionId, serverApi, cancelMonitor);
113113
var isCached = cacheConnectionIdPerVersion.containsKey(connectionId) && cacheConnectionIdPerVersion.get(connectionId).compareTo(version) == 0;

backend/core/src/main/java/org/sonarsource/sonarlint/core/connection/ConnectionManager.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

backend/core/src/main/java/org/sonarsource/sonarlint/core/embedded/server/ShowHotspotRequestHandler.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import org.apache.hc.core5.http.io.entity.StringEntity;
3636
import org.apache.hc.core5.http.protocol.HttpContext;
3737
import org.apache.hc.core5.net.URIBuilder;
38-
import org.sonarsource.sonarlint.core.ServerApiProvider;
38+
import org.sonarsource.sonarlint.core.ConnectionManager;
3939
import org.sonarsource.sonarlint.core.commons.api.TextRange;
4040
import org.sonarsource.sonarlint.core.commons.progress.SonarLintCancelMonitor;
4141
import org.sonarsource.sonarlint.core.file.FilePathTranslation;
@@ -57,15 +57,15 @@
5757
@Singleton
5858
public class ShowHotspotRequestHandler implements HttpRequestHandler {
5959
private final SonarLintRpcClient client;
60-
private final ServerApiProvider serverApiProvider;
60+
private final ConnectionManager connectionManager;
6161
private final TelemetryService telemetryService;
6262
private final RequestHandlerBindingAssistant requestHandlerBindingAssistant;
6363
private final PathTranslationService pathTranslationService;
6464

65-
public ShowHotspotRequestHandler(SonarLintRpcClient client, ServerApiProvider serverApiProvider, TelemetryService telemetryService,
65+
public ShowHotspotRequestHandler(SonarLintRpcClient client, ConnectionManager connectionManager, TelemetryService telemetryService,
6666
RequestHandlerBindingAssistant requestHandlerBindingAssistant, PathTranslationService pathTranslationService) {
6767
this.client = client;
68-
this.serverApiProvider = serverApiProvider;
68+
this.connectionManager = connectionManager;
6969
this.telemetryService = telemetryService;
7070
this.requestHandlerBindingAssistant = requestHandlerBindingAssistant;
7171
this.pathTranslationService = pathTranslationService;
@@ -105,7 +105,7 @@ private void showHotspotForScope(String connectionId, String configurationScopeI
105105
}
106106

107107
private Optional<ServerHotspotDetails> tryFetchHotspot(String connectionId, String hotspotKey, SonarLintCancelMonitor cancelMonitor) {
108-
return serverApiProvider.withValidConnectionFlatMapOptionalAndReturn(connectionId,api -> api.hotspot().fetch(hotspotKey, cancelMonitor));
108+
return connectionManager.withValidConnectionFlatMapOptionalAndReturn(connectionId, api -> api.hotspot().fetch(hotspotKey, cancelMonitor));
109109
}
110110

111111
private static HotspotDetailsDto adapt(String hotspotKey, ServerHotspotDetails hotspot, FilePathTranslation translation) {

0 commit comments

Comments
 (0)