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);
}
}
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);
}
}
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);
}
}
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();
}
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;
}
Aggregations