From f40d89dd7beef93e10fea0d9a8885a7ac78543bd Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Sun, 16 Mar 2014 00:08:39 +0100 Subject: [PATCH] Refactor org.sonar.server.plugins.ApplicationDeployer --- .../ClassLoaderUtils.java | 14 +++++----- .../org/sonar/server/platform/Platform.java | 3 +- .../RailsAppsDeployer.java} | 26 +++++++----------- .../ClassLoaderUtilsTest.java | 5 ++-- .../RailsAppsDeployerTest.java} | 24 ++++++++-------- .../ClassLoaderUtilsTest.jar | Bin .../FakeRubyRailsApp.jar | Bin 7 files changed, 34 insertions(+), 38 deletions(-) rename sonar-server/src/main/java/org/sonar/server/{plugins => platform}/ClassLoaderUtils.java (88%) rename sonar-server/src/main/java/org/sonar/server/{plugins/ApplicationDeployer.java => platform/RailsAppsDeployer.java} (82%) rename sonar-server/src/test/java/org/sonar/server/{plugins => platform}/ClassLoaderUtilsTest.java (95%) rename sonar-server/src/test/java/org/sonar/server/{plugins/ApplicationDeployerTest.java => platform/RailsAppsDeployerTest.java} (80%) rename sonar-server/src/test/resources/org/sonar/server/{plugins => platform}/ClassLoaderUtilsTest/ClassLoaderUtilsTest.jar (100%) rename sonar-server/src/test/resources/org/sonar/server/{plugins/ApplicationDeployerTest => platform/RailsAppsDeployerTest}/FakeRubyRailsApp.jar (100%) diff --git a/sonar-server/src/main/java/org/sonar/server/plugins/ClassLoaderUtils.java b/sonar-server/src/main/java/org/sonar/server/platform/ClassLoaderUtils.java similarity index 88% rename from sonar-server/src/main/java/org/sonar/server/plugins/ClassLoaderUtils.java rename to sonar-server/src/main/java/org/sonar/server/platform/ClassLoaderUtils.java index abe2b3660fb..bf2e685a087 100644 --- a/sonar-server/src/main/java/org/sonar/server/plugins/ClassLoaderUtils.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/ClassLoaderUtils.java @@ -17,7 +17,7 @@ * 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.plugins; +package org.sonar.server.platform; import com.google.common.base.*; import com.google.common.collect.Lists; @@ -40,16 +40,16 @@ import java.util.jar.JarFile; /** * @since 3.0 */ -public final class ClassLoaderUtils { +class ClassLoaderUtils { private ClassLoaderUtils() { } - public static File copyResources(ClassLoader classLoader, String rootPath, File toDir) { + static File copyResources(ClassLoader classLoader, String rootPath, File toDir) { return copyResources(classLoader, rootPath, toDir, Functions.identity()); } - public static File copyResources(ClassLoader classLoader, String rootPath, File toDir, Function relocationFunction) { + static File copyResources(ClassLoader classLoader, String rootPath, File toDir, Function relocationFunction) { Collection relativePaths = listFiles(classLoader, rootPath); for (String relativePath : relativePaths) { URL resource = classLoader.getResource(relativePath); @@ -72,7 +72,7 @@ public final class ClassLoaderUtils { * @param rootPath the root directory, for example org/sonar/sqale * @return a list of relative paths, for example {"org/sonar/sqale/foo/bar.txt}. Never null. */ - public static Collection listFiles(ClassLoader classLoader, String rootPath) { + static Collection listFiles(ClassLoader classLoader, String rootPath) { return listResources(classLoader, rootPath, new Predicate() { public boolean apply(@Nullable String path) { return !StringUtils.endsWith(path, "/"); @@ -81,7 +81,7 @@ public final class ClassLoaderUtils { } - public static Collection listResources(ClassLoader classLoader, String rootPath) { + static Collection listResources(ClassLoader classLoader, String rootPath) { return listResources(classLoader, rootPath, Predicates.alwaysTrue()); } @@ -93,7 +93,7 @@ public final class ClassLoaderUtils { * @param * @return a list of relative paths, for example {"org/sonar/sqale", "org/sonar/sqale/foo", "org/sonar/sqale/foo/bar.txt}. Never null. */ - public static Collection listResources(ClassLoader classLoader, String rootPath, Predicate predicate) { + static Collection listResources(ClassLoader classLoader, String rootPath, Predicate predicate) { String jarPath = null; JarFile jar = null; try { diff --git a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java index 3e69a9f5e43..62ebbfa76ef 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java @@ -51,7 +51,6 @@ import org.sonar.server.db.migrations.DatabaseMigrator; import org.sonar.server.es.ESNode; import org.sonar.server.platform.ws.PlatformWs; import org.sonar.server.platform.ws.RestartHandler; -import org.sonar.server.plugins.ApplicationDeployer; import org.sonar.server.plugins.DefaultServerPluginRepository; import org.sonar.server.plugins.InstalledPluginReferentialFactory; import org.sonar.server.plugins.PluginDeployer; @@ -150,7 +149,7 @@ public class Platform { level1Container.addSingleton(InstalledPluginReferentialFactory.class); level1Container.addSingleton(DefaultServerPluginRepository.class); level1Container.addSingleton(DefaultServerFileSystem.class); - level1Container.addSingleton(ApplicationDeployer.class); + level1Container.addSingleton(RailsAppsDeployer.class); level1Container.addSingleton(JRubyI18n.class); level1Container.addSingleton(DefaultI18n.class); level1Container.addSingleton(RuleI18nManager.class); diff --git a/sonar-server/src/main/java/org/sonar/server/plugins/ApplicationDeployer.java b/sonar-server/src/main/java/org/sonar/server/platform/RailsAppsDeployer.java similarity index 82% rename from sonar-server/src/main/java/org/sonar/server/plugins/ApplicationDeployer.java rename to sonar-server/src/main/java/org/sonar/server/platform/RailsAppsDeployer.java index dbfecc668b4..14aa740b3cd 100644 --- a/sonar-server/src/main/java/org/sonar/server/plugins/ApplicationDeployer.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/RailsAppsDeployer.java @@ -17,7 +17,7 @@ * 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.plugins; +package org.sonar.server.platform; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Function; @@ -30,7 +30,6 @@ import org.sonar.api.platform.PluginRepository; import org.sonar.api.platform.ServerFileSystem; import javax.annotation.Nullable; - import java.io.File; import java.io.IOException; @@ -40,30 +39,26 @@ import java.io.IOException; * * @since 3.0 */ -public class ApplicationDeployer { - private static final Logger LOG = LoggerFactory.getLogger(ApplicationDeployer.class); +public class RailsAppsDeployer { + private static final Logger LOG = LoggerFactory.getLogger(RailsAppsDeployer.class); private static final String ROR_PATH = "org/sonar/ror/"; private final ServerFileSystem fileSystem; private final PluginRepository pluginRepository; - public ApplicationDeployer(ServerFileSystem fileSystem, PluginRepository pluginRepository) { + public RailsAppsDeployer(ServerFileSystem fileSystem, PluginRepository pluginRepository) { this.fileSystem = fileSystem; this.pluginRepository = pluginRepository; } public void start() { - deployRubyRailsApps(); - } - - private void deployRubyRailsApps() { LOG.info("Deploy Ruby on Rails applications"); - File appsDir = prepareRubyRailsRootDirectory(); + File appsDir = prepareRailsDirectory(); for (PluginMetadata pluginMetadata : pluginRepository.getMetadata()) { String pluginKey = pluginMetadata.getKey(); try { - deployRubyRailsApp(appsDir, pluginKey, pluginRepository.getPlugin(pluginKey).getClass().getClassLoader()); + deployRailsApp(appsDir, pluginKey, pluginRepository.getPlugin(pluginKey).getClass().getClassLoader()); } catch (Exception e) { throw new IllegalStateException("Fail to deploy Ruby on Rails application: " + pluginKey, e); } @@ -71,15 +66,15 @@ public class ApplicationDeployer { } @VisibleForTesting - File prepareRubyRailsRootDirectory() { + File prepareRailsDirectory() { File appsDir = new File(fileSystem.getTempDir(), "ror"); prepareDir(appsDir); return appsDir; } @VisibleForTesting - static void deployRubyRailsApp(File appsDir, final String pluginKey, ClassLoader appClassLoader) { - if (hasRubyRailsApp(pluginKey, appClassLoader)) { + static void deployRailsApp(File appsDir, final String pluginKey, ClassLoader appClassLoader) { + if (hasRailsApp(pluginKey, appClassLoader)) { LOG.info("Deploy app: " + pluginKey); File appDir = new File(appsDir, pluginKey); ClassLoaderUtils.copyResources(appClassLoader, pathToRubyInitFile(pluginKey), appDir, new Function() { @@ -99,9 +94,8 @@ public class ApplicationDeployer { } @VisibleForTesting - static boolean hasRubyRailsApp(String pluginKey, ClassLoader classLoader) { + static boolean hasRailsApp(String pluginKey, ClassLoader classLoader) { return classLoader.getResource(pathToRubyInitFile(pluginKey)) != null; - } private void prepareDir(File appsDir) { diff --git a/sonar-server/src/test/java/org/sonar/server/plugins/ClassLoaderUtilsTest.java b/sonar-server/src/test/java/org/sonar/server/platform/ClassLoaderUtilsTest.java similarity index 95% rename from sonar-server/src/test/java/org/sonar/server/plugins/ClassLoaderUtilsTest.java rename to sonar-server/src/test/java/org/sonar/server/platform/ClassLoaderUtilsTest.java index 21ef3d6c3f8..400da37c733 100644 --- a/sonar-server/src/test/java/org/sonar/server/plugins/ClassLoaderUtilsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/platform/ClassLoaderUtilsTest.java @@ -17,7 +17,7 @@ * 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.plugins; +package org.sonar.server.platform; import com.google.common.base.Function; import com.google.common.base.Predicate; @@ -29,6 +29,7 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.sonar.server.platform.ClassLoaderUtils; import javax.annotation.Nullable; import java.io.File; @@ -52,7 +53,7 @@ public class ClassLoaderUtilsTest { // org/sonar/sqale/app/copyright.txt // org/sonar/sqale/app/README.md // org/sonar/other/other.txt - URL jarUrl = getClass().getResource("/org/sonar/server/plugins/ClassLoaderUtilsTest/ClassLoaderUtilsTest.jar"); + URL jarUrl = getClass().getResource("/org/sonar/server/platform/ClassLoaderUtilsTest/ClassLoaderUtilsTest.jar"); classLoader = new URLClassLoader(new URL[]{jarUrl}, /* no parent classloader */null); } diff --git a/sonar-server/src/test/java/org/sonar/server/plugins/ApplicationDeployerTest.java b/sonar-server/src/test/java/org/sonar/server/platform/RailsAppsDeployerTest.java similarity index 80% rename from sonar-server/src/test/java/org/sonar/server/plugins/ApplicationDeployerTest.java rename to sonar-server/src/test/java/org/sonar/server/platform/RailsAppsDeployerTest.java index 3c5728ef582..daec51fbd2f 100644 --- a/sonar-server/src/test/java/org/sonar/server/plugins/ApplicationDeployerTest.java +++ b/sonar-server/src/test/java/org/sonar/server/platform/RailsAppsDeployerTest.java @@ -17,7 +17,7 @@ * 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.plugins; +package org.sonar.server.platform; import org.apache.commons.io.FileUtils; import org.junit.Rule; @@ -33,11 +33,13 @@ import java.net.URLClassLoader; import java.util.Collections; import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.*; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -public class ApplicationDeployerTest { +public class RailsAppsDeployerTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); @@ -45,19 +47,19 @@ public class ApplicationDeployerTest { @Test public void hasRubyRailsApp() throws Exception { ClassLoader classLoader = new URLClassLoader(new URL[]{ - getClass().getResource("/org/sonar/server/plugins/ApplicationDeployerTest/FakeRubyRailsApp.jar").toURI().toURL()}, null); + getClass().getResource("/org/sonar/server/platform/RailsAppsDeployerTest/FakeRubyRailsApp.jar").toURI().toURL()}, null); - assertTrue(ApplicationDeployer.hasRubyRailsApp("fake", classLoader)); - assertFalse(ApplicationDeployer.hasRubyRailsApp("other", classLoader)); + assertTrue(RailsAppsDeployer.hasRailsApp("fake", classLoader)); + assertFalse(RailsAppsDeployer.hasRailsApp("other", classLoader)); } @Test public void deployRubyRailsApp() throws Exception { File tempDir = this.temp.getRoot(); ClassLoader classLoader = new URLClassLoader(new URL[]{ - getClass().getResource("/org/sonar/server/plugins/ApplicationDeployerTest/FakeRubyRailsApp.jar").toURI().toURL()}, null); + getClass().getResource("/org/sonar/server/platform/RailsAppsDeployerTest/FakeRubyRailsApp.jar").toURI().toURL()}, null); - ApplicationDeployer.deployRubyRailsApp(tempDir, "fake", classLoader); + RailsAppsDeployer.deployRailsApp(tempDir, "fake", classLoader); File appDir = new File(tempDir, "fake"); assertThat(appDir.isDirectory(), is(true)); @@ -76,7 +78,7 @@ public class ApplicationDeployerTest { PluginRepository pluginRepository = mock(PluginRepository.class); when(pluginRepository.getMetadata()).thenReturn(Collections.emptyList()); - new ApplicationDeployer(fileSystem, pluginRepository).start(); + new RailsAppsDeployer(fileSystem, pluginRepository).start(); File appDir = new File(tempDir, "ror"); assertThat(appDir.isDirectory(), is(true)); @@ -90,7 +92,7 @@ public class ApplicationDeployerTest { File tempDir = this.temp.getRoot(); when(fileSystem.getTempDir()).thenReturn(tempDir); - File dir = new ApplicationDeployer(fileSystem, mock(PluginRepository.class)).prepareRubyRailsRootDirectory(); + File dir = new RailsAppsDeployer(fileSystem, mock(PluginRepository.class)).prepareRailsDirectory(); assertThat(dir.isDirectory(), is(true)); assertThat(dir.exists(), is(true)); @@ -106,7 +108,7 @@ public class ApplicationDeployerTest { File file = new File(tempDir, "ror/foo/bar.txt"); FileUtils.writeStringToFile(file, "foooo"); - File dir = new ApplicationDeployer(fileSystem, mock(PluginRepository.class)).prepareRubyRailsRootDirectory(); + File dir = new RailsAppsDeployer(fileSystem, mock(PluginRepository.class)).prepareRailsDirectory(); assertThat(dir.isDirectory(), is(true)); assertThat(dir.exists(), is(true)); diff --git a/sonar-server/src/test/resources/org/sonar/server/plugins/ClassLoaderUtilsTest/ClassLoaderUtilsTest.jar b/sonar-server/src/test/resources/org/sonar/server/platform/ClassLoaderUtilsTest/ClassLoaderUtilsTest.jar similarity index 100% rename from sonar-server/src/test/resources/org/sonar/server/plugins/ClassLoaderUtilsTest/ClassLoaderUtilsTest.jar rename to sonar-server/src/test/resources/org/sonar/server/platform/ClassLoaderUtilsTest/ClassLoaderUtilsTest.jar diff --git a/sonar-server/src/test/resources/org/sonar/server/plugins/ApplicationDeployerTest/FakeRubyRailsApp.jar b/sonar-server/src/test/resources/org/sonar/server/platform/RailsAppsDeployerTest/FakeRubyRailsApp.jar similarity index 100% rename from sonar-server/src/test/resources/org/sonar/server/plugins/ApplicationDeployerTest/FakeRubyRailsApp.jar rename to sonar-server/src/test/resources/org/sonar/server/platform/RailsAppsDeployerTest/FakeRubyRailsApp.jar -- 2.39.5