NIHVIVO-336 StartupStatus logs the messages it receives (with appropriate severity level) so the ServletContextListener doesn't need to do both.

This commit is contained in:
j2blake 2011-09-26 16:41:46 +00:00
parent ed14575517
commit 07f2295dfa
12 changed files with 55 additions and 82 deletions

View file

@ -33,6 +33,7 @@ log4j.appender.AllAppender.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS}
log4j.rootLogger=INFO, AllAppender log4j.rootLogger=INFO, AllAppender
log4j.logger.edu.cornell.mannlib.vitro.webapp.startup.StartupStatus=WARN
log4j.logger.edu.cornell.mannlib.vitro.webapp.controller.freemarker.BrowseController=WARN log4j.logger.edu.cornell.mannlib.vitro.webapp.controller.freemarker.BrowseController=WARN
log4j.logger.edu.cornell.mannlib.vitro.webapp.dao.jena.pellet.PelletListener=WARN log4j.logger.edu.cornell.mannlib.vitro.webapp.dao.jena.pellet.PelletListener=WARN
log4j.logger.edu.cornell.mannlib.vitro.webapp.dao.jena.RDBGraphGenerator=WARN log4j.logger.edu.cornell.mannlib.vitro.webapp.dao.jena.RDBGraphGenerator=WARN

View file

@ -53,7 +53,6 @@ public class PermissionSetsLoader implements ServletContextListener {
wrapper.createPermissionSet(URI_CURATOR, "Curator"); wrapper.createPermissionSet(URI_CURATOR, "Curator");
wrapper.createPermissionSet(URI_DBA, "Site Admin"); wrapper.createPermissionSet(URI_DBA, "Site Admin");
} catch (Exception e) { } catch (Exception e) {
log.error("could not run PermissionSetsLoader" + e);
ss.fatal(this, "could not run PermissionSetsLoader" + e); ss.fatal(this, "could not run PermissionSetsLoader" + e);
} }
} }

View file

@ -77,10 +77,7 @@ public class RootUserPolicy implements PolicyIface {
ServletPolicyList.addPolicy(ctx, new RootUserPolicy()); ServletPolicyList.addPolicy(ctx, new RootUserPolicy());
} catch (Exception e) { } catch (Exception e) {
log.error("could not run " + this.getClass().getSimpleName() ss.fatal(this, "Failed to set up the RootUserPolicy", e);
+ ": " + e);
ss.fatal(this, "could not run " + this.getClass().getSimpleName()
+ ": " + e);
} }
} }

View file

@ -355,10 +355,7 @@ public class PropertyRestrictionPolicyHelper {
.createBean(model); .createBean(model);
PropertyRestrictionPolicyHelper.setBean(ctx, bean); PropertyRestrictionPolicyHelper.setBean(ctx, bean);
} catch (Exception e) { } catch (Exception e) {
log.error("could not run PropertyRestrictionPolicyHelper$Setup: " ss.fatal(this, "could not set up PropertyRestrictionPolicyHelper", e);
+ e);
ss.fatal(this, "could not run PropertyRestrictionPolicyHelper$Setup: "
+ e);
} }
} }

View file

@ -6,9 +6,6 @@ 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.auth.identifier.ActiveIdentifierBundleFactories; import edu.cornell.mannlib.vitro.webapp.auth.identifier.ActiveIdentifierBundleFactories;
import edu.cornell.mannlib.vitro.webapp.auth.identifier.common.CommonIdentifierBundleFactory; import edu.cornell.mannlib.vitro.webapp.auth.identifier.common.CommonIdentifierBundleFactory;
import edu.cornell.mannlib.vitro.webapp.auth.policy.DisplayRestrictedDataByRoleLevelPolicy; import edu.cornell.mannlib.vitro.webapp.auth.policy.DisplayRestrictedDataByRoleLevelPolicy;
@ -23,8 +20,6 @@ import edu.cornell.mannlib.vitro.webapp.startup.StartupStatus;
* Set up the common policy family, with Identifier factory. * Set up the common policy family, with Identifier factory.
*/ */
public class CommonPolicyFamilySetup implements ServletContextListener { public class CommonPolicyFamilySetup implements ServletContextListener {
private static final Log log = LogFactory
.getLog(CommonPolicyFamilySetup.class);
@Override @Override
public void contextInitialized(ServletContextEvent sce) { public void contextInitialized(ServletContextEvent sce) {
@ -49,10 +44,7 @@ public class CommonPolicyFamilySetup implements ServletContextListener {
ActiveIdentifierBundleFactories.addFactory(sce, factory); ActiveIdentifierBundleFactories.addFactory(sce, factory);
} catch (Exception e) { } catch (Exception e) {
log.error("could not run " + this.getClass().getSimpleName() + ": " ss.fatal(this, "could not run CommonPolicyFamilySetup", e);
+ e);
ss.fatal(this, "could not run " + this.getClass().getSimpleName() + ": "
+ e);
} }
} }

View file

@ -72,8 +72,8 @@ public class RevisionInfoSetup implements ServletContextListener {
List<String> lines = readRevisionInfo(context); List<String> lines = readRevisionInfo(context);
bean = parseRevisionInformation(lines); bean = parseRevisionInformation(lines);
} catch (Exception e) { } catch (Exception e) {
log.error(e, e); StartupStatus.getBean(context).warning(this,
StartupStatus.getBean(context).warning(this, e.getMessage(), e); "Failed to load the revision info", e);
bean = DUMMY_BEAN; bean = DUMMY_BEAN;
} }

