Example usage for org.springframework.context.support AbstractRefreshableApplicationContext close

List of usage examples for org.springframework.context.support AbstractRefreshableApplicationContext close

Introduction

In this page you can find the example usage for org.springframework.context.support AbstractRefreshableApplicationContext close.

Prototype

@Override
public void close() 

Source Link

Document

Close this application context, destroying all beans in its bean factory.

Usage

From source file:org.openmrs.module.ModuleUtil.java

/**
 * Refreshes the given application context "properly" in OpenMRS. Will first shut down the
 * Context and destroy the classloader, then will refresh and set everything back up again.
 *
 * @param ctx Spring application context that needs refreshing.
 * @param isOpenmrsStartup if this refresh is being done at application startup.
 * @param startedModule the module that was just started and waiting on the context refresh.
 * @return AbstractRefreshableApplicationContext The newly refreshed application context.
 *///from   w  ww.ja v  a2  s .  c o m
public static AbstractRefreshableApplicationContext refreshApplicationContext(
        AbstractRefreshableApplicationContext ctx, boolean isOpenmrsStartup, Module startedModule) {
    //notify all started modules that we are about to refresh the context
    Set<Module> startedModules = new LinkedHashSet<Module>(ModuleFactory.getStartedModulesInOrder());
    for (Module module : startedModules) {
        try {
            if (module.getModuleActivator() != null) {
                module.getModuleActivator().willRefreshContext();
            }
        } catch (Exception e) {
            log.warn("Unable to call willRefreshContext() method in the module's activator", e);
        }
    }

    OpenmrsClassLoader.saveState();
    SchedulerUtil.shutdown();
    ServiceContext.destroyInstance();

    try {
        ctx.stop();
        ctx.close();
    } catch (Exception e) {
        log.warn("Exception while stopping and closing context: ", e);
        // Spring seems to be trying to refresh the context instead of /just/ stopping
        // pass
    }
    OpenmrsClassLoader.destroyInstance();
    ctx.setClassLoader(OpenmrsClassLoader.getInstance());
    Thread.currentThread().setContextClassLoader(OpenmrsClassLoader.getInstance());

    ServiceContext.getInstance().startRefreshingContext();
    try {
        ctx.refresh();
    } finally {
        ServiceContext.getInstance().doneRefreshingContext();
    }

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

    OpenmrsClassLoader.restoreState();
    SchedulerUtil.startup(Context.getRuntimeProperties());

    OpenmrsClassLoader.setThreadsToNewClassLoader();

    // reload the advice points that were lost when refreshing Spring
    if (log.isDebugEnabled()) {
        log.debug("Reloading advice for all started modules: " + startedModules.size());
    }

    try {
        //The call backs in this block may need lazy loading of objects
        //which will fail because we use an OpenSessionInViewFilter whose opened session
        //was closed when the application context was refreshed as above.
        //So we need to open another session now. TRUNK-3739
        Context.openSessionWithCurrentUser();
        for (Module module : startedModules) {
            if (!module.isStarted()) {
                continue;
            }

            ModuleFactory.loadAdvice(module);
            try {
                ModuleFactory.passDaemonToken(module);

                if (module.getModuleActivator() != null) {
                    module.getModuleActivator().contextRefreshed();
                    try {
                        //if it is system start up, call the started method for all started modules
                        if (isOpenmrsStartup) {
                            module.getModuleActivator().started();
                        }
                        //if refreshing the context after a user started or uploaded a new module
                        else if (!isOpenmrsStartup && module.equals(startedModule)) {
                            module.getModuleActivator().started();
                        }
                    } catch (Exception e) {
                        log.warn("Unable to invoke started() method on the module's activator", e);
                        ModuleFactory.stopModule(module, true, true);
                    }
                }

            } catch (Exception e) {
                log.warn("Unable to invoke method on the module's activator ", e);
            }
        }
    } finally {
        Context.closeSessionWithCurrentUser();
    }

    return ctx;
}