List of usage examples for org.springframework.context.support MessageSourceAccessor getMessage
public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException
From source file:org.openmrs.module.web.controller.ModuleListController.java
/** * The onSubmit function receives the form/command object that was modified by the input form * and saves it to the db//from w ww . ja v a 2s.com * * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(HttpServletRequest, * HttpServletResponse, Object, * BindException) */ @Override protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { if (!Context.hasPrivilege(PrivilegeConstants.MANAGE_MODULES)) { throw new APIAuthenticationException("Privilege required: " + PrivilegeConstants.MANAGE_MODULES); } HttpSession httpSession = request.getSession(); String moduleId = ServletRequestUtils.getStringParameter(request, "moduleId", ""); String view = getFormView(); String success = ""; String error = ""; MessageSourceAccessor msa = getMessageSourceAccessor(); String action = ServletRequestUtils.getStringParameter(request, "action", ""); if (ServletRequestUtils.getStringParameter(request, "start.x", null) != null) { action = "start"; } else if (ServletRequestUtils.getStringParameter(request, "stop.x", null) != null) { action = "stop"; } else if (ServletRequestUtils.getStringParameter(request, "unload.x", null) != null) { action = "unload"; } // handle module upload if ("upload".equals(action)) { // double check upload permissions if (!ModuleUtil.allowAdmin()) { error = msa.getMessage("Module.disallowUploads", new String[] { ModuleConstants.RUNTIMEPROPERTY_ALLOW_ADMIN }); } else { InputStream inputStream = null; File moduleFile = null; Module module = null; Boolean updateModule = ServletRequestUtils.getBooleanParameter(request, "update", false); Boolean downloadModule = ServletRequestUtils.getBooleanParameter(request, "download", false); List<Module> dependentModulesStopped = null; try { if (downloadModule) { String downloadURL = request.getParameter("downloadURL"); if (downloadURL == null) { throw new MalformedURLException("Couldn't download module because no url was provided"); } String fileName = downloadURL.substring(downloadURL.lastIndexOf("/") + 1); final URL url = new URL(downloadURL); inputStream = ModuleUtil.getURLStream(url); moduleFile = ModuleUtil.insertModuleFile(inputStream, fileName); } else if (request instanceof MultipartHttpServletRequest) { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MultipartFile multipartModuleFile = multipartRequest.getFile("moduleFile"); if (multipartModuleFile != null && !multipartModuleFile.isEmpty()) { String filename = WebUtil.stripFilename(multipartModuleFile.getOriginalFilename()); // if user is using the "upload an update" form instead of the main form if (updateModule) { // parse the module so that we can get the id Module tmpModule = new ModuleFileParser(multipartModuleFile.getInputStream()) .parse(); Module existingModule = ModuleFactory.getModuleById(tmpModule.getModuleId()); if (existingModule != null) { dependentModulesStopped = ModuleFactory.stopModule(existingModule, false, true); // stop the module with these parameters so that mandatory modules can be upgraded for (Module depMod : dependentModulesStopped) { WebModuleUtil.stopModule(depMod, getServletContext()); } WebModuleUtil.stopModule(existingModule, getServletContext()); ModuleFactory.unloadModule(existingModule); } inputStream = new FileInputStream(tmpModule.getFile()); moduleFile = ModuleUtil.insertModuleFile(inputStream, filename); // copy the omod over to the repo folder } else { // not an update, or a download, just copy the module file right to the repo folder inputStream = multipartModuleFile.getInputStream(); moduleFile = ModuleUtil.insertModuleFile(inputStream, filename); } } } module = ModuleFactory.loadModule(moduleFile); } catch (ModuleException me) { log.warn("Unable to load and start module", me); error = me.getMessage(); } finally { // clean up the module repository folder try { if (inputStream != null) { inputStream.close(); } } catch (IOException io) { log.warn("Unable to close temporary input stream", io); } if (module == null && moduleFile != null) { moduleFile.delete(); } } // if we didn't have trouble loading the module, start it if (module != null) { ModuleFactory.startModule(module); WebModuleUtil.startModule(module, getServletContext(), false); if (module.isStarted()) { success = msa.getMessage("Module.loadedAndStarted", new String[] { module.getName() }); if (updateModule && dependentModulesStopped != null) { for (Module depMod : sortStartupOrder(dependentModulesStopped)) { ModuleFactory.startModule(depMod); WebModuleUtil.startModule(depMod, getServletContext(), false); } } } else { success = msa.getMessage("Module.loaded", new String[] { module.getName() }); } } } } else if ("".equals(moduleId)) { if (action.equals(msa.getMessage("Module.startAll"))) { boolean someModuleNeedsARefresh = false; Collection<Module> modules = ModuleFactory.getLoadedModules(); Collection<Module> modulesInOrder = ModuleFactory.getModulesInStartupOrder(modules); for (Module module : modulesInOrder) { if (ModuleFactory.isModuleStarted(module)) { continue; } ModuleFactory.startModule(module); boolean thisModuleCausesRefresh = WebModuleUtil.startModule(module, getServletContext(), true); someModuleNeedsARefresh = someModuleNeedsARefresh || thisModuleCausesRefresh; } if (someModuleNeedsARefresh) { WebModuleUtil.refreshWAC(getServletContext(), false, null); } } else { ModuleUtil.checkForModuleUpdates(); } } else if (action.equals(msa.getMessage("Module.installUpdate"))) { // download and install update if (!ModuleUtil.allowAdmin()) { error = msa.getMessage("Module.disallowAdministration", new String[] { ModuleConstants.RUNTIMEPROPERTY_ALLOW_ADMIN }); } Module mod = ModuleFactory.getModuleById(moduleId); if (mod.getDownloadURL() != null) { ModuleFactory.stopModule(mod, false, true); // stop the module with these parameters so that mandatory modules can be upgraded WebModuleUtil.stopModule(mod, getServletContext()); Module newModule = ModuleFactory.updateModule(mod); WebModuleUtil.startModule(newModule, getServletContext(), false); } } else { // moduleId is not empty if (!ModuleUtil.allowAdmin()) { error = msa.getMessage("Module.disallowAdministration", new String[] { ModuleConstants.RUNTIMEPROPERTY_ALLOW_ADMIN }); } else { log.debug("Module id: " + moduleId); Module mod = ModuleFactory.getModuleById(moduleId); // Argument to pass to the success/error message Object[] args = new Object[] { moduleId }; if (mod == null) { error = msa.getMessage("Module.invalid", args); } else { if ("stop".equals(action)) { mod.clearStartupError(); ModuleFactory.stopModule(mod); WebModuleUtil.stopModule(mod, getServletContext()); success = msa.getMessage("Module.stopped", args); } else if ("start".equals(action)) { ModuleFactory.startModule(mod); WebModuleUtil.startModule(mod, getServletContext(), false); if (mod.isStarted()) { success = msa.getMessage("Module.started", args); } else { error = msa.getMessage("Module.not.started", args); } } else if ("unload".equals(action)) { if (ModuleFactory.isModuleStarted(mod)) { ModuleFactory.stopModule(mod); // stop the module so that when the web stop is done properly WebModuleUtil.stopModule(mod, getServletContext()); } ModuleFactory.unloadModule(mod); success = msa.getMessage("Module.unloaded", args); } } } } view = getSuccessView(); if (!"".equals(success)) { httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, success); } if (!"".equals(error)) { httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, error); } return new ModelAndView(new RedirectView(view)); }