]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5408 - ProcessWrapper passes properties file as args[0]
authorStephane Gamard <stephane.gamard@searchbox.com>
Fri, 18 Jul 2014 12:38:50 +0000 (14:38 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Fri, 18 Jul 2014 13:52:16 +0000 (15:52 +0200)
server/sonar-process/src/main/java/org/sonar/process/Process.java
server/sonar-process/src/main/java/org/sonar/process/ProcessWrapper.java
server/sonar-search/src/main/java/org/sonar/search/ElasticSearch.java

index f345adf4f425c32d8a38b1edaf4dd8f2b47e90c4..c511bf7f127a58acd4977809254ac2fd2b306245 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.process;
 
+import com.google.common.annotations.VisibleForTesting;
 import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -30,7 +31,10 @@ import javax.management.MalformedObjectNameException;
 import javax.management.NotCompliantMBeanException;
 import javax.management.ObjectName;
 import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
 import java.lang.management.ManagementFactory;
+import java.util.Properties;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ScheduledFuture;
@@ -54,11 +58,11 @@ public abstract class Process implements ProcessMXBean {
 
   protected Long lastPing;
 
-  final String name;
-  final Integer port;
+  String name;
+  Integer port;
 
-  final protected Props props;
-  final private Thread shutdownHook;
+  protected final Props props;
+  private Thread shutdownHook;
 
   private static final long MAX_ALLOWED_TIME = 3000L;
   private ScheduledFuture<?> pingTask = null;
@@ -75,15 +79,40 @@ public abstract class Process implements ProcessMXBean {
     }
   };
 
+  public Process(String[] args) {
+    // loading arguments from file and system.
+    if (args.length < 1) {
+      throw new IllegalStateException("Process is missing argument!");
+    }
+
+    File propertyFile = new File(args[0]);
+    if (!propertyFile.exists()) {
+      throw new IllegalStateException("Property file '" + args[0] + "' does not exist! ");
+    }
+
+    Properties properties = new Properties();
+    try {
+      properties.load(new FileReader(propertyFile));
+    } catch (IOException e) {
+      throw new IllegalStateException("Could not read properties from file '" + args[0] + "'", e);
+    }
+    props = Props.create(properties);
+    init();
+  }
+
+  @VisibleForTesting
   public Process(Props props) {
-  
-    validateSonarHome(props);
-    
-    // Loading all Properties from file
     this.props = props;
+    init();
+  }
+
+  private void init() {
+
+    // Loading all Properties from file
     this.name = props.of(NAME_PROPERTY, null);
     this.port = props.intOf(PORT_PROPERTY);
 
+    validateSonarHome(props);
 
     // Testing required properties
     if (StringUtils.isEmpty(this.name)) {
index c1b1ff5067bc0befe5a5498885b62a537d55f256..9e0495eafc3d81f31bf7aeccf6aac55c17b6c1d1 100644 (file)
@@ -143,7 +143,7 @@ public class ProcessWrapper extends Thread {
     if (properties.containsKey(Process.JAVA_OPS)) {
       return properties.get(Process.JAVA_OPS);
     } else {
-      return "";
+      return null;
     }
   }
 
@@ -187,13 +187,16 @@ public class ProcessWrapper extends Thread {
 
     ProcessBuilder processBuilder = new ProcessBuilder();
     processBuilder.command().add(getJavaCommand());
-    processBuilder.command().add(getJavaOptions());
+
+    String javaOptions = getJavaOptions();
+    if (!StringUtils.isEmpty(javaOptions)) {
+      processBuilder.command().add(getJavaOptions());
+    }
     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);
 
index 29d0d880b7237737fe0dc9f77d59779bb329dfcd..a98e9e3b27bb917e5270e9f5c4bf4ac23d1203de 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.search;
 
+import com.google.common.annotations.VisibleForTesting;
 import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
 import org.elasticsearch.common.settings.ImmutableSettings;
 import org.elasticsearch.common.unit.TimeValue;
@@ -44,12 +45,32 @@ public class ElasticSearch extends Process {
 
   public static final String DEFAULT_CLUSTER_NAME = "sonarqube";
 
-  private final Node node;
+  private Node node;
 
+  public ElasticSearch(String... args) {
+    super(args);
+  }
+
+  @VisibleForTesting
   public ElasticSearch(Props props) {
     super(props);
+  }
 
+  @Override
+  public boolean isReady() {
+    try {
+      return (node.client().admin().cluster().prepareHealth()
+        .setWaitForYellowStatus()
+        .setTimeout(TimeValue.timeValueSeconds(3L))
+        .get()
+        .getStatus() != ClusterHealthStatus.RED);
+    } catch (Exception e) {
+      return false;
+    }
+  }
 
+  @Override
+  public void onStart() {
     String home = props.of(ES_HOME_PROPERTY);
     if (home == null) {
       throw new IllegalStateException(MISSING_ES_HOME);
@@ -99,25 +120,7 @@ public class ElasticSearch extends Process {
     node = NodeBuilder.nodeBuilder()
       .settings(esSettings)
       .build();
-  }
-
 
-  @Override
-  public boolean isReady() {
-    try {
-      return (node.client().admin().cluster().prepareHealth()
-        .setWaitForYellowStatus()
-        .setTimeout(TimeValue.timeValueSeconds(3L))
-        .get()
-        .getStatus() != ClusterHealthStatus.RED);
-    } catch (Exception e) {
-      return false;
-    }
-  }
-
-  @Override
-  public void onStart() {
-    node.start();
     while (!node.isClosed()) {
       try {
         Thread.sleep(1000);
@@ -134,8 +137,7 @@ public class ElasticSearch extends Process {
   }
 
   public static void main(String... args) throws InterruptedException {
-    Props props = Props.create(System.getProperties());
-    final ElasticSearch elasticSearch = new ElasticSearch(props);
+    final ElasticSearch elasticSearch = new ElasticSearch(args);
     elasticSearch.start();
   }
 }