Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: openmrs/openmrs-core
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 9fe81003b34f
Choose a base ref
...
head repository: openmrs/openmrs-core
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 388fce51d3fc
Choose a head ref
  • 2 commits
  • 3 files changed
  • 1 contributor

Commits on Apr 24, 2013

  1. Fixing memory leak in the static content dispatcher servlet when

    refreshing spring: Investigate possible memory leak in 1.9.0 or one of
    its bundled modules. - TRUNK-3440
    dkayiwa committed Apr 24, 2013
    Copy the full SHA
    8d9b60b View commit details
  2. Merge pull request #287 from dkayiwa/TRUNK-3440

    Fixing memory leak in the static content dispatcher servlet when refreshing spring: Investigate possible memory leak in 1.9.0 or one of its bundled modules. - TRUNK-3440
    dkayiwa committed Apr 24, 2013
    Copy the full SHA
    388fce5 View commit details
21 changes: 21 additions & 0 deletions web/src/main/java/org/openmrs/module/web/WebModuleUtil.java
Original file line number Diff line number Diff line change
@@ -58,6 +58,7 @@
import org.openmrs.util.OpenmrsUtil;
import org.openmrs.util.PrivilegeConstants;
import org.openmrs.web.DispatcherServlet;
import org.openmrs.web.StaticDispatcherServlet;
import org.openmrs.web.dwr.OpenmrsDWRServlet;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;
@@ -76,6 +77,8 @@ public class WebModuleUtil {

private static DispatcherServlet dispatcherServlet = null;

private static StaticDispatcherServlet staticDispatcherServlet = null;

private static OpenmrsDWRServlet dwrServlet = null;

// caches all of the modules' mapped servlets
@@ -847,6 +850,10 @@ public static XmlWebApplicationContext refreshWAC(ServletContext servletContext,
dispatcherServlet.stopAndCloseApplicationContext();
}

if (staticDispatcherServlet != null) {
staticDispatcherServlet.stopAndCloseApplicationContext();
}

XmlWebApplicationContext newAppContext = (XmlWebApplicationContext) ModuleUtil.refreshApplicationContext(wac,
isOpenmrsStartup, startedModule);

@@ -855,6 +862,10 @@ public static XmlWebApplicationContext refreshWAC(ServletContext servletContext,
//the new handlerMappings
if (dispatcherServlet != null)
dispatcherServlet.reInitFrameworkServlet();

if (staticDispatcherServlet != null) {
staticDispatcherServlet.refreshApplicationContext();
}
}
catch (ServletException se) {
log.warn("Caught a servlet exception while refreshing the dispatcher servlet", se);
@@ -881,6 +892,16 @@ public static void setDispatcherServlet(DispatcherServlet ds) {
dispatcherServlet = ds;
}

/**
* Save the static content dispatcher servlet for use later when refreshing spring
*
* @param ds
*/
public static void setStaticDispatcherServlet(StaticDispatcherServlet ds) {
log.debug("Setting dispatcher servlet for static content: " + ds);
staticDispatcherServlet = ds;
}

/**
* Save the dwr servlet for use later (reinitializing things)
*
79 changes: 79 additions & 0 deletions web/src/main/java/org/openmrs/web/StaticDispatcherServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* The contents of this file are subject to the OpenMRS Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://license.openmrs.org
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs.web;

import javax.servlet.ServletException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.module.web.WebModuleUtil;
import org.openmrs.util.OpenmrsClassLoader;
import org.springframework.beans.BeansException;
import org.springframework.web.context.support.XmlWebApplicationContext;

/**
* This class is only used to get access to the dispatcher servlet that handles static content. <br/>
* <br/>
* After creation, this object is saved to WebModuleUtil for later use. When Spring's root
* webApplicationContext is refreshed, this dispatcher servlet needs to be refreshed too.
*
* @see #reInitFrameworkServlet()
*/
public class StaticDispatcherServlet extends org.springframework.web.servlet.DispatcherServlet {

private static final long serialVersionUID = 1L;

private Log log = LogFactory.getLog(this.getClass());

/**
* @see org.springframework.web.servlet.FrameworkServlet#initFrameworkServlet()
*/
@Override
protected void initFrameworkServlet() throws ServletException, BeansException {

Thread.currentThread().setContextClassLoader(OpenmrsClassLoader.getInstance());

log.info("Framework being initialized for static content");
WebModuleUtil.setStaticDispatcherServlet(this);

super.initFrameworkServlet();
}

/**
* Called by the ModuleUtil after adding in a new, updating, starting, or stopping a module.
* This needs to be called because each spring dispatcher servlet creates a new application
* context, which therefore needs to be refreshed too.
*
* @throws ServletException
*/
public void refreshApplicationContext() throws ServletException {
log.info("Application context for the static content dispatcher servlet is being refreshed");

Thread.currentThread().setContextClassLoader(OpenmrsClassLoader.getInstance());
((XmlWebApplicationContext) getWebApplicationContext()).setClassLoader(OpenmrsClassLoader.getInstance());

refresh();
}

public void stopAndCloseApplicationContext() {
try {
XmlWebApplicationContext ctx = (XmlWebApplicationContext) getWebApplicationContext();
ctx.stop();
ctx.close();
}
catch (Exception e) {
log.error("Exception while stopping and closing static content dispatcher servlet context: ", e);
}
}
}
2 changes: 1 addition & 1 deletion webapp/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -369,7 +369,7 @@

<servlet>
<servlet-name>openmrs_static_content</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<servlet-class>org.openmrs.web.StaticDispatcherServlet</servlet-class>
<!-- Don't use "load-on-startup" in case initial setup wizard is needed -->
</servlet>