]> source.dussan.org Git - sonarqube.git/commitdiff
LICENSE-98 add EditionProvider
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 31 May 2018 09:02:55 +0000 (11:02 +0200)
committerSonarTech <sonartech@sonarsource.com>
Tue, 12 Jun 2018 18:21:00 +0000 (20:21 +0200)
server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java
server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java
server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java
server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelStartup.java
sonar-core/src/main/java/org/sonar/core/platform/EditionProvider.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/core/platform/PlatformEditionProvider.java [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/core/platform/PlatformEditionProviderTest.java [new file with mode: 0644]

index bfc640055d9ed18311d3792e241da10895fb4148..2a9888cfe2e64c9876b5074be92dec864dc336a6 100644 (file)
@@ -41,6 +41,7 @@ import org.sonar.api.utils.Durations;
 import org.sonar.api.utils.System2;
 import org.sonar.api.utils.UriReader;
 import org.sonar.api.utils.Version;
+import org.sonar.api.utils.log.Loggers;
 import org.sonar.ce.CeConfigurationModule;
 import org.sonar.ce.CeDistributedInformationImpl;
 import org.sonar.ce.CeHttpModule;
@@ -62,9 +63,14 @@ import org.sonar.ce.taskprocessor.CeTaskProcessorModule;
 import org.sonar.ce.user.CeUserSession;
 import org.sonar.core.component.DefaultResourceTypes;
 import org.sonar.core.config.CorePropertyDefinitions;
+import org.sonar.core.extension.CoreExtensionRepositoryImpl;
+import org.sonar.core.extension.CoreExtensionsInstaller;
+import org.sonar.core.extension.CoreExtensionsLoader;
 import org.sonar.core.i18n.RuleI18nManager;
 import org.sonar.core.platform.ComponentContainer;
+import org.sonar.core.platform.EditionProvider;
 import org.sonar.core.platform.Module;
+import org.sonar.core.platform.PlatformEditionProvider;
 import org.sonar.core.platform.PluginClassloaderFactory;
 import org.sonar.core.platform.PluginLoader;
 import org.sonar.core.timemachine.Periods;
@@ -89,9 +95,6 @@ import org.sonar.server.debt.DebtRulesXMLImporter;
 import org.sonar.server.es.EsModule;
 import org.sonar.server.es.ProjectIndexersImpl;
 import org.sonar.server.event.NewAlerts;
-import org.sonar.core.extension.CoreExtensionRepositoryImpl;
-import org.sonar.core.extension.CoreExtensionsInstaller;
-import org.sonar.core.extension.CoreExtensionsLoader;
 import org.sonar.server.favorite.FavoriteUpdater;
 import org.sonar.server.issue.IssueFieldsSetter;
 import org.sonar.server.issue.index.IssueIndex;
@@ -212,6 +215,9 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer {
     CoreExtensionsInstaller coreExtensionsInstaller = this.level4.getComponentByType(CECoreExtensionsInstaller.class);
     coreExtensionsInstaller.install(this.level4, t -> true);
     this.level4.startComponents();
+    PlatformEditionProvider editionProvider = this.level4.getComponentByType(PlatformEditionProvider.class);
+    Loggers.get(ComputeEngineContainerImpl.class)
+      .info("Running {} edition", editionProvider.get().map(EditionProvider.Edition::getLabel).orElse(""));
 
     startupTasks();
 
@@ -432,6 +438,9 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer {
       // System
       ServerLogging.class,
 
+      // SonarSource editions
+      PlatformEditionProvider.class,
+
       // privileged plugins
       CECoreExtensionsInstaller.class,
       PrivilegedPluginsBootstraper.class,
index b079bae241e2322cb0703fe9d44ab7838ce8f647..9401436c0f859fbea59186935eac20dbf5bad6b8 100644 (file)
@@ -94,7 +94,7 @@ public class ComputeEngineContainerImplTest {
       assertThat(picoContainer.getComponentAdapters())
         .hasSize(
           CONTAINER_ITSELF
-            + 84 // level 4
+            + 85 // level 4
             + 21 // content of QualityGateModule
             + 6 // content of CeConfigurationModule
             + 4 // content of CeQueueModule
index 7a5d0407cc7572035abe6ea0b45e18933a1d7c44..2794fc763c2e29a889098a2e9a114a90aeb5f089 100644 (file)
@@ -34,6 +34,7 @@ import org.sonar.ce.settings.ProjectConfigurationFactory;
 import org.sonar.core.component.DefaultResourceTypes;
 import org.sonar.core.extension.CoreExtensionsInstaller;
 import org.sonar.core.platform.ComponentContainer;
+import org.sonar.core.platform.PlatformEditionProvider;
 import org.sonar.core.timemachine.Periods;
 import org.sonar.server.authentication.AuthenticationModule;
 import org.sonar.server.authentication.LogOAuthWarning;
@@ -544,6 +545,9 @@ public class PlatformLevel4 extends PlatformLevel {
       CeModule.class,
       CeWsModule.class,
 
+      // SonarSource editions
+      PlatformEditionProvider.class,
+
       InternalPropertiesImpl.class,
       ProjectConfigurationFactory.class,
 
index 4cb4c934f57980b38372bd45f062af39e4a6c60b..52c00c12c9067480e5d72c7395f4d4ae632a7d55 100644 (file)
@@ -19,6 +19,9 @@
  */
 package org.sonar.server.platform.platformlevel;
 
+import org.sonar.api.utils.log.Loggers;
+import org.sonar.core.platform.EditionProvider;
+import org.sonar.core.platform.PlatformEditionProvider;
 import org.sonar.server.app.ProcessCommandWrapper;
 import org.sonar.server.es.IndexerStartupTask;
 import org.sonar.server.organization.DefaultOrganizationEnforcer;
@@ -81,6 +84,8 @@ public class PlatformLevelStartup extends PlatformLevel {
         get(ServerLifecycleNotifier.class).notifyStart();
         get(ProcessCommandWrapper.class).notifyOperational();
         get(WebServerRuleFinder.class).stopCaching();
+        Loggers.get(PlatformLevelStartup.class)
+          .info("Running {} Edition", get(PlatformEditionProvider.class).get().map(EditionProvider.Edition::getLabel).orElse(""));
       }
     });
 
diff --git a/sonar-core/src/main/java/org/sonar/core/platform/EditionProvider.java b/sonar-core/src/main/java/org/sonar/core/platform/EditionProvider.java
new file mode 100644 (file)
index 0000000..fa0dd84
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 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.core.platform;
+
+import java.util.Optional;
+import org.sonar.api.ce.ComputeEngineSide;
+import org.sonar.api.server.ServerSide;
+
+@ServerSide
+@ComputeEngineSide
+public interface EditionProvider {
+  enum Edition {
+    COMMUNITY("Community"),
+    DEVELOPER("Developer"),
+    ENTERPRISE("Enterprise"),
+    DATA_CENTER("Data Center");
+
+    private final String label;
+
+    Edition(String label) {
+      this.label = label;
+    }
+
+    public String getLabel() {
+      return label;
+    }
+  }
+
+  Optional<Edition> get();
+}
diff --git a/sonar-core/src/main/java/org/sonar/core/platform/PlatformEditionProvider.java b/sonar-core/src/main/java/org/sonar/core/platform/PlatformEditionProvider.java
new file mode 100644 (file)
index 0000000..8c42c66
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 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.core.platform;
+
+import java.util.Optional;
+
+import static com.google.common.base.Preconditions.checkState;
+import static org.sonar.core.platform.EditionProvider.Edition.COMMUNITY;
+
+public class PlatformEditionProvider implements EditionProvider {
+  private static final EditionProvider[] NO_OTHER_PROVIDERS = new EditionProvider[0];
+  private final EditionProvider[] otherEditionProviders;
+
+  public PlatformEditionProvider() {
+    this(NO_OTHER_PROVIDERS);
+  }
+
+  public PlatformEditionProvider(EditionProvider[] otherEditionProviders) {
+    this.otherEditionProviders = otherEditionProviders;
+  }
+
+  @Override
+  public Optional<Edition> get() {
+    checkState(otherEditionProviders.length <= 1, "There can't be more than 1 other EditionProvider");
+    if (otherEditionProviders.length == 1) {
+      return otherEditionProviders[0].get();
+    }
+    return Optional.of(COMMUNITY);
+  }
+}
diff --git a/sonar-core/src/test/java/org/sonar/core/platform/PlatformEditionProviderTest.java b/sonar-core/src/test/java/org/sonar/core/platform/PlatformEditionProviderTest.java
new file mode 100644 (file)
index 0000000..032d9fb
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 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.core.platform;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import static java.util.Optional.of;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.core.platform.EditionProvider.Edition.COMMUNITY;
+import static org.sonar.core.platform.EditionProvider.Edition.DATA_CENTER;
+import static org.sonar.core.platform.EditionProvider.Edition.DEVELOPER;
+
+public class PlatformEditionProviderTest {
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  @Test
+  public void returns_COMMUNITY_when_there_is_no_other_EditionProvider() {
+    ComponentContainer container = new ComponentContainer();
+    ComponentContainer child = new ComponentContainer(container);
+    child.add(PlatformEditionProvider.class);
+
+    assertThat(container.getComponentByType(PlatformEditionProvider.class)).isNull();
+    PlatformEditionProvider platformEditionProvider = child.getComponentByType(PlatformEditionProvider.class);
+    assertThat(platformEditionProvider.get()).contains(COMMUNITY);
+  }
+
+  @Test
+  public void returns_edition_from_other_EditionProvider_in_any_container() {
+    ComponentContainer container = new ComponentContainer();
+    container.add((EditionProvider) () -> of(DATA_CENTER));
+    ComponentContainer child = new ComponentContainer(container);
+    child.add(PlatformEditionProvider.class);
+
+    assertThat(container.getComponentByType(PlatformEditionProvider.class)).isNull();
+    PlatformEditionProvider platformEditionProvider = child.getComponentByType(PlatformEditionProvider.class);
+    assertThat(platformEditionProvider.get()).contains(DATA_CENTER);
+  }
+
+  @Test
+  public void get_fails_with_ISE_if_there_is_more_than_one_other_EditionProvider_in_any_container() {
+    ComponentContainer container = new ComponentContainer();
+    container.add((EditionProvider) () -> of(DATA_CENTER));
+    ComponentContainer child = new ComponentContainer(container);
+    child.add((EditionProvider) () -> of(DEVELOPER));
+    child.add(PlatformEditionProvider.class);
+
+    assertThat(container.getComponentByType(PlatformEditionProvider.class)).isNull();
+    PlatformEditionProvider platformEditionProvider = child.getComponentByType(PlatformEditionProvider.class);
+
+    expectedException.expect(IllegalStateException.class);
+    expectedException.expectMessage("There can't be more than 1 other EditionProvider");
+
+    platformEditionProvider.get();
+  }
+}