NIHVIVO-15 Merge three configuration properties files into one. Create ConfigProperties as a central mechanism for reading configuration properties.

This commit is contained in:
jeb228 2010-02-03 15:09:52 +00:00
parent 863992f4ce
commit a874f4b88a
21 changed files with 382 additions and 585 deletions

View file

@ -0,0 +1,144 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.log4j.Logger;
/**
* Loads the configuration properties from a properties file. The path to the
* file is specified an an Environment name in the Context, like this:
*
* <pre>
* &lt;Context override="true">
* &lt;Environment name="path.configuration"
* value="/wherever/the/file/lives/deploy.properties"
* type="java.lang.String"
* override="false" />
* &lt;/Context>
* </pre>
*
* This initializer will look for a file at this path. If it does not find one,
* it will look for a resource at this path. So, one might reasonable set this
* to a value like <code>/usr/local/vitro/stuff/deploy.properties</code> for a
* file, or like <code>deploy.properties</code> for a resource in the classpath.
*
* @author jeb228
*/
public class ConfigurationProperties {
private static final Logger LOG = Logger
.getLogger(ConfigurationProperties.class);
/**
* The name of the JNDI environment mapping for the path to the
* configuration file (or resource).
*/
private static final String PATH_CONFIGURATION = "path.configuration";
private static volatile Map<String, String> theMap;
static {
try {
// Obtain our environment naming context
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
// Get the name of the configuration properties file.
String configPath = (String) envCtx.lookup(PATH_CONFIGURATION);
if (configPath == null) {
throw new IllegalStateException(
"Could not find a JNDI Environment naming for '"
+ PATH_CONFIGURATION + "'.");
}
InputStream inStream = null;
// Try to find this as a file.
File file = new File(configPath);
try {
inStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
inStream = null;
}
// If no file, try to find it as a resource.
if (inStream == null) {
inStream = ConfigurationProperties.class.getClassLoader()
.getResourceAsStream(configPath);
}
// If neither file nor resource, give up.
if (inStream == null) {
throw new IllegalArgumentException(
"Failed to find a configuration properties file at '"
+ file.getAbsolutePath()
+ "', or a resource at '" + configPath + "'");
}
// Load a properties object - it will handle the syntax of the file.
Properties props = new Properties();
try {
props.load(inStream);
} catch (IOException e) {
throw new IllegalStateException("Problem while reading the "
+ "configuration properties file at '" + configPath
+ "'", e);
}
// It's awkward to copy from Properties to a Map.
Map<String, String> newMap = new HashMap<String, String>();
for (Enumeration<?> keys = props.keys(); keys.hasMoreElements();) {
String key = (String) keys.nextElement();
newMap.put(key, props.getProperty(key));
}
LOG.info("Configuration properties are: " + newMap);
// Save an unmodifiable version of the Map
theMap = Collections.unmodifiableMap(newMap);
} catch (NamingException e) {
throw new IllegalStateException(e);
}
}
/**
* Get an unmodifiable copy of the map of configuration properties.
*/
public static Map<String, String> getMap() {
return theMap;
}
/**
* Get the value of the specified property, or <code>null</code> if the
* property has not been assigned a value.
*/
public static String getProperty(String key) {
return theMap.get(key);
}
/**
* Get the value of the specified property, or use the default value if the
* property has not been assigned a value.
*/
public static String getProperty(String key, String defaultValue) {
String value = theMap.get(key);
if (value == null) {
return defaultValue;
} else {
return value;
}
}
}

View file

