[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] [metrics-web/master] Move config from ernie.properties to deployment descriptor.



Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date: Thu, 7 Oct 2010 16:21:21 +0200
Subject: Move config from ernie.properties to deployment descriptor.
Commit: 413cb6bd9d81fbd373bc2a1836b6428fd15a8def

---
 etc/ernie.properties                               |   16 ----------
 etc/web.xml                                        |   22 ++++++++++++++
 .../torproject/ernie/web/DescriptorServlet.java    |    8 ++--
 src/org/torproject/ernie/web/ErnieProperties.java  |   31 --------------------
 .../ernie/web/ExtraInfoDescriptorServlet.java      |    8 ++--
 src/org/torproject/ernie/web/GraphController.java  |   19 ++++--------
 .../torproject/ernie/web/GraphImageServlet.java    |   12 +++++++-
 .../torproject/ernie/web/RelaySearchServlet.java   |    8 ++--
 src/org/torproject/ernie/web/RelayServlet.java     |    8 ++--
 .../ernie/web/ServerDescriptorServlet.java         |    8 ++--
 10 files changed, 59 insertions(+), 81 deletions(-)
 delete mode 100644 etc/ernie.properties
 delete mode 100644 src/org/torproject/ernie/web/ErnieProperties.java

diff --git a/etc/ernie.properties b/etc/ernie.properties
deleted file mode 100644
index 3b303e0..0000000
--- a/etc/ernie.properties
+++ /dev/null
@@ -1,16 +0,0 @@
-# The Postgres connection string
-jdbc.url=jdbc:postgresql://localhost/tordir?user=ernie&password=password
-
-# The Rserve host
-rserve.host=localhost
-
-# The Rserve port
-rserve.port=6311
-
-# Maximum age in seconds of a graph in the cache
-max.cache.age=21600
-
-# Directory to store cached graph. Must be writable by tomcat
-# and rserve processes
-cached.graphs.dir=/tmp/ernie/
-
diff --git a/etc/web.xml b/etc/web.xml
index be2137a..7e0eccd 100644
--- a/etc/web.xml
+++ b/etc/web.xml
@@ -40,6 +40,22 @@
   <servlet>
     <servlet-name>GraphImageServlet</servlet-name>
     <servlet-class>org.torproject.ernie.web.GraphImageServlet</servlet-class>
+    <init-param>
+      <param-name>rserveHost</param-name>
+      <param-value>localhost</param-value>
+    </init-param>
+    <init-param>
+      <param-name>rservePort</param-name>
+      <param-value>6311</param-value>
+    </init-param>
+    <init-param>
+      <param-name>maxCacheAge</param-name>
+      <param-value>21600</param-value>
+    </init-param>
+    <init-param>
+      <param-name>cachedGraphsDir</param-name>
+      <param-value>/srv/metrics.torproject.org/graphs/</param-value>
+    </init-param>
   </servlet>
   <servlet-mapping>
     <servlet-name>GraphImageServlet</servlet-name>
@@ -137,4 +153,10 @@
     <servlet-name>ErnieGeneratedFile</servlet-name>
     <url-pattern>/consensus-health-2.html</url-pattern>
   </servlet-mapping>
+
+  <context-param>
+    <param-name>jdbcUrl</param-name>
+    <param-value>jdbc:postgresql://localhost/tordir?user=ernie&amp;password=password</param-value>
+  </context-param>
 </web-app>
