allowing servlet context listeners to prevent other context listeners from running

This commit is contained in:
bjl23 2011-02-03 19:50:11 +00:00
parent cedbc8ff05
commit 3bfc823e41
22 changed files with 180 additions and 57 deletions

View file

@ -9,6 +9,8 @@ import javax.servlet.ServletContextListener;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.servlet.setup.AbortStartup;
/** /**
* Setups context so that Identifiers for Individuals associated with Users are * Setups context so that Identifiers for Individuals associated with Users are
* added to requests. * added to requests.
@ -22,6 +24,11 @@ public class UserToIndIdentifierFactorySetup implements ServletContextListener{
private static final Log log = LogFactory.getLog(UserToIndIdentifierFactorySetup.class.getName()); private static final Log log = LogFactory.getLog(UserToIndIdentifierFactorySetup.class.getName());
public void contextInitialized(ServletContextEvent sce) { public void contextInitialized(ServletContextEvent sce) {
if (AbortStartup.isStartupAborted(sce.getServletContext())) {
return;
}
ServletContext sc = sce.getServletContext(); ServletContext sc = sce.getServletContext();
ServletIdentifierBundleFactory ServletIdentifierBundleFactory
.addIdentifierBundleFactory(sc, new UserToIndIdentifierFactory()); .addIdentifierBundleFactory(sc, new UserToIndIdentifierFactory());

View file

@ -11,6 +11,7 @@ import javax.servlet.ServletContextListener;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.servlet.setup.AbortStartup;
import edu.cornell.mannlib.vitro.webapp.web.templatemodels.BaseTemplateModel; import edu.cornell.mannlib.vitro.webapp.web.templatemodels.BaseTemplateModel;
import freemarker.template.Configuration; import freemarker.template.Configuration;
@ -20,6 +21,10 @@ public class FreemarkerSetup implements ServletContextListener {
public void contextInitialized(ServletContextEvent event) { public void contextInitialized(ServletContextEvent event) {
if (AbortStartup.isStartupAborted(event.getServletContext())) {
return;
}
ServletContext sc = event.getServletContext(); ServletContext sc = event.getServletContext();
sc.setAttribute("themeToConfigMap", new HashMap<String, Configuration>()); sc.setAttribute("themeToConfigMap", new HashMap<String, Configuration>());
BaseTemplateModel.setServletContext(sc); BaseTemplateModel.setServletContext(sc);

View file

@ -36,6 +36,7 @@ import edu.cornell.mannlib.vitro.webapp.dao.jena.SearchReindexingListener;
import edu.cornell.mannlib.vitro.webapp.search.beans.ObjectSourceIface; import edu.cornell.mannlib.vitro.webapp.search.beans.ObjectSourceIface;
import edu.cornell.mannlib.vitro.webapp.search.beans.ProhibitedFromSearch; import edu.cornell.mannlib.vitro.webapp.search.beans.ProhibitedFromSearch;
import edu.cornell.mannlib.vitro.webapp.search.indexing.IndexBuilder; import edu.cornell.mannlib.vitro.webapp.search.indexing.IndexBuilder;
import edu.cornell.mannlib.vitro.webapp.servlet.setup.AbortStartup;
import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary; import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary;
/** /**
@ -68,6 +69,11 @@ public class LuceneSetup implements javax.servlet.ServletContextListener {
* created. * created.
*/ */
public void contextInitialized(ServletContextEvent sce) { public void contextInitialized(ServletContextEvent sce) {
if (AbortStartup.isStartupAborted(sce.getServletContext())) {
return;
}
try { try {
ServletContext context = sce.getServletContext(); ServletContext context = sce.getServletContext();

View file

@ -0,0 +1,25 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.servlet.setup;
import javax.servlet.ServletContext;
public class AbortStartup {
private static final String ATTRIBUTE_NAME = AbortStartup.class.getName();
/**
* Sets a context attribute to prevent other context listeners from running.
*/
public static void abortStartup(ServletContext context) {
context.setAttribute(ATTRIBUTE_NAME, new Boolean(true));
}
/**
* Checks whether a previous context listener has caused startup to be aborted.
*/
public static boolean isStartupAborted(ServletContext context) {
return (context.getAttribute(ATTRIBUTE_NAME) != null);
}
}

View file

@ -42,6 +42,11 @@ public class AssembleModelsSetup implements ServletContextListener {
private String SYNTAX = "N3"; private String SYNTAX = "N3";
public void contextInitialized(ServletContextEvent sce) { public void contextInitialized(ServletContextEvent sce) {
if (AbortStartup.isStartupAborted(sce.getServletContext())) {
return;
}
OntModel jenaOntModel = null; OntModel jenaOntModel = null;
try { try {
jenaOntModel = (OntModel) sce.getServletContext().getAttribute("baseOntModel"); jenaOntModel = (OntModel) sce.getServletContext().getAttribute("baseOntModel");

View file

@ -28,6 +28,11 @@ public class AttachSubmodels implements ServletContextListener {
private static final Log log = LogFactory.getLog( AttachSubmodels.class ); private static final Log log = LogFactory.getLog( AttachSubmodels.class );
public void contextInitialized( ServletContextEvent sce ) { public void contextInitialized( ServletContextEvent sce ) {
if (AbortStartup.isStartupAborted(sce.getServletContext())) {
return;
}
try { try {
//FIXME refactor this //FIXME refactor this

View file

@ -16,6 +16,10 @@ public class DefaultThemeSetup implements ServletContextListener {
// Set default theme based on themes present on the file system // Set default theme based on themes present on the file system
public void contextInitialized(ServletContextEvent event) { public void contextInitialized(ServletContextEvent event) {
if (AbortStartup.isStartupAborted(event.getServletContext())) {
return;
}
// Find the themes directory in the file system // Find the themes directory in the file system
ServletContext sc = event.getServletContext(); ServletContext sc = event.getServletContext();
boolean doSort = true; boolean doSort = true;

View file

@ -41,6 +41,10 @@ public class FileGraphSetup implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) { public void contextInitialized(ServletContextEvent sce) {
if (AbortStartup.isStartupAborted(sce.getServletContext())) {
return;
}
try { try {
OntModelSelectorImpl baseOms = (OntModelSelectorImpl) sce.getServletContext().getAttribute("baseOntModelSelector"); OntModelSelectorImpl baseOms = (OntModelSelectorImpl) sce.getServletContext().getAttribute("baseOntModelSelector");
Store kbStore = (Store) sce.getServletContext().getAttribute("kbStore"); Store kbStore = (Store) sce.getServletContext().getAttribute("kbStore");

View file

@ -25,6 +25,11 @@ public class HeapDefragement implements ServletContextListener {
@Override @Override
public void contextInitialized(ServletContextEvent arg0) { public void contextInitialized(ServletContextEvent arg0) {
if (AbortStartup.isStartupAborted(arg0.getServletContext())) {
return;
}
try{ try{
log.info("Calling System.gc() to defragement the heap."); log.info("Calling System.gc() to defragement the heap.");
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();

View file

@ -44,6 +44,11 @@ public class JenaDataSourceSetup extends JenaDataSourceSetupBase implements java
private static final Log log = LogFactory.getLog(JenaDataSourceSetup.class.getName()); private static final Log log = LogFactory.getLog(JenaDataSourceSetup.class.getName());
public void contextInitialized(ServletContextEvent sce) { public void contextInitialized(ServletContextEvent sce) {
if (AbortStartup.isStartupAborted(sce.getServletContext())) {
return;
}
try { try {
String tripleStoreTypeStr = String tripleStoreTypeStr =

View file

@ -60,6 +60,11 @@ public class JenaDataSourceSetupSDB extends JenaDataSourceSetupBase implements j
private static final Log log = LogFactory.getLog(JenaDataSourceSetupSDB.class); private static final Log log = LogFactory.getLog(JenaDataSourceSetupSDB.class);
public void contextInitialized(ServletContextEvent sce) { public void contextInitialized(ServletContextEvent sce) {
if (AbortStartup.isStartupAborted(sce.getServletContext())) {
return;
}
try { try {
// JenaPersistentDataSourceSetup should have already set this up - it just sets // JenaPersistentDataSourceSetup should have already set this up - it just sets

View file

@ -28,6 +28,10 @@ public class JenaPersistentDataSourceSetup extends JenaDataSourceSetupBase
public void contextInitialized(ServletContextEvent sce) { public void contextInitialized(ServletContextEvent sce) {
if (AbortStartup.isStartupAborted(sce.getServletContext())) {
return;
}
Model dbModel; Model dbModel;
OntModel memModel = ModelFactory.createOntologyModel( OntModel memModel = ModelFactory.createOntologyModel(
this.DB_ONT_MODEL_SPEC); this.DB_ONT_MODEL_SPEC);

View file

@ -13,6 +13,11 @@ import com.hp.hpl.jena.rdf.model.Model;
public class NamespacePrefixMapSetup implements ServletContextListener { public class NamespacePrefixMapSetup implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) { public void contextInitialized(ServletContextEvent sce) {
if (AbortStartup.isStartupAborted(sce.getServletContext())) {
return;
}
HashMap<String,String> prefixToNamespace = new HashMap<String,String>(); HashMap<String,String> prefixToNamespace = new HashMap<String,String>();
HashMap<String,String> namespaceToPrefix = new HashMap<String,String>(); HashMap<String,String> namespaceToPrefix = new HashMap<String,String>();
Model model = (Model) sce.getServletContext().getAttribute("jenaOntModel"); Model model = (Model) sce.getServletContext().getAttribute("jenaOntModel");

View file

@ -61,6 +61,11 @@ public class NightlyDefragement implements ServletContextListener, Runnable {
@Override @Override
public void contextInitialized(ServletContextEvent arg0) { public void contextInitialized(ServletContextEvent arg0) {
if (AbortStartup.isStartupAborted(arg0.getServletContext())) {
return;
}
lastRun = new DateTime().minusHours( 400 ); lastRun = new DateTime().minusHours( 400 );
if( nightlyDefragement != null ){ if( nightlyDefragement != null ){
log.warn("NightlyDefragement listener has already been setup. Check your web.xml for duplicate listeners."); log.warn("NightlyDefragement listener has already been setup. Check your web.xml for duplicate listeners.");

View file

@ -26,6 +26,10 @@ public class PelletReasonerSetup implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) { public void contextInitialized(ServletContextEvent sce) {
if (AbortStartup.isStartupAborted(sce.getServletContext())) {
return;
}
try { try {
//FIXME refactor this //FIXME refactor this

View file

@ -21,6 +21,10 @@ public class PelletReasonerSetupComplete implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) { public void contextInitialized(ServletContextEvent sce) {
if (AbortStartup.isStartupAborted(sce.getServletContext())) {
return;
}
try { try {
OntModel memoryModel = (OntModel) sce.getServletContext().getAttribute("jenaOntModel"); OntModel memoryModel = (OntModel) sce.getServletContext().getAttribute("jenaOntModel");

View file

@ -21,6 +21,10 @@ public class PelletReasonerSetupPseudocomplete implements ServletContextListener
public void contextInitialized(ServletContextEvent sce) { public void contextInitialized(ServletContextEvent sce) {
if (AbortStartup.isStartupAborted(sce.getServletContext())) {
return;
}
try { try {
OntModel memoryModel = (OntModel) sce.getServletContext().getAttribute("jenaOntModel"); OntModel memoryModel = (OntModel) sce.getServletContext().getAttribute("jenaOntModel");

View file

@ -22,6 +22,10 @@ public class PelletReasonerSetupPseudocompleteIgnoreDataproperties implements
public void contextInitialized(ServletContextEvent sce) { public void contextInitialized(ServletContextEvent sce) {
if (AbortStartup.isStartupAborted(sce.getServletContext())) {
return;
}
try { try {
OntModel memoryModel = (OntModel) sce.getServletContext().getAttribute("jenaOntModel"); OntModel memoryModel = (OntModel) sce.getServletContext().getAttribute("jenaOntModel");

View file

@ -42,6 +42,10 @@ public class RunSparqlConstructs implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) { public void contextInitialized(ServletContextEvent sce) {
if (AbortStartup.isStartupAborted(sce.getServletContext())) {
return;
}
try { try {
WebappDaoFactory wadf = (WebappDaoFactory) sce.getServletContext().getAttribute("webappDaoFactory"); WebappDaoFactory wadf = (WebappDaoFactory) sce.getServletContext().getAttribute("webappDaoFactory");

View file

@ -47,6 +47,10 @@ public class SimpleReasonerSetup implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) { public void contextInitialized(ServletContextEvent sce) {
if (AbortStartup.isStartupAborted(sce.getServletContext())) {
return;
}
try { try {
// set up Pellet reasoning for the TBox // set up Pellet reasoning for the TBox

View file

@ -65,6 +65,10 @@ public class UpdateKnowledgeBase implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) { public void contextInitialized(ServletContextEvent sce) {
if (AbortStartup.isStartupAborted(sce.getServletContext())) {
return;
}
try { try {
ServletContext ctx = sce.getServletContext(); ServletContext ctx = sce.getServletContext();

View file

@ -48,6 +48,11 @@ public class UpdateUploadedFiles implements ServletContextListener {
*/ */
@Override @Override
public void contextInitialized(ServletContextEvent sce) { public void contextInitialized(ServletContextEvent sce) {
if (AbortStartup.isStartupAborted(sce.getServletContext())) {
return;
}
try { try {
ServletContext ctx = sce.getServletContext(); ServletContext ctx = sce.getServletContext();