Skip to content

Commit

Permalink
Fix #541 routes.cache should be saved in tmpDir as configured in xitr…
Browse files Browse the repository at this point in the history
…um.conf
  • Loading branch information
ngocdaothanh committed May 30, 2015
1 parent 0148f5f commit a3996b9
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 17 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
@@ -1,3 +1,8 @@
3.24.1:

* `#541 <https://github.com/xitrum-framework/xitrum/issues/541>`_
routes.cache should be saved in tmpDir as configured in xitrum.conf

3.24.0:

* `#535 <https://github.com/xitrum-framework/xitrum/issues/535>`_
Expand Down
4 changes: 2 additions & 2 deletions build.sbt
Expand Up @@ -6,7 +6,7 @@ crossScalaVersions := Seq("2.11.6", "2.10.5")

// Run sbt mima-report-binary-issues to check for binary compatibility ---------
// http://www.typesafe.com/community/core-tools/migration-manager
version := "3.24.0-SNAPSHOT"
version := "3.24.1-SNAPSHOT"
com.typesafe.tools.mima.plugin.MimaPlugin.mimaDefaultSettings
com.typesafe.tools.mima.plugin.MimaKeys.previousArtifact := Some("tv.cntt" % ("xitrum_" + scalaBinaryVersion.value) % "3.23")

Expand Down Expand Up @@ -53,7 +53,7 @@ libraryDependencies += "tv.cntt" %% "glokka" % "2.3"
libraryDependencies += "com.beachape.filemanagement" %% "schwatcher" % "0.1.8"

// For scanning routes
libraryDependencies += "tv.cntt" %% "sclasner" % "1.6"
libraryDependencies += "tv.cntt" %% "sclasner" % "1.7.0"

