@SuppressWarnings("deprecation") public String getTestDatasetFilename(String testDatasetName) throws Exception { InputStream propertiesFileStream = null; // try to load the file if its a straight up path to the file or // if its a classpath path to the file if (new File(TEST_DATASETS_PROPERTIES_FILE).exists()) { propertiesFileStream = new FileInputStream(TEST_DATASETS_PROPERTIES_FILE); } else { propertiesFileStream = getClass().getClassLoader().getResourceAsStream(TEST_DATASETS_PROPERTIES_FILE); if (propertiesFileStream == null) throw new FileNotFoundException( "Unable to find '" + TEST_DATASETS_PROPERTIES_FILE + "' in the classpath"); } Properties props = new Properties(); OpenmrsUtil.loadProperties(props, propertiesFileStream); if (props.getProperty(testDatasetName) == null) { throw new Exception( "Test dataset named " + testDatasetName + " not found in properties file"); } return props.getProperty(testDatasetName); }
// public SessionFactory newSessionFactory(Configuration config) throws HibernateException { public Configuration newConfiguration() throws HibernateException { Configuration config = super.newConfiguration(); log.debug("Configuring hibernate sessionFactory properties"); Properties moduleProperties = Context.getConfigProperties(); // override or initialize config properties with module-provided ones for (Object key : moduleProperties.keySet()) { String prop = (String) key; String value = (String) moduleProperties.get(key); log.trace("Setting module property: " + prop + ":" + value); config.setProperty(prop, value); if (!prop.startsWith("hibernate")) config.setProperty("hibernate." + prop, value); } Properties properties = Context.getRuntimeProperties(); // loop over runtime properties and override each in the configuration for (Object key : properties.keySet()) { String prop = (String) key; String value = (String) properties.get(key); log.trace("Setting property: " + prop + ":" + value); config.setProperty(prop, value); if (!prop.startsWith("hibernate")) config.setProperty("hibernate." + prop, value); } // load in the default hibernate properties try { InputStream propertyStream = ConfigHelper.getResourceAsStream("/hibernate.default.properties"); Properties props = new Properties(); OpenmrsUtil.loadProperties(props, propertyStream); propertyStream.close(); // Only load in the default properties if they don't exist config.mergeProperties(props); } catch (IOException e) { log.fatal("Unable to load default hibernate properties", e); } log.debug( "Setting global Hibernate Session Interceptor for SessionFactory, Interceptor: " + chainingInterceptor); // make sure all autowired interceptors are put onto our chaining interceptor // sort on the keys so that the devs/modules have some sort of control over the order of the // interceptors List<String> keys = new ArrayList<String>(interceptors.keySet()); Collections.sort(keys); for (String key : keys) { chainingInterceptor.addInterceptor(interceptors.get(key)); } config.setInterceptor(chainingInterceptor); return config; }
/** * @see * org.openmrs.messagesource.MutableMessageSource#removePresentation(org.openmrs.messagesource.PresentationMessage) */ public void removePresentation(PresentationMessage message) { File propertyFile = findPropertiesFileFor(message.getCode()); if (propertyFile != null) { Properties props = new Properties(); try { OpenmrsUtil.loadProperties(props, propertyFile); props.remove(message.getCode()); OpenmrsUtil.storeProperties(props, propertyFile, PROPERTIES_FILE_COMMENT); } catch (Exception e) { log.error("Error generated", e); } } }
/** * @see * org.openmrs.messagesource.MutableMessageSource#addPresentation(org.openmrs.messagesource.PresentationMessage) */ public void addPresentation(PresentationMessage message) { File propertyFile = findPropertiesFileFor(message.getCode()); if (propertyFile != null) { Properties props = new Properties(); try { OpenmrsUtil.loadProperties(props, propertyFile); props.setProperty(message.getCode(), message.getMessage()); OpenmrsUtil.storeProperties(props, propertyFile, "OpenMRS Application Messages"); } catch (Exception e) { log.error("Error generated", e); } } }
/** * Mimics org.openmrs.web.Listener.getRuntimeProperties() * * @param webappName name to use when looking up the runtime properties env var or filename * @return Properties runtime */ public static Properties getRuntimeProperties(String webappName) { Properties props = new Properties(); try { FileInputStream propertyStream = null; // Look for environment variable // {WEBAPP.NAME}_RUNTIME_PROPERTIES_FILE String env = webappName.toUpperCase() + "_RUNTIME_PROPERTIES_FILE"; String filepath = System.getenv(env); if (filepath != null) { try { propertyStream = new FileInputStream(filepath); } catch (IOException e) { } } // env is the name of the file to look for in the directories String filename = webappName + "-runtime.properties"; if (propertyStream == null) { filepath = OpenmrsUtil.getApplicationDataDirectory() + filename; try { propertyStream = new FileInputStream(filepath); } catch (IOException e) { } } // look in current directory last if (propertyStream == null) { filepath = filename; try { propertyStream = new FileInputStream(filepath); } catch (IOException e) { } } if (propertyStream == null) throw new IOException("Could not open '" + filename + "' in user or local directory."); OpenmrsUtil.loadProperties(props, propertyStream); propertyStream.close(); } catch (IOException e) { } return props; }
/** * Convenience method to scan the available properties files, looking for the one that has a * definition for the given code. * * @param code * @return the file which defines the code, or null if not found */ private File findPropertiesFileFor(String code) { Properties props = new Properties(); File foundFile = null; for (File propertiesFile : findPropertiesFiles()) { props.clear(); try { OpenmrsUtil.loadProperties(props, propertiesFile); } catch (Exception e) { log.error("Error generated", e); } if (props.containsKey(code)) { foundFile = propertiesFile; break; } } return foundFile; }
/** * Returns all available messages. * * @see org.openmrs.messagesource.MessageSourceService#getPresentations() */ public Collection<PresentationMessage> getPresentations() { Collection<PresentationMessage> presentations = new Vector<PresentationMessage>(); for (File propertiesFile : findPropertiesFiles()) { Locale currentLocale = parseLocaleFrom(propertiesFile.getName()); Properties props = new Properties(); try { OpenmrsUtil.loadProperties(props, propertiesFile); for (Map.Entry<Object, Object> property : props.entrySet()) { presentations.add( new PresentationMessage( property.getKey().toString(), currentLocale, property.getValue().toString(), "")); } } catch (Exception e) { // skip over errors in loading a single file log.error("Unable to load properties from file: " + propertiesFile.getAbsolutePath(), e); } } return presentations; }
/** @see org.openmrs.messagesource.MutableMessageSource#merge(MutableMessageSource, boolean) */ public void merge(MutableMessageSource fromSource, boolean overwrite) { // collect all existing properties Collection<File> propertiesFiles = findPropertiesFiles(); Map<Locale, List<File>> localeToFilesMap = new HashMap<Locale, List<File>>(); Map<File, Properties> fileToPropertiesMap = new HashMap<File, Properties>(); for (File propertiesFile : propertiesFiles) { Properties props = new Properties(); Locale propsLocale = parseLocaleFrom(propertiesFile.getName()); List<File> propList = localeToFilesMap.get(propsLocale); if (propList == null) { propList = new ArrayList<File>(); localeToFilesMap.put(propsLocale, propList); } propList.add(propertiesFile); try { OpenmrsUtil.loadProperties(props, propertiesFile); fileToPropertiesMap.put(propertiesFile, props); } catch (Exception e) { // skip over errors in loading a single file log.error("Unable to load properties from file: " + propertiesFile.getAbsolutePath(), e); } } // merge in the new properties for (PresentationMessage message : fromSource.getPresentations()) { Locale messageLocale = message.getLocale(); List<File> filelist = localeToFilesMap.get(messageLocale); if (filelist != null) { Properties propertyDestination = null; boolean propExists = false; for (File propertiesFile : filelist) { Properties possibleDestination = fileToPropertiesMap.get(propertiesFile); if (possibleDestination.containsKey(message.getCode())) { propertyDestination = possibleDestination; propExists = true; break; } else if (propertyDestination == null) { propertyDestination = possibleDestination; } } if ((propExists && overwrite) || !propExists) { propertyDestination.put(message.getCode(), message.getMessage()); } } else { // no properties files for this locale, create one File newPropertiesFile = new File(basenames[0] + "_" + messageLocale.toString() + ".properties"); Properties newProperties = new Properties(); fileToPropertiesMap.put(newPropertiesFile, newProperties); newProperties.put(message.getCode(), message.getMessage()); List<File> newFilelist = new ArrayList<File>(); newFilelist.add(newPropertiesFile); localeToFilesMap.put(messageLocale, newFilelist); } message.getCode(); } }