Search in sources :

Example 11 with Module

use of org.openmrs.module.Module in project openmrs-core by openmrs.

the class WebModuleUtil method stopModule.

/**
 * Reverses all visible activities done by startModule(org.openmrs.module.Module)
 *
 * @param mod
 * @param servletContext
 * @param skipRefresh
 */
public static void stopModule(Module mod, ServletContext servletContext, boolean skipRefresh) {
    String moduleId = mod.getModuleId();
    String modulePackage = mod.getPackageName();
    // stop all dependent modules
    for (Module dependentModule : ModuleFactory.getStartedModules()) {
        if (!dependentModule.equals(mod) && dependentModule.getRequiredModules().contains(modulePackage)) {
            stopModule(dependentModule, servletContext, skipRefresh);
        }
    }
    String realPath = getRealPath(servletContext);
    // delete the web files from the webapp
    String absPath = realPath + "/WEB-INF/view/module/" + moduleId;
    File moduleWebFolder = new File(absPath.replace("/", File.separator));
    if (moduleWebFolder.exists()) {
        try {
            OpenmrsUtil.deleteDirectory(moduleWebFolder);
        } catch (IOException io) {
            log.warn("Couldn't delete: " + moduleWebFolder.getAbsolutePath(), io);
        }
    }
    // (not) deleting module message properties
    // remove the module's servlets
    unloadServlets(mod);
    // remove the module's filters and filter mappings
    unloadFilters(mod);
    // stop all tasks associated with mod
    stopTasks(mod);
    // remove this module's entries in the dwr xml file
    InputStream inputStream = null;
    try {
        Document config = mod.getConfig();
        Element root = config.getDocumentElement();
        // if they defined any xml element
        if (root.getElementsByTagName("dwr").getLength() > 0) {
            // get the dwr-module.xml file that we're appending our code to
            File f = new File(realPath + "/WEB-INF/dwr-modules.xml".replace("/", File.separator));
            // testing if file exists
            if (!f.exists()) {
                // if it does not -> needs to be created
                createDwrModulesXml(realPath);
            }
            inputStream = new FileInputStream(f);
            Document dwrmodulexml = getDWRModuleXML(inputStream, realPath);
            Element outputRoot = dwrmodulexml.getDocumentElement();
            // loop over all of the children of the "dwr" tag
            // and remove all "allow" and "signature" tags that have the
            // same moduleId attr as the module being stopped
            NodeList nodeList = outputRoot.getChildNodes();
            int i = 0;
            while (i < nodeList.getLength()) {
                Node current = nodeList.item(i);
                if ("allow".equals(current.getNodeName()) || "signatures".equals(current.getNodeName())) {
                    NamedNodeMap attrs = current.getAttributes();
                    Node attr = attrs.getNamedItem("moduleId");
                    if (attr != null && moduleId.equals(attr.getNodeValue())) {
                        outputRoot.removeChild(current);
                    } else {
                        i++;
                    }
                } else {
                    i++;
                }
            }
            // save the dwr-modules.xml file.
            OpenmrsUtil.saveDocument(dwrmodulexml, f);
        }
    } catch (FileNotFoundException e) {
        throw new ModuleException(realPath + "/WEB-INF/dwr-modules.xml file doesn't exist.", e);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException io) {
                log.error("Error while closing input stream", io);
            }
        }
    }
    if (!skipRefresh) {
        refreshWAC(servletContext, false, null);
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Document(org.w3c.dom.Document) FileInputStream(java.io.FileInputStream) Module(org.openmrs.module.Module) ModuleException(org.openmrs.module.ModuleException) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 12 with Module

use of org.openmrs.module.Module in project openmrs-core by openmrs.

the class WebModuleUtil method shutdownModules.

/**
 * Reverses all activities done by startModule(org.openmrs.module.Module) Normal stop/shutdown
 * is done by ModuleFactory
 */
public static void shutdownModules(ServletContext servletContext) {
    String realPath = getRealPath(servletContext);
    // clear the module messages
    String messagesPath = realPath + "/WEB-INF/";
    File folder = new File(messagesPath.replace("/", File.separator));
    File[] files = folder.listFiles();
    if (folder.exists() && files != null) {
        Properties emptyProperties = new Properties();
        for (File f : files) {
            if (f.getName().startsWith("module_messages")) {
                OpenmrsUtil.storeProperties(emptyProperties, f, "");
            }
        }
    }
    // call web shutdown for each module
    for (Module mod : ModuleFactory.getLoadedModules()) {
        stopModule(mod, servletContext, true);
    }
}
Also used : Properties(java.util.Properties) Module(org.openmrs.module.Module) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 13 with Module

use of org.openmrs.module.Module in project openmrs-core by openmrs.

the class DispatcherServlet method reInitFrameworkServlet.

/**
 * Called by the ModuleUtil after adding in a new module. This needs to be called because the
 * new mappings and advice that a new module adds in are cached by Spring's DispatcherServlet.
 * This method will reload that cache.
 *
 * @throws ServletException
 */
public void reInitFrameworkServlet() throws ServletException {
    log.debug("Framework being REinitialized");
    Thread.currentThread().setContextClassLoader(OpenmrsClassLoader.getInstance());
    ((XmlWebApplicationContext) getWebApplicationContext()).setClassLoader(OpenmrsClassLoader.getInstance());
    refresh();
    // reload the advice points that were lost when refreshing Spring
    for (Module module : ModuleFactory.getStartedModules()) {
        ModuleFactory.loadAdvice(module);
    }
}
Also used : XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) Module(org.openmrs.module.Module)

