diff options
author | David A. Madore <david+git@madore.org> | 2014-10-05 00:01:10 +0200 |
---|---|---|
committer | David A. Madore <david+git@madore.org> | 2014-10-05 00:01:10 +0200 |
commit | 81105d8785341817023d3ee2d19f6f814748ae77 (patch) | |
tree | d5d859b0098a5570c32deb38c0e9a7939595c333 | |
parent | 4c2f3f483afb8835e0845046d575adc087dd17e4 (diff) | |
download | damlengine-81105d8785341817023d3ee2d19f6f814748ae77.tar.gz damlengine-81105d8785341817023d3ee2d19f6f814748ae77.tar.bz2 damlengine-81105d8785341817023d3ee2d19f6f814748ae77.zip |
First servlet version that is actually capable of (sometimes) producing some output.
Concurrency is completely broken, however.
-rw-r--r-- | org/madore/damlengine/WeblogDatabaseConnection.java | 79 | ||||
-rw-r--r-- | org/madore/damlengine/WeblogServlet.java | 42 |
2 files changed, 83 insertions, 38 deletions
diff --git a/org/madore/damlengine/WeblogDatabaseConnection.java b/org/madore/damlengine/WeblogDatabaseConnection.java index 900f3a0..fc485a6 100644 --- a/org/madore/damlengine/WeblogDatabaseConnection.java +++ b/org/madore/damlengine/WeblogDatabaseConnection.java @@ -20,68 +20,81 @@ public final class WeblogDatabaseConnection { public static Connection getConnection() throws SQLException { if ( conn == null ) { - String dbHost = System.getenv("DAMLENGINE_PGHOST"); + String dbHost = null; + if ( ! DamlEngine.runAsServlet ) + dbHost = System.getenv("DAMLENGINE_PGHOST"); if ( dbHost == null ) dbHost = DamlEngine.appProps.getProperty("pghost"); - if ( dbHost == null ) + if ( dbHost == null && ! DamlEngine.runAsServlet ) dbHost = System.getenv("PGHOST"); if ( dbHost == null ) dbHost = "localhost"; - String dbPort = System.getenv("DAMLENGINE_PGPORT"); + String dbPort = null; + if ( ! DamlEngine.runAsServlet ) + dbPort = System.getenv("DAMLENGINE_PGPORT"); if ( dbPort == null ) dbPort = DamlEngine.appProps.getProperty("pgport"); - if ( dbPort == null ) + if ( dbPort == null && ! DamlEngine.runAsServlet ) dbPort = System.getenv("PGPORT"); if ( dbPort == null ) dbPort = "5432"; - String dbName = System.getenv("DAMLENGINE_DBNAME"); + String dbName = null; + if ( ! DamlEngine.runAsServlet ) + dbName = System.getenv("DAMLENGINE_DBNAME"); if ( dbName == null ) dbName = DamlEngine.appProps.getProperty("dbname"); if ( dbName == null ) dbName = "weblog"; - String dbUser = System.getenv("DAMLENGINE_PGUSER"); + String dbUser = null; + if ( ! DamlEngine.runAsServlet ) + dbUser = System.getenv("DAMLENGINE_PGUSER"); if ( dbUser == null ) dbUser = DamlEngine.appProps.getProperty("pguser"); - if ( dbUser == null ) + if ( dbUser == null && ! DamlEngine.runAsServlet ) dbUser = System.getenv("PGUSER"); - if ( dbUser == null ) + if ( dbUser == null && ! DamlEngine.runAsServlet ) dbUser = System.getenv("USER"); if ( dbUser == null ) dbUser = System.getProperty("user.name"); if ( dbUser == null ) dbUser = dbName; - String dbPass = System.getenv("DAMLENGINE_PGPASSWORD"); + String dbPass = null; + if ( ! DamlEngine.runAsServlet ) + dbPass = System.getenv("DAMLENGINE_PGPASSWORD"); if ( dbPass == null ) dbPass = DamlEngine.appProps.getProperty("pgpassword"); - if ( dbPass == null ) + if ( dbPass == null && ! DamlEngine.runAsServlet ) dbPass = System.getenv("PGPASSWORD"); - if ( dbPass == null ) - try { - String dbPassFile = System.getenv("DAMLENGINE_PGPASSFILE"); - if ( dbPassFile == null ) - dbPassFile = DamlEngine.appProps.getProperty("pgpassfile"); - if ( dbPassFile == null ) - dbPassFile = System.getenv("PGPASSFILE"); - if ( dbPassFile == null ) - dbPassFile = System.getProperty("user.home") - + "/.pgpass"; + if ( dbPass == null ) { + String dbPassFile = null; + if ( ! DamlEngine.runAsServlet ) + dbPassFile = System.getenv("DAMLENGINE_PGPASSFILE"); + if ( dbPassFile == null ) + dbPassFile = DamlEngine.appProps.getProperty("pgpassfile"); + if ( dbPassFile == null && ! DamlEngine.runAsServlet ) + dbPassFile = System.getenv("PGPASSFILE"); + if ( dbPassFile == null && ! DamlEngine.runAsServlet ) + dbPassFile = System.getProperty("user.home") + + "/.pgpass"; + if ( dbPassFile != null ) try { BufferedReader buf = new BufferedReader(new InputStreamReader(new FileInputStream(dbPassFile))); - String line; - while ( ( line = buf.readLine() ) != null ) { - String[] elts = line.split(":"); - if ( elts.length == 5 - && ( elts[0].equals("*") || elts[0].equals(dbHost) ) - && ( elts[1].equals("*") || elts[1].equals(dbPort) ) - && ( elts[2].equals("*") || elts[2].equals(dbName) ) - && ( elts[3].equals("*") || elts[3].equals(dbUser) ) ) { - dbPass = elts[4]; - break; + String line; + while ( ( line = buf.readLine() ) != null ) { + String[] elts = line.split(":"); + if ( elts.length == 5 + && ( elts[0].equals("*") || elts[0].equals(dbHost) ) + && ( elts[1].equals("*") || elts[1].equals(dbPort) ) + && ( elts[2].equals("*") || elts[2].equals(dbName) ) + && ( elts[3].equals("*") || elts[3].equals(dbUser) ) ) { + dbPass = elts[4]; + break; + } } + } catch (IOException e) { + // Ignore } - } catch (IOException e) { - // Ignore - } + } if ( dbPass == null ) dbPass = ""; final String dbUrl diff --git a/org/madore/damlengine/WeblogServlet.java b/org/madore/damlengine/WeblogServlet.java index 181c782..6b9b738 100644 --- a/org/madore/damlengine/WeblogServlet.java +++ b/org/madore/damlengine/WeblogServlet.java @@ -1,27 +1,58 @@ package org.madore.damlengine; +import java.util.Properties; +import java.util.Enumeration; import java.io.IOException; import java.io.OutputStream; -import java.util.ResourceBundle; -import java.util.Date; -import java.text.DateFormat; -import java.text.SimpleDateFormat; +import java.nio.file.Paths; +import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +/* Thinkos about the mess that is tomcat: + + * - Make sure that /usr/share/java/postgresql-jdbc3.jar (or whatever + * contains the class org.postgresql.Driver) is symlinked from + * /var/lib/tomcat7/common (or some path listed in the common.loader + * property of the catalina.properties file). Similarly, + * xercesImpl.jar and probably xml-resolver.jar need to be linked. + + * - The following parameters need to be defined in <init-param> tags + * inside the <servlet> block: base_path, template_path, pghost, + * pgport, dbname, pguser and pgpass or pgpassfile. + + */ + public class WeblogServlet extends HttpServlet { - private static final long serialVersionUID = 2014100301L; + private static final long serialVersionUID = 2014100406L; @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { + DamlEngine.runAsServlet = true; + DamlEngine.appProps = new Properties(); + ServletConfig cfg = getServletConfig(); + for (Enumeration<String> e = cfg.getInitParameterNames() ; e.hasMoreElements() ; ) { + String k = e.nextElement(); + DamlEngine.appProps.setProperty(k, cfg.getInitParameter(k)); + } + + if ( DamlEngine.appProps.getProperty("base_path") != null ) + DamlEngine.basePath = Paths.get(DamlEngine.appProps.getProperty("base_path")); + if ( DamlEngine.basePath == null ) + DamlEngine.basePath = Paths.get(getServletContext().getRealPath("/")); + if ( DamlEngine.appProps.getProperty("template_path") != null ) + DamlEngine.templatePath = Paths.get(DamlEngine.appProps.getProperty("template_path")); + if ( DamlEngine.templatePath == null ) + DamlEngine.templatePath = DamlEngine.basePath.resolve("templates"); + response.setContentType("text/html; charset=utf-8"); response.setCharacterEncoding("UTF-8"); OutputStream out = response.getOutputStream(); @@ -32,5 +63,6 @@ public class WeblogServlet extends HttpServlet { } catch (Exception e) { throw new ServletException("exception during WeblogSelect.fullProcess()", e); } + } } |