View file

@ -168,7 +168,6 @@ public class FreemarkerEmailFactory {
+ "the system will not send mail to users."); + "the system will not send mail to users.");
} }
} catch (Exception e) { } catch (Exception e) {
log.error(e, e);
ss.warning(this, ss.warning(this,
"Failed to initialize FreemarkerEmailFactory. " "Failed to initialize FreemarkerEmailFactory. "
+ "The system will not be able to send email " + "The system will not be able to send email "

View file

@ -122,7 +122,6 @@ public class SolrSetup implements javax.servlet.ServletContextListener{
log.info("Setup of Solr index completed."); log.info("Setup of Solr index completed.");
ss.info(this, "Setup of Solr index completed."); ss.info(this, "Setup of Solr index completed.");
} catch (Throwable e) { } catch (Throwable e) {
log.error("could not setup local solr server",e);
ss.fatal(this, "could not setup local solr server",e); ss.fatal(this, "could not setup local solr server",e);
} }

View file

@ -311,13 +311,6 @@ public class JenaDataSourceSetup extends JenaDataSourceSetupBase implements java
if( portalURIs.size() > 0 ){ if( portalURIs.size() > 0 ){
for( String portalUri : portalURIs){ for( String portalUri : portalURIs){
if( portalUri != null && ! portalUri.startsWith(defaultNamespaceFromDeployProperites)){ if( portalUri != null && ! portalUri.startsWith(defaultNamespaceFromDeployProperites)){
log.error("Namespace mismatch between db and deploy.properties.");
log.error("Vivo will not start up correctly because the default namespace specified in deploy.properties does not match the namespace of " +
"a portal in the database. Namespace from deploy.properties: \"" + defaultNamespaceFromDeployProperites +
"\" Namespace from an existing portal: \"" + portalUri + "\" To get the application to start with this " +
"database change the default namespace in deploy.properties " + portalUri.substring(0, portalUri.lastIndexOf("/")+1) +
" Another possibility is that deploy.properties does not specify the intended database.");
StartupStatus ss = StartupStatus.getBean(ctx); StartupStatus ss = StartupStatus.getBean(ctx);
ss.warning(this, "Namespace mismatch between db and deploy.properties."); ss.warning(this, "Namespace mismatch between db and deploy.properties.");
ss.warning(this, "Vivo will not start up correctly because the default namespace specified in deploy.properties does not match the namespace of " + ss.warning(this, "Vivo will not start up correctly because the default namespace specified in deploy.properties does not match the namespace of " +

View file

@ -66,7 +66,8 @@ public class StartupManager implements ServletContextListener {
} }
} }
} catch (Exception e) { } catch (Exception e) {
recordFatal("Startup threw an unexpected exception.", e); ss.fatal(this, "Startup threw an unexpected exception.", e);
log.error("Startup threw an unexpected exception.", e);
} }
} }
@ -110,10 +111,10 @@ public class StartupManager implements ServletContextListener {
} }
} }
} catch (NullPointerException e) { } catch (NullPointerException e) {
recordFatal("Unable to locate the list of startup listeners: " ss.fatal(this, "Unable to locate the list of startup listeners: "
+ FILE_OF_STARTUP_LISTENERS); + FILE_OF_STARTUP_LISTENERS);
} catch (IOException e) { } catch (IOException e) {
recordFatal( ss.fatal(this,
"Failed while processing the list of startup listeners: " "Failed while processing the list of startup listeners: "
+ FILE_OF_STARTUP_LISTENERS, e); + FILE_OF_STARTUP_LISTENERS, e);
} finally { } finally {
@ -147,29 +148,17 @@ public class StartupManager implements ServletContextListener {
Class<?> c = Class.forName(className); Class<?> c = Class.forName(className);
Object o = c.newInstance(); Object o = c.newInstance();
return (ServletContextListener) o; return (ServletContextListener) o;
} catch (ClassNotFoundException e) {
recordFatal("Failed to instantiate listener: '" + className + "'",
e);
return null;
} catch (InstantiationException e) {
recordFatal("Failed to instantiate listener: '" + className + "'",
e);
return null;
} catch (IllegalAccessException e) {
recordFatal("Failed to instantiate listener: '" + className + "'",
e);
return null;
} catch (ClassCastException e) { } catch (ClassCastException e) {
recordFatal("Instance of '" + className ss.fatal(this, "Instance of '" + className
+ "' is not a ServletContextListener", e); + "' is not a ServletContextListener", e);
return null; return null;
} catch (Exception e) { } catch (Exception e) {
recordFatal("Failed to instantiate listener: '" + className + "'", ss.fatal(this, "Failed to instantiate listener: '" + className
e); + "'", e);
return null; return null;
} catch (ExceptionInInitializerError e) { } catch (ExceptionInInitializerError e) {
recordFatal("Failed to instantiate listener: '" + className + "'", ss.fatal(this, "Failed to instantiate listener: '" + className
e); + "'", e);
return null; return null;
} }
} }
@ -187,8 +176,6 @@ public class StartupManager implements ServletContextListener {
ss.listenerExecuted(listener); ss.listenerExecuted(listener);
} catch (Exception e) { } catch (Exception e) {
ss.fatal(listener, "Threw unexpected exception", e); ss.fatal(listener, "Threw unexpected exception", e);
log.error("Listener threw fatal exception: '"
+ listener.getClass().getName() + "'", e);
} }
} }
@ -201,23 +188,14 @@ public class StartupManager implements ServletContextListener {
ServletContextListener iListener = initializeList.get(i); ServletContextListener iListener = initializeList.get(i);
ServletContextListener jListener = initializeList.get(j); ServletContextListener jListener = initializeList.get(j);
if (iListener.getClass().equals(jListener.getClass())) { if (iListener.getClass().equals(jListener.getClass())) {
recordFatal("File contains duplicate listener classes: '" ss.fatal(this,
+ iListener.getClass().getName() + "'"); ("File contains duplicate listener classes: '"
+ iListener.getClass().getName() + "'"));
} }
} }
} }
} }
private void recordFatal(String message) {
ss.fatal(this, message);
log.error(message);
}
private void recordFatal(String message, Throwable cause) {
ss.fatal(this, message, cause);
log.error(message, cause);
}
/** /**
* Notify the listeners that the context is being destroyed, in the reverse * Notify the listeners that the context is being destroyed, in the reverse
* order from how they were notified at initialization. * order from how they were notified at initialization.

View file

@ -42,7 +42,7 @@ public class StartupStatus {
} }
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// methods to set status // methods to set status - note that these write to the log also.
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
private List<StatusItem> itemList = new ArrayList<StatusItem>(); private List<StatusItem> itemList = new ArrayList<StatusItem>();
@ -94,7 +94,30 @@ public class StartupStatus {
private void addItem(StatusItem.Level level, ServletContextListener source, private void addItem(StatusItem.Level level, ServletContextListener source,
String message, Throwable cause) { String message, Throwable cause) {
itemList.add(new StatusItem(level, source, message, cause)); StatusItem item = new StatusItem(level, source, message, cause);
itemList.add(item);
String logMessage = "From " + item.getShortSourceName() + ": "
+ item.getMessage();
if (item.getLevel() == StatusItem.Level.FATAL) {
if (cause == null) {
log.fatal(logMessage);
} else {
log.fatal(logMessage, cause);
}
} else if (item.getLevel() == StatusItem.Level.WARNING) {
if (cause == null) {
log.warn(logMessage);
} else {
log.warn(logMessage, cause);
}
} else {
if (cause == null) {
log.info(logMessage);
} else {
log.info(logMessage, cause);
}
}
} }
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------

View file

@ -44,7 +44,8 @@ public class StartupManagerTest extends AbstractTestClass {
ss = StartupStatus.getBean(ctx); ss = StartupStatus.getBean(ctx);
// setLoggerLevel(this.getClass(), Level.DEBUG); // setLoggerLevel(this.getClass(), Level.DEBUG);
// setLoggerLevel(StartupManager.class, Level.DEBUG); setLoggerLevel(StartupStatus.class, Level.OFF);
setLoggerLevel(StartupManager.class, Level.OFF);
} }
@After @After
@ -56,7 +57,6 @@ public class StartupManagerTest extends AbstractTestClass {
@Test @Test
public void noSuchFile() { public void noSuchFile() {
setLoggerLevel(StartupManager.class, Level.OFF);
assertStartupFails((String) null); assertStartupFails((String) null);
} }
@ -78,49 +78,41 @@ public class StartupManagerTest extends AbstractTestClass {
@Test @Test
public void classDoesNotExist() { public void classDoesNotExist() {
setLoggerLevel(StartupManager.class, Level.OFF);
assertStartupFails("no.such.class\n"); assertStartupFails("no.such.class\n");
} }
@Test @Test
public void classThrowsExceptionWhenLoading() { public void classThrowsExceptionWhenLoading() {
setLoggerLevel(StartupManager.class, Level.OFF);
assertStartupFails(ThrowsExceptionWhenLoading.class); assertStartupFails(ThrowsExceptionWhenLoading.class);
} }
@Test @Test
public void classIsPrivate() { public void classIsPrivate() {
setLoggerLevel(StartupManager.class, Level.OFF);
assertStartupFails(PrivateClass.class); assertStartupFails(PrivateClass.class);
} }
@Test @Test
public void noDefaultConstructor() { public void noDefaultConstructor() {
setLoggerLevel(StartupManager.class, Level.OFF);
assertStartupFails(NoDefaultConstructor.class); assertStartupFails(NoDefaultConstructor.class);
} }
@Test @Test
public void constructorIsPrivate() { public void constructorIsPrivate() {
setLoggerLevel(StartupManager.class, Level.OFF);
assertStartupFails(PrivateConstructor.class); assertStartupFails(PrivateConstructor.class);
} }
@Test @Test
public void constructorThrowsException() { public void constructorThrowsException() {
setLoggerLevel(StartupManager.class, Level.OFF);
assertStartupFails(ConstructorThrowsException.class); assertStartupFails(ConstructorThrowsException.class);
} }
@Test @Test
public void notAServletContextListener() { public void notAServletContextListener() {
setLoggerLevel(StartupManager.class, Level.OFF);
assertStartupFails(NotAListener.class); assertStartupFails(NotAListener.class);
} }
@Test @Test
public void listenerThrowsException() { public void listenerThrowsException() {
setLoggerLevel(StartupManager.class, Level.OFF);
assertStartupFails(InitThrowsException.class); assertStartupFails(InitThrowsException.class);
} }
@ -139,28 +131,30 @@ public class StartupManagerTest extends AbstractTestClass {
// Did they initialize in the correct order? // Did they initialize in the correct order?
List<StatusItem> items = ss.getStatusItems(); List<StatusItem> items = ss.getStatusItems();
assertEquals("how many", 2, items.size()); assertEquals("how many", 2, items.size());
assertEquals("init order 1", listener1Name, items.get(0).getSourceName()); assertEquals("init order 1", listener1Name, items.get(0)
assertEquals("init order 2", listener2Name, items.get(1).getSourceName()); .getSourceName());
assertEquals("init order 2", listener2Name, items.get(1)
.getSourceName());
sm.contextDestroyed(sce); sm.contextDestroyed(sce);
// Did they destroy in reverse order? // Did they destroy in reverse order?
items = ss.getStatusItems(); items = ss.getStatusItems();
assertEquals("how many", 4, items.size()); assertEquals("how many", 4, items.size());
assertEquals("destroy order 1", listener2Name, items.get(2).getSourceName()); assertEquals("destroy order 1", listener2Name, items.get(2)
assertEquals("destroy order 2", listener1Name, items.get(3).getSourceName()); .getSourceName());
assertEquals("destroy order 2", listener1Name, items.get(3)
.getSourceName());
} }
@Test @Test
public void duplicateListeners() { public void duplicateListeners() {
setLoggerLevel(StartupManager.class, Level.OFF);
assertStartupFails(SucceedsWithInfo.class, SucceedsWithWarning.class, assertStartupFails(SucceedsWithInfo.class, SucceedsWithWarning.class,
SucceedsWithInfo.class); SucceedsWithInfo.class);
} }
@Test @Test
public void dontExecuteAfterFailure() { public void dontExecuteAfterFailure() {
setLoggerLevel(StartupManager.class, Level.OFF);
assertStartupFails(InitThrowsException.class, SucceedsWithInfo.class); assertStartupFails(InitThrowsException.class, SucceedsWithInfo.class);
for (StatusItem item : ss.getStatusItems()) { for (StatusItem item : ss.getStatusItems()) {
@ -321,4 +315,5 @@ public class StartupManagerTest extends AbstractTestClass {
item.getSourceName(), item.getMessage(), item.getCause())); item.getSourceName(), item.getMessage(), item.getCause()));
} }
} }
} }