Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* SonarLint Language Server
* Copyright (C) 2009-2020 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonarsource.sonarlint.ls.settings;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.eclipse.lsp4j.ConfigurationItem;
import org.eclipse.lsp4j.ConfigurationParams;
import org.eclipse.lsp4j.services.LanguageClient;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonarsource.sonarlint.ls.Utils;
import org.sonarsource.sonarlint.ls.folders.WorkspaceFolderLifecycleListener;
import org.sonarsource.sonarlint.ls.folders.WorkspaceFolderWrapper;
import org.sonarsource.sonarlint.ls.folders.WorkspaceFoldersManager;
import static java.util.Arrays.stream;
import static org.apache.commons.lang.StringUtils.defaultIfBlank;
import static org.apache.commons.lang.StringUtils.isBlank;
import static org.sonarsource.sonarlint.ls.Utils.interrupted;
public class SettingsManager implements WorkspaceFolderLifecycleListener {
private static final String ORGANIZATION_KEY = "organizationKey";
private static final String DISABLE_NOTIFICATIONS = "disableNotifications";
private static final String PROJECT = "project";
private static final String DEFAULT_CONNECTION_ID = "";
private static final String SERVER_URL = "serverUrl";
private static final String SERVER_ID = "serverId";
private static final String TOKEN = "token";
private static final String CONNECTION_ID = "connectionId";
private static final String SONARLINT_CONFIGURATION_NAMESPACE = "sonarlint";
private static final String DISABLE_TELEMETRY = "disableTelemetry";
private static final String RULES = "rules";
private static final String TEST_FILE_PATTERN = "testFilePattern";
private static final String ANALYZER_PROPERTIES = "analyzerProperties";
private static final String OUTPUT = "output";
private static final String SHOW_ANALYZER_LOGS = "showAnalyzerLogs";
private static final String SHOW_VERBOSE_LOGS = "showVerboseLogs";
private static final String PATH_TO_NODE_EXECUTABLE = "pathToNodeExecutable";
private static final Logger LOG = Loggers.get(SettingsManager.class);
private final LanguageClient client;
private final WorkspaceFoldersManager foldersManager;
private WorkspaceSettings currentSettings = null;
private final CountDownLatch initLatch = new CountDownLatch(1);
// Setting that are normally specific per workspace folder, but we also keep a cache of global values to analyze files outside any
// workspace
private WorkspaceFolderSettings currentDefaultSettings = null;
private final ExecutorService executor;
private final List globalListeners = new ArrayList<>();
private final List folderListeners = new ArrayList<>();
public SettingsManager(LanguageClient client, WorkspaceFoldersManager foldersManager) {
this.client = client;
this.foldersManager = foldersManager;
this.executor = Executors.newCachedThreadPool(Utils.threadFactory("SonarLint settings manager", false));
}
/**
* Get workspace level settings, waiting for them to be initialized
*/
public WorkspaceSettings getCurrentSettings() {
try {
if (initLatch.await(1, TimeUnit.MINUTES)) {
return currentSettings;
}
} catch (InterruptedException e) {
interrupted(e);
}
throw new IllegalStateException("Unable to get settings in time");
}
/**
* Get default workspace folder level settings, waiting for them to be initialized
*/
public WorkspaceFolderSettings getCurrentDefaultFolderSettings() {
try {
if (initLatch.await(1, TimeUnit.MINUTES)) {
return currentDefaultSettings;
}
} catch (InterruptedException e) {
interrupted(e);
}
throw new IllegalStateException("Unable to get settings in time");
}
public void didChangeConfiguration() {
executor.execute(() -> {
try {
Map workspaceSettingsMap = requestSonarLintConfigurationAsync(null).get(1, TimeUnit.MINUTES);
WorkspaceSettings newWorkspaceSettings = parseSettings(workspaceSettingsMap);
WorkspaceSettings oldWorkspaceSettings = currentSettings;
this.currentSettings = newWorkspaceSettings;
WorkspaceFolderSettings newDefaultFolderSettings = parseFolderSettings(workspaceSettingsMap);
WorkspaceFolderSettings oldDefaultFolderSettings = currentDefaultSettings;
this.currentDefaultSettings = newDefaultFolderSettings;
initLatch.countDown();
foldersManager.getAll().forEach(f -> updateWorkspaceFolderSettings(f, true));
notifyListeners(newWorkspaceSettings, oldWorkspaceSettings, newDefaultFolderSettings, oldDefaultFolderSettings);
} catch (InterruptedException e) {
interrupted(e);
} catch (Exception e) {
LOG.error("Unable to update configuration", e);
}
});
}
private void notifyListeners(WorkspaceSettings newWorkspaceSettings, WorkspaceSettings oldWorkspaceSettings, WorkspaceFolderSettings newDefaultFolderSettings,
WorkspaceFolderSettings oldDefaultFolderSettings) {
if (!Objects.equals(oldWorkspaceSettings, newWorkspaceSettings)) {
LOG.debug("Global settings updated: {}", newWorkspaceSettings);
globalListeners.forEach(l -> l.onChange(oldWorkspaceSettings, newWorkspaceSettings));
}
if (!Objects.equals(oldDefaultFolderSettings, newDefaultFolderSettings)) {
LOG.debug("Default settings updated: {}", newDefaultFolderSettings);
folderListeners.forEach(l -> l.onChange(null, oldDefaultFolderSettings, newDefaultFolderSettings));
}
}
// Visible for testing
CompletableFuture