+
diff --git a/src/org/torproject/ernie/web/DescriptorServlet.java b/src/org/torproject/ernie/web/DescriptorServlet.java
index 6efaef1..71e69e4 100644
--- a/src/org/torproject/ernie/web/DescriptorServlet.java
+++ b/src/org/torproject/ernie/web/DescriptorServlet.java
@@ -16,7 +16,7 @@ public class DescriptorServlet extends HttpServlet {
 
   private Connection conn = null;
 
-  public DescriptorServlet() {
+  public void init() {
 
     /* Try to load the database driver. */
     try {
@@ -27,9 +27,9 @@ public class DescriptorServlet extends HttpServlet {
       return;
     }
 
-    /* Read JDBC URL from property file. */
-    ErnieProperties props = new ErnieProperties();
-    String connectionURL = props.getProperty("jdbc.url");
+    /* Read JDBC URL from deployment descriptor. */
+    String connectionURL = getServletContext().
+        getInitParameter("jdbcUrl");
 
     /* Try to connect to database. */
     try {
diff --git a/src/org/torproject/ernie/web/ErnieProperties.java b/src/org/torproject/ernie/web/ErnieProperties.java
deleted file mode 100644
index 7858285..0000000
--- a/src/org/torproject/ernie/web/ErnieProperties.java
+++ /dev/null
@@ -1,31 +0,0 @@
-package org.torproject.ernie.web;
-
-import java.util.Properties;
-
-public class ErnieProperties  {
-
-  private static final String PROPS_RESOURCE = "ernie.properties";
-  private static Properties props;
-
-  static {
-    props = new Properties();
-    try {
-      props.load(ErnieProperties.class.getClassLoader()
-          .getResourceAsStream(PROPS_RESOURCE));
-    } catch (Exception e) {
-    }
-  }
-
-  public String getProperty(String key) {
-    return props.getProperty(key);
-  }
-
-  public String getProperty(String key, String defaultValue)  {
-    return props.getProperty(key, defaultValue);
-  }
-
-  public int getInt(String key) {
-    String value = getProperty(key);
-    return value == null ? 0 : Integer.parseInt(value);
-  }
-}
diff --git a/src/org/torproject/ernie/web/ExtraInfoDescriptorServlet.java b/src/org/torproject/ernie/web/ExtraInfoDescriptorServlet.java
index d447a7e..aee9be0 100644
--- a/src/org/torproject/ernie/web/ExtraInfoDescriptorServlet.java
+++ b/src/org/torproject/ernie/web/ExtraInfoDescriptorServlet.java
@@ -10,7 +10,7 @@ public class ExtraInfoDescriptorServlet extends HttpServlet {
 
   private Connection conn = null;
 
-  public ExtraInfoDescriptorServlet() {
+  public void init() {
 
     /* Try to load the database driver. */
     try {
@@ -21,9 +21,9 @@ public class ExtraInfoDescriptorServlet extends HttpServlet {
       return;
     }
 
-    /* Read JDBC URL from property file. */
-    ErnieProperties props = new ErnieProperties();
-    String connectionURL = props.getProperty("jdbc.url");
+    /* Read JDBC URL from deployment descriptor. */
+    String connectionURL = getServletContext().
+        getInitParameter("jdbcUrl");
 
     /* Try to connect to database. */
     try {
diff --git a/src/org/torproject/ernie/web/GraphController.java b/src/org/torproject/ernie/web/GraphController.java
index 32b7f4f..01d59de 100644
--- a/src/org/torproject/ernie/web/GraphController.java
+++ b/src/org/torproject/ernie/web/GraphController.java
@@ -8,12 +8,6 @@ import org.rosuda.REngine.*;
 
 public class GraphController {
 
-  /* Singleton instance and getInstance method of this class. */
-  private static GraphController instance = new GraphController();
-  public static GraphController getInstance() {
-    return instance;
-  }
-
   /* Host and port where Rserve is listening. */
   private String rserveHost;
   private int rservePort;
@@ -22,14 +16,13 @@ public class GraphController {
   private String cachedGraphsDirectory;
   private long maxCacheAge;
 
-  protected GraphController ()  {
+  public GraphController(String rserveHost, String rservePort,
+      String maxCacheAge, String cachedGraphsDir) {
 
-    /* Read properties from property file. */
-    ErnieProperties props = new ErnieProperties();
-    this.cachedGraphsDirectory = props.getProperty("cached.graphs.dir");
-    this.maxCacheAge = (long) props.getInt("max.cache.age");
-    this.rserveHost = props.getProperty("rserve.host");
-    this.rservePort = props.getInt("rserve.port");
+    this.rserveHost = rserveHost;
+    this.rservePort = Integer.parseInt(rservePort);
+    this.cachedGraphsDirectory = cachedGraphsDir;
+    this.maxCacheAge = Long.parseLong(maxCacheAge);
   }
 
   /* Generate a graph using the given R query that has a placeholder for
diff --git a/src/org/torproject/ernie/web/GraphImageServlet.java b/src/org/torproject/ernie/web/GraphImageServlet.java
index 8534049..4f0570e 100644
--- a/src/org/torproject/ernie/web/GraphImageServlet.java
+++ b/src/org/torproject/ernie/web/GraphImageServlet.java
@@ -24,7 +24,6 @@ public class GraphImageServlet extends HttpServlet {
   private Map<String, String> knownParameterValues;
 
   public GraphImageServlet()  {
-    this.graphController = GraphController.getInstance();
     this.dateFormat = new SimpleDateFormat("yyyy-MM-dd");
     this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
 
@@ -53,6 +52,17 @@ public class GraphImageServlet extends HttpServlet {
     this.knownParameterValues.put("filesize", "50kb,1mb,5mb");
   }
 
+  public void init() {
+    ServletConfig servletConfig = getServletConfig();
+    String rserveHost = servletConfig.getInitParameter("rserveHost");
+    String rservePort = servletConfig.getInitParameter("rservePort");
+    String maxCacheAge = servletConfig.getInitParameter("maxCacheAge");
+    String cachedGraphsDir = servletConfig.getInitParameter(
+        "cachedGraphsDir");
+    this.graphController = new GraphController(rserveHost, rservePort,
+        maxCacheAge, cachedGraphsDir);
+  }
+
   public void doGet(HttpServletRequest request,
       HttpServletResponse response) throws IOException,
       ServletException {
diff --git a/src/org/torproject/ernie/web/RelaySearchServlet.java b/src/org/torproject/ernie/web/RelaySearchServlet.java
index f6c44a7..ac2ced8 100644
--- a/src/org/torproject/ernie/web/RelaySearchServlet.java
+++ b/src/org/torproject/ernie/web/RelaySearchServlet.java
@@ -60,7 +60,7 @@ public class RelaySearchServlet extends HttpServlet {
 
   private Connection conn = null;
 
-  public RelaySearchServlet() {
+  public void init() {
 
     /* Try to load the database driver. */
     try {
@@ -71,9 +71,9 @@ public class RelaySearchServlet extends HttpServlet {
       return;
     }
 
-    /* Read JDBC URL from property file. */
-    ErnieProperties props = new ErnieProperties();
-    String connectionURL = props.getProperty("jdbc.url");
+    /* Read JDBC URL from deployment descriptor. */
+    String connectionURL = getServletContext().
+        getInitParameter("jdbcUrl");
 
     /* Try to connect to database. */
     try {
diff --git a/src/org/torproject/ernie/web/RelayServlet.java b/src/org/torproject/ernie/web/RelayServlet.java
index 9492090..c59ce1b 100644
--- a/src/org/torproject/ernie/web/RelayServlet.java
+++ b/src/org/torproject/ernie/web/RelayServlet.java
@@ -23,7 +23,7 @@ public class RelayServlet extends HttpServlet {
 
   private Connection conn = null;
 
-  public RelayServlet() {
+  public void init() {
 
     /* Try to load the database driver. */
     try {
@@ -34,9 +34,9 @@ public class RelayServlet extends HttpServlet {
       return;
     }
 
-    /* Read JDBC URL from property file. */
-    ErnieProperties props = new ErnieProperties();
-    String connectionURL = props.getProperty("jdbc.url");
+    /* Read JDBC URL from deployment descriptor. */
+    String connectionURL = getServletContext().
+        getInitParameter("jdbcUrl");
 
     /* Try to connect to database. */
     try {
diff --git a/src/org/torproject/ernie/web/ServerDescriptorServlet.java b/src/org/torproject/ernie/web/ServerDescriptorServlet.java
index 69f5bd6..23e4625 100644
--- a/src/org/torproject/ernie/web/ServerDescriptorServlet.java
+++ b/src/org/torproject/ernie/web/ServerDescriptorServlet.java
@@ -10,7 +10,7 @@ public class ServerDescriptorServlet extends HttpServlet {
 
   private Connection conn = null;
 
-  public ServerDescriptorServlet() {
+  public void init() {
 
     /* Try to load the database driver. */
     try {
@@ -21,9 +21,9 @@ public class ServerDescriptorServlet extends HttpServlet {
       return;
     }
 
-    /* Read JDBC URL from property file. */
-    ErnieProperties props = new ErnieProperties();
-    String connectionURL = props.getProperty("jdbc.url");
+    /* Read JDBC URL from deployment descriptor. */
+    String connectionURL = getServletContext().
+        getInitParameter("jdbcUrl");
 
     /* Try to connect to database. */
     try {
-- 
1.7.1