~ one can override default parameters on the command line
authorAlexandre Bertails <bertails@w3.org>
Fri, 05 Nov 2010 18:35:18 -0400
changeset 269 c12793e7e6ba
parent 268 691822da279e
child 270 00f76f463c09
~ one can override default parameters on the command line
sparql2sqlendpoint/src/main/scala/Configuration.scala
sparql2sqlendpoint/src/main/scala/Servlet.scala
--- a/sparql2sqlendpoint/src/main/scala/Configuration.scala	Fri Nov 05 18:34:30 2010 -0400
+++ b/sparql2sqlendpoint/src/main/scala/Configuration.scala	Fri Nov 05 18:35:18 2010 -0400
@@ -7,7 +7,7 @@
 
 // adapted from http://github.com/codahale/fig
 
-object Configuration {
+object JsonBasedConfiguration {
   /**
    * returns the content of a file, if accessible from the classpath
    */
@@ -42,7 +42,7 @@
  *
  * @author coda
  */
-class Configuration(filename: String) {
+class JsonBasedConfiguration(filename: String) {
   case class Value(path: String, value: JsonAST.JValue) {
     /**
      * Returns the value as an instance of type A.
@@ -93,11 +93,27 @@
   }
 
   private val json = {
-    val content = Configuration.option(Source.fromFile(new File(filename)).mkString) getOrElse Configuration.fromFileInClasspath(filename)
+    val content = JsonBasedConfiguration.option(Source.fromFile(new File(filename)).mkString) getOrElse JsonBasedConfiguration.fromFileInClasspath(filename)
     JsonParser.parse(content.replaceAll("""(^//.*|[\s]+//.*)""", ""))
   }
 
   /**
+   * returns all the available keys from the configuration file
+   */
+  val availableKeys:List[String] = {
+    def getKeys(json:JsonAST.JValue):List[String] = {
+      json match {
+        case JsonAST.JField(name, value) if value.children.size == 0 =>
+          List(name)
+        case JsonAST.JField(name, value) =>
+          value.children flatMap { getKeys(_) } map { name + "." + _ }
+        case _ => List()
+      }
+    }
+    json.children flatMap { getKeys(_) }
+  }
+  
+  /**
    * Given a dot-notation JSON path (e.g., "parent.child.fieldname"), returns
    * a Value which can be converted into a specific type or Option thereof.
    */
--- a/sparql2sqlendpoint/src/main/scala/Servlet.scala	Fri Nov 05 18:34:30 2010 -0400
+++ b/sparql2sqlendpoint/src/main/scala/Servlet.scala	Fri Nov 05 18:35:18 2010 -0400
@@ -99,51 +99,39 @@
 
 import org.w3.util.fig._
 
-trait Config {
+trait Configuration {
+  def getConfigurationValue(key:String):String
+  def availableKeys:List[String]
+}
 
-  val config = new Configuration("default-config.json")
-  Class.forName(config("db.driver").as[String]).newInstance
+trait DefaultJsonConfiguration extends Configuration {
+
+  val config = new JsonBasedConfiguration("default-config.json")
+
+  def getConfigurationValue(key:String) = config(key).as[String]
+
+  val availableKeys = config.availableKeys
+
+  Class.forName(getConfigurationValue("db.driver")).newInstance
 
   def getConnection():Connection =
-    DriverManager.getConnection(config("db.url").as[String],
-                                config("db.user").as[String],
-                                config("db.password").as[String])
+    DriverManager.getConnection(getConfigurationValue("db.url"),
+                                getConfigurationValue("db.user"),
+                                getConfigurationValue("db.password"))
   
-  val defaultStemURI:String = config("ui.stemuri").as[String]
+  val defaultStemURI:String = getConfigurationValue("ui.stemuri")
 
-  val defaultSparqlQuery:String = config("ui.sparql-query").as[String]
+  val defaultSparqlQuery:String = getConfigurationValue("ui.sparql-query")
 
   val db:org.w3.sw.rdb.RDB.Database = {
     val DDLParser = new org.w3.sw.sql.SqlParser()
-    val ddl = config("default.ddl").as[String]
-    println(ddl)
+    val ddl = getConfigurationValue("default.ddl")
     DDLParser.parseAll(DDLParser.ddl, ddl).get
   }
 
 }
 
-object SPARQL2SQLEndpoint {
-
-  import org.eclipse.jetty.server.Server
-  import org.eclipse.jetty.servlet.{ServletContextHandler, ServletHolder}
-
-  /* see http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty */
-  def main(args: Array[String]) {
-
-    val server:Server = new Server(8080)
-    val context:ServletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS)
-    context.setContextPath("/")
-    server.setHandler(context)
-  
-    context.addServlet(new ServletHolder(new SPARQL2SQLEndpoint()),"/*");
-    server.start
-    server.join
-
-  }
-
-}
-
-class SPARQL2SQLEndpoint extends HttpServlet with Config {
+class SPARQL2SQLEndpoint extends HttpServlet with DefaultJsonConfiguration {
 
   val encoding = "utf-8"
 
@@ -213,3 +201,45 @@
   }
 
 }
+
+object SPARQL2SQLEndpoint {
+
+  import org.eclipse.jetty.server.Server
+  import org.eclipse.jetty.servlet.{ServletContextHandler, ServletHolder}
+
+  /**
+   * returns the value associated to a key
+   * example: javar -jar mybundle.jar -db.user root
+   */
+  def getElementAfter(args: List[String], key:String):Option[String] = {
+    args match {
+      case head :: tail if head == key => tail.headOption
+      case head :: tail => getElementAfter(tail, key)
+      case Nil => None
+    }
+  }
+
+  def main(args: Array[String]) {
+
+    val endpoint = new SPARQL2SQLEndpoint() {
+      override def getConfigurationValue(key:String) = getElementAfter(args.toList, "-"+key) getOrElse super.getConfigurationValue(key)
+    }
+
+    val message = endpoint.availableKeys map { key => "* " + key } mkString(start="You can override the following parameters by adding -<parameter> <value> on the command line:\n",
+                                                                            sep="\n",
+                                                                            end="")
+    println(message)
+
+    /* see http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty */
+    val server:Server = new Server(8080)
+    val context:ServletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS)
+    context.setContextPath("/")
+    server.setHandler(context)
+    
+    context.addServlet(new ServletHolder(endpoint),"/*");
+    server.start
+    server.join
+
+  }
+
+}