Code refactoring, one place!

This commit is contained in:
Himadri Singh 2013-12-10 02:09:41 +05:30
parent 8117fff4e2
commit b5f6dbc32f
3 changed files with 99 additions and 56 deletions

View File

@ -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<String, String> getExtensionVersions()
private static List<ModuleVersion> getExtensionVersions()
{
final Injector injector = Initialization.makeStartupInjector();
final ExtensionsConfig config = injector.getInstance(ExtensionsConfig.class);
final List<DruidModule> druidModules = Initialization.getFromExtensions(config, DruidModule.class);
Map<String, String> moduleVersions = new HashMap<>();
List<ModuleVersion> 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<String, String> extensionsVersion;
final String version;
final List<ModuleVersion> modules;
final Memory memory;
public Status(
String serverVersion,
Map<String, String> extensionsVersion,
Memory memory
String version, List<ModuleVersion> 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<String, String> getExtensionsVersion()
public List<ModuleVersion> 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;
}
}
}

View File

@ -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)

View File

@ -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());
}
}