NIHVIVO-510 Create the FileStorageSetup initializer, instead of the FileStorageFactory

This commit is contained in:
jeb228 2010-06-03 21:16:03 +00:00
parent bcf06634ee
commit 5b9bf808c8
9 changed files with 459 additions and 328 deletions

View file

@ -6,24 +6,10 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
/**
* The interface for the File Storage system.
*/
public interface FileStorage {
/**
* The default implementation will use this key to ask
* {@link ConfigurationProperties} for the file storage base directory.
*/
String PROPERTY_FILE_STORAGE_BASE_DIR = "upload.directory";
/**
* The default implementation will use this key to ask
* {@link ConfigurationProperties} for the default URI namespace.
*/
String PROPERTY_DEFAULT_NAMESPACE = "Vitro.defaultNamespace";
/**
* The name of the root directory, within the base directory.
*/

View file

@ -1,56 +0,0 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.filestorage.backend;
import java.io.IOException;
/**
* Create an instance of {@link FileStorage} -- either the default
* implementation, or one specified by a system property.
*/
public class FileStorageFactory {
/**
* If this system property is set, it will be taken as the name of the
* implementing class.
*/
public static final String PROPERTY_IMPLEMETATION_CLASSNAME = FileStorage.class
.getName();
/**
* <p>
* Get an instance of {@link FileStorage}. By default, this will be an
* instance of {@link FileStorageImpl}.
* </p>
* <p>
* If the System Property named by
* {#SYSTEM_PROPERTY_IMPLEMETATION_CLASSNAME} is set, it must contain the
* name of the implementation class, which must be a sub-class of
* {@link FileStorage}, and must have a public, no-argument constructor.
* </p>
*/
public static FileStorage getFileStorage() throws IOException {
String className = System.getProperty(PROPERTY_IMPLEMETATION_CLASSNAME);
if (className == null) {
return new FileStorageImpl();
}
try {
Class<?> clazz = Class.forName(className);
Object instance = clazz.newInstance();
return FileStorage.class.cast(instance);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException(
"Can't create a FileStorage instance", e);
} catch (ClassCastException e) {
throw new IllegalArgumentException(
"Can't create a FileStorage instance", e);
} catch (InstantiationException e) {
throw new IllegalArgumentException(
"Can't create a FileStorage instance", e);
} catch (IllegalAccessException e) {
throw new IllegalArgumentException(
"Can't create a FileStorage instance", e);
}
}
}

View file

@ -16,7 +16,6 @@ import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@ -24,8 +23,6 @@ import java.util.Properties;
import java.util.Set;
import java.util.Map.Entry;
import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
/**
* The default implementation of {@link FileStorage}.
*/
@ -40,21 +37,6 @@ public class FileStorageImpl implements FileStorage {
// Constructors and helper methods.
// ----------------------------------------------------------------------
/**
* Use the configuration properties to create an instance.
*
* @throws IllegalArgumentException
* if the configuration property for the base directory is
* missing, or if it doesn't point to an existing, writeable
* directory.
* @throws IllegalArgumentException
* if the configuration property for the default namespace is
* missing, or if it isn't in the expected form.
*/
FileStorageImpl() throws IOException {
this(figureBaseDir(), figureFileNamespace());
}
/**
* Use the arguments to create an instance. If the base directory is empty,
* initialize it. Otherwise, check that it was initialized to the same
@ -123,57 +105,6 @@ public class FileStorageImpl implements FileStorage {
}
}
/**
* Get the configuration property for the file storage base directory, and
* check that it points to an existing, writeable directory.
*
* For use by the constructor in implementations of {@link FileStorage}.
*/
static File figureBaseDir() {
String baseDirPath = ConfigurationProperties
.getProperty(PROPERTY_FILE_STORAGE_BASE_DIR);
if (baseDirPath == null) {
throw new IllegalArgumentException(
"Configuration properties must contain a value for '"
+ PROPERTY_FILE_STORAGE_BASE_DIR + "'");
}
return new File(baseDirPath);
}
/**
* Get the configuration property for the default namespace, and derive the
* file namespace from it. The default namespace is assumed to be in this
* form: <code>http://vivo.mydomain.edu/individual/</code>
*
* For use by the constructor in implementations of {@link FileStorage}.
*
* @returns the file namespace is assumed to be in this form:
* <code>http://vivo.mydomain.edu/file/</code>
*/
static Collection<String> figureFileNamespace() {
String defaultNamespace = ConfigurationProperties
.getProperty(PROPERTY_DEFAULT_NAMESPACE);
if (defaultNamespace == null) {
throw new IllegalArgumentException(
"Configuration properties must contain a value for '"
+ PROPERTY_DEFAULT_NAMESPACE + "'");
}
String defaultSuffix = "/individual/";
String fileSuffix = "/file/";
if (!defaultNamespace.endsWith(defaultSuffix)) {
throw new IllegalArgumentException(
"Default namespace does not match the expected form: '"
+ defaultNamespace + "'");
}
int hostLength = defaultNamespace.length() - defaultSuffix.length();
String fileNamespace = defaultNamespace.substring(0, hostLength)
+ fileSuffix;
return Collections.singleton(fileNamespace);
}
/**
* Assign arbitrary prefixes to these namespaces.
*/

View file

@ -0,0 +1,116 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.filestorage.backend;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
/**
* Initializes the file storage system, and stores a reference in the servlet
* context.
*/
public class FileStorageSetup implements ServletContextListener {
/**
* The implementation of the {@link FileStorage} system will be stored in
* the {@link ServletContext} as an attribute with this name.
*/
public static final String ATTRIBUTE_NAME = FileStorage.class.getName();
/**
* The default implementation will use this key to ask
* {@link ConfigurationProperties} for the file storage base directory.
*/
static final String PROPERTY_FILE_STORAGE_BASE_DIR = "upload.directory";
/**
* The default implementation will use this key to ask
* {@link ConfigurationProperties} for the default URI namespace.
*/
static final String PROPERTY_DEFAULT_NAMESPACE = "Vitro.defaultNamespace";
/**
* Create an implementation of {@link FileStorage} and store it in the
* {@link ServletContext}, as an attribute named according to
* {@link #ATTRIBUTE_NAME}.
*/
@Override
public void contextInitialized(ServletContextEvent sce) {
FileStorage fs;
try {
File baseDirectory = figureBaseDir();
Collection<String> fileNamespace = figureFileNamespace();
fs = new FileStorageImpl(baseDirectory, fileNamespace);
} catch (IOException e) {
throw new IllegalStateException(
"Failed to initialize the file system.", e);
}
ServletContext sc = sce.getServletContext();
sc.setAttribute(ATTRIBUTE_NAME, fs);
}
/**
* Get the configuration property for the file storage base directory, and
* check that it points to an existing, writeable directory.
*
* For use by the constructor in implementations of {@link FileStorage}.
*/
private File figureBaseDir() {
String baseDirPath = ConfigurationProperties
.getProperty(PROPERTY_FILE_STORAGE_BASE_DIR);
if (baseDirPath == null) {
throw new IllegalArgumentException(
"Configuration properties must contain a value for '"
+ PROPERTY_FILE_STORAGE_BASE_DIR + "'");
}
return new File(baseDirPath);
}
/**
* Get the configuration property for the default namespace, and derive the
* file namespace from it. The default namespace is assumed to be in this
* form: <code>http://vivo.mydomain.edu/individual/</code>
*
* For use by the constructor in implementations of {@link FileStorage}.
*
* @returns the file namespace is assumed to be in this form:
* <code>http://vivo.mydomain.edu/file/</code>
*/
private Collection<String> figureFileNamespace() {
String defaultNamespace = ConfigurationProperties
.getProperty(PROPERTY_DEFAULT_NAMESPACE);
if (defaultNamespace == null) {
throw new IllegalArgumentException(
"Configuration properties must contain a value for '"
+ PROPERTY_DEFAULT_NAMESPACE + "'");
}
String defaultSuffix = "/individual/";
String fileSuffix = "/file/";
if (!defaultNamespace.endsWith(defaultSuffix)) {
throw new IllegalArgumentException(
"Default namespace does not match the expected form: '"
+ defaultNamespace + "'");
}
int hostLength = defaultNamespace.length() - defaultSuffix.length();
String fileNamespace = defaultNamespace.substring(0, hostLength)
+ fileSuffix;
return Collections.singleton(fileNamespace);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
// Nothing to do here.
}
}