Example 14 with Module

use of org.openmrs.module.Module in project openmrs-core by openmrs.

the class WebModuleUtilTest method startModule_shouldCreateDwrModulesXmlIfNotExists.

/**
 * @throws ParserConfigurationException
 * @see WebModuleUtil#startModule(Module, ServletContext, boolean)
 */
@Test
public void startModule_shouldCreateDwrModulesXmlIfNotExists() throws ParserConfigurationException {
    // create dummy module and start it
    Module mod = buildModuleForMessageTest();
    ModuleFactory.getStartedModulesMap().put(mod.getModuleId(), mod);
    ServletContext servletContext = mock(ServletContext.class);
    String realPath = servletContext.getRealPath("");
    if (realPath == null)
        realPath = System.getProperty("user.dir");
    // manually delete dwr-modules.xml
    File f = new File(realPath + "/WEB-INF/dwr-modules.xml");
    f.delete();
    // start the dummy module
    WebModuleUtil.startModule(mod, servletContext, true);
    // test if dwr-modules.xml is created
    assertTrue(f.exists());
    ModuleFactory.getStartedModulesMap().clear();
}
Also used : ServletContext(javax.servlet.ServletContext) Module(org.openmrs.module.Module) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 15 with Module

use of org.openmrs.module.Module in project openmrs-core by openmrs.

the class OpenmrsProfileExcludeFilter method matchOpenmrsProfileAttributes.

public boolean matchOpenmrsProfileAttributes(Map<String, Object> openmrsProfile) {
    Object openmrsPlatformVersion = openmrsProfile.get("openmrsPlatformVersion");
    if (StringUtils.isBlank((String) openmrsPlatformVersion)) {
        // Left for backwards compatibility
        openmrsPlatformVersion = openmrsProfile.get("openmrsVersion");
    }
    if (StringUtils.isNotBlank((String) openmrsPlatformVersion) && !ModuleUtil.matchRequiredVersions(OpenmrsConstants.OPENMRS_VERSION_SHORT, (String) openmrsPlatformVersion)) {
        return false;
    }
    String[] modules = (String[]) openmrsProfile.get("modules");
    for (String moduleAndVersion : modules) {
        if ("!".equals(moduleAndVersion.substring(0, 1))) {
            if (ModuleFactory.isModuleStarted(moduleAndVersion.substring(1, moduleAndVersion.length()))) {
                return false;
            }
        } else {
            String[] splitModuleAndVersion = moduleAndVersion.split(":");
            String moduleId = splitModuleAndVersion[0];
            String moduleVersion = splitModuleAndVersion[1];
            boolean moduleMatched = false;
            for (Module module : ModuleFactory.getStartedModules()) {
                if (module.getModuleId().equals(moduleId) && ModuleUtil.matchRequiredVersions(module.getVersion(), moduleVersion)) {
                    moduleMatched = true;
                    break;
                }
            }
            if (!moduleMatched) {
                return false;
            }
        }
    }
    return true;
}
Also used : Module(org.openmrs.module.Module)

Aggregations

Module (org.openmrs.module.Module)22 File (java.io.File)8 Test (org.junit.Test)7 BaseModuleActivatorTest (org.openmrs.module.BaseModuleActivatorTest)5 IOException (java.io.IOException)3 URL (java.net.URL)3 Properties (java.util.Properties)3 JarFile (java.util.jar.JarFile)2 ServletContext (javax.servlet.ServletContext)2 ServletException (javax.servlet.ServletException)2 AdministrationService (org.openmrs.api.AdministrationService)2 MandatoryModuleException (org.openmrs.module.MandatoryModuleException)2 ModuleMustStartException (org.openmrs.module.ModuleMustStartException)2 OpenmrsCoreModuleException (org.openmrs.module.OpenmrsCoreModuleException)2 Config (org.openmrs.module.pihcore.config.Config)2 DatabaseUpdateException (org.openmrs.util.DatabaseUpdateException)2 InputRequiredException (org.openmrs.util.InputRequiredException)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 BeanCreationException (org.springframework.beans.factory.BeanCreationException)2 FileInputStream (java.io.FileInputStream)1