public class CeJvmOptions extends JvmOptions<CeJvmOptions> {
- public CeJvmOptions(File tmpDir, JavaVersion javaVersion) {
- super(mandatoryOptions(tmpDir, javaVersion));
+ public CeJvmOptions(File tmpDir) {
+ super(mandatoryOptions(tmpDir));
}
- private static Map<String, String> mandatoryOptions(File tmpDir, JavaVersion javaVersion) {
+ private static Map<String, String> mandatoryOptions(File tmpDir) {
Map<String, String> res = new LinkedHashMap<>(3);
res.put("-Djava.awt.headless=", "true");
res.put("-Dfile.encoding=", "UTF-8");
res.put("-Djava.io.tmpdir=", tmpDir.getAbsolutePath());
- if (javaVersion.isAtLeastJava11()) {
- // avoid illegal reflective access operations done by MyBatis
- res.put("--add-opens=java.base/java.util=ALL-UNNAMED", "");
- }
+ // avoid illegal reflective access operations done by MyBatis
+ res.put("--add-opens=java.base/java.util=ALL-UNNAMED", "");
+
return res;
}
}
private final Props props;
private final File tempDir;
private final System2 system2;
- private final JavaVersion javaVersion;
- public CommandFactoryImpl(Props props, File tempDir, System2 system2, JavaVersion javaVersion) {
+ public CommandFactoryImpl(Props props, File tempDir, System2 system2) {
this.props = props;
this.tempDir = tempDir;
this.system2 = system2;
- this.javaVersion = javaVersion;
String javaToolOptions = system2.getenv(ENV_VAR_JAVA_TOOL_OPTIONS);
if (javaToolOptions != null && !javaToolOptions.trim().isEmpty()) {
LoggerFactory.getLogger(CommandFactoryImpl.class)
public JavaCommand createWebCommand(boolean leader) {
File homeDir = props.nonNullValueAsFile(PATH_HOME.getKey());
- WebJvmOptions jvmOptions = new WebJvmOptions(tempDir, javaVersion)
+ WebJvmOptions jvmOptions = new WebJvmOptions(tempDir)
.addFromMandatoryProperty(props, WEB_JAVA_OPTS.getKey())
.addFromMandatoryProperty(props, WEB_JAVA_ADDITIONAL_OPTS.getKey());
addProxyJvmOptions(jvmOptions);
public JavaCommand createCeCommand() {
File homeDir = props.nonNullValueAsFile(PATH_HOME.getKey());
- CeJvmOptions jvmOptions = new CeJvmOptions(tempDir, javaVersion)
+ CeJvmOptions jvmOptions = new CeJvmOptions(tempDir)
.addFromMandatoryProperty(props, CE_JAVA_OPTS.getKey())
.addFromMandatoryProperty(props, CE_JAVA_ADDITIONAL_OPTS.getKey());
addProxyJvmOptions(jvmOptions);
import java.util.Map;
import java.util.stream.Collectors;
import org.sonar.process.Props;
-import org.sonar.process.System2;
public class EsJvmOptions extends JvmOptions<EsJvmOptions> {
private static final String ELASTICSEARCH_JVM_OPTIONS_HEADER = "# This file has been automatically generated by SonarQube during startup.\n" +
"\n";
public EsJvmOptions(Props props, File tmpDir) {
- this(System2.INSTANCE, props, tmpDir);
- }
-
- EsJvmOptions(System2 system2, Props props, File tmpDir) {
- super(mandatoryOptions(system2, props, tmpDir));
+ super(mandatoryOptions(tmpDir, props));
}
// this basically writes down the content of jvm.options file distributed in vanilla Elasticsearch package
// with some changes to fit running bundled in SQ
- private static Map<String, String> mandatoryOptions(System2 system2, Props props, File tmpDir) {
+ private static Map<String, String> mandatoryOptions(File tmpDir, Props props) {
Map<String, String> res = new LinkedHashMap<>(30);
// GC configuration
res.put("-XX:+UseConcMarkSweepGC", "");
// res.put("8:-XX:GCLogFileSize", "64m");
// JDK 9+ GC logging
// res.put("9-:-Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m", "");
- // due to internationalization enhancements in JDK 9 Elasticsearch need to set the provider to COMPAT otherwise
- // time/date parsing will break in an incompatible way for some date patterns and locals
- if (system2.isJava9()) {
- res.put("-Djava.locale.providers=", "COMPAT");
- }
-
- if (system2.isJava10()) {
- // temporary workaround for C2 bug with JDK 10 on hardware with AVX-512
- res.put("-XX:UseAVX=", "2");
- }
if (!props.value("sonar.jdbc.url", "").contains("jdbc:h2") && !props.valueAsBoolean("sonar.es.bootstrap.checks.disable")) {
res.put("-Des.enforce.bootstrap.checks=", "true");
import java.util.Map;
public class WebJvmOptions extends JvmOptions<WebJvmOptions> {
- public WebJvmOptions(File tmpDir, JavaVersion javaVersion) {
- super(mandatoryOptions(tmpDir, javaVersion));
+ public WebJvmOptions(File tmpDir) {
+ super(mandatoryOptions(tmpDir));
}
- private static Map<String, String> mandatoryOptions(File tmpDir, JavaVersion javaVersion) {
+ private static Map<String, String> mandatoryOptions(File tmpDir) {
Map<String, String> res = new LinkedHashMap<>(3);
res.put("-Djava.awt.headless=", "true");
res.put("-Dfile.encoding=", "UTF-8");
res.put("-Djava.io.tmpdir=", tmpDir.getAbsolutePath());
- if (javaVersion.isAtLeastJava11()) {
- // avoid illegal reflective access operations done by MyBatis
- res.put("--add-opens=java.base/java.util=ALL-UNNAMED", "");
+ // avoid illegal reflective access operations done by MyBatis
+ res.put("--add-opens=java.base/java.util=ALL-UNNAMED", "");
+
+ // avoid illegal reflective access operations done by Tomcat
+ res.put("--add-opens=java.base/java.lang=ALL-UNNAMED", "");
+ res.put("--add-opens=java.base/java.io=ALL-UNNAMED", "");
+ res.put("--add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED", "");
- // avoid illegal reflective access operations done by Tomcat
- res.put("--add-opens=java.base/java.lang=ALL-UNNAMED", "");
- res.put("--add-opens=java.base/java.io=ALL-UNNAMED", "");
- res.put("--add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED", "");
- }
return res;
}
}
import org.junit.rules.TemporaryFolder;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
public class CeJvmOptionsTest {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
private File tmpDir;
- private JavaVersion javaVersion = mock(JavaVersion.class);
private CeJvmOptions underTest;
@Before
public void setUp() throws IOException {
tmpDir = temporaryFolder.newFolder();
- underTest = new CeJvmOptions(tmpDir, javaVersion);
+ underTest = new CeJvmOptions(tmpDir);
}
@Test
- public void constructor_sets_mandatory_JVM_options_before_java11() throws IOException {
- when(javaVersion.isAtLeastJava11()).thenReturn(false);
-
- @Test
- public void constructor_sets_mandatory_JVM_options_before_java11() {
- when(javaVersion.isAtLeastJava11()).thenReturn(false);
- underTest = new CeJvmOptions(tmpDir, javaVersion);
- assertThat(underTest.getAll()).containsExactly(
- "-Djava.awt.headless=true", "-Dfile.encoding=UTF-8", "-Djava.io.tmpdir=" + tmpDir.getAbsolutePath());
- }
-
- @Test
- public void constructor_sets_mandatory_JVM_options_for_java11() throws IOException {
- when(javaVersion.isAtLeastJava11()).thenReturn(true);
-
+ public void constructor_sets_mandatory_JVM_options() {
assertThat(underTest.getAll()).containsExactly(
"-Djava.awt.headless=true", "-Dfile.encoding=UTF-8", "-Djava.io.tmpdir=" + tmpDir.getAbsolutePath(),
"--add-opens=java.base/java.util=ALL-UNNAMED");
public TemporaryFolder temp = new TemporaryFolder();
private System2 system2 = Mockito.mock(System2.class);
- private JavaVersion javaVersion = Mockito.mock(JavaVersion.class);
private File homeDir;
private File tempDir;
private File logsDir;
public void constructor_logs_no_warning_if_env_variable_JAVA_TOOL_OPTIONS_is_not_set() {
attachMemoryAppenderToLoggerOf(CommandFactoryImpl.class);
- new CommandFactoryImpl(new Props(new Properties()), tempDir, system2, javaVersion);
+ new CommandFactoryImpl(new Props(new Properties()), tempDir, system2);
assertThat(listAppender.getLogs()).isEmpty();
}
when(system2.getenv("JAVA_TOOL_OPTIONS")).thenReturn("sds");
attachMemoryAppenderToLoggerOf(CommandFactoryImpl.class);
- new CommandFactoryImpl(new Props(new Properties()), tempDir, system2, javaVersion);
+ new CommandFactoryImpl(new Props(new Properties()), tempDir, system2);
assertThat(listAppender.getLogs())
.extracting(ILoggingEvent::getMessage)
when(system2.getenv("ES_JAVA_OPTS")).thenReturn("xyz");
attachMemoryAppenderToLoggerOf(CommandFactoryImpl.class);
- new CommandFactoryImpl(new Props(new Properties()), tempDir, system2, javaVersion);
+ new CommandFactoryImpl(new Props(new Properties()), tempDir, system2);
assertThat(listAppender.getLogs())
.extracting(ILoggingEvent::getMessage)
ServiceLoaderWrapper serviceLoaderWrapper = mock(ServiceLoaderWrapper.class);
when(serviceLoaderWrapper.load()).thenReturn(ImmutableSet.of());
new ProcessProperties(serviceLoaderWrapper).completeDefaults(props);
- return new CommandFactoryImpl(props, tempDir, system2, javaVersion);
+ return new CommandFactoryImpl(props, tempDir, system2);
}
private <T> void attachMemoryAppenderToLoggerOf(Class<T> loggerClass) {
*/
package org.sonar.application.command;
-import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
-import com.tngtech.java.junit.dataprovider.UseDataProvider;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.sonar.process.Props;
-import org.sonar.process.System2;
import org.sonar.test.ExceptionCauseMatcher;
import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
@RunWith(DataProviderRunner.class)
public class EsJvmOptionsTest {
private Properties properties = new Properties();
@Test
- @UseDataProvider("java8or11")
- public void constructor_sets_mandatory_JVM_options_on_Java_8_and_11(System2 system2) throws IOException {
+ public void constructor_sets_mandatory_JVM_options_on_Java_11() throws IOException {
File tmpDir = temporaryFolder.newFolder();
- EsJvmOptions underTest = new EsJvmOptions(system2, new Props(properties), tmpDir);
+ EsJvmOptions underTest = new EsJvmOptions(new Props(properties), tmpDir);
assertThat(underTest.getAll())
.containsExactly(
}
@Test
- @UseDataProvider("java8or11")
- public void constructor_does_not_force_boostrap_checks_if_sonarqube_property_is_true(System2 system2) throws IOException {
+ public void constructor_does_not_force_boostrap_checks_if_sonarqube_property_is_true() throws IOException {
properties.put("sonar.es.bootstrap.checks.disable", "true");
File tmpDir = temporaryFolder.newFolder();
- EsJvmOptions underTest = new EsJvmOptions(system2, new Props(properties), tmpDir);
+ EsJvmOptions underTest = new EsJvmOptions(new Props(properties), tmpDir);
assertThat(underTest.getAll())
.doesNotContain("-Des.enforce.bootstrap.checks=true");
}
@Test
- @UseDataProvider("java8or11")
- public void constructor_forces_boostrap_checks_if_jdbc_url_property_does_not_exist(System2 system2) throws IOException {
+ public void constructor_forces_boostrap_checks_if_jdbc_url_property_does_not_exist() throws IOException {
File tmpDir = temporaryFolder.newFolder();
- EsJvmOptions underTest = new EsJvmOptions(system2, new Props(properties), tmpDir);
+ EsJvmOptions underTest = new EsJvmOptions(new Props(properties), tmpDir);
assertThat(underTest.getAll())
.contains("-Des.enforce.bootstrap.checks=true");
}
@Test
- @UseDataProvider("java8or11")
- public void constructor_forces_boostrap_checks_if_jdbc_url_property_is_not_h2(System2 system2) throws IOException {
+ public void constructor_forces_boostrap_checks_if_jdbc_url_property_is_not_h2() throws IOException {
properties.put("sonar.jdbc.url", randomAlphanumeric(53));
File tmpDir = temporaryFolder.newFolder();
- EsJvmOptions underTest = new EsJvmOptions(system2, new Props(properties), tmpDir);
+ EsJvmOptions underTest = new EsJvmOptions(new Props(properties), tmpDir);
assertThat(underTest.getAll())
.contains("-Des.enforce.bootstrap.checks=true");
}
@Test
- @UseDataProvider("java8or11")
- public void constructor_does_not_force_boostrap_checks_if_jdbc_url_property_contains_h2(System2 system2) throws IOException {
+ public void constructor_does_not_force_boostrap_checks_if_jdbc_url_property_contains_h2() throws IOException {
properties.put("sonar.jdbc.url", "jdbc:h2:tcp://ffoo:bar/sonar");
File tmpDir = temporaryFolder.newFolder();
- EsJvmOptions underTest = new EsJvmOptions(system2, new Props(properties), tmpDir);
+ EsJvmOptions underTest = new EsJvmOptions(new Props(properties), tmpDir);
assertThat(underTest.getAll())
.doesNotContain("-Des.enforce.bootstrap.checks=true");
}
- @DataProvider
- public static Object[][] java8or11() {
- System2 java8 = mock(System2.class);
- when(java8.isJava9()).thenReturn(false);
- when(java8.isJava10()).thenReturn(false);
- System2 java10 = mock(System2.class);
- when(java10.isJava9()).thenReturn(false);
- when(java10.isJava10()).thenReturn(false);
- return new Object[][] {
- {java8},
- {java10}
- };
- }
-
- @Test
- public void constructor_sets_mandatory_JVM_options_on_Java_9() throws IOException {
- System2 java9 = mock(System2.class);
- when(java9.isJava9()).thenReturn(true);
- when(java9.isJava10()).thenReturn(false);
-
- File tmpDir = temporaryFolder.newFolder();
- EsJvmOptions underTest = new EsJvmOptions(java9, new Props(properties), tmpDir);
-
- assertThat(underTest.getAll())
- .containsExactly(
- "-XX:+UseConcMarkSweepGC",
- "-XX:CMSInitiatingOccupancyFraction=75",
- "-XX:+UseCMSInitiatingOccupancyOnly",
- "-Des.networkaddress.cache.ttl=60",
- "-Des.networkaddress.cache.negative.ttl=10",
- "-XX:+AlwaysPreTouch",
- "-Xss1m",
- "-Djava.awt.headless=true",
- "-Dfile.encoding=UTF-8",
- "-Djna.nosys=true",
- "-XX:-OmitStackTraceInFastThrow",
- "-Dio.netty.noUnsafe=true",
- "-Dio.netty.noKeySetOptimization=true",
- "-Dio.netty.recycler.maxCapacityPerThread=0",
- "-Dlog4j.shutdownHookEnabled=false",
- "-Dlog4j2.disable.jmx=true",
- "-Djava.io.tmpdir=" + tmpDir.getAbsolutePath(),
- "-XX:ErrorFile=../logs/es_hs_err_pid%p.log",
- "-Djava.locale.providers=COMPAT",
- "-Des.enforce.bootstrap.checks=true");
- }
-
- @Test
- public void constructor_sets_mandatory_JVM_options_on_Java_10() throws IOException {
- System2 java10 = mock(System2.class);
- when(java10.isJava9()).thenReturn(false);
- when(java10.isJava10()).thenReturn(true);
-
- File tmpDir = temporaryFolder.newFolder();
- EsJvmOptions underTest = new EsJvmOptions(java10, new Props(properties), tmpDir);
-
- assertThat(underTest.getAll())
- .containsExactly(
- "-XX:+UseConcMarkSweepGC",
- "-XX:CMSInitiatingOccupancyFraction=75",
- "-XX:+UseCMSInitiatingOccupancyOnly",
- "-Des.networkaddress.cache.ttl=60",
- "-Des.networkaddress.cache.negative.ttl=10",
- "-XX:+AlwaysPreTouch",
- "-Xss1m",
- "-Djava.awt.headless=true",
- "-Dfile.encoding=UTF-8",
- "-Djna.nosys=true",
- "-XX:-OmitStackTraceInFastThrow",
- "-Dio.netty.noUnsafe=true",
- "-Dio.netty.noKeySetOptimization=true",
- "-Dio.netty.recycler.maxCapacityPerThread=0",
- "-Dlog4j.shutdownHookEnabled=false",
- "-Dlog4j2.disable.jmx=true",
- "-Djava.io.tmpdir=" + tmpDir.getAbsolutePath(),
- "-XX:ErrorFile=../logs/es_hs_err_pid%p.log",
- "-XX:UseAVX=2",
- "-Des.enforce.bootstrap.checks=true");
- }
-
/**
* This test may fail if SQ's test are not executed with target Java version 8.
*/
import org.junit.rules.TemporaryFolder;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
public class WebJvmOptionsTest {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
private File tmpDir;
- private JavaVersion javaVersion = mock(JavaVersion.class);
private WebJvmOptions underTest;
@Before
public void setUp() throws IOException {
tmpDir = temporaryFolder.newFolder();
- underTest = new WebJvmOptions(tmpDir, javaVersion);
+ underTest = new WebJvmOptions(tmpDir);
}
@Test
- public void constructor_sets_mandatory_JVM_options_before_java11() throws IOException {
- when(javaVersion.isAtLeastJava11()).thenReturn(false);
-
- @Test
- public void constructor_sets_mandatory_JVM_options_before_java11() {
- when(javaVersion.isAtLeastJava11()).thenReturn(false);
- underTest = new WebJvmOptions(tmpDir, javaVersion);
- assertThat(underTest.getAll()).containsExactly(
- "-Djava.awt.headless=true", "-Dfile.encoding=UTF-8", "-Djava.io.tmpdir=" + tmpDir.getAbsolutePath());
- }
-
- @Test
- public void constructor_sets_mandatory_JVM_options_for_java11() throws IOException {
- when(javaVersion.isAtLeastJava11()).thenReturn(true);
-
+ public void constructor_sets_mandatory_JVM_options() {
assertThat(underTest.getAll()).containsExactly(
"-Djava.awt.headless=true", "-Dfile.encoding=UTF-8", "-Djava.io.tmpdir=" + tmpDir.getAbsolutePath(),
"--add-opens=java.base/java.util=ALL-UNNAMED",
"--add-opens=java.base/java.io=ALL-UNNAMED",
"--add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED");
}
-
}
public boolean isOsWindows() {
return SystemUtils.IS_OS_WINDOWS;
}
-
- public boolean isJava9() {
- return SystemUtils.JAVA_VERSION != null && SystemUtils.JAVA_VERSION.startsWith("9");
- }
-
- public boolean isJava10() {
- return SystemUtils.JAVA_VERSION != null && SystemUtils.JAVA_VERSION.startsWith("10");
- }
};
/**
* True if this is MS Windows.
*/
boolean isOsWindows();
-
- /**
- * True is current Java version is Java 9.
- */
- boolean isJava9();
-
- /**
- * True is current Java version is Java 10.
- */
- boolean isJava10();
}
package org.sonar.application;
import java.io.IOException;
-import org.slf4j.LoggerFactory;
import org.sonar.api.SonarEdition;
import org.sonar.api.internal.MetadataLoader;
import org.sonar.application.command.CommandFactory;
appState.registerClusterName(settings.getProps().nonNullValue(CLUSTER_NAME.getKey()));
AppReloader appReloader = new AppReloaderImpl(settingsLoader, fileSystem, appState, logging);
fileSystem.reset();
- CommandFactory commandFactory = new CommandFactoryImpl(settings.getProps(), fileSystem.getTempDir(), System2.INSTANCE, JavaVersion.INSTANCE);
+ CommandFactory commandFactory = new CommandFactoryImpl(settings.getProps(), fileSystem.getTempDir(), System2.INSTANCE);
try (ProcessLauncher processLauncher = new ProcessLauncherImpl(fileSystem.getTempDir())) {
Scheduler scheduler = new SchedulerImpl(settings, appReloader, commandFactory, processLauncher, appState);
}
if (!javaVersion.isAtLeastJava11()) {
- LoggerFactory.getLogger(this.getClass()).warn("SonarQube will require Java 11+ starting on next version");
+ throw new IllegalStateException("SonarQube requires Java 11+ to run");
}
}
try {
String.class.getMethod("isBlank");
} catch (NoSuchMethodException e) {
- LOG.warn("SonarQube scanners will require Java 11+ starting on next version");
+ throw new IllegalStateException("SonarScanner requires Java 11+ to run");
}
}
/**
* Connect to any SonarQube server available through HTTP or HTTPS.
- * <p>TLS 1.0, 1.1 and 1.2 are supported on both Java 7 and 8. SSLv3 is not supported.</p>
* <p>The JVM system proxies are used.</p>
*/
public class HttpConnector implements WsConnector {