]> source.dussan.org Git - sonar-scanner-cli.git/commitdiff
SONARPLUGINS-2202 Make it possible to pass environment information
authorFabrice Bellingard <fabrice.bellingard@sonarsource.com>
Tue, 11 Sep 2012 12:23:34 +0000 (14:23 +0200)
committerFabrice Bellingard <fabrice.bellingard@sonarsource.com>
Tue, 11 Sep 2012 12:23:34 +0000 (14:23 +0200)
Necessary for the Ant task for instance:
- masked packages
- container extensions

=> This part of the API should be improved

src/main/java/org/sonar/runner/Runner.java
src/main/java/org/sonar/runner/internal/batch/Launcher.java
src/test/java/org/sonar/runner/internal/batch/LauncherTest.java

index d911b0b7f960113e2196c1abd16c7f36d6467430..f2cd6066f0ccc3d2171663b201e095f618b0b36d 100644 (file)
@@ -29,6 +29,8 @@ import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Properties;
 
 /**
@@ -106,10 +108,13 @@ public final class Runner {
 
   private File projectDir;
   private File workDir;
+  private String[] unmaskedPackages;
+  private List<Object> containerExtensions = new ArrayList<Object>();
   private Properties properties;
 
   private Runner(Properties props) {
     this.properties = props;
+    this.unmaskedPackages = new String[0];
     // set the default values for the Sonar Runner - they can be overriden with #setEnvironmentInformation
     this.properties.put(PROPERTY_ENVIRONMENT_INFORMATION_KEY, "Runner");
     this.properties.put(PROPERTY_ENVIRONMENT_INFORMATION_VERSION, SonarRunnerVersion.getVersion());
@@ -204,7 +209,8 @@ public final class Runner {
     URL url = getClass().getProtectionDomain().getCodeSource().getLocation();
     return bootstrapper.createClassLoader(
         new URL[] {url}, // Add JAR with Sonar Runner - it's a Jar which contains this class
-        getClass().getClassLoader());
+        getClass().getClassLoader(),
+        unmaskedPackages);
   }
 
   static boolean isUnsupportedVersion(String version) {
@@ -230,8 +236,8 @@ public final class Runner {
     try {
       Thread.currentThread().setContextClassLoader(sonarClassLoader);
       Class<?> launcherClass = sonarClassLoader.findClass("org.sonar.runner.internal.batch.Launcher");
-      Constructor<?> constructor = launcherClass.getConstructor(Properties.class);
-      Object launcher = constructor.newInstance(getProperties());
+      Constructor<?> constructor = launcherClass.getConstructor(Properties.class, List.class);
+      Object launcher = constructor.newInstance(getProperties(), containerExtensions);
       Method method = launcherClass.getMethod("execute");
       method.invoke(launcher);
     } catch (InvocationTargetException e) {
@@ -255,4 +261,13 @@ public final class Runner {
     this.properties.put(PROPERTY_ENVIRONMENT_INFORMATION_KEY, key);
     this.properties.put(PROPERTY_ENVIRONMENT_INFORMATION_VERSION, version);
   }
+
+  public void setUnmaskedPackages(String... unmaskedPackages) {
+    this.unmaskedPackages = unmaskedPackages;
+  }
+
+  public void addContainerExtension(Object extension) {
+    containerExtensions.add(extension);
+  }
+
 }
index 213756711b97af507f9d29ca7ef9d52eb054bb34..502e5cf74eb7781096474ef0418621aca57f5331 100644 (file)
@@ -39,6 +39,7 @@ import org.sonar.batch.bootstrapper.EnvironmentInformation;
 import org.sonar.runner.Runner;
 
 import java.io.InputStream;
+import java.util.List;
 import java.util.Properties;
 
 /**
@@ -48,9 +49,11 @@ import java.util.Properties;
 public class Launcher {
 
   private Properties propertiesFromRunner;
+  private List<Object> containerExtensions;
 
-  public Launcher(Properties properties) {
+  public Launcher(Properties properties, List<Object> containerExtensions) {
     this.propertiesFromRunner = properties;
+    this.containerExtensions = containerExtensions;
   }
 
   /**
@@ -64,13 +67,22 @@ public class Launcher {
   }
 
   private void executeBatch(ProjectDefinition project, Configuration initialConfiguration) {
-    ProjectReactor reactor = new ProjectReactor(project);
+    setContainerExtensionsOnProject(project);
     String envKey = propertiesFromRunner.getProperty(Runner.PROPERTY_ENVIRONMENT_INFORMATION_KEY);
     String envVersion = propertiesFromRunner.getProperty(Runner.PROPERTY_ENVIRONMENT_INFORMATION_VERSION);
-    Batch batch = Batch.create(reactor, initialConfiguration, new EnvironmentInformation(envKey, envVersion));
+    Batch batch = Batch.create(new ProjectReactor(project), initialConfiguration, new EnvironmentInformation(envKey, envVersion));
     batch.execute();
   }
 
+  private void setContainerExtensionsOnProject(ProjectDefinition projectDefinition) {
+    for (Object extension : containerExtensions) {
+      projectDefinition.addContainerExtension(extension);
+    }
+    for (ProjectDefinition module : projectDefinition.getSubProjects()) {
+      setContainerExtensionsOnProject(module);
+    }
+  }
+
   private void initLogging(Configuration initialConfiguration) {
     LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
     JoranConfigurator jc = new JoranConfigurator();
index 489b00527046f697dd41d9b41fab60785cc9a3f3..979a5176aada6a93280650403173876703116c09 100644 (file)
@@ -19,7 +19,7 @@
  */
 package org.sonar.runner.internal.batch;
 
-
+import com.google.common.collect.Lists;
 import org.apache.commons.configuration.BaseConfiguration;
 import org.apache.commons.configuration.Configuration;
 import org.junit.Test;
@@ -60,7 +60,7 @@ public class LauncherTest {
   @Test
   public void shouldDetermineVerboseMode() {
     Properties properties = new Properties();
-    Launcher launcher = new Launcher(properties);
+    Launcher launcher = new Launcher(properties, Lists.newArrayList());
     assertThat(launcher.isDebug()).isFalse();
     properties.setProperty(Runner.PROPERTY_VERBOSE, "true");
     assertThat(launcher.isDebug()).isTrue();
@@ -69,7 +69,7 @@ public class LauncherTest {
   @Test
   public void shouldSupportDeprecatedDebugProperty() {
     Properties properties = new Properties();
-    Launcher launcher = new Launcher(properties);
+    Launcher launcher = new Launcher(properties, Lists.newArrayList());
     properties.setProperty(Runner.PROPERTY_OLD_DEBUG_MODE, "true");
     assertThat(launcher.isDebug()).isTrue();
   }