From 8f336b350518a28a233813b50430ec3a06f3c6f2 Mon Sep 17 00:00:00 2001 From: j2blake Date: Wed, 17 Jul 2013 13:01:08 -0400 Subject: [PATCH] VIVO-213 smoke test to check JVM temp directory If it isn't set, if it isn't a directory, if we can't access it, if we can't create a temp file, complain. --- .../webapp/servlet/setup/JvmSmokeTests.java | 79 +++++++++++++++++++ .../WEB-INF/resources/startup_listeners.txt | 2 + 2 files changed, 81 insertions(+) create mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JvmSmokeTests.java diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JvmSmokeTests.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JvmSmokeTests.java new file mode 100644 index 000000000..c9241320f --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/JvmSmokeTests.java @@ -0,0 +1,79 @@ +/* $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 javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; + +import edu.cornell.mannlib.vitro.webapp.startup.StartupStatus; + +/** + * Test that the JVM is properly configured. + * + * For now, we just test the temp directory. Other + */ +public class JvmSmokeTests implements ServletContextListener { + + @Override + public void contextInitialized(ServletContextEvent sce) { + ServletContext ctx = sce.getServletContext(); + StartupStatus ss = StartupStatus.getBean(ctx); + + checkTempDirectory(ss); + } + + /** + * Check the Java temp directory. Make sure that it exists, it is a + * directory, we can read it, we can write to it. + * + * Create a temp file, and delete it. + */ + private void checkTempDirectory(StartupStatus ss) { + String tempDirPath = System.getProperty("java.io.tmpdir", ""); + if (tempDirPath.isEmpty()) { + ss.fatal(this, "Problem with Java temp directory: " + + "System property 'java.io.tmpdir' is not set."); + return; + } + File tempDir = new File(tempDirPath); + if (!tempDir.exists()) { + ss.fatal(this, "Problem with Java temp directory: '" + tempDirPath + + "' does not exist."); + return; + } + if (!tempDir.isDirectory()) { + ss.fatal(this, "Problem with Java temp directory: '" + tempDirPath + + "' exists, but is not a directory."); + return; + } + if (!tempDir.canRead()) { + ss.fatal(this, "Problem with Java temp directory: " + + "No read access to '" + tempDirPath + "'."); + return; + } + if (!tempDir.canWrite()) { + ss.fatal(this, "Problem with Java temp directory: " + + "No write access to '" + tempDirPath + "'."); + return; + } + try { + File testFile = File.createTempFile("smoke", null); + testFile.delete(); + } catch (Exception e) { + ss.fatal(this, "Problem with Java temp directory: " + + "Failed to create a temporary file in '" + tempDirPath + + "'.", e); + return; + } + ss.info(this, "Java temp directory is '" + tempDirPath + "'"); + } + + @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 202508ebd..a5c581ecc 100644 --- a/webapp/web/WEB-INF/resources/startup_listeners.txt +++ b/webapp/web/WEB-INF/resources/startup_listeners.txt @@ -5,6 +5,8 @@ # https://sourceforge.net/apps/mediawiki/vivo/index.php?title=The_StartupManager # +edu.cornell.mannlib.vitro.webapp.servlet.setup.JvmSmokeTests + edu.cornell.mannlib.vitro.webapp.config.ConfigurationPropertiesSetup edu.cornell.mannlib.vitro.webapp.config.ConfigurationPropertiesSmokeTests