protected void doReload() {
   applicationContext.refresh();
 }
Example #2
0
  /**
   * 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.
   */
  public static AbstractRefreshableApplicationContext refreshApplicationContext(
      AbstractRefreshableApplicationContext ctx, boolean isOpenmrsStartup, Module startedModule) {
    // notify all started modules that we are about to refresh the context
    for (Module module : ModuleFactory.getStartedModules()) {
      try {
        if (module.getModuleActivator() != null) module.getModuleActivator().willRefreshContext();
      } catch (Throwable t) {
        log.warn("Unable to call willRefreshContext() method in the module's activator", t);
      }
    }

    OpenmrsClassLoader.saveState();
    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();

    OpenmrsClassLoader.setThreadsToNewClassLoader();

    // reload the advice points that were lost when refreshing Spring
    if (log.isDebugEnabled())
      log.debug(
          "Reloading advice for all started modules: " + ModuleFactory.getStartedModules().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 : ModuleFactory.getStartedModules()) {
        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);
            }
          }

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

    return ctx;
  }