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

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

Introduction

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

Prototype

public void setClassLoader(@Nullable ClassLoader classLoader) 

Source Link

Document

Specify the ClassLoader to load class path resources with, or null for using the thread context class loader at the time of actual resource access.

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.
 *///  ww w.ja  va  2 s.  c om
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;
}