Skip to content

Commit

Permalink
Back porting to 1.9.x: Refactor InitializationFilter so that first-time
Browse files Browse the repository at this point in the history
startup of OpenMRS behaves identically to normal startup - TRUNK-3998
  • Loading branch information
dkayiwa committed Jun 11, 2013
1 parent 7aded68 commit 7eefb75
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 33 deletions.
5 changes: 3 additions & 2 deletions web/src/main/java/org/openmrs/web/WebDaemon.java
Expand Up @@ -14,11 +14,12 @@
package org.openmrs.web;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.openmrs.api.context.Context;
import org.openmrs.api.context.Daemon;
import org.openmrs.module.ModuleException;
import org.openmrs.util.DatabaseUpdateException;
import org.openmrs.util.InputRequiredException;

/**
* This class provides {@link Daemon} functionality in a web context.
Expand All @@ -32,7 +33,7 @@ public class WebDaemon extends Daemon {
*
* @param servletContext the servlet context.
*/
public static void startOpenmrs(final ServletContext servletContext) throws ServletException {
public static void startOpenmrs(final ServletContext servletContext) throws DatabaseUpdateException, InputRequiredException {

// create a new thread and execute that task in it
DaemonThread startOpenmrsThread = new DaemonThread() {
Expand Down
Expand Up @@ -42,6 +42,7 @@
import javax.servlet.http.HttpServletResponse;

import liquibase.changelog.ChangeSet;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand All @@ -68,6 +69,7 @@
import org.openmrs.util.Security;
import org.openmrs.web.Listener;
import org.openmrs.web.WebConstants;
import org.openmrs.web.WebDaemon;
import org.openmrs.web.filter.StartupFilter;
import org.openmrs.web.filter.util.CustomResourceLoader;
import org.openmrs.web.filter.util.ErrorMessageConstants;
Expand Down Expand Up @@ -1552,14 +1554,25 @@ public void executing(ChangeSet changeSet, int numChangeSetsToRun) {
ContextLoader contextLoader = new ContextLoader();
contextLoader.initWebApplicationContext(filterConfig.getServletContext());

// start openmrs
// output properties to the openmrs runtime properties file so that this wizard is not run again
FileOutputStream fos = null;
try {
Context.openSession();

// load core modules so that required modules are known at openmrs startup
Listener.loadBundledModules(filterConfig.getServletContext());
fos = new FileOutputStream(getRuntimePropertiesFile());
OpenmrsUtil.storeProperties(runtimeProperties, fos,
"Auto generated by OpenMRS initialization wizard");
wizardModel.workLog.add("Saved runtime properties file " + getRuntimePropertiesFile());

Context.startup(runtimeProperties);
// don't need to catch errors here because we tested it at the beginning of the wizard
}
finally {
if (fos != null) {
fos.close();
}
}

// start openmrs
try {
WebDaemon.startOpenmrs(filterConfig.getServletContext());
}
catch (DatabaseUpdateException updateEx) {
log.warn("Error while running the database update file", updateEx);
Expand Down Expand Up @@ -1594,6 +1607,8 @@ public void executing(ChangeSet changeSet, int numChangeSetsToRun) {

// TODO catch openmrs errors here and drop the user back out to the setup screen

Context.openSession();

if (!wizardModel.implementationId.equals("")) {
try {
Context.addProxyPrivilege(PrivilegeConstants.MANAGE_GLOBAL_PROPERTIES);
Expand Down Expand Up @@ -1632,12 +1647,6 @@ public void executing(ChangeSet changeSet, int numChangeSetsToRun) {
Context.getUserService().changePassword("test", wizardModel.adminUserPassword);
Context.logout();
}

// web load modules
Listener.performWebStartOfModules(filterConfig.getServletContext());

// start the scheduled tasks
SchedulerUtil.startup(runtimeProperties);
}
catch (Throwable t) {
Context.shutdown();
Expand All @@ -1648,22 +1657,6 @@ public void executing(ChangeSet changeSet, int numChangeSetsToRun) {
return;
}

// output properties to the openmrs runtime properties file so that this wizard is not run again
FileOutputStream fos = null;
try {
fos = new FileOutputStream(getRuntimePropertiesFile());
OpenmrsUtil.storeProperties(runtimeProperties, fos,
"Auto generated by OpenMRS initialization wizard");
wizardModel.workLog.add("Saved runtime properties file " + getRuntimePropertiesFile());

// don't need to catch errors here because we tested it at the beginning of the wizard
}
finally {
if (fos != null) {
fos.close();
}
}

// set this so that the wizard isn't run again on next page load
Context.closeSession();
}
Expand Down
Expand Up @@ -428,7 +428,7 @@ protected boolean isSuperUser(Connection connection, Integer userId) throws Exce
* @param servletContext the servletContext from the filterconfig
* @see Listener#startOpenmrs(ServletContext)
*/
private void startOpenmrs(ServletContext servletContext) throws IOException, ServletException {
private void startOpenmrs(ServletContext servletContext) throws Exception {
// start spring
// after this point, all errors need to also call: contextLoader.closeWebApplicationContext(event.getServletContext())
// logic copied from org.springframework.web.context.ContextLoaderListener
Expand All @@ -438,9 +438,9 @@ private void startOpenmrs(ServletContext servletContext) throws IOException, Ser
try {
WebDaemon.startOpenmrs(servletContext);
}
catch (ServletException servletException) {
catch (Exception exception) {
contextLoader.closeWebApplicationContext(servletContext);
throw servletException;
throw exception;
}
}

Expand Down

0 comments on commit 7eefb75

Please sign in to comment.