Skip to content

Commit 13bf32d

Browse files
committed
Extract an interface for ScannerEngineFacade
1 parent fe7830b commit 13bf32d

File tree

8 files changed

+140
-58
lines changed

8 files changed

+140
-58
lines changed

lib/src/main/java/org/sonarsource/scanner/lib/ScannerEngineFacade.java

Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -19,62 +19,34 @@
1919
*/
2020
package org.sonarsource.scanner.lib;
2121

22-
import java.util.HashMap;
2322
import java.util.Map;
24-
import javax.annotation.Nullable;
25-
import org.sonarsource.scanner.lib.internal.facade.forked.JreCacheHit;
2623

27-
public abstract class ScannerEngineFacade implements AutoCloseable {
2824

29-
private final Map<String, String> bootstrapProperties;
30-
private final boolean isSonarCloud;
31-
private final String serverVersion;
32-
private final boolean wasEngineCacheHit;
33-
private final JreCacheHit wasJreCacheHit;
25+
public interface ScannerEngineFacade extends AutoCloseable {
3426

35-
protected ScannerEngineFacade(Map<String, String> bootstrapProperties, boolean isSonarCloud, @Nullable String serverVersion,
36-
boolean wasEngineCacheHit, @Nullable JreCacheHit wasJreCacheHit) {
37-
this.bootstrapProperties = bootstrapProperties;
38-
this.isSonarCloud = isSonarCloud;
39-
this.serverVersion = serverVersion;
40-
this.wasEngineCacheHit = wasEngineCacheHit;
41-
this.wasJreCacheHit = wasJreCacheHit;
42-
}
27+
/**
28+
* Get the properties that will be passed to the bootstrapped scanner engine.
29+
*/
30+
Map<String, String> getBootstrapProperties();
4331

44-
public String getServerVersion() {
45-
if (isSonarCloud) {
46-
throw new UnsupportedOperationException("Server version is not available for SonarCloud.");
47-
}
48-
return serverVersion;
49-
}
32+
/**
33+
* Get the version of the SonarQube Server that the scanner is connected to. Don't call this method if the scanner is connected to SonarQube Cloud.
34+
*
35+
* @return the version of the SonarQube Server
36+
* @throws UnsupportedOperationException if the scanner is connected to SonarQube Cloud
37+
*/
38+
String getServerVersion();
5039

51-
public boolean isSonarCloud() {
52-
return isSonarCloud;
53-
}
40+
/**
41+
* @return true if the scanner is connected to SonarQube Cloud, false otherwise
42+
*/
43+
boolean isSonarCloud();
5444

55-
public boolean analyze(Map<String, String> analysisProps) {
56-
Map<String, String> allProps = new HashMap<>();
57-
allProps.putAll(bootstrapProperties);
58-
allProps.putAll(analysisProps);
59-
initAnalysisProperties(allProps);
60-
addStatsProperties(allProps);
61-
return doAnalyze(allProps);
62-
}
6345

64-
private void addStatsProperties(Map<String, String> allProps) {
65-
if (wasJreCacheHit != null) {
66-
allProps.put("sonar.scanner.wasJreCacheHit", wasJreCacheHit.name());
67-
}
68-
allProps.put("sonar.scanner.wasEngineCacheHit", String.valueOf(wasEngineCacheHit));
69-
}
70-
71-
protected abstract boolean doAnalyze(Map<String, String> allProps);
72-
73-
private static void initAnalysisProperties(Map<String, String> p) {
74-
new Dirs().init(p);
75-
}
76-
77-
public Map<String, String> getBootstrapProperties() {
78-
return bootstrapProperties;
79-
}
46+
/**
47+
* Run the analysis. In case of failure, a log message should have been emitted.
48+
*
49+
* @return true if the analysis succeeded, false otherwise.
50+
*/
51+
boolean analyze(Map<String, String> analysisProps);
8052
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* SonarScanner Java Library
3+
* Copyright (C) 2011-2024 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonarsource.scanner.lib.internal.facade;
21+
22+
import java.util.HashMap;
23+
import java.util.Map;
24+
import javax.annotation.Nullable;
25+
import org.sonarsource.scanner.lib.ScannerEngineFacade;
26+
import org.sonarsource.scanner.lib.internal.facade.forked.JreCacheHit;
27+
28+
public abstract class AbstractScannerEngineFacade implements ScannerEngineFacade {
29+
30+
private final Map<String, String> bootstrapProperties;
31+
private final boolean isSonarCloud;
32+
private final String serverVersion;
33+
private final boolean wasEngineCacheHit;
34+
private final JreCacheHit wasJreCacheHit;
35+
36+
protected AbstractScannerEngineFacade(Map<String, String> bootstrapProperties, boolean isSonarCloud, @Nullable String serverVersion,
37+
boolean wasEngineCacheHit, @Nullable JreCacheHit wasJreCacheHit) {
38+
this.bootstrapProperties = bootstrapProperties;
39+
this.isSonarCloud = isSonarCloud;
40+
this.serverVersion = serverVersion;
41+
this.wasEngineCacheHit = wasEngineCacheHit;
42+
this.wasJreCacheHit = wasJreCacheHit;
43+
}
44+
45+
@Override
46+
public String getServerVersion() {
47+
if (isSonarCloud) {
48+
throw new UnsupportedOperationException("Server version is not available for SonarCloud.");
49+
}
50+
return serverVersion;
51+
}
52+
53+
@Override
54+
public boolean isSonarCloud() {
55+
return isSonarCloud;
56+
}
57+
58+
@Override
59+
public boolean analyze(Map<String, String> analysisProps) {
60+
Map<String, String> allProps = new HashMap<>();
61+
allProps.putAll(bootstrapProperties);
62+
allProps.putAll(analysisProps);
63+
initAnalysisProperties(allProps);
64+
addStatsProperties(allProps);
65+
return doAnalyze(allProps);
66+
}
67+
68+
private void addStatsProperties(Map<String, String> allProps) {
69+
if (wasJreCacheHit != null) {
70+
allProps.put("sonar.scanner.wasJreCacheHit", wasJreCacheHit.name());
71+
}
72+
allProps.put("sonar.scanner.wasEngineCacheHit", String.valueOf(wasEngineCacheHit));
73+
}
74+
75+
protected abstract boolean doAnalyze(Map<String, String> allProps);
76+
77+
private static void initAnalysisProperties(Map<String, String> p) {
78+
new Dirs().init(p);
79+
}
80+
81+
@Override
82+
public Map<String, String> getBootstrapProperties() {
83+
return bootstrapProperties;
84+
}
85+
}

lib/src/main/java/org/sonarsource/scanner/lib/Dirs.java renamed to lib/src/main/java/org/sonarsource/scanner/lib/internal/facade/Dirs.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* along with this program; if not, write to the Free Software Foundation,
1818
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1919
*/
20-
package org.sonarsource.scanner.lib;
20+
package org.sonarsource.scanner.lib.internal.facade;
2121

2222
import java.nio.file.Files;
2323
import java.nio.file.Path;
@@ -26,6 +26,8 @@
2626
import java.util.Optional;
2727
import org.slf4j.Logger;
2828
import org.slf4j.LoggerFactory;
29+
import org.sonarsource.scanner.lib.AnalysisProperties;
30+
import org.sonarsource.scanner.lib.ScannerProperties;
2931

3032
class Dirs {
3133

lib/src/main/java/org/sonarsource/scanner/lib/internal/facade/forked/NewScannerEngineFacade.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121

2222
import java.util.Map;
2323
import javax.annotation.Nullable;
24-
import org.sonarsource.scanner.lib.ScannerEngineFacade;
24+
import org.sonarsource.scanner.lib.internal.facade.AbstractScannerEngineFacade;
2525

26-
public class NewScannerEngineFacade extends ScannerEngineFacade {
26+
public class NewScannerEngineFacade extends AbstractScannerEngineFacade {
2727
private final ScannerEngineLauncher launcher;
2828

2929
public NewScannerEngineFacade(Map<String, String> bootstrapProperties, ScannerEngineLauncher launcher,

lib/src/main/java/org/sonarsource/scanner/lib/internal/facade/inprocess/InProcessScannerEngineFacade.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121

2222
import java.util.Map;
2323
import javax.annotation.Nullable;
24-
import org.sonarsource.scanner.lib.ScannerEngineFacade;
24+
import org.sonarsource.scanner.lib.internal.facade.AbstractScannerEngineFacade;
2525

26-
public class InProcessScannerEngineFacade extends ScannerEngineFacade {
26+
public class InProcessScannerEngineFacade extends AbstractScannerEngineFacade {
2727

2828
private final IsolatedLauncherFactory.IsolatedLauncherAndClassloader launcherAndCl;
2929

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* SonarScanner Java Library
3+
* Copyright (C) 2011-2024 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
@javax.annotation.ParametersAreNonnullByDefault
21+
package org.sonarsource.scanner.lib.internal.facade;

lib/src/main/java/org/sonarsource/scanner/lib/internal/facade/simulation/SimulationScannerEngineFacade.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
import javax.annotation.Nullable;
3232
import org.slf4j.Logger;
3333
import org.slf4j.LoggerFactory;
34-
import org.sonarsource.scanner.lib.ScannerEngineFacade;
3534
import org.sonarsource.scanner.lib.internal.InternalProperties;
35+
import org.sonarsource.scanner.lib.internal.facade.AbstractScannerEngineFacade;
3636

37-
public class SimulationScannerEngineFacade extends ScannerEngineFacade {
37+
public class SimulationScannerEngineFacade extends AbstractScannerEngineFacade {
3838

3939
private static final Logger LOG = LoggerFactory.getLogger(SimulationScannerEngineFacade.class);
4040

lib/src/test/java/org/sonarsource/scanner/lib/DirsTest.java renamed to lib/src/test/java/org/sonarsource/scanner/lib/internal/facade/DirsTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
* along with this program; if not, write to the Free Software Foundation,
1818
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1919
*/
20-
package org.sonarsource.scanner.lib;
20+
package org.sonarsource.scanner.lib.internal.facade;
2121

2222
import java.io.File;
2323
import java.util.HashMap;
2424
import java.util.Map;
2525
import org.junit.jupiter.api.Test;
2626
import org.junit.jupiter.api.io.TempDir;
27+
import org.sonarsource.scanner.lib.AnalysisProperties;
28+
import org.sonarsource.scanner.lib.ScannerProperties;
2729

2830
import static org.assertj.core.api.Assertions.assertThat;
2931

0 commit comments

Comments
 (0)