~ externalize the ResourceManager stuff in its own file
authorAlexandre Bertails <bertails@w3.org>
Wed, 24 Aug 2011 14:18:55 -0400
changeset 61 95ad8e0f7ae0
parent 60 66c931fabb4f
child 62 7cf163bf2865
~ externalize the ResourceManager stuff in its own file
src/main/scala/Main.scala
src/main/scala/Util.scala
--- a/src/main/scala/Main.scala	Wed Aug 24 14:14:48 2011 -0400
+++ b/src/main/scala/Main.scala	Wed Aug 24 14:18:55 2011 -0400
@@ -1,5 +1,7 @@
 package org.w3.virtualtrainer
 
+import org.w3.virtualtrainer.util.ResourceManager.fromClasspath
+
 import unfiltered.request._
 import unfiltered.response._
 import unfiltered.scalate._
@@ -10,69 +12,9 @@
 import com.novus.salat.global._
 import com.mongodb.casbah.Imports._
 
-import java.io.{File, FileWriter}
 import org.fusesource.scalate.TemplateEngine
-import java.util.jar._
-import scala.collection.JavaConversions._
-import scala.io.Source
-import java.net.{URL, URLDecoder}
 import org.slf4j.{Logger, LoggerFactory}
 
-object ResourceManager {
-  
-  val logger:Logger = LoggerFactory.getLogger(this.getClass)
-
-  val clazz:Class[_] = this.getClass
-  val classloader = this.getClass.getClassLoader
-  
-  /** http://www.uofr.net/~greg/java/get-resource-listing.html
-   */
-  def getResourceListing(path:String):List[String] = {
-    var dirURL:URL = classloader.getResource(path)
-    if (dirURL != null && dirURL.getProtocol == "file") {
-      /* A file path: easy enough */
-      new File(dirURL.toURI).list.toList
-    } else {
-      if (dirURL == null) {
-        val me = clazz.getName().replace(".", "/")+".class"
-        dirURL = classloader.getResource(me)
-      }
-      if (dirURL.getProtocol == "jar") {
-        val jarPath = dirURL.getPath.substring(5, dirURL.getPath().indexOf("!"))
-        val jar:JarFile = new JarFile(URLDecoder.decode(jarPath, "UTF-8"))
-        val entries = jar.entries filter { _.getName startsWith path } map { e => {
-          var entry = e.getName substring path.length
-          val checkSubdir = entry indexOf "/"
-          if (checkSubdir >= 0) entry = entry.substring(0, checkSubdir)
-          entry
-        } }
-        entries filterNot { _.isEmpty } toList
-      } else
-        sys.error("Cannot list files for URL "+dirURL);
-    }
-  }
-  
-  /** extract a path found in the classpath
-   * 
-   *  @return the file on disk
-   */
-  def fromClasspath(path:String):File = {
-    val dir = new File("/tmp", "virtual-trainer-" + scala.util.Random.nextInt(10000).toString)
-    if (! dir.mkdir()) logger.error("Couldn't extract %s from jar to %s" format (path, dir.getAbsolutePath))
-    val entries = getResourceListing(path) foreach { entry =>
-      val url = classloader.getResource(path + entry)
-      val content = Source.fromURL(url, "UTF-8").getLines.mkString("\n")
-      val writer = new FileWriter(new File(dir, entry))
-      writer.write(content)
-      writer.close()
-    }
-    dir
-  }
-  
-}
-
-import ResourceManager.fromClasspath
-
 object VirtualTrainerApp {
   
   val logger:Logger = LoggerFactory.getLogger(this.getClass)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/Util.scala	Wed Aug 24 14:18:55 2011 -0400
@@ -0,0 +1,62 @@
+package org.w3.virtualtrainer.util
+
+import java.io.{File, FileWriter}
+import java.util.jar._
+import scala.collection.JavaConversions._
+import scala.io.Source
+import java.net.{URL, URLDecoder}
+import org.slf4j.{Logger, LoggerFactory}
+
+/** useful stuff to read resources from the classpath */
+object ResourceManager {
+  
+  val logger:Logger = LoggerFactory.getLogger(this.getClass)
+
+  val clazz:Class[_] = this.getClass
+  val classloader = this.getClass.getClassLoader
+  
+  /** http://www.uofr.net/~greg/java/get-resource-listing.html
+   */
+  def getResourceListing(path:String):List[String] = {
+    var dirURL:URL = classloader.getResource(path)
+    if (dirURL != null && dirURL.getProtocol == "file") {
+      /* A file path: easy enough */
+      new File(dirURL.toURI).list.toList
+    } else {
+      if (dirURL == null) {
+        val me = clazz.getName().replace(".", "/")+".class"
+        dirURL = classloader.getResource(me)
+      }
+      if (dirURL.getProtocol == "jar") {
+        val jarPath = dirURL.getPath.substring(5, dirURL.getPath().indexOf("!"))
+        val jar:JarFile = new JarFile(URLDecoder.decode(jarPath, "UTF-8"))
+        val entries = jar.entries filter { _.getName startsWith path } map { e => {
+          var entry = e.getName substring path.length
+          val checkSubdir = entry indexOf "/"
+          if (checkSubdir >= 0) entry = entry.substring(0, checkSubdir)
+          entry
+        } }
+        entries filterNot { _.isEmpty } toList
+      } else
+        sys.error("Cannot list files for URL "+dirURL);
+    }
+  }
+  
+  /** extract a path found in the classpath
+   * 
+   *  @return the file on disk
+   */
+  def fromClasspath(path:String):File = {
+    val dir = new File("/tmp", "virtual-trainer-" + scala.util.Random.nextInt(10000).toString)
+    if (! dir.mkdir()) logger.error("Couldn't extract %s from jar to %s" format (path, dir.getAbsolutePath))
+    val entries = getResourceListing(path) foreach { entry =>
+      val url = classloader.getResource(path + entry)
+      val content = Source.fromURL(url, "UTF-8").getLines.mkString("\n")
+      val writer = new FileWriter(new File(dir, entry))
+      writer.write(content)
+      writer.close()
+    }
+    dir
+  }
+  
+}
\ No newline at end of file