]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5408 - reading properties from sonar.properties for child processes
authorStephane Gamard <stephane.gamard@searchbox.com>
Fri, 18 Jul 2014 14:08:20 +0000 (16:08 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Fri, 18 Jul 2014 14:08:20 +0000 (16:08 +0200)
server/sonar-process/src/main/java/org/sonar/process/ProcessWrapper.java
sonar-start/src/main/java/org/sonar/start/Env.java
sonar-start/src/main/java/org/sonar/start/StartServer.java

index 428d85d42fbcbcc54790637520432c56d5344a62..ea9b86a662184b48dc3961a5b90602ad74391abc 100644 (file)
@@ -53,6 +53,7 @@ public class ProcessWrapper extends Thread {
 
   final int port;
   final String workDir;
+  final String javaOpts;
   final String className;
   final String[] classPath;
   final Map<String, String> properties;
@@ -66,10 +67,16 @@ public class ProcessWrapper extends Thread {
   final ProcessMXBean processMXBean;
 
   public ProcessWrapper(String workDir, String className, Map<String, String> properties, final String name, String... classPath) {
+    this(workDir, null, className, properties, name, className);
+    LOGGER.warn("Creating process '{}' with no JAVA_OPTS", name);
+  }
+
+  public ProcessWrapper(String workDir, String javaOpts, String className, Map<String, String> properties, final String name, String... classPath) {
     super(name);
     this.port = NetworkUtils.freePort();
     LOGGER.info("Creating Process for '{}' with workDir: '{}' and monitoring port: {}", name, workDir, port);
     this.workDir = workDir;
+    this.javaOpts = javaOpts;
     this.className = className;
     this.classPath = classPath;
     this.properties = properties;
@@ -139,14 +146,6 @@ public class ProcessWrapper extends Thread {
       + separator + "bin" + separator + "java";
   }
 
-  private String getJavaOptions() {
-    if (properties.containsKey(Process.JAVA_OPS)) {
-      return properties.get(Process.JAVA_OPS);
-    } else {
-      return null;
-    }
-  }
-
   private List<String> getJMXOptions() {
     return ImmutableList.<String>of(
       "-Dcom.sun.management.jmxremote",
@@ -192,22 +191,14 @@ public class ProcessWrapper extends Thread {
     ProcessBuilder processBuilder = new ProcessBuilder();
     processBuilder.command().add(getJavaCommand());
 
-    String javaOptions = getJavaOptions();
-    if (!StringUtils.isEmpty(javaOptions)) {
-      processBuilder.command().add(getJavaOptions());
+    if (!StringUtils.isEmpty(javaOpts)) {
+      processBuilder.command().add(javaOpts);
     }
     processBuilder.command().addAll(getJMXOptions());
     processBuilder.command().addAll(getClassPath());
     processBuilder.command().add(className);
     processBuilder.command().add(getPropertyFile());
 
-    //TODO remove once Process uses the temp file generated by getPropertyFile();
-    processBuilder.environment().putAll(properties);
-
-    processBuilder.environment().put(Process.SONAR_HOME, workDir);
-    processBuilder.environment().put(Process.NAME_PROPERTY, this.getName());
-    processBuilder.environment().put(Process.PORT_PROPERTY, Integer.toString(port));
-
     //check that working directory exists.
     File workDirectory = new File(workDir);
     if (!workDirectory.exists()) {
index dbb947341ec3cb89cbcfbfc89b524cb9659a2603..af00390d98cdc1c43dfd9ce26335ad0366186256 100644 (file)
@@ -37,6 +37,10 @@ class Env {
     this.confFile = new File(CONF_DIRECTORY+"/sonar.properties");
   }
 
+  public File getConfFile() {
+    return confFile;
+  }
+
   File rootDir() {
     return homeDir;
   }
index f363ba618639c52296d2011e0d7cb21674733c1c..bbd99d3237416ac57f576c3fee920700def1283c 100644 (file)
@@ -28,11 +28,13 @@ import org.sonar.process.NetworkUtils;
 import org.sonar.process.ProcessWrapper;
 
 import java.io.File;
+import java.io.FileReader;
 import java.io.IOException;
 import java.net.URISyntaxException;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Properties;
 
 public final class StartServer {
 
@@ -50,7 +52,7 @@ public final class StartServer {
   private ProcessWrapper elasticsearch;
   private ProcessWrapper sonarqube;
 
-  public StartServer(Env env, String... args) throws IOException {
+  public StartServer(Env env, String... args) throws IOException, InterruptedException {
     this.env = env;
     this.esPort = Integer.toString(NetworkUtils.freePort());
     this.properties = new HashMap<String, String>();
@@ -64,9 +66,7 @@ public final class StartServer {
     shutdownHook = new Thread(new Runnable() {
       @Override
       public void run() {
-        System.out.println("Before");
         stop();
-        System.out.println("After");
       }
     });
 
@@ -96,11 +96,20 @@ public final class StartServer {
 
   public void start() {
 
+    //Loading properties from sonar.properties file
+    Properties sonarProperties = new Properties();
+    try {
+      sonarProperties.load(new FileReader(env.getConfFile()));
+    } catch (IOException e) {
+      throw new IllegalStateException("Could not read properties from env", e);
+    }
+
     String workingDirectory = env.rootDir().getAbsolutePath();
 
     // Start ES
     elasticsearch = new ProcessWrapper(
       env.rootDir().getAbsolutePath(),
+      sonarProperties.getProperty("sonar.es.java_opts"),
       "org.sonar.search.ElasticSearch",
       ImmutableMap.of(
         "esDebug", properties.containsKey("esDebug") ? properties.get("esDebug") : "false",