Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit 2613b1d

Browse files
committed
Display all version numbers on startup
1 parent 761dfb6 commit 2613b1d

File tree

4 files changed

+69
-29
lines changed

4 files changed

+69
-29
lines changed

src/main/scala/ScalatraBootstrap.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import javax.servlet.ServletContext
2+
import org.codeoverflow.chatoverflow.VersionInfo
23
import org.codeoverflow.chatoverflow.ui.web.rest.config.ConfigController
34
import org.codeoverflow.chatoverflow.ui.web.rest.connector.ConnectorController
45
import org.codeoverflow.chatoverflow.ui.web.rest.events.{EventsController, EventsDispatcher}
@@ -8,11 +9,10 @@ import org.codeoverflow.chatoverflow.ui.web.{CodeOverflowSwagger, GUIServlet, Op
89
import org.scalatra._
910

1011
/**
11-
* This class provides all runtime information for Scalatra. Servlets are mounted here.
12-
*/
12+
* This class provides all runtime information for Scalatra. Servlets are mounted here.
13+
*/
1314
class ScalatraBootstrap extends LifeCycle {
14-
val apiVersion = "3.0.0-3"
15-
implicit val swagger: CodeOverflowSwagger = new CodeOverflowSwagger(apiVersion)
15+
implicit val swagger: CodeOverflowSwagger = new CodeOverflowSwagger(VersionInfo.rest)
1616

1717
override def init(context: ServletContext) {
1818

src/main/scala/org/codeoverflow/chatoverflow/ChatOverflow.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package org.codeoverflow.chatoverflow
33
import java.io.File
44
import java.security.Policy
55

6-
import org.codeoverflow.chatoverflow.api.APIVersion
76
import org.codeoverflow.chatoverflow.configuration.{ConfigurationService, CredentialsService}
87
import org.codeoverflow.chatoverflow.connector.ConnectorRegistry
98
import org.codeoverflow.chatoverflow.framework.PluginFramework
@@ -45,8 +44,9 @@ class ChatOverflow(val pluginFolderPath: String,
4544
def init(): Unit = {
4645
logger info "Minzig!"
4746
logger debug s"Starting Chat Overflow Framework. " +
48-
s"API Version is '${APIVersion.MAJOR_VERSION}.${APIVersion.MINOR_VERSION}'. " +
49-
s"For more information and updates, please visit: http://codeoverflow.org"
47+
"For more information and updates, please visit: http://codeoverflow.org"
48+
49+
VersionInfo.logSummary()
5050

5151
logger debug "Initialization started."
5252

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.codeoverflow.chatoverflow
2+
3+
import org.codeoverflow.chatoverflow.api.APIVersion._
4+
import org.codeoverflow.chatoverflow.ui.web.GUIServlet._
5+
6+
import scala.io.Source
7+
import scala.xml.XML
8+
9+
/**
10+
* VersionInfo stores the version numbers for the api, framework, rest interface, gui and the used rest interface of the gui.
11+
*/
12+
object VersionInfo extends WithLogger {
13+
val api: String = s"$MAJOR_VERSION.$MINOR_VERSION"
14+
val framework: String = {
15+
try {
16+
(XML.load(getClass.getResourceAsStream("/dependencies.pom")) \ "version").text
17+
} catch {
18+
case _: Exception => "unknown"
19+
}
20+
}
21+
val rest: String = "3.0.0-3"
22+
val gui: String = getGUIVersionFile("/version_gui.txt")
23+
val usedRest: String = getGUIVersionFile("/version_gui_rest.txt")
24+
25+
def logSummary(): Unit = {
26+
logger info s"Running Chat Overflow version $framework with api version $api."
27+
logger info s"Providing rest interface with version $rest."
28+
if (gui != "unknown") {
29+
logger info s"GUI version $gui using rest interface version $usedRest detected."
30+
} else {
31+
logger warn "No GUI detected!"
32+
}
33+
if (usedRest != "unknown" && usedRest != rest) {
34+
logger warn "Provided rest interface version and the used one of the GUI differ. " +
35+
"GUI functionality may be restricted!"
36+
}
37+
}
38+
39+
private def getGUIVersionFile(name: String): String = try {
40+
guiJar.map(jar => Source.fromInputStream(jar.getInputStream(jar.getJarEntry(name))).mkString).getOrElse("unknown")
41+
} catch {
42+
case _: Exception => "unknown"
43+
}
44+
}

src/main/scala/org/codeoverflow/chatoverflow/ui/web/GUIServlet.scala

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,35 @@ import java.util.jar.JarFile
66

77
import org.codeoverflow.chatoverflow.WithLogger
88
import org.eclipse.jetty.http.MimeTypes
9-
import org.eclipse.jetty.util.Loader
109
import org.scalatra.{ActionResult, ScalatraServlet}
1110

11+
import scala.util.Try
12+
1213
/**
1314
* A servlet to serve the GUI files of the chatoverflow-gui dir from the classpath.
1415
* This directory is provided if the gui jar is added on the classpath.
1516
* Responds with an error if the gui jar isn't on the classpath.
1617
*/
1718
class GUIServlet extends ScalatraServlet with WithLogger {
1819

19-
private val jarFilePath = {
20-
val res = Loader.getResource(s"/chatoverflow-gui/")
21-
22-
// directory couldn't be found
23-
if (res == null) {
24-
logger error "GUI couldn't be found on the classpath! Has the GUI been built?"
25-
None
26-
} else {
27-
// remove the path inside the jar and only keep the file path to the jar file
28-
val jarPath = res.getFile.split("!").head
29-
logger info s"GUI jar file found at ${new File(".").toURI.relativize(new URI(jarPath))}"
30-
31-
Some(jarPath)
32-
}
33-
}
20+
import GUIServlet.guiJar
3421

3522
get("/*") {
36-
if (jarFilePath.isEmpty) {
23+
if (guiJar.isEmpty) {
3724
ActionResult(500, "GUI couldn't be found on the classpath! Has the GUI been built?", Map("Cache-Control" -> "no-cache,no-store"))
3825
} else {
39-
val jarFile = new JarFile(new File(new URI(jarFilePath.get)))
40-
4126
val path = if (requestPath == "/")
4227
"/index.html"
4328
else
4429
requestPath
4530

46-
val entry = jarFile.getEntry(s"/chatoverflow-gui$path")
31+
val entry = guiJar.get.getEntry(s"/chatoverflow-gui$path")
4732

4833
val res = if (entry == null) {
4934
ActionResult(404, s"Requested file '$path' couldn't be found in the GUI jar!", Map())
5035
} else {
5136
contentType = MimeTypes.getDefaultMimeByExtension(entry.getName)
52-
val is = new BufferedInputStream(jarFile.getInputStream(entry))
37+
val is = new BufferedInputStream(guiJar.get.getInputStream(entry))
5338
val os = response.getOutputStream
5439

5540
Iterator.continually(is.read)
@@ -58,8 +43,19 @@ class GUIServlet extends ScalatraServlet with WithLogger {
5843
}
5944

6045
response.setHeader("Cache-Control", "no-cache,no-store")
61-
jarFile.close()
6246
res
6347
}
6448
}
6549
}
50+
51+
/**
52+
* This companion object holds a reference to the gui jar file.
53+
*/
54+
object GUIServlet extends WithLogger {
55+
val guiJar: Option[JarFile] = Try {
56+
val guiJarPath = getClass.getClassLoader.getResource("/chatoverflow-gui").getFile.split("!").head
57+
58+
logger info s"GUI jar file found at ${new File(".").toURI.relativize(new URI(guiJarPath))}"
59+
new JarFile(new File(new URI(guiJarPath)))
60+
}.toOption
61+
}

0 commit comments

Comments
 (0)