From e8ef04be6f224cd6f16757e07bb2778bbb165671 Mon Sep 17 00:00:00 2001 From: Sébastien Lesaint Date: Tue, 28 Jul 2015 18:47:31 +0200 Subject: SONAR-6749 add sonar-views-bridge module and start/stop Views from SQ --- pom.xml | 5 + server/pom.xml | 1 + server/sonar-server/pom.xml | 4 + .../platform/platformlevel/PlatformLevel4.java | 4 + .../platformlevel/PlatformLevelStartup.java | 4 +- .../sonar/server/view/bridge/ViewsBootstrap.java | 56 +++++++++++ .../org/sonar/server/view/bridge/ViewsStopper.java | 56 +++++++++++ .../server/platform/ViewsIntegrationTest.java | 106 +++++++++++++++++++++ server/sonar-views-bridge/pom.xml | 54 +++++++++++ .../java/org/sonar/server/views/ViewsBridge.java | 51 ++++++++++ 10 files changed, 340 insertions(+), 1 deletion(-) create mode 100644 server/sonar-server/src/main/java/org/sonar/server/view/bridge/ViewsBootstrap.java create mode 100644 server/sonar-server/src/main/java/org/sonar/server/view/bridge/ViewsStopper.java create mode 100644 server/sonar-server/src/test/java/org/sonar/server/platform/ViewsIntegrationTest.java create mode 100644 server/sonar-views-bridge/pom.xml create mode 100644 server/sonar-views-bridge/src/main/java/org/sonar/server/views/ViewsBridge.java diff --git a/pom.xml b/pom.xml index 0b4f76febea..93225c3b253 100644 --- a/pom.xml +++ b/pom.xml @@ -691,6 +691,11 @@ sonar-testing-harness ${project.version} + + org.codehaus.sonar + sonar-views-bridge + ${project.version} + org.sonarsource.java sonar-java-plugin diff --git a/server/pom.xml b/server/pom.xml index e7b1d8c88f0..e1fafe5fa9c 100644 --- a/server/pom.xml +++ b/server/pom.xml @@ -14,6 +14,7 @@ sonar-process sonar-process-monitor sonar-search + sonar-views-bridge sonar-server sonar-web sonar-server-benchmarks diff --git a/server/sonar-server/pom.xml b/server/sonar-server/pom.xml index 5caf02b187b..b83c1d783da 100644 --- a/server/sonar-server/pom.xml +++ b/server/sonar-server/pom.xml @@ -183,6 +183,10 @@ jsr305 provided + + org.codehaus.sonar + sonar-views-bridge + org.codehaus.sonar diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java index efed066e65e..534edeffa91 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java @@ -311,6 +311,7 @@ import org.sonar.server.user.ws.UserPropertiesWs; import org.sonar.server.user.ws.UsersWs; import org.sonar.server.usergroups.ws.UserGroupsModule; import org.sonar.server.util.TypeValidationModule; +import org.sonar.server.view.bridge.ViewsStopper; import org.sonar.server.view.index.ViewIndex; import org.sonar.server.view.index.ViewIndexDefinition; import org.sonar.server.view.index.ViewIndexer; @@ -724,6 +725,9 @@ public class PlatformLevel4 extends PlatformLevel { ProjectSettingsFactory.class, IndexPurgeListener.class, + // Views plugin + ViewsStopper.class, + // UI GlobalNavigationAction.class, SettingsNavigationAction.class, diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelStartup.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelStartup.java index 80141a36266..f76e269d59e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelStartup.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevelStartup.java @@ -39,6 +39,7 @@ import org.sonar.server.startup.RenameDeprecatedPropertyKeys; import org.sonar.server.startup.RenameIssueWidgets; import org.sonar.server.user.DoPrivileged; import org.sonar.server.user.ThreadLocalUserSession; +import org.sonar.server.view.bridge.ViewsBootstrap; public class PlatformLevelStartup extends PlatformLevel { public PlatformLevelStartup(PlatformLevel parent) { @@ -65,7 +66,8 @@ public class PlatformLevelStartup extends PlatformLevel { ReportQueueCleaner.class, RegisterIssueFilters.class, RenameIssueWidgets.class, - ServerLifecycleNotifier.class); + ServerLifecycleNotifier.class, + ViewsBootstrap.class); } @Override diff --git a/server/sonar-server/src/main/java/org/sonar/server/view/bridge/ViewsBootstrap.java b/server/sonar-server/src/main/java/org/sonar/server/view/bridge/ViewsBootstrap.java new file mode 100644 index 00000000000..691515905fd --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/view/bridge/ViewsBootstrap.java @@ -0,0 +1,56 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.server.view.bridge; + +import org.picocontainer.Startable; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; +import org.sonar.api.utils.log.Profiler; +import org.sonar.core.platform.ComponentContainer; +import org.sonar.server.views.ViewsBridge; + +/** + * Startup task to responsible to bootstrap the Views plugin when it is installed. + */ +public class ViewsBootstrap implements Startable { + private static final Logger LOGGER = Loggers.get(ViewsBootstrap.class); + + private final ComponentContainer componentContainer; + + public ViewsBootstrap(ComponentContainer componentContainer) { + this.componentContainer = componentContainer; + } + + @Override + public void start() { + ViewsBridge viewsBridge = componentContainer.getComponentByType(ViewsBridge.class); + if (viewsBridge != null) { + Profiler profiler = Profiler.create(LOGGER).startInfo("Bootstrapping views"); + viewsBridge.startViews(componentContainer); + profiler.stopInfo(); + } + } + + @Override + public void stop() { + // this class is stopped right after it has been started as an element of PlatformLevelStartup + // stopping Views is handled by ViewsStopper + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/view/bridge/ViewsStopper.java b/server/sonar-server/src/main/java/org/sonar/server/view/bridge/ViewsStopper.java new file mode 100644 index 00000000000..f82d78d0d34 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/view/bridge/ViewsStopper.java @@ -0,0 +1,56 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.server.view.bridge; + +import org.picocontainer.Startable; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; +import org.sonar.api.utils.log.Profiler; +import org.sonar.core.platform.ComponentContainer; +import org.sonar.server.views.ViewsBridge; + +/** + * As an component of PlatformLevel4, this class is responsible for notifying shutdown to the Views plugin when its + * installed. + */ +public class ViewsStopper implements Startable { + private static final Logger LOGGER = Loggers.get(ViewsStopper.class); + + private final ComponentContainer platformContainer; + + public ViewsStopper(ComponentContainer platformContainer) { + this.platformContainer = platformContainer; + } + + @Override + public void start() { + // nothing to do, Views plugins is started by ViewsBootstrap + } + + @Override + public void stop() { + ViewsBridge viewsBridge = platformContainer.getComponentByType(ViewsBridge.class); + if (viewsBridge != null) { + Profiler profiler = Profiler.create(LOGGER).startInfo("Stopping views"); + viewsBridge.stopViews(); + profiler.stopInfo(); + } + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/ViewsIntegrationTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/ViewsIntegrationTest.java new file mode 100644 index 00000000000..f1eb44a33ff --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/ViewsIntegrationTest.java @@ -0,0 +1,106 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.server.platform; + +import org.junit.After; +import org.junit.Test; +import org.sonar.core.platform.ComponentContainer; +import org.sonar.server.tester.ServerTester; +import org.sonar.server.views.ViewsBridge; + +import static com.google.common.base.Preconditions.checkArgument; +import static org.assertj.core.api.Assertions.assertThat; + +public class ViewsIntegrationTest { + private final ViewsBridgeSimulator viewsBridgeSimulator = new ViewsBridgeSimulator(); + + private ServerTester serverTester = new ServerTester(); + + @After + public void tearDown() throws Exception { + serverTester.stop(); + } + + @Test + public void verify_no_interaction_when_not_added_to_the_server() { + serverTester.start(); + + assertThat(viewsBridgeSimulator.isBootstrapCalled()).isFalse(); + assertThat(viewsBridgeSimulator.getStopCalls()).isEqualTo(0); + assertThat(viewsBridgeSimulator.getUpdateViewsCalls()).isEqualTo(0); + } + + @Test + public void verify_no_interaction_when_startup_tasks_are_disabled() { + serverTester.addComponents(viewsBridgeSimulator).start(); + + assertThat(viewsBridgeSimulator.isBootstrapCalled()).isFalse(); + assertThat(viewsBridgeSimulator.getStopCalls()).isEqualTo(0); + assertThat(viewsBridgeSimulator.getUpdateViewsCalls()).isEqualTo(0); + } + + @Test + public void verify_bootstrapped_only_with_startup_tasks_and_stopped_with_the_server() { + serverTester.addComponents(viewsBridgeSimulator).withStartupTasks().start(); + + assertThat(viewsBridgeSimulator.isBootstrapCalled()).isTrue(); + assertThat(viewsBridgeSimulator.getStopCalls()).isEqualTo(0); + assertThat(viewsBridgeSimulator.getUpdateViewsCalls()).isEqualTo(0); + + serverTester.stop(); + + assertThat(viewsBridgeSimulator.getStopCalls()).isEqualTo(1); + assertThat(viewsBridgeSimulator.getUpdateViewsCalls()).isEqualTo(0); + } + + private static class ViewsBridgeSimulator implements ViewsBridge { + private boolean bootstrapCalled = false; + private int stopCalls = 0; + private int updateViewsCalls = 0; + + @Override + public void startViews(ComponentContainer parent) { + checkArgument(!bootstrapCalled, "Bootstrap already called"); + this.bootstrapCalled = true; + } + + @Override + public void stopViews() { + this.stopCalls += 1; + } + + @Override + public void updateViews() { + this.updateViewsCalls += 1; + } + + public boolean isBootstrapCalled() { + return bootstrapCalled; + } + + public int getStopCalls() { + return stopCalls; + } + + public int getUpdateViewsCalls() { + return updateViewsCalls; + } + } +} diff --git a/server/sonar-views-bridge/pom.xml b/server/sonar-views-bridge/pom.xml new file mode 100644 index 00000000000..e434b99fedd --- /dev/null +++ b/server/sonar-views-bridge/pom.xml @@ -0,0 +1,54 @@ + + + 4.0.0 + + org.codehaus.sonar + server + 5.2-SNAPSHOT + .. + + sonar-views-bridge + SonarQube :: Views Bridge + + + + org.codehaus.sonar + sonar-plugin-api + + + org.codehaus.sonar + sonar-core + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + ${skipServerTests} + + + + + + + + release + + + + maven-deploy-plugin + + true + + + + + + + + + diff --git a/server/sonar-views-bridge/src/main/java/org/sonar/server/views/ViewsBridge.java b/server/sonar-views-bridge/src/main/java/org/sonar/server/views/ViewsBridge.java new file mode 100644 index 00000000000..759468e2b98 --- /dev/null +++ b/server/sonar-views-bridge/src/main/java/org/sonar/server/views/ViewsBridge.java @@ -0,0 +1,51 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.server.views; + +import org.sonar.core.platform.ComponentContainer; + +/** + * Interface implemented by the Extension point exposed by the Views plugin that serves as the unique access point from + * the whole SQ instance into the Views plugin. + */ +public interface ViewsBridge { + + /** + * Bootstraps the Views plugin. + * + * @param parent the parent ComponentContainer which provides Platform components for Views to use. + * + * @throws IllegalStateException if called more than once + */ + void startViews(ComponentContainer parent); + + /** + * This method is called when Platform is shutting down. + */ + void stopViews(); + + /** + * Triggers an update of Views tree and measures. + * + * @throws IllegalStateException if {@link #startViews(ComponentContainer)} has not been called + */ + void updateViews(); + +} -- cgit v1.2.3