+ externalization of some configuration properties
authorAlexandre Bertails <alexandre@bertails.org>
Sun, 13 Jun 2010 19:31:21 -0400
changeset 217 af32394cf372
parent 216 054900c2f4d6
child 218 7a169dfe5368
+ externalization of some configuration properties
.hgignore
src/main/resources/database.properties
src/main/resources/ddl.txt
src/main/resources/default-sparql-query.txt
src/main/resources/rdb2rdf.properties
src/main/scala/Config.scala
src/main/scala/Servlet.scala
--- a/.hgignore	Sun Jun 13 18:35:41 2010 -0400
+++ b/.hgignore	Sun Jun 13 19:31:21 2010 -0400
@@ -11,4 +11,3 @@
 *.log
 \#*
 .\#*
-src/main/resources
\ No newline at end of file
--- a/src/main/resources/database.properties	Sun Jun 13 18:35:41 2010 -0400
+++ b/src/main/resources/database.properties	Sun Jun 13 19:31:21 2010 -0400
@@ -1,4 +1,3 @@
-# this file in unmanaged
 host = localhost
 user = root
 password = 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/resources/ddl.txt	Sun Jun 13 19:31:21 2010 -0400
@@ -0,0 +1,3 @@
+CREATE TABLE Employee (empid INT, PRIMARY KEY (empid), lastName STRING, birthday DATE, manager INT, FOREIGN KEY (manager) REFERENCES Employee(empid));
+CREATE TABLE Tasks (taskid INT, PRIMARY KEY (taskid), name STRING, lead INT, FOREIGN KEY (lead) REFERENCES Employee(empid));
+CREATE TABLE TaskAssignments (id INT PRIMARY KEY, PRIMARY KEY (id), task INT, FOREIGN KEY (task) REFERENCES Tasks(taskid), employee INT, FOREIGN KEY (employee) REFERENCES Employee(empid));
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/resources/default-sparql-query.txt	Sun Jun 13 19:31:21 2010 -0400
@@ -0,0 +1,17 @@
+PREFIX empP : <http://hr.example/DB/Employee#>
+PREFIX task : <http://hr.example/DB/Tasks#>
+PREFIX tass : <http://hr.example/DB/TaskAssignments#>
+PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
+SELECT ?name ?bday
+ WHERE { { ?above   tass:employee  ?who .
+           ?above   tass:task      ?atask .
+           ?atask   task:lead      ?taskLead .
+           ?taskLead empP:lastName  ?name }
+         UNION
+         { ?below   tass:task     ?btask .
+           ?btask   task:lead     ?who .
+           ?below   tass:employee ?managed .
+           ?managed empP:lastName  ?name .
+           ?managed empP:birthday  ?bday } 
+         ?who empP:lastName "Smith"^^xsd:string .
+         ?who empP:birthday ?bday }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/resources/rdb2rdf.properties	Sun Jun 13 19:31:21 2010 -0400
@@ -0,0 +1,1 @@
+default-stemuri = http://hr.example/DB/
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/Config.scala	Sun Jun 13 19:31:21 2010 -0400
@@ -0,0 +1,42 @@
+package org.w3.sparql2sql.servlet
+
+import java.util.Properties
+import scala.io.{Source, Codec}
+
+import w3c.sw.sql.{Sql,DatabaseDesc}
+
+trait ConfigHelper {
+
+  class P(p:Properties) {
+    def getProperty(key:String):Option[String] = 
+      try { Some(p.getProperty(key)) } catch {	case _ => None }
+  }
+  
+  def load(filename:String) = {
+    val prop = new Properties
+    prop.load(this.getClass.getResourceAsStream("/"+filename))
+    new P(prop)
+  }
+
+  def getContent(filename:String):String = {
+    val is = this.getClass.getResourceAsStream("/"+filename)
+    Source.fromInputStream(is)(Codec.UTF8).getLines().mkString("\n")
+  }
+
+}
+
+object Config extends ConfigHelper {
+
+  val rdb2rdfProp = load("rdb2rdf.properties")
+
+  val DDLParser = Sql()
+
+  val dbDdl = getContent("ddl.txt")
+
+  val db:DatabaseDesc = DDLParser.parseAll(DDLParser.ddl, dbDdl).get
+
+  val defaultSparqlQuery = getContent("default-sparql-query.txt")
+
+  val defaultStemURI = rdb2rdfProp.getProperty("default-stemuri").get
+
+}
--- a/src/main/scala/Servlet.scala	Sun Jun 13 18:35:41 2010 -0400
+++ b/src/main/scala/Servlet.scala	Sun Jun 13 19:31:21 2010 -0400
@@ -14,58 +14,6 @@
 import w3c.sw.sql
 import w3c.sw.sparql2sql.{SparqlToSql,StemURI,SqlToXMLRes}
 
-object Config {
-
-  // print the classpath, should be just logged
-  val urls = this.getClass.getClassLoader.asInstanceOf[java.net.URLClassLoader].getURLs
-  println("found in classpath:")
-  for(url <- urls) println("\t"+url.getFile)
-
-  private lazy val properties = {
-    val prop = new java.util.Properties
-    prop.load(this.getClass.getResourceAsStream("/calendar.properties"))
-    prop
-  }
-
-  def getProperty(key:String):Option[String] = 
-    try {
-      Some(properties.getProperty(key))
-    } catch {
-      case _ => None
-    }
-
-  val DDLParser = Sql()
-
-  val dbDdl = """
-CREATE TABLE Employee (empid INT, PRIMARY KEY (empid), lastName STRING, birthday DATE, manager INT, FOREIGN KEY (manager) REFERENCES Employee(empid));
-CREATE TABLE Tasks (taskid INT, PRIMARY KEY (taskid), name STRING, lead INT, FOREIGN KEY (lead) REFERENCES Employee(empid));
-CREATE TABLE TaskAssignments (id INT PRIMARY KEY, PRIMARY KEY (id), task INT, FOREIGN KEY (task) REFERENCES Tasks(taskid), employee INT, FOREIGN KEY (employee) REFERENCES Employee(empid));
-"""
-  val db:DatabaseDesc = DDLParser.parseAll(DDLParser.ddl, dbDdl).get
-
-  val defaultSparqlQuery = """PREFIX empP : <http://hr.example/DB/Employee#>
-PREFIX task : <http://hr.example/DB/Tasks#>
-PREFIX tass : <http://hr.example/DB/TaskAssignments#>
-PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
-SELECT ?name ?bday
- WHERE { { ?above   tass:employee  ?who .
-           ?above   tass:task      ?atask .
-           ?atask   task:lead      ?taskLead .
-           ?taskLead empP:lastName  ?name }
-         UNION
-         { ?below   tass:task     ?btask .
-           ?btask   task:lead     ?who .
-           ?below   tass:employee ?managed .
-           ?managed empP:lastName  ?name .
-           ?managed empP:birthday  ?bday } 
-         ?who empP:lastName "Smith"^^xsd:string .
-         ?who empP:birthday ?bday }
-"""
-
-  val defaultStemURI = "http://hr.example/DB/"
-
-}
-
 object Control {
 
   private def using[Closeable <: {def close(): Unit}, B](closeable: Closeable)(getB: Closeable => B): B =