From f3098b2705ac17ef441e10b6e3cbb1c372671a5e Mon Sep 17 00:00:00 2001 From: j2blake Date: Fri, 7 Oct 2011 22:21:22 +0000 Subject: [PATCH] Create basic smoke tests for startup; confirm that the home directory exists and is read/write; confirm that we can create a database connection. --- .../webapp/servlet/setup/BasicSmokeTests.java | 140 ++++++++++++++++++ .../WEB-INF/resources/startup_listeners.txt | 2 + 2 files changed, 142 insertions(+) create mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/BasicSmokeTests.java diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/BasicSmokeTests.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/BasicSmokeTests.java new file mode 100644 index 000000000..3bb00239c --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/BasicSmokeTests.java @@ -0,0 +1,140 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.servlet.setup; + +import java.io.File; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.Properties; + +import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; + +import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties; +import edu.cornell.mannlib.vitro.webapp.startup.StartupStatus; + +public class BasicSmokeTests implements ServletContextListener { + + private static final String PROPERTY_HOME_DIRECTORY = "vitro.home.directory"; + private static final String PROPERTY_DB_URL = "VitroConnection.DataSource.url"; + private static final String PROPERTY_DB_USERNAME = "VitroConnection.DataSource.username"; + private static final String PROPERTY_DB_PASSWORD = "VitroConnection.DataSource.password"; + private static final String PROPERTY_DB_DRIVER_CLASS_NAME = "VitroConnection.DataSource.driver"; + + private static final String DEFAULT_DB_DRIVER_CLASS = "com.mysql.jdbc.Driver"; + + @Override + public void contextInitialized(ServletContextEvent sce) { + ServletContext ctx = sce.getServletContext(); + ConfigurationProperties props = ConfigurationProperties.getBean(ctx); + StartupStatus ss = StartupStatus.getBean(ctx); + + checkHomeDirectory(ctx, props, ss); + checkDatabaseConnection(ctx, props, ss); + } + + /** + * Confirm that: a home directory has been specified; it exists; it is a + * directory; it is readable and writable. + */ + private void checkHomeDirectory(ServletContext ctx, + ConfigurationProperties props, StartupStatus ss) { + String homeDirectoryPath = props.getProperty(PROPERTY_HOME_DIRECTORY); + if (homeDirectoryPath == null || homeDirectoryPath.isEmpty()) { + ss.fatal(this, "deploy.properties does not contain a value for '" + + PROPERTY_HOME_DIRECTORY + "'"); + return; + } + + File homeDirectory = new File(homeDirectoryPath); + if (!homeDirectory.exists()) { + ss.fatal(this, PROPERTY_HOME_DIRECTORY + " '" + homeDirectoryPath + + "' does not exist."); + return; + } + if (!homeDirectory.isDirectory()) { + ss.fatal(this, PROPERTY_HOME_DIRECTORY + " '" + homeDirectoryPath + + "' is not a directory."); + return; + } + + if (!homeDirectory.canRead()) { + ss.fatal(this, PROPERTY_HOME_DIRECTORY + " '" + homeDirectoryPath + + "' cannot be read."); + } + if (!homeDirectory.canWrite()) { + ss.fatal(this, PROPERTY_HOME_DIRECTORY + " '" + homeDirectoryPath + + "' cannot be written to."); + } + } + + /** + * Confirm that the URL, Username and Password have been specified for the + * Database connection. Confirm that we can load the database driver. + * Confirm that we can connect to the database using those properties. + */ + private void checkDatabaseConnection(ServletContext ctx, + ConfigurationProperties props, StartupStatus ss) { + String url = props.getProperty(PROPERTY_DB_URL); + if (url == null || url.isEmpty()) { + ss.fatal(this, "deploy.properties does not contain a value for '" + + PROPERTY_DB_URL + "'"); + return; + } + String username = props.getProperty(PROPERTY_DB_USERNAME); + if (username == null || username.isEmpty()) { + ss.fatal(this, "deploy.properties does not contain a value for '" + + PROPERTY_DB_USERNAME + "'"); + return; + } + String password = props.getProperty(PROPERTY_DB_PASSWORD); + if (password == null || password.isEmpty()) { + ss.fatal(this, "deploy.properties does not contain a value for '" + + PROPERTY_DB_PASSWORD + "'"); + return; + } + + Properties connectionProps = new Properties(); + connectionProps.put("user", username); + connectionProps.put("password", password); + + String driverClassName = props + .getProperty(PROPERTY_DB_DRIVER_CLASS_NAME); + if (driverClassName == null) { + try { + Class.forName(DEFAULT_DB_DRIVER_CLASS).newInstance(); + } catch (Exception e) { + ss.fatal(this, "The default Database Driver failed to load. " + + "The driver class is '" + DEFAULT_DB_DRIVER_CLASS + + "'", e); + } + } else { + try { + Class.forName(driverClassName).newInstance(); + } catch (Exception e) { + ss.fatal(this, "The Database Driver failed to load. " + + "The driver class was set by " + + PROPERTY_DB_DRIVER_CLASS_NAME + " to be '" + + driverClassName + "'", e); + } + } + + Connection conn = null; + try { + conn = DriverManager.getConnection(url, connectionProps); + conn.close(); + } catch (SQLException e) { + ss.fatal(this, "Can't connect to the database: " + PROPERTY_DB_URL + + "='" + url + "', " + PROPERTY_DB_USERNAME + "='" + + username + "'", e); + } + } + + @Override + public void contextDestroyed(ServletContextEvent sce) { + // nothing to do at shutdown + } + +} diff --git a/webapp/web/WEB-INF/resources/startup_listeners.txt b/webapp/web/WEB-INF/resources/startup_listeners.txt index a2bbc32d4..881c24793 100644 --- a/webapp/web/WEB-INF/resources/startup_listeners.txt +++ b/webapp/web/WEB-INF/resources/startup_listeners.txt @@ -7,6 +7,8 @@ edu.cornell.mannlib.vitro.webapp.config.ConfigurationPropertiesSetup +edu.cornell.mannlib.vitro.webapp.servlet.setup.BasicSmokeTests + edu.cornell.mannlib.vitro.webapp.config.RevisionInfoSetup edu.cornell.mannlib.vitro.webapp.email.FreemarkerEmailFactory$Setup