Refactoring commands in separate classes. Change-Id: I2833eeb6497eddc96bdfb7f9e9bebe71a71312bf
diff --git a/src/main/scala/com/googlesource/gerrit/plugins/support/GerritSupportCommands.scala b/src/main/scala/com/googlesource/gerrit/plugins/support/GerritSupportCommands.scala index d589feb..881fa3e 100644 --- a/src/main/scala/com/googlesource/gerrit/plugins/support/GerritSupportCommands.scala +++ b/src/main/scala/com/googlesource/gerrit/plugins/support/GerritSupportCommands.scala
@@ -16,53 +16,47 @@ package com.googlesource.gerrit.plugins.support -import com.google.gerrit.common.Version -import com.google.gson.{Gson, JsonElement, JsonObject, JsonPrimitive} +import com.google.gson.{Gson, JsonElement, JsonObject} import com.google.inject._ -import org.jutils.jhardware.HardwareInfo.{getMemoryInfo, getProcessorInfo} +import org.slf4j.LoggerFactory import scala.util.Try case class CommandResult(entryName: String, content: JsonElement) -trait GerritSupportCommand { - def execute: CommandResult +abstract class GerritSupportCommand { + val log = LoggerFactory.getLogger(classOf[GerritSupportCommand]) + implicit val gson = new Gson + val name = camelToUnderscores(this.getClass.getSimpleName.stripSuffix("Command")) + .stripPrefix("_") + + def getResult: Any + + def execute = { + CommandResult(s"${name}.json", + gson.toJsonTree( + Try { + getResult + } getOrElse { + val error = s"${name} not available on ${System.getProperty("os.name")}" + log.error(error); + ErrorInfo("error" -> error) + })) + } + + private def camelToUnderscores(name: String) = "[A-Z\\d]".r.replaceAllIn(name, { m => + "_" + m.group(0).toLowerCase() + }) } @Singleton class GerritSupportCommandFactory @Inject()(val injector: Injector) { + def apply(name: String): GerritSupportCommand = injector.getInstance( - Class.forName(s"com.googlesource.gerrit.plugins.support.${name.capitalize}Command") + Class.forName(s"com.googlesource.gerrit.plugins.support.commands.${name.capitalize}Command") .asInstanceOf[Class[_ <: GerritSupportCommand]]) -} -class GerritVersionCommand extends GerritSupportCommand { - def execute = CommandResult("version.json", new JsonPrimitive(Version.getVersion)) -} - -class CpuInfoCommand extends GerritSupportCommand { - implicit val gson = new Gson - - def execute = CommandResult("cpu-info.json", - gson.toJsonTree( - Try { - getProcessorInfo - } getOrElse { - ErrorInfo("error" -> s"CPU info not available on ${System.getProperty("os.name")}") - })) -} - -class MemInfoCommand extends GerritSupportCommand { - implicit val gson = new Gson - - def execute = CommandResult("mem-info.json", - gson.toJsonTree( - Try { - getMemoryInfo - } getOrElse { - ErrorInfo("error" -> s"Memory info not available on ${System.getProperty("os.name")}") - })) } object ErrorInfo {
diff --git a/src/main/scala/com/googlesource/gerrit/plugins/support/commands/CpuInfoCommand.scala b/src/main/scala/com/googlesource/gerrit/plugins/support/commands/CpuInfoCommand.scala new file mode 100644 index 0000000..8cfe2ad --- /dev/null +++ b/src/main/scala/com/googlesource/gerrit/plugins/support/commands/CpuInfoCommand.scala
@@ -0,0 +1,24 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.googlesource.gerrit.plugins.support.commands + +import com.googlesource.gerrit.plugins.support.GerritSupportCommand +import org.jutils.jhardware.HardwareInfo + +class CpuInfoCommand extends GerritSupportCommand { + override def getResult = HardwareInfo.getProcessorInfo +} \ No newline at end of file
diff --git a/src/main/scala/com/googlesource/gerrit/plugins/support/commands/GerritVersionCommand.scala b/src/main/scala/com/googlesource/gerrit/plugins/support/commands/GerritVersionCommand.scala new file mode 100644 index 0000000..fe1782d --- /dev/null +++ b/src/main/scala/com/googlesource/gerrit/plugins/support/commands/GerritVersionCommand.scala
@@ -0,0 +1,24 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.googlesource.gerrit.plugins.support.commands + +import com.google.gerrit.common.Version +import com.googlesource.gerrit.plugins.support.GerritSupportCommand + +class GerritVersionCommand extends GerritSupportCommand { + override def getResult = Version.getVersion +} \ No newline at end of file
diff --git a/src/main/scala/com/googlesource/gerrit/plugins/support/commands/MemInfoCommand.scala b/src/main/scala/com/googlesource/gerrit/plugins/support/commands/MemInfoCommand.scala new file mode 100644 index 0000000..bf29439 --- /dev/null +++ b/src/main/scala/com/googlesource/gerrit/plugins/support/commands/MemInfoCommand.scala
@@ -0,0 +1,24 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.googlesource.gerrit.plugins.support.commands + +import com.googlesource.gerrit.plugins.support.GerritSupportCommand +import org.jutils.jhardware.HardwareInfo + +class MemInfoCommand extends GerritSupportCommand { + override def getResult = HardwareInfo.getMemoryInfo +} \ No newline at end of file
diff --git a/src/test/scala/com/googlesource/gerrit/plugins/support/GerritSupportTest.scala b/src/test/scala/com/googlesource/gerrit/plugins/support/GerritSupportTest.scala index db30eae..dddabcf 100644 --- a/src/test/scala/com/googlesource/gerrit/plugins/support/GerritSupportTest.scala +++ b/src/test/scala/com/googlesource/gerrit/plugins/support/GerritSupportTest.scala
@@ -21,6 +21,7 @@ import com.google.gson.{Gson, JsonPrimitive} import com.googlesource.gerrit.plugins.support.FileMatchers._ +import com.googlesource.gerrit.plugins.support.commands._ import org.scalatest.{FlatSpec, Matchers} import scala.collection.JavaConverters._