Skip to content

Commit

Permalink
Scanner cleanup
Browse files Browse the repository at this point in the history
* Fail fast when a WS call is made in medium test mode
* Rename many Batch* -> Scanner*
  • Loading branch information
henryju committed Jan 9, 2017
1 parent a21a43d commit 3a78e2d
Show file tree
Hide file tree
Showing 80 changed files with 283 additions and 340 deletions.
Expand Up @@ -136,7 +136,7 @@ public Batch executeTask(Map<String, String> analysisProperties, IssueListener i


private void checkStarted() { private void checkStarted() {
if (!started) { if (!started) {
throw new IllegalStateException("Batch is not started. Unable to execute task."); throw new IllegalStateException("Scanner engine is not started. Unable to execute task.");
} }
} }


Expand Down
Expand Up @@ -26,7 +26,6 @@
import org.sonar.api.CoreProperties; import org.sonar.api.CoreProperties;
import org.sonar.scanner.bootstrap.AbstractAnalysisMode; import org.sonar.scanner.bootstrap.AbstractAnalysisMode;
import org.sonar.scanner.bootstrap.GlobalProperties; import org.sonar.scanner.bootstrap.GlobalProperties;
import org.sonar.scanner.mediumtest.FakePluginInstaller;


/** /**
* @since 4.0 * @since 4.0
Expand All @@ -36,17 +35,12 @@ public class DefaultAnalysisMode extends AbstractAnalysisMode {
private static final Logger LOG = LoggerFactory.getLogger(DefaultAnalysisMode.class); private static final Logger LOG = LoggerFactory.getLogger(DefaultAnalysisMode.class);
private static final String KEY_SCAN_ALL = "sonar.scanAllFiles"; private static final String KEY_SCAN_ALL = "sonar.scanAllFiles";


private boolean mediumTestMode;
private boolean scanAllFiles; private boolean scanAllFiles;


public DefaultAnalysisMode(GlobalProperties globalProps, AnalysisProperties props) { public DefaultAnalysisMode(GlobalProperties globalProps, AnalysisProperties props) {
init(globalProps.properties(), props.properties()); init(globalProps.properties(), props.properties());
} }


public boolean isMediumTest() {
return mediumTestMode;
}

public boolean scanAllFiles() { public boolean scanAllFiles() {
return scanAllFiles; return scanAllFiles;
} }
Expand All @@ -67,7 +61,7 @@ private void load(Map<String, String> globalProps, Map<String, String> analysisP
String mode = getPropertyWithFallback(analysisProps, globalProps, CoreProperties.ANALYSIS_MODE); String mode = getPropertyWithFallback(analysisProps, globalProps, CoreProperties.ANALYSIS_MODE);
validate(mode); validate(mode);
issues = CoreProperties.ANALYSIS_MODE_ISSUES.equals(mode) || CoreProperties.ANALYSIS_MODE_PREVIEW.equals(mode); issues = CoreProperties.ANALYSIS_MODE_ISSUES.equals(mode) || CoreProperties.ANALYSIS_MODE_PREVIEW.equals(mode);
mediumTestMode = "true".equals(getPropertyWithFallback(analysisProps, globalProps, FakePluginInstaller.MEDIUM_TEST_ENABLED)); mediumTestMode = "true".equals(getPropertyWithFallback(analysisProps, globalProps, MEDIUM_TEST_ENABLED));
String scanAllStr = getPropertyWithFallback(analysisProps, globalProps, KEY_SCAN_ALL); String scanAllStr = getPropertyWithFallback(analysisProps, globalProps, KEY_SCAN_ALL);
scanAllFiles = !issues || "true".equals(scanAllStr); scanAllFiles = !issues || "true".equals(scanAllStr);
} }
Expand Down
Expand Up @@ -19,18 +19,19 @@
*/ */
package org.sonar.scanner.bootstrap; package org.sonar.scanner.bootstrap;


import java.util.Arrays;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.sonar.api.CoreProperties; import org.sonar.api.CoreProperties;

import java.util.Arrays;

import org.sonar.api.batch.AnalysisMode; import org.sonar.api.batch.AnalysisMode;


public abstract class AbstractAnalysisMode implements AnalysisMode { public abstract class AbstractAnalysisMode implements AnalysisMode {

private static final String[] VALID_MODES = {CoreProperties.ANALYSIS_MODE_PREVIEW, CoreProperties.ANALYSIS_MODE_PUBLISH, CoreProperties.ANALYSIS_MODE_ISSUES}; private static final String[] VALID_MODES = {CoreProperties.ANALYSIS_MODE_PREVIEW, CoreProperties.ANALYSIS_MODE_PUBLISH, CoreProperties.ANALYSIS_MODE_ISSUES};
public static final String MEDIUM_TEST_ENABLED = "sonar.mediumTest.enabled";


protected boolean preview; protected boolean preview;
protected boolean issues; protected boolean issues;
protected boolean mediumTestMode;


protected AbstractAnalysisMode() { protected AbstractAnalysisMode() {
} }
Expand All @@ -50,6 +51,10 @@ public boolean isPublish() {
return !preview && !issues; return !preview && !issues;
} }


public boolean isMediumTest() {
return mediumTestMode;
}

protected static void validate(String mode) { protected static void validate(String mode) {
if (StringUtils.isEmpty(mode)) { if (StringUtils.isEmpty(mode)) {
return; return;
Expand Down
Expand Up @@ -73,18 +73,18 @@ private void addBootstrapComponents() {
Version apiVersion = ApiVersion.load(System2.INSTANCE); Version apiVersion = ApiVersion.load(System2.INSTANCE);
add( add(
// plugins // plugins
BatchPluginRepository.class, ScannerPluginRepository.class,
PluginLoader.class, PluginLoader.class,
PluginClassloaderFactory.class, PluginClassloaderFactory.class,
BatchPluginJarExploder.class, ScannerPluginJarExploder.class,
BatchPluginPredicate.class, ScannerPluginPredicate.class,
ExtensionInstaller.class, ExtensionInstaller.class,


new SonarQubeVersion(apiVersion), new SonarQubeVersion(apiVersion),
SonarRuntimeImpl.forSonarQube(apiVersion, SonarQubeSide.SCANNER), SonarRuntimeImpl.forSonarQube(apiVersion, SonarQubeSide.SCANNER),
StoragesManager.class, StoragesManager.class,
GlobalSettings.class, GlobalSettings.class,
new BatchWsClientProvider(), new ScannerWsClientProvider(),
DefaultServer.class, DefaultServer.class,
new GlobalTempFolderProvider(), new GlobalTempFolderProvider(),
DefaultHttpDownloader.class, DefaultHttpDownloader.class,
Expand All @@ -93,7 +93,7 @@ private void addBootstrapComponents() {
System2.INSTANCE, System2.INSTANCE,
new GlobalRepositoriesProvider(), new GlobalRepositoriesProvider(),
UuidFactoryImpl.INSTANCE); UuidFactoryImpl.INSTANCE);
addIfMissing(BatchPluginInstaller.class, PluginInstaller.class); addIfMissing(ScannerPluginInstaller.class, PluginInstaller.class);
addIfMissing(DefaultGlobalRepositoriesLoader.class, GlobalRepositoriesLoader.class); addIfMissing(DefaultGlobalRepositoriesLoader.class, GlobalRepositoriesLoader.class);
} }


Expand Down
Expand Up @@ -30,13 +30,17 @@ public GlobalMode(GlobalProperties props) {
String mode = props.property(CoreProperties.ANALYSIS_MODE); String mode = props.property(CoreProperties.ANALYSIS_MODE);
validate(mode); validate(mode);
issues = CoreProperties.ANALYSIS_MODE_ISSUES.equals(mode) || CoreProperties.ANALYSIS_MODE_PREVIEW.equals(mode); issues = CoreProperties.ANALYSIS_MODE_ISSUES.equals(mode) || CoreProperties.ANALYSIS_MODE_PREVIEW.equals(mode);

mediumTestMode = "true".equals(props.property(MEDIUM_TEST_ENABLED));
if (preview) { if (preview) {
LOG.debug("Preview global mode"); LOG.debug("Preview global mode");
} else if (issues) { } else if (issues) {
LOG.debug("Issues global mode"); LOG.debug("Issues global mode");
} else { } else {
LOG.debug("Publish global mode"); LOG.debug("Publish global mode");
} }
if (mediumTestMode) {
LOG.info("Medium test mode");
}
} }

} }
Expand Up @@ -36,7 +36,7 @@ public interface PluginInstaller {


/** /**
* Used only by tests. * Used only by tests.
* @see org.sonar.scanner.mediumtest.BatchMediumTester * @see org.sonar.scanner.mediumtest.ScannerMediumTester
*/ */
Map<String, Plugin> installLocals(); Map<String, Plugin> installLocals();
} }
Expand Up @@ -53,15 +53,15 @@
/** /**
* @since 2.6 * @since 2.6
*/ */
public class BatchExtensionDictionnary { public class ScannerExtensionDictionnary {


private final ComponentContainer componentContainer; private final ComponentContainer componentContainer;
private final SensorContext sensorContext; private final SensorContext sensorContext;
private final SensorOptimizer sensorOptimizer; private final SensorOptimizer sensorOptimizer;
private final PostJobContext postJobContext; private final PostJobContext postJobContext;
private final PostJobOptimizer postJobOptimizer; private final PostJobOptimizer postJobOptimizer;


public BatchExtensionDictionnary(ComponentContainer componentContainer, DefaultSensorContext sensorContext, SensorOptimizer sensorOptimizer, PostJobContext postJobContext, public ScannerExtensionDictionnary(ComponentContainer componentContainer, DefaultSensorContext sensorContext, SensorOptimizer sensorOptimizer, PostJobContext postJobContext,
PostJobOptimizer postJobOptimizer) { PostJobOptimizer postJobOptimizer) {
this.componentContainer = componentContainer; this.componentContainer = componentContainer;
this.sensorContext = sensorContext; this.sensorContext = sensorContext;
Expand Down
Expand Up @@ -50,16 +50,16 @@
* Downloads the plugins installed on server and stores them in a local user cache * Downloads the plugins installed on server and stores them in a local user cache
* (see {@link FileCacheProvider}). * (see {@link FileCacheProvider}).
*/ */
public class BatchPluginInstaller implements PluginInstaller { public class ScannerPluginInstaller implements PluginInstaller {


private static final Logger LOG = Loggers.get(BatchPluginInstaller.class); private static final Logger LOG = Loggers.get(ScannerPluginInstaller.class);
private static final String PLUGINS_INDEX_URL = "/deploy/plugins/index.txt"; private static final String PLUGINS_INDEX_URL = "/deploy/plugins/index.txt";


private final FileCache fileCache; private final FileCache fileCache;
private final BatchPluginPredicate pluginPredicate; private final ScannerPluginPredicate pluginPredicate;
private final BatchWsClient wsClient; private final ScannerWsClient wsClient;


public BatchPluginInstaller(BatchWsClient wsClient, FileCache fileCache, BatchPluginPredicate pluginPredicate) { public ScannerPluginInstaller(ScannerWsClient wsClient, FileCache fileCache, ScannerPluginPredicate pluginPredicate) {
this.fileCache = fileCache; this.fileCache = fileCache;
this.pluginPredicate = pluginPredicate; this.pluginPredicate = pluginPredicate;
this.wsClient = wsClient; this.wsClient = wsClient;
Expand Down Expand Up @@ -89,7 +89,7 @@ private Map<String, PluginInfo> loadPlugins(List<RemotePlugin> remotePlugins) {


/** /**
* Returns empty on purpose. This method is used only by tests. * Returns empty on purpose. This method is used only by tests.
* @see org.sonar.scanner.mediumtest.BatchMediumTester * @see org.sonar.scanner.mediumtest.ScannerMediumTester
*/ */
@Override @Override
public Map<String, Plugin> installLocals() { public Map<String, Plugin> installLocals() {
Expand Down
Expand Up @@ -34,11 +34,11 @@
import static org.sonar.core.util.FileUtils.deleteQuietly; import static org.sonar.core.util.FileUtils.deleteQuietly;


@ScannerSide @ScannerSide
public class BatchPluginJarExploder extends PluginJarExploder { public class ScannerPluginJarExploder extends PluginJarExploder {


private final FileCache fileCache; private final FileCache fileCache;


public BatchPluginJarExploder(FileCache fileCache) { public ScannerPluginJarExploder(FileCache fileCache) {
this.fileCache = fileCache; this.fileCache = fileCache;
} }


Expand Down
Expand Up @@ -41,9 +41,9 @@
* Filters the plugins to be enabled during analysis * Filters the plugins to be enabled during analysis
*/ */
@ScannerSide @ScannerSide
public class BatchPluginPredicate implements Predicate<String> { public class ScannerPluginPredicate implements Predicate<String> {


private static final Logger LOG = Loggers.get(BatchPluginPredicate.class); private static final Logger LOG = Loggers.get(ScannerPluginPredicate.class);


private static final String BUILDBREAKER_PLUGIN_KEY = "buildbreaker"; private static final String BUILDBREAKER_PLUGIN_KEY = "buildbreaker";
private static final Joiner COMMA_JOINER = Joiner.on(", "); private static final Joiner COMMA_JOINER = Joiner.on(", ");
Expand All @@ -52,7 +52,7 @@ public class BatchPluginPredicate implements Predicate<String> {
private final Set<String> blacks = newHashSet(); private final Set<String> blacks = newHashSet();
private final GlobalMode mode; private final GlobalMode mode;


public BatchPluginPredicate(Settings settings, GlobalMode mode) { public ScannerPluginPredicate(Settings settings, GlobalMode mode) {
this.mode = mode; this.mode = mode;
if (mode.isPreview() || mode.isIssues()) { if (mode.isPreview() || mode.isIssues()) {
// These default values are not supported by Settings because the class CorePlugin // These default values are not supported by Settings because the class CorePlugin
Expand Down
Expand Up @@ -34,16 +34,16 @@
/** /**
* Orchestrates the installation and loading of plugins * Orchestrates the installation and loading of plugins
*/ */
public class BatchPluginRepository implements PluginRepository, Startable { public class ScannerPluginRepository implements PluginRepository, Startable {
private static final Logger LOG = Loggers.get(BatchPluginRepository.class); private static final Logger LOG = Loggers.get(ScannerPluginRepository.class);


private final PluginInstaller installer; private final PluginInstaller installer;
private final PluginLoader loader; private final PluginLoader loader;


private Map<String, Plugin> pluginInstancesByKeys; private Map<String, Plugin> pluginInstancesByKeys;
private Map<String, PluginInfo> infosByKeys; private Map<String, PluginInfo> infosByKeys;


public BatchPluginRepository(PluginInstaller installer, PluginLoader loader) { public ScannerPluginRepository(PluginInstaller installer, PluginLoader loader) {
this.installer = installer; this.installer = installer;
this.loader = loader; this.loader = loader;
} }
Expand Down
Expand Up @@ -21,6 +21,7 @@


import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
Expand All @@ -43,16 +44,18 @@
import static java.net.HttpURLConnection.HTTP_FORBIDDEN; import static java.net.HttpURLConnection.HTTP_FORBIDDEN;
import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED; import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;


public class BatchWsClient { public class ScannerWsClient {


private static final Logger LOG = Loggers.get(BatchWsClient.class); private static final Logger LOG = Loggers.get(ScannerWsClient.class);


private final WsClient target; private final WsClient target;
private final boolean hasCredentials; private final boolean hasCredentials;
private final GlobalMode globalMode;


public BatchWsClient(WsClient target, boolean hasCredentials) { public ScannerWsClient(WsClient target, boolean hasCredentials, GlobalMode globalMode) {
this.target = target; this.target = target;
this.hasCredentials = hasCredentials; this.hasCredentials = hasCredentials;
this.globalMode = globalMode;
} }


/** /**
Expand All @@ -65,6 +68,7 @@ public BatchWsClient(WsClient target, boolean hasCredentials) {
* @throws HttpException if the response code is not in range [200..300) * @throws HttpException if the response code is not in range [200..300)
*/ */
public WsResponse call(WsRequest request) { public WsResponse call(WsRequest request) {
Preconditions.checkState(!globalMode.isMediumTest(), "No WS call should be made in medium test mode");
Profiler profiler = Profiler.createIfDebug(LOG).start(); Profiler profiler = Profiler.createIfDebug(LOG).start();
WsResponse response = target.wsConnector().call(request); WsResponse response = target.wsConnector().call(request);
profiler.stopDebug(format("%s %d %s", request.getMethod(), response.code(), response.requestUrl())); profiler.stopDebug(format("%s %d %s", request.getMethod(), response.code(), response.requestUrl()));
Expand Down
Expand Up @@ -31,15 +31,15 @@
import static org.apache.commons.lang.StringUtils.defaultIfBlank; import static org.apache.commons.lang.StringUtils.defaultIfBlank;


@ScannerSide @ScannerSide
public class BatchWsClientProvider extends ProviderAdapter { public class ScannerWsClientProvider extends ProviderAdapter {


static final int CONNECT_TIMEOUT_MS = 5_000; static final int CONNECT_TIMEOUT_MS = 5_000;
static final String READ_TIMEOUT_SEC_PROPERTY = "sonar.ws.timeout"; static final String READ_TIMEOUT_SEC_PROPERTY = "sonar.ws.timeout";
static final int DEFAULT_READ_TIMEOUT_SEC = 60; static final int DEFAULT_READ_TIMEOUT_SEC = 60;


private BatchWsClient wsClient; private ScannerWsClient wsClient;


public synchronized BatchWsClient provide(final GlobalProperties settings, final EnvironmentInformation env) { public synchronized ScannerWsClient provide(final GlobalProperties settings, final EnvironmentInformation env, GlobalMode globalMode) {
if (wsClient == null) { if (wsClient == null) {
String url = defaultIfBlank(settings.property("sonar.host.url"), CoreProperties.SERVER_BASE_URL_DEFAULT_VALUE); String url = defaultIfBlank(settings.property("sonar.host.url"), CoreProperties.SERVER_BASE_URL_DEFAULT_VALUE);
HttpConnector.Builder connectorBuilder = HttpConnector.newBuilder(); HttpConnector.Builder connectorBuilder = HttpConnector.newBuilder();
Expand All @@ -59,7 +59,7 @@ public synchronized BatchWsClient provide(final GlobalProperties settings, final
connectorBuilder.proxyCredentials(proxyUser, System.getProperty("http.proxyPassword")); connectorBuilder.proxyCredentials(proxyUser, System.getProperty("http.proxyPassword"));
} }


wsClient = new BatchWsClient(WsClientFactories.getDefault().newClient(connectorBuilder.build()), login != null); wsClient = new ScannerWsClient(WsClientFactories.getDefault().newClient(connectorBuilder.build()), login != null, globalMode);
} }
return wsClient; return wsClient;
} }
Expand Down

This file was deleted.

Expand Up @@ -29,13 +29,13 @@
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.sonar.api.utils.log.Loggers; import org.sonar.api.utils.log.Loggers;
import org.sonar.api.utils.log.Profiler; import org.sonar.api.utils.log.Profiler;
import org.sonar.scanner.bootstrap.BatchWsClient; import org.sonar.scanner.bootstrap.ScannerWsClient;
import org.sonar.scanner.util.BatchUtils; import org.sonar.scanner.util.BatchUtils;


public class DefaultServerLineHashesLoader implements ServerLineHashesLoader { public class DefaultServerLineHashesLoader implements ServerLineHashesLoader {
private BatchWsClient wsClient; private ScannerWsClient wsClient;


public DefaultServerLineHashesLoader(BatchWsClient wsClient) { public DefaultServerLineHashesLoader(ScannerWsClient wsClient) {
this.wsClient = wsClient; this.wsClient = wsClient;
} }


Expand Down
Expand Up @@ -27,7 +27,6 @@
import org.sonar.scanner.bootstrap.PluginInstaller; import org.sonar.scanner.bootstrap.PluginInstaller;


public class FakePluginInstaller implements PluginInstaller { public class FakePluginInstaller implements PluginInstaller {
public static final String MEDIUM_TEST_ENABLED = "sonar.mediumTest.enabled";


private final Map<String, PluginInfo> infosByKeys = new HashMap<>(); private final Map<String, PluginInfo> infosByKeys = new HashMap<>();
private final Map<String, Plugin> instancesByKeys = new HashMap<>(); private final Map<String, Plugin> instancesByKeys = new HashMap<>();
Expand Down

0 comments on commit 3a78e2d

Please sign in to comment.