Example usage for java.lang Module equals

List of usage examples for java.lang Module equals

Introduction

In this page you can find the example usage for java.lang Module equals.

Prototype

public boolean equals(Object obj) 

Source Link

Document

Indicates whether some other object is "equal to" this one.

Usage

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

/**
 * Runs through the advice and extension points and removes from api.<br>
 * <code>skipOverStartedProperty</code> should only be true when openmrs is stopping modules
 * because it is shutting down. When normally stopping a module, use {@link #stopModule(Module)}
 * (or leave value as false). This property controls whether the globalproperty is set for
 * startup/shutdown. <br>/*from w  w  w . j a  va  2 s.co m*/
 * Also calls module's {@link Activator#shutdown()}
 * 
 * @param mod module to stop
 * @param skipOverStartedProperty true if we don't want to set &lt;moduleid&gt;.started to false
 * @param isFailedStartup true if this is being called as a cleanup because of a failed module
 *            startup
 * @return list of dependent modules that were stopped because this module was stopped. This
 *         will never be null.
 */
public static List<Module> stopModule(Module mod, boolean skipOverStartedProperty, boolean isFailedStartup)
        throws ModuleMustStartException {

    List<Module> dependentModulesStopped = new Vector<Module>();

    if (mod != null) {

        if (!ModuleFactory.isModuleStarted(mod)) {
            return dependentModulesStopped;
        }

        try {
            if (mod.getModuleActivator() != null) { // if extends BaseModuleActivator
                mod.getModuleActivator().willStop();
            }
        } catch (Exception t) {
            log.warn("Unable to call module's Activator.willStop() method", t);
        }

        String moduleId = mod.getModuleId();

        // don't allow mandatory modules to be stopped
        // don't use database checks here because spring might be in a bad state
        if (!isFailedStartup && mod.isMandatory()) {
            throw new MandatoryModuleException(moduleId);
        }

        if (!isFailedStartup && ModuleConstants.CORE_MODULES.containsKey(moduleId)) {
            throw new OpenmrsCoreModuleException(moduleId);
        }

        String modulePackage = mod.getPackageName();

        // stop all dependent modules
        // copy modules to new list to avoid "concurrent modification exception"
        List<Module> startedModulesCopy = new ArrayList<Module>();
        startedModulesCopy.addAll(getStartedModules());
        for (Module dependentModule : startedModulesCopy) {
            if (dependentModule != null && !dependentModule.equals(mod)) {
                if (dependentModule.getRequiredModules() != null
                        && dependentModule.getRequiredModules().contains(modulePackage)) {
                    dependentModulesStopped.add(dependentModule);
                    dependentModulesStopped
                            .addAll(stopModule(dependentModule, skipOverStartedProperty, isFailedStartup));
                }
            }
        }

        getStartedModulesMap().remove(moduleId);
        if (actualStartupOrder != null) {
            actualStartupOrder.remove(moduleId);
            for (Module depModule : dependentModulesStopped) {
                actualStartupOrder.remove(depModule.getModuleId());
            }
        }

        if (!skipOverStartedProperty && !Context.isRefreshingContext()) {
            saveGlobalProperty(moduleId + ".started", "false", getGlobalPropertyStartedDescription(moduleId));
        }

        ModuleClassLoader moduleClassLoader = getModuleClassLoaderMap().get(mod);
        if (moduleClassLoader != null) {
            unregisterProvidedPackages(moduleClassLoader);

            log.debug("Mod was in classloader map.  Removing advice and extensions.");
            // remove all advice by this module
            try {
                for (AdvicePoint advice : mod.getAdvicePoints()) {
                    Class cls = null;
                    try {
                        cls = Context.loadClass(advice.getPoint());
                        Object aopObject = advice.getClassInstance();
                        if (Advisor.class.isInstance(aopObject)) {
                            log.debug("adding advisor: " + aopObject.getClass());
                            Context.removeAdvisor(cls, (Advisor) aopObject);
                        } else {
                            log.debug("Adding advice: " + aopObject.getClass());
                            Context.removeAdvice(cls, (Advice) aopObject);
                        }
                    } catch (Exception t) {
                        log.warn("Could not remove advice point: " + advice.getPoint(), t);
                    }
                }
            } catch (Exception t) {
                log.warn("Error while getting advicePoints from module: " + moduleId, t);
            }

            // remove all extensions by this module
            try {
                for (Extension ext : mod.getExtensions()) {
                    String extId = ext.getExtensionId();
                    try {
                        List<Extension> tmpExtensions = getExtensions(extId);
                        if (tmpExtensions == null) {
                            tmpExtensions = new Vector<Extension>();
                        }

                        tmpExtensions.remove(ext);
                        getExtensionMap().put(extId, tmpExtensions);
                    } catch (Exception exterror) {
                        log.warn("Error while getting extension: " + ext, exterror);
                    }
                }
            } catch (Exception t) {
                log.warn("Error while getting extensions from module: " + moduleId, t);
            }
        }

        //Run the onShutdown() method for openmrs services in this module.
        List<OpenmrsService> services = Context.getModuleOpenmrsServices(modulePackage);
        if (services != null) {
            for (OpenmrsService service : services) {
                service.onShutdown();
            }
        }

        try {
            if (mod.getModuleActivator() != null) {// extends BaseModuleActivator
                mod.getModuleActivator().stopped();
            } else {
                mod.getActivator().shutdown(); // implements old Activator interface
            }
        } catch (Exception t) {
            log.warn("Unable to call module's Activator.shutdown() method", t);
        }

        //Since extensions are loaded by the module class loader which is about to be disposed,
        //we need to clear them, else we shall never be able to unload the class loader until
        //when we unload the module, hence resulting into two problems:
        // 1) Memory leakage for start/stop module.
        // 2) Calls to Context.getService(Service.class) which are made within these extensions 
        //     will throw APIException("Service not found: ") because their calls to Service.class
        //    will pass in a Class from the old module class loader (which loaded them) yet the
        //    ServiceContext will have new services from a new module class loader.
        //
        //Same thing applies to activator, moduleActivator and AdvicePoint classInstance.
        mod.getExtensions().clear();
        mod.setActivator(null);
        mod.setModuleActivator(null);
        mod.disposeAdvicePointsClassInstance();

        ModuleClassLoader cl = removeClassLoader(mod);
        if (cl != null) {
            cl.dispose();
            cl = null;
            // remove files from lib cache
            File folder = OpenmrsClassLoader.getLibCacheFolder();
            File tmpModuleDir = new File(folder, moduleId);
            try {
                OpenmrsUtil.deleteDirectory(tmpModuleDir);
            } catch (IOException e) {
                log.warn("Unable to delete libcachefolder for " + moduleId);
            }
        }
    }

    return dependentModulesStopped;
}

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