use of org.openmrs.module.ModuleException in project openmrs-core by openmrs.
the class OpenmrsUtil method saveDocument.
/**
* Save the given xml document to the given outfile
*
* @param doc Document to be saved
* @param outFile file pointer to the location the xml file is to be saved to
*/
public static void saveDocument(Document doc, File outFile) {
OutputStream outStream = null;
try {
outStream = new FileOutputStream(outFile);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DocumentType doctype = doc.getDoctype();
if (doctype != null) {
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, doctype.getPublicId());
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, doctype.getSystemId());
}
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(outStream);
transformer.transform(source, result);
} catch (TransformerException e) {
throw new ModuleException("Error while saving dwrmodulexml back to dwr-modules.xml", e);
} catch (FileNotFoundException e) {
throw new ModuleException(outFile.getAbsolutePath() + " file doesn't exist.", e);
} finally {
try {
if (outStream != null) {
outStream.close();
}
} catch (Exception e) {
log.warn("Unable to close outstream", e);
}
}
}
use of org.openmrs.module.ModuleException in project openmrs-core by openmrs.
the class WebModuleUtil method getDWRModuleXML.
/**
* @param inputStream
* @param realPath
* @return
*/
private static Document getDWRModuleXML(InputStream inputStream, String realPath) {
Document dwrmodulexml;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
db.setEntityResolver((publicId, systemId) -> {
// with no data at the end, causing the parser to ignore the DTD.
return new InputSource(new StringReader(""));
});
dwrmodulexml = db.parse(inputStream);
} catch (Exception e) {
throw new ModuleException("Error parsing dwr-modules.xml file", e);
}
return dwrmodulexml;
}
use of org.openmrs.module.ModuleException in project openmrs-core by openmrs.
the class WebModuleUtil method startModule.
/**
* Performs the webapp specific startup needs for modules Normal startup is done in
* {@link ModuleFactory#startModule(Module)} If delayContextRefresh is true, the spring context
* is not rerun. This will save a lot of time, but it also means that the calling method is
* responsible for restarting the context if necessary (the calling method will also have to
* call {@link #loadServlets(Module, ServletContext)} and
* {@link #loadFilters(Module, ServletContext)}).<br>
* <br>
* If delayContextRefresh is true and this module should have caused a context refresh, a true
* value is returned. Otherwise, false is returned
*
* @param mod Module to start
* @param servletContext the current ServletContext
* @param delayContextRefresh true/false whether or not to do the context refresh
* @return boolean whether or not the spring context need to be refreshed
*/
public static boolean startModule(Module mod, ServletContext servletContext, boolean delayContextRefresh) {
if (log.isDebugEnabled()) {
log.debug("trying to start module " + mod);
}
// problem.
if (ModuleFactory.isModuleStarted(mod) && !mod.hasStartupError()) {
String realPath = getRealPath(servletContext);
if (realPath == null) {
realPath = System.getProperty("user.dir");
}
File webInf = new File(realPath + "/WEB-INF".replace("/", File.separator));
if (!webInf.exists()) {
webInf.mkdir();
}
// flag to tell whether we added any xml/dwr/etc changes that necessitate a refresh
// of the web application context
boolean moduleNeedsContextRefresh = false;
// copy the html files into the webapp (from /web/module/ in the module)
// also looks for a spring context file. If found, schedules spring to be restarted
JarFile jarFile = null;
OutputStream outStream = null;
InputStream inStream = null;
try {
File modFile = mod.getFile();
jarFile = new JarFile(modFile);
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String name = entry.getName();
log.debug("Entry name: " + name);
if (name.startsWith("web/module/")) {
// trim out the starting path of "web/module/"
String filepath = name.substring(11);
StringBuilder absPath = new StringBuilder(realPath + "/WEB-INF");
// If this is within the tag file directory, copy it into /WEB-INF/tags/module/moduleId/...
if (filepath.startsWith("tags/")) {
filepath = filepath.substring(5);
absPath.append("/tags/module/");
} else // Otherwise, copy it into /WEB-INF/view/module/moduleId/...
{
absPath.append("/view/module/");
}
// if a module id has a . in it, we should treat that as a /, i.e. files in the module
// ui.springmvc should go in folder names like .../ui/springmvc/...
absPath.append(mod.getModuleIdAsPath()).append("/").append(filepath);
if (log.isDebugEnabled()) {
log.debug("Moving file from: " + name + " to " + absPath);
}
// get the output file
File outFile = new File(absPath.toString().replace("/", File.separator));
if (entry.isDirectory()) {
if (!outFile.exists()) {
outFile.mkdirs();
}
} else {
// make the parent directories in case it doesn't exist
File parentDir = outFile.getParentFile();
if (!parentDir.exists()) {
parentDir.mkdirs();
}
// copy the contents over to the webapp for non directories
outStream = new FileOutputStream(outFile, false);
inStream = jarFile.getInputStream(entry);
OpenmrsUtil.copyFile(inStream, outStream);
}
} else if ("moduleApplicationContext.xml".equals(name) || "webModuleApplicationContext.xml".equals(name)) {
moduleNeedsContextRefresh = true;
} else if (name.equals(mod.getModuleId() + "Context.xml")) {
String msg = "DEPRECATED: '" + name + "' should be named 'moduleApplicationContext.xml' now. Please update/upgrade. ";
throw new ModuleException(msg, mod.getModuleId());
}
}
} catch (IOException io) {
log.warn("Unable to copy files from module " + mod.getModuleId() + " to the web layer", io);
} finally {
if (jarFile != null) {
try {
jarFile.close();
} catch (IOException io) {
log.warn("Couldn't close jar file: " + jarFile.getName(), io);
}
}
if (inStream != null) {
try {
inStream.close();
} catch (IOException io) {
log.warn("Couldn't close InputStream: " + io);
}
}
if (outStream != null) {
try {
outStream.close();
} catch (IOException io) {
log.warn("Couldn't close OutputStream: " + io);
}
}
}
// find and add the dwr code to the dwr-modules.xml file (if defined)
InputStream inputStream = null;
try {
Document config = mod.getConfig();
Element root = config.getDocumentElement();
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
Node node = root.getElementsByTagName("dwr").item(0);
Node current = node.getFirstChild();
while (current != null) {
if ("allow".equals(current.getNodeName()) || "signatures".equals(current.getNodeName()) || "init".equals(current.getNodeName())) {
((Element) current).setAttribute("moduleId", mod.getModuleId());
outputRoot.appendChild(dwrmodulexml.importNode(current, true));
}
current = current.getNextSibling();
}
moduleNeedsContextRefresh = true;
// 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);
}
}
}
// mark to delete the entire module web directory on exit
// this will usually only be used when an improper shutdown has occurred.
String folderPath = realPath + "/WEB-INF/view/module/" + mod.getModuleIdAsPath();
File outFile = new File(folderPath.replace("/", File.separator));
outFile.deleteOnExit();
// additional checks on module needing a context refresh
if (!moduleNeedsContextRefresh && mod.getAdvicePoints() != null && !mod.getAdvicePoints().isEmpty()) {
// AOP advice points are only loaded during the context refresh now.
// if the context hasn't been marked to be refreshed yet, mark it
// now if this module defines some advice
moduleNeedsContextRefresh = true;
}
// files into it (if we copied an xml file)
if (moduleNeedsContextRefresh && !delayContextRefresh) {
if (log.isDebugEnabled()) {
log.debug("Refreshing context for module" + mod);
}
try {
refreshWAC(servletContext, false, mod);
log.debug("Done Refreshing WAC");
} catch (Exception e) {
String msg = "Unable to refresh the WebApplicationContext";
mod.setStartupErrorMessage(msg, e);
if (log.isWarnEnabled()) {
log.warn(msg + " for module: " + mod.getModuleId(), e);
}
try {
stopModule(mod, servletContext, true);
// remove jar from classloader play
ModuleFactory.stopModule(mod, true, true);
} catch (Exception e2) {
// exception expected with most modules here
if (log.isWarnEnabled()) {
log.warn("Error while stopping a module that had an error on refreshWAC", e2);
}
}
// try starting the application context again
refreshWAC(servletContext, false, mod);
notifySuperUsersAboutModuleFailure(mod);
}
}
if (!delayContextRefresh && ModuleFactory.isModuleStarted(mod)) {
// only loading the servlets/filters if spring is refreshed because one
// might depend on files being available in spring
// if the caller wanted to delay the refresh then they are responsible for
// calling these two methods on the module
// find and cache the module's servlets
// (only if the module started successfully previously)
log.debug("Loading servlets and filters for module: " + mod);
loadServlets(mod, servletContext);
loadFilters(mod, servletContext);
}
// return true if the module needs a context refresh and we didn't do it here
return (moduleNeedsContextRefresh && delayContextRefresh);
}
// we aren't processing this module, so a context refresh is not necessary
return false;
}
Aggregations