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

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

Introduction

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

Prototype

@Override
    public void refresh() throws BeansException, IllegalStateException 

Source Link

Usage

From source file:cross.applicationContext.DefaultApplicationContextFactory.java

/**
 * Creates a new application context from the current list of
 * <code>applicationContextPaths</code>, interpreted as class path
 * resources, and the current <code>configuration</code>.
 *
 * @return the application context//from   w w w  .  j a  v  a 2 s  . co  m
 * @throws BeansException if any beans are not configured correctly
 */
public ApplicationContext createClassPathApplicationContext() throws BeansException {
    AbstractRefreshableApplicationContext context = null;
    try {
        final ConfiguringBeanPostProcessor cbp = new ConfiguringBeanPostProcessor();
        cbp.setConfiguration(configuration);
        context = new ClassPathXmlApplicationContext(
                applicationContextPaths.toArray(new String[applicationContextPaths.size()]), context);
        context.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() {
            @Override
            public void postProcessBeanFactory(ConfigurableListableBeanFactory clbf) throws BeansException {
                clbf.addBeanPostProcessor(cbp);
            }
        });
        context.refresh();
    } catch (BeansException e2) {
        throw e2;
    }
    return context;
}

From source file:cross.applicationContext.DefaultApplicationContextFactory.java

/**
 * Creates a new application context from the current list of
 * <code>applicationContextPaths</code>, interpreted as file system
 * resources, and the current/*from   w ww.j a  v a 2s.c o m*/
 * <code>configuration</code>.
 *
 * @return the application context
 * @throws BeansException if any beans are not configured correctly
 */
public ApplicationContext createApplicationContext() throws BeansException {
    AbstractRefreshableApplicationContext context = null;
    try {
        final ConfiguringBeanPostProcessor cbp = new ConfiguringBeanPostProcessor();
        cbp.setConfiguration(configuration);
        context = new FileSystemXmlApplicationContext(
                applicationContextPaths.toArray(new String[applicationContextPaths.size()]), context);
        context.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() {
            @Override
            public void postProcessBeanFactory(ConfigurableListableBeanFactory clbf) throws BeansException {
                clbf.addBeanPostProcessor(cbp);
            }
        });
        context.refresh();
    } catch (BeansException e2) {
        throw e2;
    }
    return context;
}

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  ww  w .j  a v a 2s. co 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;
}