From cf62a834433c8629932d678fb97a65bc9004a57e Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 14 Dec 2015 13:37:59 +0100 Subject: SONAR-7140 Do not check java.io.tmpdir in bootstrap process --- .../org/sonar/process/MinimumViableSystem.java | 35 ++++++++------------ .../org/sonar/process/MinimumViableSystemTest.java | 38 ++++++---------------- .../main/java/org/sonar/search/SearchServer.java | 4 ++- .../main/java/org/sonar/server/app/WebServer.java | 6 ++-- .../src/main/java/org/sonar/application/App.java | 2 +- 5 files changed, 31 insertions(+), 54 deletions(-) diff --git a/server/sonar-process/src/main/java/org/sonar/process/MinimumViableSystem.java b/server/sonar-process/src/main/java/org/sonar/process/MinimumViableSystem.java index fdbc7217611..9a166fdaed7 100644 --- a/server/sonar-process/src/main/java/org/sonar/process/MinimumViableSystem.java +++ b/server/sonar-process/src/main/java/org/sonar/process/MinimumViableSystem.java @@ -27,59 +27,50 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; -public class MinimumViableSystem { - - private final Map requiredJavaOptions = new HashMap<>(); - - public MinimumViableSystem setRequiredJavaOption(String propertyKey, String expectedValue) { - requiredJavaOptions.put(propertyKey, expectedValue); - return this; - } +import static java.lang.String.format; - /** - * Entry point for all checks - */ - public void check() { - checkJavaVersion(); - checkJavaOptions(); - checkWritableTempDir(); - } +public class MinimumViableSystem { /** * Verify that temp directory is writable */ - private void checkWritableTempDir() { + public MinimumViableSystem checkWritableTempDir() { checkWritableDir(System.getProperty("java.io.tmpdir")); + return this; } + // Visible for testing void checkWritableDir(String tempPath) { try { File tempFile = File.createTempFile("check", "tmp", new File(tempPath)); FileUtils.deleteQuietly(tempFile); } catch (IOException e) { - throw new IllegalStateException(String.format("Temp directory is not writable: %s", tempPath), e); + throw new IllegalStateException(format("Temp directory is not writable: %s", tempPath), e); } } - void checkJavaOptions() { + public MinimumViableSystem checkRequiredJavaOptions(Map requiredJavaOptions) { for (Map.Entry entry : requiredJavaOptions.entrySet()) { String value = System.getProperty(entry.getKey()); if (!StringUtils.equals(value, entry.getValue())) { - throw new MessageException(String.format( + throw new MessageException(format( "JVM option '%s' must be set to '%s'. Got '%s'", entry.getKey(), entry.getValue(), StringUtils.defaultString(value))); } } + return this; } - void checkJavaVersion() { + public MinimumViableSystem checkJavaVersion() { String javaVersion = System.getProperty("java.specification.version"); checkJavaVersion(javaVersion); + return this; } + // Visible for testing void checkJavaVersion(String javaVersion) { if (!javaVersion.startsWith("1.6") && !javaVersion.startsWith("1.7") && !javaVersion.startsWith("1.8")) { // still better than "java.lang.UnsupportedClassVersionError: Unsupported major.minor version 49.0 - throw new MessageException(String.format("Supported versions of Java are 1.6, 1.7 and 1.8. Got %s.", javaVersion)); + throw new MessageException(format("Supported versions of Java are 1.6, 1.7 and 1.8. Got %s.", javaVersion)); } } diff --git a/server/sonar-process/src/test/java/org/sonar/process/MinimumViableSystemTest.java b/server/sonar-process/src/test/java/org/sonar/process/MinimumViableSystemTest.java index 88e813f4fbb..a3b3f582ee7 100644 --- a/server/sonar-process/src/test/java/org/sonar/process/MinimumViableSystemTest.java +++ b/server/sonar-process/src/test/java/org/sonar/process/MinimumViableSystemTest.java @@ -19,6 +19,7 @@ */ package org.sonar.process; +import com.google.common.collect.ImmutableMap; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -33,32 +34,16 @@ public class MinimumViableSystemTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); - /** - * Verifies that all checks can be verified without error. - * Test environment does not necessarily follows all checks. - */ - @Test - public void check() { - MinimumViableSystem mve = new MinimumViableSystem(); - - try { - mve.check(); - // ok - } catch (MessageException e) { - // also ok. All other exceptions are errors. - } - } + MinimumViableSystem underTest = new MinimumViableSystem(); @Test public void checkJavaVersion() { - MinimumViableSystem mve = new MinimumViableSystem(); - // yes, sources are compiled with a supported Java version! - mve.checkJavaVersion(); - mve.checkJavaVersion("1.6"); + underTest.checkJavaVersion(); + underTest.checkJavaVersion("1.6"); try { - mve.checkJavaVersion("1.9"); + underTest.checkJavaVersion("1.9"); fail(); } catch (MessageException e) { assertThat(e).hasMessage("Supported versions of Java are 1.6, 1.7 and 1.8. Got 1.9."); @@ -66,34 +51,31 @@ public class MinimumViableSystemTest { } @Test - public void checkJavaOption() { + public void checkRequiredJavaOptions() { String key = "MinimumViableEnvironmentTest.test.prop"; - MinimumViableSystem mve = new MinimumViableSystem() - .setRequiredJavaOption(key, "true"); try { System.setProperty(key, "false"); - mve.checkJavaOptions(); + underTest.checkRequiredJavaOptions(ImmutableMap.of(key, "true")); fail(); } catch (MessageException e) { assertThat(e).hasMessage("JVM option '" + key + "' must be set to 'true'. Got 'false'"); } System.setProperty(key, "true"); - mve.checkJavaOptions(); // do not fail + underTest.checkRequiredJavaOptions(ImmutableMap.of(key, "true")); } @Test public void checkWritableTempDir() throws Exception { File dir = temp.newFolder(); - MinimumViableSystem mve = new MinimumViableSystem(); - mve.checkWritableDir(dir.getAbsolutePath()); + underTest.checkWritableDir(dir.getAbsolutePath()); dir.delete(); try { - mve.checkWritableDir(dir.getAbsolutePath()); + underTest.checkWritableDir(dir.getAbsolutePath()); fail(); } catch (IllegalStateException e) { assertThat(e).hasMessage("Temp directory is not writable: " + dir.getAbsolutePath()); diff --git a/server/sonar-search/src/main/java/org/sonar/search/SearchServer.java b/server/sonar-search/src/main/java/org/sonar/search/SearchServer.java index 13dde539c10..77db6b77a1d 100644 --- a/server/sonar-search/src/main/java/org/sonar/search/SearchServer.java +++ b/server/sonar-search/src/main/java/org/sonar/search/SearchServer.java @@ -34,7 +34,9 @@ public class SearchServer implements Monitored { public SearchServer(Props props) { this.settings = new SearchSettings(props); - new MinimumViableSystem().check(); + new MinimumViableSystem() + .checkJavaVersion() + .checkWritableTempDir(); } @Override diff --git a/server/sonar-server/src/main/java/org/sonar/server/app/WebServer.java b/server/sonar-server/src/main/java/org/sonar/server/app/WebServer.java index 2907102148f..9a997dcc302 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/app/WebServer.java +++ b/server/sonar-server/src/main/java/org/sonar/server/app/WebServer.java @@ -19,6 +19,7 @@ */ package org.sonar.server.app; +import com.google.common.collect.ImmutableMap; import org.sonar.process.MinimumViableSystem; import org.sonar.process.Monitored; import org.sonar.process.ProcessEntryPoint; @@ -30,8 +31,9 @@ public class WebServer implements Monitored { WebServer(Props props) throws Exception { new MinimumViableSystem() - .setRequiredJavaOption("file.encoding", "UTF-8") - .check(); + .checkJavaVersion() + .checkWritableTempDir() + .checkRequiredJavaOptions(ImmutableMap.of("file.encoding", "UTF-8")); this.tomcat = new EmbeddedTomcat(props); } diff --git a/sonar-application/src/main/java/org/sonar/application/App.java b/sonar-application/src/main/java/org/sonar/application/App.java index 9547d505895..8c6afac739a 100644 --- a/sonar-application/src/main/java/org/sonar/application/App.java +++ b/sonar-application/src/main/java/org/sonar/application/App.java @@ -109,7 +109,7 @@ public class App implements Stoppable { } public static void main(String[] args) throws Exception { - new MinimumViableSystem().check(); + new MinimumViableSystem().checkJavaVersion(); CommandLineParser cli = new CommandLineParser(); Properties rawProperties = cli.parseArguments(args); Props props = new PropsBuilder(rawProperties, new JdbcSettings()).build(); -- cgit v1.2.3