/** * Convenience method to replace Properties.store(), which isn't UTF-8 compliant <br> * NOTE: In Java 6, you will be able to pass the load() and store() methods a UTF-8 * Reader/Writer object as an argument, making this method unnecessary. * * @param properties * @param file * @param comment */ public static void storeProperties(Properties properties, File file, String comment) { OutputStream outStream = null; try { outStream = new FileOutputStream(file, true); storeProperties(properties, outStream, comment); } catch (IOException ex) { log.error("Unable to create file " + file.getAbsolutePath() + " in storeProperties routine."); } finally { try { if (outStream != null) { outStream.close(); } } catch (IOException ioe) { // pass } } }
/** * 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); } }
@Test public void storeProperties_shouldEscapeSlashes() throws IOException { Charset utf8 = StandardCharsets.UTF_8; String expectedProperty = "blacklistRegex"; String expectedValue = "[^\\p{InBasicLatin}\\p{InLatin1Supplement}]"; Properties properties = new Properties(); properties.setProperty(expectedProperty, expectedValue); ByteArrayOutputStream actual = new ByteArrayOutputStream(); ByteArrayOutputStream expected = new ByteArrayOutputStream(); OpenmrsUtil.storeProperties(properties, actual, null); // Java's underlying implementation correctly writes: // blacklistRegex=[^\\p{InBasicLatin}\\p{InLatin1Supplement}] // This method didn't exist in Java 5, which is why we wrote a utility method in the first place, so we should // just get rid of our own implementation, and use the underlying java one. properties.store(new OutputStreamWriter(expected, utf8), null); assertThat(actual.toByteArray(), is(expected.toByteArray())); }
try { fos = new FileOutputStream(getRuntimePropertiesFile()); OpenmrsUtil.storeProperties(runtimeProperties, fos, "Auto generated by OpenMRS initialization wizard"); wizardModel.workLog.add("Saved runtime properties file " + getRuntimePropertiesFile());