diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2019-05-09 09:33:29 +0200 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2019-06-03 20:21:22 +0200 |
commit | 77a4a2c15df8bfff0656b5fa84a64b5278c6c937 (patch) | |
tree | fda24aa1d3c22f2579632ea4e1c59360471c54f0 /sonar-shutdowner | |
parent | c8506cc161b5f71be32575e459d599ca320e2195 (diff) | |
download | sonarqube-77a4a2c15df8bfff0656b5fa84a64b5278c6c937.tar.gz sonarqube-77a4a2c15df8bfff0656b5fa84a64b5278c6c937.zip |
SONAR-12042 add force-stop option to Linux's sonar.sh
Diffstat (limited to 'sonar-shutdowner')
4 files changed, 269 insertions, 0 deletions
diff --git a/sonar-shutdowner/build.gradle b/sonar-shutdowner/build.gradle new file mode 100644 index 00000000000..230ee861851 --- /dev/null +++ b/sonar-shutdowner/build.gradle @@ -0,0 +1,22 @@ +sonarqube { + properties { + property 'sonar.projectName', "${projectTitle} :: Shutdowner" + } +} + +dependencies { + // please keep list ordered + compileOnly 'com.google.code.findbugs:jsr305' + + testCompile 'junit:junit' + testCompile 'org.assertj:assertj-core' + testCompile 'org.mockito:mockito-core' +} + +jar { + manifest { + attributes( + 'Main-Class': 'org.sonar.application.Shutdowner' + ) + } +} diff --git a/sonar-shutdowner/src/main/java/org/sonar/application/Shutdowner.java b/sonar-shutdowner/src/main/java/org/sonar/application/Shutdowner.java new file mode 100644 index 00000000000..a37e5446895 --- /dev/null +++ b/sonar-shutdowner/src/main/java/org/sonar/application/Shutdowner.java @@ -0,0 +1,102 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.sonar.application; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.RandomAccessFile; +import java.io.Reader; +import java.net.URISyntaxException; +import java.nio.MappedByteBuffer; +import java.nio.channels.FileChannel; +import java.util.Optional; +import java.util.Properties; + +import static java.nio.charset.StandardCharsets.UTF_8; + +public class Shutdowner { + public static void main(String[] args) { + try { + new Shutdowner().run(); + } catch (Exception e) { + System.err.println(e.getMessage()); + System.exit(1); + } + } + + void run() throws IOException { + File homeDir = detectHomeDir(); + Properties p = loadPropertiesFile(homeDir); + File tmpDir = resolveTempDir(p); + + askForHardStop(tmpDir); + } + + // assuming jar file is in directory SQ_HOME/lib/ + + static File detectHomeDir() { + try { + File appJar = new File(Shutdowner.class.getProtectionDomain().getCodeSource().getLocation().toURI()); + return appJar.getParentFile().getParentFile(); + } catch (URISyntaxException e) { + throw new IllegalStateException("Cannot detect path of shutdowner jar file", e); + } + } + + /** + * Loads the configuration file ${homeDir}/conf/sonar.properties. + * An empty {@link Properties} is returned if the file does not exist. + */ + static Properties loadPropertiesFile(File homeDir) { + Properties p = new Properties(); + File propsFile = new File(new File(homeDir, "conf"), "sonar.properties"); + if (propsFile.exists()) { + try (Reader reader = new InputStreamReader(new FileInputStream(propsFile), UTF_8)) { + p.load(reader); + return p; + } catch (IOException e) { + throw new IllegalStateException("Cannot open file " + propsFile, e); + } + } else { + throw new IllegalStateException("Configuration file not found: " + propsFile); + } + } + + static File resolveTempDir(Properties p) { + return new File(Optional.ofNullable(p.getProperty("sonar.path.temp")).orElse("temp")); + } + + static void askForHardStop(File tmpDir) throws IOException { + writeToShareMemory(tmpDir, 1, (byte) 0xFF); + } + + private static void writeToShareMemory(File tmpDir, int offset, byte value) throws IOException { + try (RandomAccessFile sharedMemory = new RandomAccessFile(new File(tmpDir, "sharedmemory"), "rw")) { + // Using values from org.sonar.process.ProcessCommands + MappedByteBuffer mappedByteBuffer = sharedMemory.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 50L * 10); + + // Now we are stopping all processes as quick as possible + // by asking for stop of "app" process + mappedByteBuffer.put(offset, value); + } + } +} diff --git a/sonar-shutdowner/src/main/java/org/sonar/application/package-info.java b/sonar-shutdowner/src/main/java/org/sonar/application/package-info.java new file mode 100644 index 00000000000..97a2444a3e9 --- /dev/null +++ b/sonar-shutdowner/src/main/java/org/sonar/application/package-info.java @@ -0,0 +1,23 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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. + */ +@ParametersAreNonnullByDefault +package org.sonar.application; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-shutdowner/src/test/java/org/sonar/application/ShutdownerTest.java b/sonar-shutdowner/src/test/java/org/sonar/application/ShutdownerTest.java new file mode 100644 index 00000000000..77462c365a4 --- /dev/null +++ b/sonar-shutdowner/src/test/java/org/sonar/application/ShutdownerTest.java @@ -0,0 +1,122 @@ +/* + * SonarQube + * Copyright (C) 2009-2019 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.sonar.application; + +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.MappedByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.file.Files; +import java.util.Arrays; +import java.util.Properties; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.rules.TemporaryFolder; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ShutdownerTest { + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private File srcFile; + + @Before + public void setUp() throws Exception { + srcFile = new File(Shutdowner.class.getProtectionDomain().getCodeSource().getLocation().toURI()); + } + + @Test + public void detectHomeDir_assumes_Shutdowner_jar_is_in_lib_dir_root() { + assertThat(Shutdowner.detectHomeDir()) + .isEqualTo(srcFile.getParentFile().getParentFile()); + } + + @Test + public void loadPropertiesFile_fails_with_ISE_if_sonar_properties_not_in_conf_dir() throws IOException { + File homeDir = temporaryFolder.newFolder(); + + expectedException.expect(IllegalStateException.class); + expectedException.expectMessage("Configuration file not found: " + new File(new File(homeDir, "conf"), "sonar.properties").getAbsolutePath()); + + Shutdowner.loadPropertiesFile(homeDir); + } + + @Test + public void loadPropertiesFile_reads_sonar_properties_content() throws IOException { + File homeDir = temporaryFolder.newFolder(); + File confDir = new File(homeDir, "conf"); + confDir.mkdirs(); + File sonarProperties = new File(confDir, "sonar.properties"); + sonarProperties.createNewFile(); + Files.write(sonarProperties.toPath(), Arrays.asList("foo=bar")); + + Properties properties = Shutdowner.loadPropertiesFile(homeDir); + + assertThat(properties.getProperty("foo")).isEqualTo("bar"); + } + + @Test + public void resolveTempDir_defaults_to_temp() { + File file = Shutdowner.resolveTempDir(new Properties()); + + assertThat(file).isEqualTo(new File("temp")); + } + + @Test + public void resolveTempDir_reads_relative_temp_dir_location_from_sonar_path_temp() { + String tempDirPath = "blablabl"; + Properties properties = new Properties(); + properties.put("sonar.path.temp", tempDirPath); + File file = Shutdowner.resolveTempDir(properties); + + assertThat(file).isEqualTo(new File(tempDirPath)); + } + + @Test + public void resolveTempDir_reads_absolute_temp_dir_location_from_sonar_path_temp() throws IOException { + File tempDirLocation = temporaryFolder.newFolder(); + Properties properties = new Properties(); + properties.put("sonar.path.temp", tempDirLocation.getAbsolutePath()); + File file = Shutdowner.resolveTempDir(properties); + + assertThat(file).isEqualTo(tempDirLocation); + } + + @Test + public void askForHardStop_write_right_bit_with_right_value_in_right_file() throws Exception { + File tempFolder = temporaryFolder.newFolder(); + + Shutdowner.askForHardStop(tempFolder); + try (RandomAccessFile sharedMemory = new RandomAccessFile(new File(tempFolder, "sharedmemory"), "r")) { + // Using values from org.sonar.process.ProcessCommands + MappedByteBuffer mappedByteBuffer = sharedMemory.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, 50L * 10); + + assertThat(mappedByteBuffer.get(1)).isEqualTo((byte) 0xFF); + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} |