From b5f6dbc32f9fdd3745f78da9ea7353c308de4b1b Mon Sep 17 00:00:00 2001 From: Himadri Singh Date: Tue, 10 Dec 2013 02:09:41 +0530 Subject: [PATCH] Code refactoring, one place! --- .../java/io/druid/server/StatusResource.java | 117 +++++++++++++++--- services/src/main/java/io/druid/cli/Main.java | 6 - .../src/main/java/io/druid/cli/Version.java | 32 +---- 3 files changed, 99 insertions(+), 56 deletions(-) diff --git a/server/src/main/java/io/druid/server/StatusResource.java b/server/src/main/java/io/druid/server/StatusResource.java index 8fb6733d97f..f2c01050496 100644 --- a/server/src/main/java/io/druid/server/StatusResource.java +++ b/server/src/main/java/io/druid/server/StatusResource.java @@ -28,12 +28,8 @@ import io.druid.server.initialization.ExtensionsConfig; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; -import java.io.IOException; -import java.io.InputStream; -import java.util.HashMap; +import java.util.ArrayList; import java.util.List; -import java.util.Map; -import java.util.Properties; /** */ @@ -43,6 +39,11 @@ public class StatusResource @GET @Produces("application/json") public Status doGet() + { + return getStatus(); + } + + public static Status getStatus() { return new Status( Initialization.class.getPackage().getImplementationVersion(), @@ -56,46 +57,55 @@ public class StatusResource * * @return map of extensions loaded with their respective implementation versions. */ - private Map getExtensionVersions() + private static List getExtensionVersions() { final Injector injector = Initialization.makeStartupInjector(); final ExtensionsConfig config = injector.getInstance(ExtensionsConfig.class); final List druidModules = Initialization.getFromExtensions(config, DruidModule.class); - Map moduleVersions = new HashMap<>(); + + List moduleVersions = new ArrayList<>(); for (DruidModule module : druidModules) { - Package pkg = module.getClass().getPackage(); - moduleVersions.put(pkg.getImplementationTitle(), pkg.getImplementationVersion()); + + String artifact = module.getClass().getPackage().getImplementationTitle(); + String version = module.getClass().getPackage().getImplementationVersion(); + + ModuleVersion moduleVersion; + if (artifact != null) { + moduleVersion = new ModuleVersion(module.getClass().getCanonicalName(), artifact, version); + } else { + moduleVersion = new ModuleVersion(module.getClass().getCanonicalName()); + } + + moduleVersions.add(moduleVersion); } return moduleVersions; } public static class Status { - final String serverVersion; - final Map extensionsVersion; + final String version; + final List modules; final Memory memory; public Status( - String serverVersion, - Map extensionsVersion, - Memory memory + String version, List modules, Memory memory ) { - this.serverVersion = serverVersion; - this.extensionsVersion = extensionsVersion; + this.version = version; + this.modules = modules; this.memory = memory; } @JsonProperty - public String getServerVersion() + public String getVersion() { - return serverVersion; + return version; } @JsonProperty - public Map getExtensionsVersion() + public List getModules() { - return extensionsVersion; + return modules; } @JsonProperty @@ -103,6 +113,72 @@ public class StatusResource { return memory; } + + @Override + public String toString() + { + final String NL = "\n"; + StringBuilder output = new StringBuilder(); + output.append(String.format("Druid version - %s", version)).append(NL).append(NL); + + if (modules.size() > 0) { + output.append("Registered Druid Modules").append(NL); + } else { + output.append("No Druid Modules loaded !"); + } + + for (ModuleVersion moduleVersion : modules) { + output.append(moduleVersion).append(NL); + } + return output.toString(); + } + } + + public static class ModuleVersion + { + final String name; + final String artifact; + final String version; + + public ModuleVersion(String name) + { + this(name, "", ""); + } + + public ModuleVersion(String name, String artifact, String version) + { + this.name = name; + this.artifact = artifact; + this.version = version; + } + + @JsonProperty + public String getName() + { + return name; + } + + @JsonProperty + public String getArtifact() + { + return artifact; + } + + @JsonProperty + public String getVersion() + { + return version; + } + + @Override + public String toString() + { + if (artifact.isEmpty()) { + return String.format(" - %s ", name); + } else { + return String.format(" - %s (%s-%s)", name, artifact, version); + } + } } public static class Memory @@ -143,5 +219,6 @@ public class StatusResource { return usedMemory; } + } } diff --git a/services/src/main/java/io/druid/cli/Main.java b/services/src/main/java/io/druid/cli/Main.java index 91b56a83977..7e9a633e8ed 100644 --- a/services/src/main/java/io/druid/cli/Main.java +++ b/services/src/main/java/io/druid/cli/Main.java @@ -45,12 +45,6 @@ public class Main .withDefaultCommand(Help.class) .withCommands(Help.class, Version.class); - Runnable cmd = builder.build().parse(args); - if (cmd instanceof Version) { - Logger.getRootLogger().setLevel(Level.OFF); - } - - builder.withGroup("server") .withDescription("Run one of the Druid server types.") .withDefaultCommand(Help.class) diff --git a/services/src/main/java/io/druid/cli/Version.java b/services/src/main/java/io/druid/cli/Version.java index 89306fc42fc..9212a3cb800 100644 --- a/services/src/main/java/io/druid/cli/Version.java +++ b/services/src/main/java/io/druid/cli/Version.java @@ -20,11 +20,7 @@ package io.druid.cli; import io.airlift.command.Command; -import io.druid.initialization.DruidModule; -import io.druid.initialization.Initialization; -import io.druid.server.initialization.ExtensionsConfig; - -import java.lang.StringBuilder; +import io.druid.server.StatusResource; @Command( name = "version", @@ -32,33 +28,9 @@ import java.lang.StringBuilder; ) public class Version implements Runnable { - private static final String NL = "\n"; @Override public void run() { - StringBuilder output = new StringBuilder(); - output.append("Druid version ").append(NL); - output.append(Initialization.class.getPackage().getImplementationVersion()).append(NL).append(NL); - - ExtensionsConfig config = Initialization.makeStartupInjector().getInstance(ExtensionsConfig.class); - - output.append("Registered Druid Modules").append(NL); - - for (DruidModule module : Initialization.getFromExtensions(config, DruidModule.class)) { - String artifact = module.getClass().getPackage().getImplementationTitle(); - String version = module.getClass().getPackage().getImplementationVersion(); - - if (artifact != null) { - output.append( - String.format(" - %s (%s-%s)", module.getClass().getCanonicalName(), artifact, version) - ).append(NL); - } else { - output.append( - String.format(" - %s", module.getClass().getCanonicalName()) - ).append(NL); - } - } - - System.out.println(output.toString()); + System.out.println(StatusResource.getStatus()); } }