use of org.jaffa.util.FindFiles in project jaffa-framework by jaffa-projects.
the class LabelEditorComponent method performSyncToSource.
/**
* This will perform the following tasks.
* - Migrate all the displayed override values to the appropriate fragment file
* - Migrate all the displayed override values to the ApplicationResources.default file
* - Delete all displayed override entries from the ApplicationResources.override file
* - Migrate all changes to the ApplicationResources.properties file by invoking InitApp.generateApplicationResources()
* - Flush the struts properties cache by invoking the flushCache() method on the MessageResources, provided its an instance of 'org.jaffa.util.PropertyMessageResources'
* @param messageResources The MessageResources object, which will be flushed, if its an instance of 'org.jaffa.util.PropertyMessageResources'.
* @throws FrameworkException if any error occurs.
*/
protected void performSyncToSource(MessageResources messageResources) throws FrameworkException {
String applicationResourcesDefaultLocation = (String) Config.getProperty(Config.PROP_APPLICATION_RESOURCES_DEFAULT_LOCATION, null);
String applicationResourcesOverrideLocation = (String) Config.getProperty(Config.PROP_APPLICATION_RESOURCES_OVERRIDE_LOCATION, null);
try {
// load the ApplicationResources.default and ApplicationResources.override files
Properties applicationResourcesDefaultProperties = null;
Properties applicationResourcesOverrideProperties = null;
String localeKey = localeKey(LocaleContext.getLocale());
if (ApplicationResourceLoader.getApplicationResourcesManager().getApplicationResourcesLocaleRepository().query(localeKey) != null) {
applicationResourcesDefaultProperties = ApplicationResourceLoader.getInstance().getApplicationResourcesLocale(localeKey);
applicationResourcesOverrideProperties = ApplicationResourceLoader.getInstance().getApplicationResourcesOverride(localeKey);
} else {
applicationResourcesDefaultProperties = ApplicationResourceLoader.getInstance().getApplicationResourcesDefault();
applicationResourcesOverrideProperties = ApplicationResourceLoader.getInstance().getApplicationResourcesOverride(null);
}
// Migrate all the displayed override values to the appropriate fragment file
if (m_searchPathForSourceFragments != null && m_sourceFragmentName != null) {
FindFiles ff = new FindFiles(m_searchPathForSourceFragments, m_sourceFragmentName);
List files = ff.getList();
if (files != null) {
for (Iterator i = files.iterator(); i.hasNext(); ) {
File file = (File) i.next();
Properties properties = loadPropertiesFromFile(file.getPath(), true);
boolean modified = false;
for (Iterator itr = m_labels.keySet().iterator(); itr.hasNext(); ) {
String label = (String) itr.next();
if (properties.containsKey(label)) {
Map map = (Map) m_labels.get(label);
String override = (String) map.get(OVERRIDE);
if (override != null) {
// Migrate to the source file and ApplicationResources.default file
// Delete from the ApplicationResources.override file
properties.setProperty(label, override);
// applicationResourcesDefaultProperties.setProperty(label, override);
applicationResourcesOverrideProperties.remove(label);
if (log.isInfoEnabled())
log.info(file.getPath() + ": Migrated the override value: " + label + '=' + override);
modified = true;
}
}
}
// Update the fragment file, maintaining existing comments and order
if (modified)
storePropertiesToFile(properties, file.getPath());
}
}
}
// Save the ApplicationResources.default and ApplicationResources.override files
// storePropertiesToFile(applicationResourcesDefaultProperties, applicationResourcesDefaultLocation);
storePropertiesToFile(applicationResourcesOverrideProperties, applicationResourcesOverrideLocation);
// Flush the struts properties cache by invoking the flushCache() method on the MessageResources, provided its an instance of 'org.jaffa.util.PropertyMessageResources'
if (messageResources != null && messageResources instanceof PropertyMessageResources) {
((PropertyMessageResources) messageResources).flushCache();
if (log.isDebugEnabled())
log.debug("Flushed the struts message cache");
} else {
if (log.isDebugEnabled())
log.debug("The struts message cache has not been implemented using the org.jaffa.util.PropertyMessageResources class. Reload the webapp to see the changes to the labels.");
}
} catch (IOException e) {
String str = "Sync to Source failed";
log.error(str, e);
throw new LabelEditorException(LabelEditorException.SYNC_TO_SOURCE_FAILED, null, e);
}
}
Aggregations