// For binary (de)serializing
libraryDependencies += "com.twitter" %% "chill" % "0.6.0"
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala-2.10/xitrum/routing/RouteCollector.scala
Expand Up @@ -23,7 +23,7 @@ case class DiscoveredAcc(
object RouteCollector {
import ActionAnnotations._

def deserializeCacheFileOrRecollect(cachedFileName: String, cl: ClassLoader): DiscoveredAcc = {
def deserializeCacheFileOrRecollect(cachedFile: File, cl: ClassLoader): DiscoveredAcc = {
var acc = DiscoveredAcc(
"<Invalid Xitrum version>",
new SerializableRouteCollection,
Expand All @@ -37,7 +37,7 @@ object RouteCollector {
// is for the (Swagger) annotation inheritance feature. The traits/classes
// are then loaded to get annotations.
val xitrumVersion = xitrum.version.toString
val actionTreeBuilder = Scanner.foldLeft(cachedFileName, new ActionTreeBuilder(xitrumVersion), discovered(cl))
val actionTreeBuilder = Scanner.foldLeft(cachedFile, new ActionTreeBuilder(xitrumVersion), discovered(cl) _)

if (actionTreeBuilder.xitrumVersion != xitrumVersion) {
// The caller should see that the Xitrum version is invalid and act properly
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala-2.11/xitrum/routing/RouteCollector.scala
Expand Up @@ -23,7 +23,7 @@ case class DiscoveredAcc(
object RouteCollector {
import ActionAnnotations._

def deserializeCacheFileOrRecollect(cachedFileName: String, cl: ClassLoader): DiscoveredAcc = {
def deserializeCacheFileOrRecollect(cachedFile: File, cl: ClassLoader): DiscoveredAcc = {
var acc = DiscoveredAcc(
"<Invalid Xitrum version>",
new SerializableRouteCollection,
Expand All @@ -37,7 +37,7 @@ object RouteCollector {
// is for the (Swagger) annotation inheritance feature. The traits/classes
// are then loaded to get annotations.
val xitrumVersion = xitrum.version.toString
val actionTreeBuilder = Scanner.foldLeft(cachedFileName, new ActionTreeBuilder(xitrumVersion), discovered(cl))
val actionTreeBuilder = Scanner.foldLeft(cachedFile, new ActionTreeBuilder(xitrumVersion), discovered(cl) _)

if (actionTreeBuilder.xitrumVersion != xitrumVersion) {
// The caller should see that the Xitrum version is invalid and act properly
Expand Down
11 changes: 5 additions & 6 deletions src/main/scala/xitrum/Config.scala
Expand Up @@ -195,8 +195,9 @@ class XitrumConfig(val config: TConfig) {
}
dir
} else {
// To create a temp directory, we File.createTempFile then
// delete the file and create a new directory there instead
val f = File.createTempFile("xitrum-", "-tmp")
// Delete the file so we can make a new directory there instead
f.delete()

val ret = if (f.mkdirs()) {
Expand Down Expand Up @@ -389,7 +390,7 @@ object Config {

//----------------------------------------------------------------------------

private[this] val ROUTES_CACHE = "tmp/routes.cache"
private[this] val ROUTES_CACHE = new File(xitrum.tmpDir, "routes.cache")

var routes = loadRoutes(Thread.currentThread.getContextClassLoader, false)

Expand All @@ -406,8 +407,7 @@ object Config {
val discoveredAcc = RouteCollector.deserializeCacheFileOrRecollect(ROUTES_CACHE, cl)
if (discoveredAcc.xitrumVersion != _root_.xitrum.version.toString) {
Log.info(s"Xitrum version changed. Delete $ROUTES_CACHE and retry...")
val file = new File(ROUTES_CACHE)
file.delete()
ROUTES_CACHE.delete()
loadRouteCacheFileOrRecollectWithRetry(cl, quiet, true)
} else {
val withSwagger = xitrum.swaggerApiVersion.isDefined
Expand All @@ -420,8 +420,7 @@ object Config {
throw e
} else {
Log.info(s"Could not load $ROUTES_CACHE, delete and retry...")
val file = new File(ROUTES_CACHE)
file.delete()
ROUTES_CACHE.delete()
loadRouteCacheFileOrRecollectWithRetry(cl, quiet, true)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/xitrum/handler/inbound/RouteReloader.scala
Expand Up @@ -8,7 +8,7 @@ import xitrum.util.{ClassFileLoader, FileMonitor}
// In development mode, classes may be reloaded thus routes should also be
// reloaded.
private class RouteReloader {
private val CLASSES_DIRS = sclasner.Discoverer.files.filter(_.isDirectory).map(_.toPath)
private val CLASSES_DIRS = sclasner.Discoverer.containers.filter(_.isDirectory).map(_.toPath)
CLASSES_DIRS.foreach(monitorClassesDir)

private var shouldReloadOnNextRequest = false
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/xitrum/i18n/PoLoader.scala
Expand Up @@ -84,7 +84,7 @@ object PoLoader {
* (for development mode) to reload .po files automatically.
*/
private def watch() {
val searchDirs = Discoverer.files.filter(_.isDirectory) ++ Seq(new File(DEV_RESOURCES_DIR))
val searchDirs = Discoverer.containers.filter(_.isDirectory) ++ Seq(new File(DEV_RESOURCES_DIR))
searchDirs.foreach { dir =>
val withI18n = new File(dir, "i18n")
if (withI18n.exists && withI18n.isDirectory) {
Expand Down
5 changes: 2 additions & 3 deletions src/main/scala/xitrum/util/ClassFileLoader.scala
Expand Up @@ -8,11 +8,10 @@ import sclasner.Discoverer
/**
* This utility is useful for hot reloading .class files in defined directories
* during development.
*
* @param searchDirs Directories to search for .class files, example: Seq("target/scala-2.11/classes")
*/
class ClassFileLoader extends ClassLoader {
private val searchDirs = Discoverer.files.filter(_.isDirectory).map(_.toPath)
// Directories to search for .class files, example: Seq("target/scala-2.11/classes")
private val searchDirs = Discoverer.containers.filter(_.isDirectory).map(_.toPath)

// Need to cache because calling defineClass twice will cause exception
protected val cache = MMap[String, Class[_]]()
Expand Down

0 comments on commit a3996b9

Please sign in to comment.