Skip to content

Commit

Permalink
SAK-34049 SakaiContextLoader with improved Spring integration
Browse files Browse the repository at this point in the history
  • Loading branch information
ern committed Jun 19, 2018
1 parent 88767af commit 52386b2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.core.io.FileSystemResource;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import org.sakaiproject.component.cover.ComponentManager;
import org.sakaiproject.component.cover.ServerConfigurationService;
Expand All @@ -56,46 +54,35 @@ public class SakaiContextLoader extends ContextLoader
public static final String SPRING_CONTEXT_SUFFIX = "-context.xml";

/**
* Allows loading/override of custom bean definitions from sakai.home
*
* <p>The pattern is the 'servlet_name-context.xml'</p>
*
* @param servletContext current servlet context
* @return the new WebApplicationContext
* @throws org.springframework.beans.BeansException
* if the context couldn't be initialized
*/
@Override
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) throws BeansException {

ConfigurableWebApplicationContext cwac = null;
if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) == null) {
cwac = (ConfigurableWebApplicationContext) super.initWebApplicationContext(servletContext);
} else {
log.debug("Servlet {} contains attribute ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE with value {} ", servletContext.getServletContextName(), servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE));
cwac = (ConfigurableWebApplicationContext) createWebApplicationContext(servletContext);
}
// optionally look in sakai home for additional bean deifinitions to load
if (cwac != null) {
final String servletName = servletContext.getServletContextName();
* Allows loading/override of custom bean definitions from sakai.home
*
* <p>The pattern is the 'servlet_name-context.xml'</p>
*
* @param sc current servlet context
* @param wac the new WebApplicationContext
*/
@Override
protected void customizeContext(ServletContext sc, ConfigurableWebApplicationContext wac) {
super.customizeContext(sc, wac);
if (wac != null) {
final String servletName = sc.getServletContextName();
String location = getHomeBeanDefinitionIfExists(servletName);
if (StringUtils.isNotBlank(location)) {
log.debug("Servlet " + servletName + " is attempting to load bean definition [" + location + "]");
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader((BeanDefinitionRegistry) cwac.getBeanFactory());
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader((BeanDefinitionRegistry) wac.getBeanFactory());
try {
int loaded = reader.loadBeanDefinitions(new FileSystemResource(location));
log.info("Servlet " + servletName + " loaded " + loaded + " beans from [" + location + "]");
AnnotationConfigUtils.registerAnnotationConfigProcessors(reader.getRegistry());
cwac.getBeanFactory().preInstantiateSingletons();
// AnnotationConfigUtils.registerAnnotationConfigProcessors(reader.getRegistry());
// wac.getBeanFactory().preInstantiateSingletons();
} catch (BeanDefinitionStoreException bdse) {
log.warn("Failure loading beans from [" + location + "]", bdse);
} catch (BeanCreationException bce) {
log.warn("Failure instantiating beans from [" + location + "]", bce);
}
}
}
return cwac;
}
}

/**
* Spring allows a parent ApplicationContext to be set during the creation of a new ApplicationContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@

package org.sakaiproject.util;

import java.util.Enumeration;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import lombok.extern.slf4j.Slf4j;

import org.sakaiproject.component.cover.ComponentManager;
import org.sakaiproject.component.impl.SakaiContextLoader;
import org.sakaiproject.component.impl.SpringCompMgr;
import org.springframework.beans.factory.DisposableBean;

/**
* <p>
Expand Down Expand Up @@ -57,11 +59,34 @@ public void contextInitialized(ServletContextEvent event)
public void contextDestroyed(ServletContextEvent event)
{
closeWebApplicationContext(event.getServletContext());
//ContextCleanupListener.cleanupAttributes(event.getServletContext());
log.info("Destroying Components in "+event.getServletContext().getServletContextName());
cleanupAttributes(event.getServletContext());

log.debug("Destroying Components in {}", event.getServletContext().getServletContextName());

// decrement the count of children for the component manager
((SpringCompMgr) ComponentManager.getInstance()).removeChildAc();
}

/**
* Find all ServletContext attributes which implement {@link DisposableBean}
* and destroy them, removing all affected ServletContext attributes eventually.
* @param sc the ServletContext to check
*/
static void cleanupAttributes(ServletContext sc) {
Enumeration<String> attrNames = sc.getAttributeNames();
while (attrNames.hasMoreElements()) {
String attrName = attrNames.nextElement();
if (attrName.startsWith("org.springframework.")) {
Object attrValue = sc.getAttribute(attrName);
if (attrValue instanceof DisposableBean) {
try {
((DisposableBean) attrValue).destroy();
}
catch (Throwable ex) {
log.error("Couldn't invoke destroy method of attribute with name '" + attrName + "'", ex);
}
}
}
}
}
}
2 changes: 1 addition & 1 deletion master/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.3.4.Final</version>
<version>5.3.5.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down

0 comments on commit 52386b2

Please sign in to comment.