@ -33,7 +33,8 @@ public class CommentsController extends VitroHttpServlet{
if (!ContactMailServlet.isSmtpHostConfigured()) {
request.setAttribute("title", "Comments and Feedback Form");
request.setAttribute("bodyJsp", "/contact_err.jsp");// <<< this is where the body gets set
request.setAttribute("ERR","This application has not yet been configured to send mail -- smtp host has not been specified in connection.properties");
request.setAttribute("ERR","This application has not yet been configured to send mail -- " +
"smtp host has not been specified in the configuration properties file.");
RequestDispatcher errd = request.getRequestDispatcher(Controllers.BASIC_JSP);
errd.forward(request, response);
}

View file

@ -2,12 +2,8 @@
package edu.cornell.mannlib.vitro.webapp.controller;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;
@ -22,31 +18,29 @@ import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import com.hp.hpl.jena.ontology.OntModel;
import edu.cornell.mannlib.vitro.webapp.auth.policy.JenaNetidPolicy.ContextSetup;
import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
import edu.cornell.mannlib.vitro.webapp.beans.Portal;
import edu.cornell.mannlib.vitro.webapp.controller.BrowseController.RebuildGroupCacheThread;
public class ContactMailServlet extends VitroHttpServlet {
private static final Logger LOG = Logger.getLogger(ContactMailServlet.class);
public static HttpServletRequest request;
public static HttpServletRequest response;
protected final static String CONNECTION_PROP_LOCATION = "/WEB-INF/classes/connection.properties";
private static String smtpHost = null;
private static final Log log = LogFactory.getLog(ContactMailServlet.class.getName());
public void init(ServletConfig servletConfig) throws javax.servlet.ServletException {
super.init(servletConfig);
ServletContext sContext = servletConfig.getServletContext();
smtpHost = getSmtpHostFromPropertiesFile(sContext.getRealPath(CONNECTION_PROP_LOCATION));
smtpHost = getSmtpHostFromProperties();
}
public static boolean isSmtpHostConfigured() {
@ -56,43 +50,15 @@ public class ContactMailServlet extends VitroHttpServlet {
return true;
}
private String getSmtpHostFromPropertiesFile(final String filename){
if (filename == null || filename.length() <= 0) {
throw new Error(
"To establish the Contact Us mail capability you must "
+ "specify an SMTP server host name in the "
+ "connection.properties file along with the "
+ "database connection parameters.");
}
File propF = new File(filename );
InputStream is;
try {
is = new FileInputStream(propF);
} catch (FileNotFoundException e) {
log.error("Could not load file "+filename);
throw new Error("Could not load file " + filename
+ '\n' + e.getMessage());
}
Properties dbProps = new Properties();
try {
dbProps.load(is);
String host = dbProps.getProperty("Vitro.smtpHost");
/* doesn't display in catalina.out, not sure why
if (host!=null && !host.equals("")){
System.out.println("Found Vitro.smtpHost value of "+host+" in "+filename);
} else {
System.out.println("No Vitro.smtpHost specified in "+filename);
} */
return (host != null && host.length()>0) ? host : null;
} catch (IOException e) {
throw new Error("Could not load any properties from file " + filename + '\n'
+ e.getMessage());
}
}
private String getSmtpHostFromProperties() {
String host = ConfigurationProperties.getProperty("Vitro.smtpHost");
if (host != null && !host.equals("")) {
LOG.info("Found Vitro.smtpHost value of " + host);
} else {
System.out.println("No Vitro.smtpHost specified");
}
return (host != null && host.length() > 0) ? host : null;
}
public void doGet( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException {
@ -104,7 +70,8 @@ if (host!=null && !host.equals("")){
String status = null; // holds the error status
if (smtpHost==null || smtpHost.equals("")){
status = "This application has not yet been configured to send mail -- smtp host has not been identified in connection.properties";
status = "This application has not yet been configured to send mail " +
"-- smtp host has not been identified in the Configuration Properties file.";
response.sendRedirect( "test?bodyJsp=" + errpage + "&ERR=" + status + "&home=" + portal.getPortalId() );
return;
}

View file

@ -4,7 +4,6 @@ package edu.cornell.mannlib.vitro.webapp.controller;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
@ -31,12 +30,11 @@ import org.apache.commons.logging.LogFactory;
import com.hp.hpl.jena.ontology.OntModel;
import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
import edu.cornell.mannlib.vitro.webapp.beans.DataProperty;
import edu.cornell.mannlib.vitro.webapp.beans.DataPropertyStatement;
import edu.cornell.mannlib.vitro.webapp.beans.DataPropertyStatementImpl;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.controller.edit.N3MultiPartUpload;
import edu.cornell.mannlib.vitro.webapp.controller.edit.N3MultiPartUpload.PostUpload;
import edu.cornell.mannlib.vitro.webapp.dao.IndividualDao;
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
@ -54,7 +52,6 @@ import fedora.server.types.gen.Datastream;
*/
public class FedoraDatastreamController extends VitroHttpServlet implements Constants{
private static String FEDORA_PROPERTIES = "/WEB-INF/fedora.properties";
private static String FEDORA_FOXML_1_1 = "info:fedora/fedora-system:FOXML-1.1";
private static String DEFAULT_DSID = "DS1";
private String fedoraUrl = null;
@ -71,7 +68,6 @@ public class FedoraDatastreamController extends VitroHttpServlet implements Cons
private static String fileUriPrefix = DEFAULT_FILE_URI_PREFIX;
private static String baseDirectoryForFiles = DEFAULT_BASE_DIR;
private static int maxFileSize = DEFAULT_MAX_SIZE;
private static PostUpload postUpload = null;
/**
* The get will present a form to the user.
@ -394,59 +390,23 @@ public class FedoraDatastreamController extends VitroHttpServlet implements Cons
}
}
public void init() throws ServletException {
super.init();
String realPropFile = getServletContext().getRealPath(
N3MultiPartUpload.FILE_UPLOAD_PROP_FILE);
File propF = new File(realPropFile);
if (propF != null && propF.exists() && propF.isFile()) {
if (!propF.canRead()) {
log.error("There is a file upload configuration file at "
+ realPropFile + " but is is not readable");
return;
}
public void init() throws ServletException {
super.init();
InputStream is;
try {
is = new FileInputStream(propF);
} catch (FileNotFoundException e) {
log.error("Could not load file " + realPropFile, e);
return;
}
fileUriPrefix = ConfigurationProperties.getProperty(
"n3.defaultUriPrefix", DEFAULT_FILE_URI_PREFIX);
baseDirectoryForFiles = ConfigurationProperties.getProperty(
"n3.baseDirectoryForFiles", DEFAULT_BASE_DIR);
Properties props = new Properties();
try {
props.load(is);
is.close();
} catch (IOException e) {
log.error(
"could not load properties from file " + realPropFile,
e);
}
fileUriPrefix = props.getProperty("defaultUriPrefix",
DEFAULT_FILE_URI_PREFIX);
baseDirectoryForFiles = props.getProperty("baseDirectoryForFiles",
DEFAULT_BASE_DIR);
// String postUploadProcess = props.getProperty("postUploadProcess");
// System.out.println("Attempting to load postUploadProcess "
// + postUploadProcess);
// postUpload = getPostUpload(postUploadProcess);
String maxSize = props.getProperty("maxSize", Long
.toString(DEFAULT_MAX_SIZE));
try {
maxFileSize = Integer.parseInt(maxSize);
} catch (NumberFormatException nfe) {
log.error(nfe);
maxFileSize = DEFAULT_MAX_SIZE;
}
} else {
System.out
.println("No properties file found for N3MultiPartUpload at "
+ realPropFile);
}
}
String maxSize = ConfigurationProperties.getProperty("n3.maxSize", Long
.toString(DEFAULT_MAX_SIZE));
try {
maxFileSize = Integer.parseInt(maxSize);
} catch (NumberFormatException nfe) {
log.error(nfe);
maxFileSize = DEFAULT_MAX_SIZE;
}
}
private void setup(OntModel model, ServletContext context) {
this.configurationStatus = "";

View file

@ -9,8 +9,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@ -45,7 +43,7 @@ import com.hp.hpl.jena.shared.Lock;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.RDFS;
import edu.cornell.mannlib.vitro.webapp.controller.FedoraDatastreamController;
import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet;
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
import edu.cornell.mannlib.vitro.webapp.dao.jena.event.EditEvent;
@ -96,44 +94,18 @@ public class N3MultiPartUpload extends VitroHttpServlet {
@Override
public void init() throws ServletException {
super.init();
String realPropFile = getServletContext().getRealPath(
FILE_UPLOAD_PROP_FILE);
File propF = new File(realPropFile);
if (propF != null && propF.exists() && propF.isFile()) {
if (!propF.canRead()) {
log.error("There is a file upload configuration file at "
+ realPropFile + " but is is not readable");
return;
}
InputStream is;
try {
is = new FileInputStream(propF);
} catch (FileNotFoundException e) {
log.error("Could not load file " + realPropFile, e);
return;
}
Properties props = new Properties();
try {
props.load(is);
is.close();
} catch (IOException e) {
log.error(
"could not load properties from file " + realPropFile,
e);
}
fileUriPrefix = props.getProperty("defaultUriPrefix",
fileUriPrefix = ConfigurationProperties.getProperty("n3.defaultUriPrefix",
DEFAULT_FILE_URI_PREFIX);
baseDirectoryForFiles = props.getProperty("baseDirectoryForFiles",
baseDirectoryForFiles = ConfigurationProperties.getProperty("n3.baseDirectoryForFiles",
DEFAULT_BASE_DIR);
String postUploadProcess = props.getProperty("postUploadProcess");
String postUploadProcess = ConfigurationProperties.getProperty("n3.postUploadProcess");
System.out.println("Attempting to load postUploadProcess "
+ postUploadProcess);
postUpload = getPostUpload(postUploadProcess);
String maxSize = props.getProperty("maxSize", Long
String maxSize = ConfigurationProperties.getProperty("n3.maxSize", Long
.toString(DEFAULT_MAX_SIZE));
try {
maxFileSize = Integer.parseInt(maxSize);
@ -141,11 +113,6 @@ public class N3MultiPartUpload extends VitroHttpServlet {
log.error(nfe);
maxFileSize = DEFAULT_MAX_SIZE;
}
} else {
System.out
.println("No properties file found for N3MultiPartUpload at "
+ realPropFile);
}
}
@Override
@ -624,8 +591,6 @@ public class N3MultiPartUpload extends VitroHttpServlet {
static Random random = new Random();
public static final String FILE_UPLOAD_PROP_FILE = "/WEB-INF/N3MultiPartUpload.properties";
private static Property FILE_LOCATION_PROP = ResourceFactory
.createProperty(VitroVocabulary.FILE_LOCATION);

View file

@ -31,6 +31,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vedit.beans.LoginFormBean;
import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
@ -42,10 +43,6 @@ public class UploadImagesServlet extends VitroHttpServlet {
private static final Log log = LogFactory.getLog(UploadImagesServlet.class.getName());
private String sourceDirName; // all uploaded images are copied to the source directory, not just the application context
private String websiteDirName; // the application context
private String webAppName; // the name of the application context (e.g., vivo, usaep)
private static String UPLOAD_SERVLET_PROPERTIES = "/upload.properties";
private static String UPLOAD_SERVLET_PROPERTIES_WIN="\\upload.properties";
/**
* Notice that init() gets called the first time the servlet is requested,
@ -57,15 +54,6 @@ public class UploadImagesServlet extends VitroHttpServlet {
// something like: /usr/local/tomcat/webapps/vivo
websiteDirName = getServletContext().getRealPath("");
//get the last directory, which should be the webapp name.
String[] dirs = new String[0];
if (File.separator.equals("\\")) {
dirs = websiteDirName.split("\\\\");
} else {
dirs = websiteDirName.split(File.separator);
}
webAppName = dirs[dirs.length-1];
// something like: /usr/local/src/Vitro/dream/common/web
try{
sourceDirName = getSourceDirName();
@ -546,24 +534,9 @@ public class UploadImagesServlet extends VitroHttpServlet {
* @throws IOException
*/
private String getSourceDirName() throws IOException{
Properties props = new Properties();
InputStream raw = this.getClass().getResourceAsStream( UPLOAD_SERVLET_PROPERTIES );
if (raw == null) {
raw = this.getClass().getResourceAsStream( UPLOAD_SERVLET_PROPERTIES_WIN );
if (raw == null)
throw new IOException("UploadImagesServlet.getSourceDirName()" +
" Failed to find resource: " + UPLOAD_SERVLET_PROPERTIES );
}
try{
props.load( raw );
} catch (Exception ex){
throw new IOException("unable to load upload.properties file: " + ex.getMessage());
} finally {
raw.close();
}
String dirName = props.getProperty("UploadImagesServlet.sourceDirName");
String dirName = ConfigurationProperties.getProperty("UploadImagesServlet.sourceDirName");
if( dirName == null ) {
log.error("getSourceDirName(): property sourceDirName not defined in upload.properties");
log.error("getSourceDirName(): property sourceDirName not defined in configuration properties");
} else {
File dir = new File(dirName);
if(!dir.exists()) {

View file

@ -4,11 +4,9 @@ package edu.cornell.mannlib.vitro.webapp.search.lucene;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
@ -18,6 +16,7 @@ import org.apache.commons.logging.LogFactory;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.search.BooleanQuery;
import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
import edu.cornell.mannlib.vitro.webapp.beans.BaseResourceBean.RoleLevel;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
import edu.cornell.mannlib.vitro.webapp.dao.filtering.WebappDaoFactoryFiltering;
@ -55,13 +54,13 @@ public class LuceneSetup implements javax.servlet.ServletContextListener {
/**
* Gets run to set up DataSource when the webapp servlet context gets created.
*/
@SuppressWarnings({ "static-access", "unchecked" })
@SuppressWarnings("unchecked")
public void contextInitialized(ServletContextEvent sce) {
try {
ServletContext context = sce.getServletContext();
log.info("**** Running "+this.getClass().getName()+".contextInitialized()");
indexDir = getIndexDirName(sce.getServletContext());
indexDir = getIndexDirName();
log.info("Directory of full text index: " + indexDir );
setBoolMax();
@ -135,62 +134,40 @@ public class LuceneSetup implements javax.servlet.ServletContextListener {
BooleanQuery.setMaxClauseCount(16384);
}
/** Directory to store lucene index use when none is specified. */
private String DEFAULT_INDEX_DIR = "/fullTextIndex";
/** name of the properties file to look for in the 'resources' */
private String LUCENE_PROPERTIES = "/LuceneSetup.properties";
/**
* Gets the name of the directory to store the lucene index in.
* This is stored in a file named LuceneSetup.properties
* which should be on the classpath in the default package.
* That file should have a property named 'LuceneSetup.indexDir'
* which has the directory to store the lucene index for this
* clone in. If the property file is not found or the
* LuceneSetup.indexDir is not found, then DEFAULT_INDEX_DIR will
* be used.
* @param servletContext
* @return a string that is the directory to store the lucene
* index.
*
* @throws IOException
*/
private String getIndexDirName(ServletContext servletContext) {
String dirName = null;
// Check if we can get the properties file
Properties props = new Properties();
InputStream raw = this.getClass().getResourceAsStream( LUCENE_PROPERTIES );
if (raw != null){
try{
props.load( raw );
}catch (Exception ex){
log.info("Could not load properties file " + LUCENE_PROPERTIES);
props = null;
}finally {
try{ raw.close(); }
catch(Exception ex){}
}
// Check if we can get the indexDir property from the file
if( props != null ){
dirName = props.getProperty("LuceneSetup.indexDir");
if( dirName == null ){
log.info("LuceneSetup.indexDir not found in file " + LUCENE_PROPERTIES + " using default.");
}
}
}
if( dirName == null ){
dirName = servletContext.getRealPath(DEFAULT_INDEX_DIR);
File dir = new File( dirName);
if( !dir.exists() )
dir.mkdir();
}
return dirName;
}
/**
* Gets the name of the directory to store the lucene index in. The
* {@link ConfigurationProperties} should have a property named
* 'LuceneSetup.indexDir' which has the directory to store the lucene index
* for this clone in. If the property is not found, an exception will be
* thrown.
*
* @return a string that is the directory to store the lucene index.
* @throws IllegalStateException
* if the property is not found.
* @throws IOException
* if the directory doesn't exist and we fail to create it.
*/
private String getIndexDirName()
throws IOException {
String dirName = ConfigurationProperties
.getProperty("LuceneSetup.indexDir");
if (dirName == null) {
throw new IllegalStateException(
"LuceneSetup.indexDir not found in properties file.");
}
File dir = new File(dirName);
if (!dir.exists()) {
boolean created = dir.mkdir();
if (!created) {
throw new IOException(
"Unable to create Lucene index directory at '" + dir
+ "'");
}
}
return dirName;
}
/**
* Gets the analyzer that will be used when building the indexing
@ -198,7 +175,6 @@ public class LuceneSetup implements javax.servlet.ServletContextListener {
*
* @return
*/
@SuppressWarnings("static-access")
private Analyzer getAnalyzer() {
return new VitroAnalyzer();
}

View file

@ -2,12 +2,11 @@
package edu.cornell.mannlib.vitro.webapp.search.lucene;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
@ -18,6 +17,7 @@ import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cjk.CJKAnalyzer;
import org.apache.lucene.search.BooleanQuery;
import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
import edu.cornell.mannlib.vitro.webapp.beans.BaseResourceBean.RoleLevel;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
import edu.cornell.mannlib.vitro.webapp.dao.filtering.WebappDaoFactoryFiltering;
@ -55,7 +55,7 @@ public class LuceneSetupCJK implements javax.servlet.ServletContextListener {
/**
* Gets run to set up DataSource when the webapp servlet context gets created.
*/
@SuppressWarnings({ "static-access", "unchecked" })
@SuppressWarnings("unchecked")
public void contextInitialized(ServletContextEvent sce) {
ServletContext context = sce.getServletContext();
log.info("**** Running "+this.getClass().getName()+".contextInitialized()");
@ -130,53 +130,40 @@ public class LuceneSetupCJK implements javax.servlet.ServletContextListener {
BooleanQuery.setMaxClauseCount(16384);
}
/** directory to use when none is specified */
private String DEFAULT_INDEX_DIR = "/usr/local/lucene/vitrodefault";
/** name of the properties file to look for in the 'resources' */
private String LUCENE_PROPERTIES = "/LuceneSetup.properties";
/**
* Gets the name of the directory to store the lucene index in.
* This is stored in a file named LuceneSetup.properties
* which should be on the classpath in the default package.
* That file should have a property named 'LuceneSetup.indexDir'
* which has the directory to store the lucene index for this
* clone in. If the property file is not found or the
* LuceneSetup.indexDir is not found, then DEFAULT_INDEX_DIR will
* be used.
* @return a string that is the directory to store the lucene
* index.
*
* @throws IOException
*/
private String getIndexDirName() {
Properties props = new Properties();
InputStream raw = this.getClass().getResourceAsStream( LUCENE_PROPERTIES );
if (raw == null){
log.warn("LuceneSetup.getIndexDirName()" +
" Failed to find resource: " + LUCENE_PROPERTIES +
". Using default directory " + DEFAULT_INDEX_DIR);
return DEFAULT_INDEX_DIR;
}
try{ props.load( raw ); }
catch (Exception ex){
log.error("LuceneSetup.getIndexDirName()" +
"unable to load properties: \n" + ex.getMessage() +
"\nUsing default directory " + DEFAULT_INDEX_DIR);
return DEFAULT_INDEX_DIR;
}
finally { try{raw.close();} catch(Exception ex){} }
String dirName = props.getProperty("LuceneSetup.indexDir");
if( dirName == null ){
log.error("LuceneSetup.getIndexDir: " +
"indexDir not found. Using default directory "+DEFAULT_INDEX_DIR );
return DEFAULT_INDEX_DIR;
}
return dirName;
}
/**
* Gets the name of the directory to store the lucene index in. The
* {@link ConfigurationProperties} should have a property named
* 'LuceneSetup.indexDir' which has the directory to store the lucene index
* for this clone in. If the property is not found, an exception will be
* thrown.
*
* @return a string that is the directory to store the lucene index.
* @throws IllegalStateException
* if the property is not found.
* @throws IOException
* if the directory doesn't exist and we fail to create it.
*/
private String getIndexDirName()
throws IOException {
String dirName = ConfigurationProperties
.getProperty("LuceneSetup.indexDir");
if (dirName == null) {
throw new IllegalStateException(
"LuceneSetup.indexDir not found in properties file.");
}
File dir = new File(dirName);
if (!dir.exists()) {
boolean created = dir.mkdir();
if (!created) {
throw new IOException(
"Unable to create Lucene index directory at '" + dir
+ "'");
}
}
return dirName;
}
/**
* Gets the analyzer that will be used when building the indexing
@ -184,7 +171,6 @@ public class LuceneSetupCJK implements javax.servlet.ServletContextListener {
*
* @return
*/
@SuppressWarnings("static-access")
private Analyzer getAnalyzer() {
return new CJKAnalyzer();
}

View file

@ -2,29 +2,21 @@
package edu.cornell.mannlib.vitro.webapp.servlet.setup;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.hp.hpl.jena.graph.Graph;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import edu.cornell.mannlib.vitro.webapp.auth.policy.JenaNetidPolicy.ContextSetup;
import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
import edu.cornell.mannlib.vitro.webapp.dao.jena.RDBGraphGenerator;
import edu.cornell.mannlib.vitro.webapp.dao.jena.RegeneratingGraph;
@ -42,8 +34,6 @@ public class JenaDataSourceSetupBase {
DEFAULT_TESTONRETURN = true,
DEFAULT_TESTWHILEIDLE = true;
protected final static String CONNECTION_PROP_LOCATION = "/WEB-INF/classes/connection.properties";
protected static String BASE = "/WEB-INF/ontologies/";
protected static String USERPATH = BASE+"user/";
protected static String SYSTEMPATH = BASE+"system/";
@ -53,73 +43,43 @@ public class JenaDataSourceSetupBase {
String DB_PASSWD = "jenatest"; // database password
String DB = "MySQL"; // database type
String DB_DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
String JENA_DB_MODEL = "http://vitro.mannlib.cornell.edu/default/vitro-kb-2";
String JENA_AUDIT_MODEL = "http://vitro.mannlib.cornell.edu/ns/db/experimental/audit";
String JENA_INF_MODEL = "http://vitro.mannlib.cornell.edu/default/vitro-kb-inf";
String JENA_USER_ACCOUNTS_MODEL = "http://vitro.mannlib.cornell.edu/default/vitro-kb-userAccounts";
String JENA_APPLICATION_METADATA_MODEL = "http://vitro.mannlib.cornell.edu/default/vitro-kb-applicationMetadata";
static final String JENA_DB_MODEL = "http://vitro.mannlib.cornell.edu/default/vitro-kb-2";
static final String JENA_AUDIT_MODEL = "http://vitro.mannlib.cornell.edu/ns/db/experimental/audit";
static final String JENA_INF_MODEL = "http://vitro.mannlib.cornell.edu/default/vitro-kb-inf";
static final String JENA_USER_ACCOUNTS_MODEL = "http://vitro.mannlib.cornell.edu/default/vitro-kb-userAccounts";
static final String JENA_APPLICATION_METADATA_MODEL = "http://vitro.mannlib.cornell.edu/default/vitro-kb-applicationMetadata";
static final String DEFAULT_DEFAULT_NAMESPACE = "http://vitro.mannlib.cornell.edu/ns/default#";
static String defaultNamespace = DEFAULT_DEFAULT_NAMESPACE; // TODO: improve this
// use OWL models with no reasoning
OntModelSpec DB_ONT_MODEL_SPEC = OntModelSpec.OWL_MEM;
OntModelSpec MEM_ONT_MODEL_SPEC = OntModelSpec.OWL_MEM;
static final OntModelSpec DB_ONT_MODEL_SPEC = OntModelSpec.OWL_MEM;
static final OntModelSpec MEM_ONT_MODEL_SPEC = OntModelSpec.OWL_MEM;
private static final Log log = LogFactory.getLog(JenaDataSourceSetupBase.class.getName());
/**
* Sets up a Model and DB connection using values from
* a properties file.
*/
public final Model makeDBModelFromPropertiesFile(final String filename) {
return makeDBModelFromPropertiesFile(filename, null, null);
}
/**
* Sets up a Model and DB connection using values from
* a properties file.
*/
public final Model makeDBModelFromPropertiesFile(final String filename, String jenaDbModelName, OntModelSpec jenaDbOntModelSpec){
if (filename == null || filename.length() <= 0) {
throw new Error(
"To establish the DB model you MUST set the "
+ "filename to the location of a "
+ "connection.properties file with the database connection parameters.");
}
File propF = new File(filename );
InputStream is;
try {
is = new FileInputStream(propF);
} catch (FileNotFoundException e) {
log.error("Could not load file "+filename);
throw new Error("Could not load file " + filename
+ '\n' + e.getMessage());
}
Properties dbProps = new Properties();
try {
dbProps.load(is);
if (jenaDbModelName == null) {
String specifiedModelName = dbProps.getProperty("Jena.modelName");
jenaDbModelName = (specifiedModelName != null) ? specifiedModelName : JENA_DB_MODEL;
}
jenaDbOntModelSpec = (jenaDbOntModelSpec != null) ? jenaDbOntModelSpec : DB_ONT_MODEL_SPEC;
String dns = dbProps.getProperty("Vitro.defaultNamespace");
defaultNamespace = (dns != null && dns.length()>0) ? dns : null;
return makeDBModelFromProperties(dbProps, jenaDbModelName, jenaDbOntModelSpec);
} catch (IOException e) {
throw new Error("Could not load properties from file " + filename + '\n'
+ e.getMessage());
}
public final Model makeDBModelFromConfigurationProperties(String jenaDbModelName, OntModelSpec jenaDbOntModelSpec){
String dbDriverClassname = ConfigurationProperties.getProperty("VitroConnection.DataSource.driver", DB_DRIVER_CLASS_NAME);
String jdbcUrl = ConfigurationProperties.getProperty("VitroConnection.DataSource.url") + "?useUnicode=yes&characterEncoding=utf8";
String username = ConfigurationProperties.getProperty("VitroConnection.DataSource.username");
String password = ConfigurationProperties.getProperty("VitroConnection.DataSource.password");
BasicDataSource ds = makeBasicDataSource(dbDriverClassname, jdbcUrl, username, password);
String dns = ConfigurationProperties.getProperty("Vitro.defaultNamespace");
defaultNamespace = (dns != null && dns.length()>0) ? dns : null;
jenaDbOntModelSpec = (jenaDbOntModelSpec != null) ? jenaDbOntModelSpec : DB_ONT_MODEL_SPEC;
return makeDBModel(ds, jenaDbModelName, jenaDbOntModelSpec);
}
protected BasicDataSource makeBasicDataSource(String dbDriverClassname, String jdbcUrl, String username, String password) {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName( (dbDriverClassname==null) ? DB_DRIVER_CLASS_NAME : dbDriverClassname );
ds.setDriverClassName(dbDriverClassname);
ds.setUrl(jdbcUrl);
ds.setUsername(username);
ds.setPassword(password);
@ -139,14 +99,6 @@ public class JenaDataSourceSetupBase {
e.printStackTrace();
}
//Class.forName(DB_DRIVER_CLASS_NAME);
//Create database connection
//IDBConnection conn = new DBConnection ( jdbcUrl, username, password, DB );
//IDBConnection conn = new DBConnection(ds.getConnection(),DB);
//ModelMaker maker = ModelFactory.createModelRDBMaker(conn) ;
//ReconnectingGraphRDBMaker maker = new ReconnectingGraphRDBMaker(jdbcUrl, username, password, DB, null, ReificationStyle.Convenient);
return ds;
}
@ -174,15 +126,6 @@ public class JenaDataSourceSetupBase {
return dbModel;
}
private Model makeDBModelFromProperties(Properties dbProps, String jenaDbModelName, OntModelSpec jenaDbOntModelSpec) {
String dbDriverClassname = dbProps.getProperty("VitroConnection.DataSource.driver");
String jdbcUrl = dbProps.getProperty("VitroConnection.DataSource.url") + "?useUnicode=yes&characterEncoding=utf8";
String username = dbProps.getProperty("VitroConnection.DataSource.username");
String password = dbProps.getProperty("VitroConnection.DataSource.password");
BasicDataSource ds = makeBasicDataSource(dbDriverClassname, jdbcUrl, username, password);
return makeDBModel(ds, jenaDbModelName, jenaDbOntModelSpec);
}
public static void readOntologyFilesInPathSet(String path, ServletContext ctx, Model model) {
Set<String> paths = ctx.getResourcePaths(path);
for(String p : paths) {

View file

@ -27,7 +27,7 @@ public class JenaPersistentDBOnlyDataSourceSetup extends JenaDataSourceSetupBase
OntModel memModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC);
OntModel dbModel = null;
try {
Model dbPlainModel = makeDBModelFromPropertiesFile(sce.getServletContext().getRealPath(CONNECTION_PROP_LOCATION), JENA_DB_MODEL, DB_ONT_MODEL_SPEC);
Model dbPlainModel = makeDBModelFromConfigurationProperties(JENA_DB_MODEL, DB_ONT_MODEL_SPEC);
dbModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC,dbPlainModel);
boolean isEmpty = true;
ClosableIterator stmtIt = dbModel.listStatements();
@ -57,7 +57,7 @@ public class JenaPersistentDBOnlyDataSourceSetup extends JenaDataSourceSetupBase
} catch (Throwable t) {
System.out.println("**** ERROR *****");
System.out.println("Vitro unable to open Jena database model.");
System.out.println("Check that connection.properties has been created WEB-INF/classes, ");
System.out.println("Check that the configuration properties file has been created in WEB-INF/classes, ");
System.out.println("and that the database connection parameters are accurate. ");
System.out.println("****************");
}
@ -77,7 +77,7 @@ public class JenaPersistentDBOnlyDataSourceSetup extends JenaDataSourceSetupBase
// default inference graph
try {
Model infDbPlainModel = makeDBModelFromPropertiesFile(sce.getServletContext().getRealPath(CONNECTION_PROP_LOCATION), JENA_INF_MODEL, DB_ONT_MODEL_SPEC);
Model infDbPlainModel = makeDBModelFromConfigurationProperties(JENA_INF_MODEL, DB_ONT_MODEL_SPEC);
OntModel infDbModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC,infDbPlainModel);
sce.getServletContext().setAttribute("inferenceOntModel",infDbModel);
} catch (Throwable e) {

View file

@ -27,7 +27,7 @@ public class JenaPersistentDataSourceSetup extends JenaDataSourceSetupBase imple
boolean firstStartup = false;
try {
dbModel = makeDBModelFromPropertiesFile(sce.getServletContext().getRealPath(CONNECTION_PROP_LOCATION), JENA_DB_MODEL, DB_ONT_MODEL_SPEC);
dbModel = makeDBModelFromConfigurationProperties(JENA_DB_MODEL, DB_ONT_MODEL_SPEC);
ClosableIterator stmtIt = dbModel.listStatements();
try {
@ -63,14 +63,14 @@ public class JenaPersistentDataSourceSetup extends JenaDataSourceSetupBase imple
} catch (Throwable t) {
System.out.println("**** ERROR *****");
System.out.println("Vitro unable to open Jena database model.");
System.out.println("Check that connection.properties has been created WEB-INF/classes, ");
System.out.println("Check that the configuration properties file has been created in WEB-INF/classes, ");
System.out.println("and that the database connection parameters are accurate. ");
System.out.println("****************");
}
// default inference graph
try {
Model infDbModel = makeDBModelFromPropertiesFile(sce.getServletContext().getRealPath(CONNECTION_PROP_LOCATION), JENA_INF_MODEL, DB_ONT_MODEL_SPEC);
Model infDbModel = makeDBModelFromConfigurationProperties(JENA_INF_MODEL, DB_ONT_MODEL_SPEC);
OntModel infModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC);
if (infDbModel != null) {
long startTime = System.currentTimeMillis();
@ -86,7 +86,7 @@ public class JenaPersistentDataSourceSetup extends JenaDataSourceSetupBase imple
// user accounts Model
try {
Model userAccountsDbModel = makeDBModelFromPropertiesFile(sce.getServletContext().getRealPath(CONNECTION_PROP_LOCATION), JENA_USER_ACCOUNTS_MODEL, DB_ONT_MODEL_SPEC);
Model userAccountsDbModel = makeDBModelFromConfigurationProperties(JENA_USER_ACCOUNTS_MODEL, DB_ONT_MODEL_SPEC);
if (firstStartup) {
readOntologyFilesInPathSet(AUTHPATH, sce.getServletContext(), userAccountsDbModel);
}

View file

@ -32,7 +32,7 @@ public class ModelAuditorSetup extends JenaDataSourceSetupBase implements Servle
Model baseModel = ontModel.getBaseModel();
OntModel dynamicUnionModel = (OntModel) sce.getServletContext().getAttribute("jenaOntModel");
log.debug("Setting model auditor database...");
OntModel auditModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC,makeDBModelFromPropertiesFile(sce.getServletContext().getRealPath(CONNECTION_PROP_LOCATION), JENA_AUDIT_MODEL, DB_ONT_MODEL_SPEC));
OntModel auditModel = ModelFactory.createOntologyModel(MEM_ONT_MODEL_SPEC,makeDBModelFromConfigurationProperties(JENA_AUDIT_MODEL, DB_ONT_MODEL_SPEC));
sce.getServletContext().setAttribute("jenaAuditModel", auditModel);
ModelAuditor ma = new ModelAuditor(auditModel,ontModel);
baseModel.register(ma);

View file

@ -2,29 +2,22 @@
package edu.cornell.mannlib.vitro.webapp.servlet.setup;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import com.hp.hpl.jena.db.DBConnection;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.ModelMaker;
import edu.cornell.mannlib.vitro.webapp.auth.policy.JenaNetidPolicy.ContextSetup;
import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
import edu.cornell.mannlib.vitro.webapp.dao.jena.VitroJenaModelMaker;
public class VitroJenaModelMakerSetup implements ServletContextListener {
private static final Logger LOG = Logger
.getLogger(VitroJenaModelMakerSetup.class);
protected final static String CONNECTION_PROP_LOCATION = "/WEB-INF/classes/connection.properties";
protected final static String DB_TYPE = "MySQL";
public void contextDestroyed(ServletContextEvent arg0) {
@ -32,78 +25,19 @@ public class VitroJenaModelMakerSetup implements ServletContextListener {
}
public void contextInitialized(ServletContextEvent arg0) {
/* Sometimes this class fails for no apparent reason and stops
the context from loading. Trying to eliminate all uncaught errors. */
Log log = null;
try {
log = LogFactory.getLog(VitroJenaModelMakerSetup.class.getName());
} catch (Throwable t) {
System.out.println("Unable to use error log for "+this.getClass().getName());
}
try {
String filename = arg0.getServletContext().getRealPath(CONNECTION_PROP_LOCATION);
if (filename == null || filename.length() <= 0) {
if (log != null) {
log.error(
"Error setting up VitroModelMaker:\n" +
"To establish the DB model you MUST set the "
+ "filename to the location of a "
+ "connection.properties file with the database connection parameters.");
}
}
File propF = new File(filename );
InputStream is = null;
try {
is = new FileInputStream(propF);
} catch (FileNotFoundException e) {
String msg = "Error setting up VitroJenaModelMaker:\nCould not load file "+filename;
if (log != null) {
log.error(msg);
} else {
System.out.println(msg);
}
}
Properties dbProps = new Properties();
try {
dbProps.load(is);
String dns = dbProps.getProperty("Vitro.defaultNamespace");
String dbDriverClassname = dbProps.getProperty("VitroConnection.DataSource.driver");
String jdbcUrl = dbProps.getProperty("VitroConnection.DataSource.url") + "?useUnicode=yes&characterEncoding=utf8";
String username = dbProps.getProperty("VitroConnection.DataSource.username");
String password = dbProps.getProperty("VitroConnection.DataSource.password");
DBConnection dbConn = new DBConnection(jdbcUrl,username,password,DB_TYPE);
ModelMaker mMaker = ModelFactory.createModelRDBMaker(dbConn);
VitroJenaModelMaker vjmm = new VitroJenaModelMaker(mMaker);
arg0.getServletContext().setAttribute("vitroJenaModelMaker",vjmm);
String msg = "VitroJenaModelMaker set up";
if (log != null) {
log.debug(msg);
} else {
System.out.println(msg);
}
} catch (IOException e) {
String msg = "Error setting up VitroJenaModelMaker:\nCould not load properties from file " + filename + '\n'
+ e.getMessage();
if (log != null) {
log.error(msg);
} else {
System.out.println(msg);
}
}
String jdbcUrl = ConfigurationProperties.getProperty("VitroConnection.DataSource.url")
+ "?useUnicode=yes&characterEncoding=utf8";
String username = ConfigurationProperties.getProperty("VitroConnection.DataSource.username");
String password = ConfigurationProperties.getProperty("VitroConnection.DataSource.password");
DBConnection dbConn = new DBConnection(jdbcUrl, username, password, DB_TYPE);
ModelMaker mMaker = ModelFactory.createModelRDBMaker(dbConn);
VitroJenaModelMaker vjmm = new VitroJenaModelMaker(mMaker);
arg0.getServletContext().setAttribute("vitroJenaModelMaker", vjmm);
LOG.debug("VitroJenaModelMaker set up");
} catch (Throwable t) {
String msg = "Unable to set up default VitroJenaModelMaker";
if (log != null) {
log.error(msg);
log.error(t);
} else {
System.out.println(msg);
}
LOG.error("Unable to set up default VitroJenaModelMaker", t);
}
}