use of org.openmrs.module.ModuleException in project openmrs-core by openmrs.
the class ModuleFilterMapping method retrieveFilterMappings.
/**
* Static method to parse through a Module's configuration file and return a List of
* ModuleFilterMapping objects for which there are configuration elements. Expected XML Format:
*
* <pre>
* <filter-mapping>
* <filter-name>MyFilterName</filter-name>
* <url-pattern>The pattern of URLs to match</filter-class>
* </filter-mapping>
* or
* <filter-mapping>
* <filter-name>MyFilterName</filter-name>
* <servlet-name>The servlet name to match</servlet-name>
* </filter-mapping>
* </pre>
*
* @param module - The {@link Module} for which you want to retrieve the defined
* {@link ModuleFilterMapping}s
* @return - a List of {@link ModuleFilterMapping}s that are defined for the passed
* {@link Module}
*/
public static List<ModuleFilterMapping> retrieveFilterMappings(Module module) {
List<ModuleFilterMapping> mappings = new ArrayList<>();
try {
Element rootNode = module.getConfig().getDocumentElement();
NodeList mappingNodes = rootNode.getElementsByTagName("filter-mapping");
if (mappingNodes.getLength() > 0) {
for (int i = 0; i < mappingNodes.getLength(); i++) {
ModuleFilterMapping mapping = new ModuleFilterMapping(module);
Node node = mappingNodes.item(i);
NodeList configNodes = node.getChildNodes();
for (int j = 0; j < configNodes.getLength(); j++) {
Node configNode = configNodes.item(j);
if ("filter-name".equals(configNode.getNodeName())) {
mapping.setFilterName(configNode.getTextContent());
} else if ("url-pattern".equals(configNode.getNodeName())) {
mapping.addUrlPattern(configNode.getTextContent());
} else if ("servlet-name".equals(configNode.getNodeName())) {
mapping.addServletName(configNode.getTextContent());
}
}
mappings.add(mapping);
}
}
} catch (Exception e) {
throw new ModuleException("Unable to parse filters in module configuration.", e);
}
log.debug("Retrieved " + mappings.size() + " filter-mappings for " + module.getModuleId() + ": " + mappings);
return mappings;
}
use of org.openmrs.module.ModuleException 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.ModuleException in project openmrs-core by openmrs.
the class WebModuleUtil method loadServlets.
/**
* This method will find and cache this module's servlets (so that it doesn't have to look them
* up every time)
*
* @param mod
* @param servletContext the servlet context
*/
public static void loadServlets(Module mod, ServletContext servletContext) {
Element rootNode = mod.getConfig().getDocumentElement();
NodeList servletTags = rootNode.getElementsByTagName("servlet");
for (int i = 0; i < servletTags.getLength(); i++) {
Node node = servletTags.item(i);
NodeList childNodes = node.getChildNodes();
String name = "", className = "";
for (int j = 0; j < childNodes.getLength(); j++) {
Node childNode = childNodes.item(j);
if ("servlet-name".equals(childNode.getNodeName())) {
if (childNode.getTextContent() != null) {
name = childNode.getTextContent().trim();
}
} else if ("servlet-class".equals(childNode.getNodeName()) && childNode.getTextContent() != null) {
className = childNode.getTextContent().trim();
}
}
if (name.length() == 0 || className.length() == 0) {
log.warn("both 'servlet-name' and 'servlet-class' are required for the 'servlet' tag. Given '" + name + "' and '" + className + "' for module " + mod.getName());
continue;
}
HttpServlet httpServlet;
try {
httpServlet = (HttpServlet) ModuleFactory.getModuleClassLoader(mod).loadClass(className).newInstance();
} catch (ClassNotFoundException e) {
log.warn("Class not found for servlet " + name + " for module " + mod.getName(), e);
continue;
} catch (IllegalAccessException e) {
log.warn("Class cannot be accessed for servlet " + name + " for module " + mod.getName(), e);
continue;
} catch (InstantiationException e) {
log.warn("Class cannot be instantiated for servlet " + name + " for module " + mod.getName(), e);
continue;
}
try {
log.debug("Initializing " + name + " servlet. - " + httpServlet + ".");
ServletConfig servletConfig = new ModuleServlet.SimpleServletConfig(name, servletContext);
httpServlet.init(servletConfig);
} catch (Exception e) {
log.warn("Unable to initialize servlet: ", e);
throw new ModuleException("Unable to initialize servlet: " + httpServlet, mod.getModuleId(), e);
}
// don't allow modules to overwrite servlets of other modules.
HttpServlet otherServletUsingSameName = moduleServlets.get(name);
if (otherServletUsingSameName != null) {
String otherServletName = otherServletUsingSameName.getClass().getPackage() + "." + otherServletUsingSameName.getClass().getName();
throw new ModuleException("A servlet mapping with name " + name + " is already in use and pointing at: " + otherServletName + " from another installed module and this module is trying" + " to use that same name. Either the module attempting to be installed (" + mod.getModuleId() + ") will not work or the other one will not. Please consult the developers of these two" + " modules to sort this out.");
}
log.debug("Caching the " + name + " servlet.");
moduleServlets.put(name, httpServlet);
}
}
use of org.openmrs.module.ModuleException in project openmrs-core by openmrs.
the class WebModuleUtil method loadFilters.
/**
* This method will initialize and store this module's filters
*
* @param module - The Module to load and register Filters
* @param servletContext - The servletContext within which this method is called
*/
public static void loadFilters(Module module, ServletContext servletContext) {
// Load Filters
Map<String, Filter> filters = new HashMap<>();
try {
for (ModuleFilterDefinition def : ModuleFilterDefinition.retrieveFilterDefinitions(module)) {
if (moduleFiltersByName.containsKey(def.getFilterName())) {
throw new ModuleException("A filter with name <" + def.getFilterName() + "> has already been registered.");
}
ModuleFilterConfig config = ModuleFilterConfig.getInstance(def, servletContext);
Filter f = (Filter) ModuleFactory.getModuleClassLoader(module).loadClass(def.getFilterClass()).newInstance();
f.init(config);
filters.put(def.getFilterName(), f);
}
} catch (ModuleException e) {
throw e;
} catch (Exception e) {
throw new ModuleException("An error occurred initializing Filters for module: " + module.getModuleId(), e);
}
moduleFilters.put(module, filters.values());
moduleFiltersByName.putAll(filters);
log.debug("Module: " + module.getModuleId() + " successfully loaded " + filters.size() + " filters.");
// Load Filter Mappings
List<ModuleFilterMapping> modMappings = ModuleFilterMapping.retrieveFilterMappings(module);
moduleFilterMappings.addAll(modMappings);
log.debug("Module: " + module.getModuleId() + " successfully loaded " + modMappings.size() + " filter mappings.");
}
use of org.openmrs.module.ModuleException in project openmrs-core by openmrs.
the class ModuleFilterDefinition method retrieveFilterDefinitions.
// Static methods
/**
* Static method to parse through a Module's configuration file and return a List of
* ModuleFilterDefinition objects for which there are configuration elements. Expected XML
* Format:
*
* <pre>
* <filter>
* <filter-name>MyFilterName</filter-name>
* <filter-class>Fully qualified classname of the Filter class</filter-class>
* <init-param>
* <param-name>filterParameterName1</param-name>
* <param-value>filterParameterValue1</param-value>
* </init-param>
* </filter>
* </pre>
*
* @param module - The {@link Module} for which to retrieve filter the defined
* {@link ModuleFilterDefinition}s
* @return List of {@link ModuleFilterDefinition}s that have been defined for the passed
* {@link Module}
*/
public static List<ModuleFilterDefinition> retrieveFilterDefinitions(Module module) {
List<ModuleFilterDefinition> filters = new ArrayList<>();
try {
Element rootNode = module.getConfig().getDocumentElement();
NodeList filterNodes = rootNode.getElementsByTagName("filter");
if (filterNodes.getLength() > 0) {
for (int i = 0; i < filterNodes.getLength(); i++) {
ModuleFilterDefinition filter = new ModuleFilterDefinition(module);
Node node = filterNodes.item(i);
NodeList configNodes = node.getChildNodes();
for (int j = 0; j < configNodes.getLength(); j++) {
Node configNode = configNodes.item(j);
if ("filter-name".equals(configNode.getNodeName())) {
filter.setFilterName(configNode.getTextContent().trim());
} else if ("filter-class".equals(configNode.getNodeName())) {
filter.setFilterClass(configNode.getTextContent().trim());
} else if ("init-param".equals(configNode.getNodeName())) {
NodeList paramNodes = configNode.getChildNodes();
String paramName = "";
String paramValue = "";
for (int k = 0; k < paramNodes.getLength(); k++) {
Node paramNode = paramNodes.item(k);
if ("param-name".equals(paramNode.getNodeName())) {
paramName = paramNode.getTextContent().trim();
} else if ("param-value".equals(paramNode.getNodeName())) {
paramValue = paramNode.getTextContent().trim();
}
}
filter.addInitParameter(paramName, paramValue);
}
}
filters.add(filter);
}
}
} catch (Exception e) {
throw new ModuleException("Unable to parse filters in module configuration.", e);
}
log.debug("Retrieved " + filters.size() + " filters for " + module + ": " + filters);
return filters;
}
Aggregations