NIHVIVO-927 Merge 5386 from branch.

This commit is contained in:
jeb228 2010-07-22 15:22:18 +00:00
parent 7911fa6490
commit 1c62224b6d
3 changed files with 38 additions and 31 deletions

View file

@ -3,7 +3,6 @@
package edu.cornell.mannlib.vitro.webapp.filestorage.backend; package edu.cornell.mannlib.vitro.webapp.filestorage.backend;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
@ -11,6 +10,9 @@ import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; import javax.servlet.ServletContextListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties; import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
/** /**
@ -18,6 +20,8 @@ import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
* context. * context.
*/ */
public class FileStorageSetup implements ServletContextListener { public class FileStorageSetup implements ServletContextListener {
private static final Log log = LogFactory.getLog(FileStorageSetup.class);
/** /**
* The implementation of the {@link FileStorage} system will be stored in * The implementation of the {@link FileStorage} system will be stored in
* the {@link ServletContext} as an attribute with this name. * the {@link ServletContext} as an attribute with this name.
@ -43,18 +47,16 @@ public class FileStorageSetup implements ServletContextListener {
*/ */
@Override @Override
public void contextInitialized(ServletContextEvent sce) { public void contextInitialized(ServletContextEvent sce) {
FileStorage fs;
try { try {
File baseDirectory = figureBaseDir(); File baseDirectory = figureBaseDir();
Collection<String> fileNamespace = confirmDefaultNamespace(); Collection<String> fileNamespace = confirmDefaultNamespace();
fs = new FileStorageImpl(baseDirectory, fileNamespace); FileStorage fs = new FileStorageImpl(baseDirectory, fileNamespace);
} catch (IOException e) {
throw new IllegalStateException(
"Failed to initialize the file system.", e);
}
ServletContext sc = sce.getServletContext(); ServletContext sc = sce.getServletContext();
sc.setAttribute(ATTRIBUTE_NAME, fs); sc.setAttribute(ATTRIBUTE_NAME, fs);
} catch (Exception e) {
log.fatal("Failed to initialize the file system.", e);
}
} }
/** /**

View file

@ -52,10 +52,9 @@ public class UpdateUploadedFiles implements ServletContextListener {
+ "The ServletContext does not contain an attribute " + "The ServletContext does not contain an attribute "
+ "for '" + "webappDaoFactory" + "'. " + "for '" + "webappDaoFactory" + "'. "
+ "Does the log contain a previous exception from " + "Does the log contain a previous exception from "
+ "JenaDataSourceSetup? Have you looked in " + "JenaDataSourceSetup? Is it possible that web.xml "
+ "localhost.log for such an exception? Is it " + "is not set up to run JenaDataSourceSetup before "
+ "possible that web.xml is not set up to run " + "UpdateUploadedFiles?");
+ "JenaDataSourceSetup before UpdateUploadedFiles?");
} }
OntModel jenaOntModel = (OntModel) ctx OntModel jenaOntModel = (OntModel) ctx
@ -65,10 +64,9 @@ public class UpdateUploadedFiles implements ServletContextListener {
+ "The ServletContext does not contain an attribute " + "The ServletContext does not contain an attribute "
+ "for '" + JENA_ONT_MODEL_ATTRIBUTE_NAME + "'. " + "for '" + JENA_ONT_MODEL_ATTRIBUTE_NAME + "'. "
+ "Does the log contain a previous exception from " + "Does the log contain a previous exception from "
+ "JenaDataSourceSetup? Have you looked in " + "JenaDataSourceSetup? Is it possible that web.xml "
+ "localhost.log for such an exception? Is it " + "is not set up to run JenaDataSourceSetup before "
+ "possible that web.xml is not set up to run " + "UpdateUploadedFiles?");
+ "JenaDataSourceSetup before UpdateUploadedFiles?");
} }
FileStorage fileStorage = (FileStorage) ctx FileStorage fileStorage = (FileStorage) ctx
@ -78,10 +76,9 @@ public class UpdateUploadedFiles implements ServletContextListener {
+ "The ServletContext does not contain an attribute " + "The ServletContext does not contain an attribute "
+ "for '" + FileStorageSetup.ATTRIBUTE_NAME + "'. " + "for '" + FileStorageSetup.ATTRIBUTE_NAME + "'. "
+ "Does the log contain a previous exception from " + "Does the log contain a previous exception from "
+ "FileStorageSetup? Have you looked in " + "FileStorageSetup? Is it possible that web.xml is "
+ "localhost.log for such an exception? Is it " + "not set up to run FileStorageSetup before "
+ "possible that web.xml is not set up to run " + "UpdateUploadedFiles?");
+ "FileStorageSetup before UpdateUploadedFiles?");
} }
String uploadDirectoryName = ConfigurationProperties String uploadDirectoryName = ConfigurationProperties
@ -104,8 +101,7 @@ public class UpdateUploadedFiles implements ServletContextListener {
fileStorage, uploadDirectory, webappImageDirectory); fileStorage, uploadDirectory, webappImageDirectory);
fsu.update(); fsu.update();
} catch (Exception e) { } catch (Exception e) {
log.error("Unknown problem", e); log.fatal("Unknown problem", e);
throw new RuntimeException(e);
} }
} }
} }

View file

@ -3,6 +3,7 @@
package edu.cornell.mannlib.vitro.webapp.filestorage.backend; package edu.cornell.mannlib.vitro.webapp.filestorage.backend;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import java.io.File; import java.io.File;
@ -57,8 +58,8 @@ public class FileStorageSetupTest extends AbstractTestClass {
System.setProperty(InitialContext.INITIAL_CONTEXT_FACTORY, System.setProperty(InitialContext.INITIAL_CONTEXT_FACTORY,
InitialContextFactoryStub.class.getName()); InitialContextFactoryStub.class.getName());
InitialContextStub.reset(); InitialContextStub.reset();
new InitialContext().bind("java:comp/env/path.configuration", propsFile new InitialContext().bind("java:comp/env/path.configuration",
.getPath()); propsFile.getPath());
} }
@Before @Before
@ -77,29 +78,37 @@ public class FileStorageSetupTest extends AbstractTestClass {
// tests // tests
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
@Test(expected = IllegalArgumentException.class) @Test
public void baseDirectoryNotSpecified() { public void baseDirectoryNotSpecified() {
setConfigurationProperties(null, "http://vivo.myDomain.edu/individual/"); setConfigurationProperties(null, "http://vivo.myDomain.edu/individual/");
fss.contextInitialized(sce); fss.contextInitialized(sce);
assertNull("no base directory",
sc.getAttribute(FileStorageSetup.ATTRIBUTE_NAME));
} }
@Test(expected = IllegalArgumentException.class) @Test
public void baseDirectoryDoesntExist() throws IOException { public void baseDirectoryDoesntExist() throws IOException {
setConfigurationProperties("/bogus/Directory", setConfigurationProperties("/bogus/Directory",
"http://vivo.myDomain.edu/individual/"); "http://vivo.myDomain.edu/individual/");
fss.contextInitialized(sce); fss.contextInitialized(sce);
assertNull("no such directory",
sc.getAttribute(FileStorageSetup.ATTRIBUTE_NAME));
} }
@Test(expected = IllegalArgumentException.class) @Test
public void defaultNamespaceNotSpecified() { public void defaultNamespaceNotSpecified() {
setConfigurationProperties(tempDir.getPath(), null); setConfigurationProperties(tempDir.getPath(), null);
fss.contextInitialized(sce); fss.contextInitialized(sce);
assertNull("no default namespace",
sc.getAttribute(FileStorageSetup.ATTRIBUTE_NAME));
} }
@Test(expected = IllegalArgumentException.class) @Test
public void defaultNamespaceIsBogus() throws IOException { public void defaultNamespaceIsBogus() throws IOException {
setConfigurationProperties(tempDir.getPath(), "namespace"); setConfigurationProperties(tempDir.getPath(), "namespace");
fss.contextInitialized(sce); fss.contextInitialized(sce);
assertNull("default namespace is bogus",
sc.getAttribute(FileStorageSetup.ATTRIBUTE_NAME));
} }
@Test @Test
@ -111,8 +120,8 @@ public class FileStorageSetupTest extends AbstractTestClass {
Object o = sc.getAttribute(FileStorageSetup.ATTRIBUTE_NAME); Object o = sc.getAttribute(FileStorageSetup.ATTRIBUTE_NAME);
FileStorage fs = (FileStorage) o; FileStorage fs = (FileStorage) o;
assertEquals("implementation class", FileStorageImpl.class, fs assertEquals("implementation class", FileStorageImpl.class,
.getClass()); fs.getClass());
} }
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------