From 76fdbe2469160f3060dbfaf8f3e0fcee64b47230 Mon Sep 17 00:00:00 2001 From: Viliam Repan Date: Sat, 2 Sep 2017 13:04:27 +0200 Subject: [PATCH] locale.properties loading improved (first search for midpoint.home/localization/locale.properties, then classpath) (cherry picked from commit 7810335) --- .../web/security/MidPointApplication.java | 147 +++++++++--------- 1 file changed, 77 insertions(+), 70 deletions(-) diff --git a/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/security/MidPointApplication.java b/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/security/MidPointApplication.java index 4fb9076c5d3..2e1398591cb 100644 --- a/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/security/MidPointApplication.java +++ b/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/security/MidPointApplication.java @@ -67,11 +67,16 @@ import org.apache.wicket.spring.injection.annot.SpringComponentInjector; import org.apache.wicket.util.lang.Bytes; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.stereotype.Component; -import java.io.*; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Reader; import java.net.URI; import java.net.URL; import java.net.URLClassLoader; @@ -92,7 +97,7 @@ public class MidPointApplication extends AuthenticatedWebApplication { public static final List AVAILABLE_LOCALES; - private static final String LOCALIZATION_DESCRIPTOR = "/localization/locale.properties"; + private static final String LOCALIZATION_DESCRIPTOR = "localization/locale.properties"; private static final String PROP_NAME = ".name"; private static final String PROP_FLAG = ".flag"; @@ -105,65 +110,32 @@ public class MidPointApplication extends AuthenticatedWebApplication { } static { + String midpointHome = System.getProperty(WebApplicationConfiguration.MIDPOINT_HOME); + File file = new File(midpointHome, LOCALIZATION_DESCRIPTOR); + + Resource[] localeDescriptorResources = new Resource[]{ + new FileSystemResource(file), + new ClassPathResource(LOCALIZATION_DESCRIPTOR) + }; + List locales = new ArrayList<>(); - try { - ClassLoader classLoader = MidPointApplication.class.getClassLoader(); - Enumeration urls = classLoader.getResources(LOCALIZATION_DESCRIPTOR); - while (urls.hasMoreElements()) { - final URL url = urls.nextElement(); - LOGGER.debug("Found localization descriptor {}.", new Object[]{url.toString()}); - - Properties properties = new Properties(); - Reader reader = null; - try { - reader = new InputStreamReader(url.openStream(), "utf-8"); - properties.load(reader); - - Map> localeMap = new HashMap<>(); - Set keys = (Set) properties.keySet(); - for (String key : keys) { - String[] array = key.split("\\."); - if (array.length != 2) { - continue; - } - - String locale = array[0]; - Map map = localeMap.get(locale); - if (map == null) { - map = new HashMap<>(); - localeMap.put(locale, map); - } - - map.put(key, properties.getProperty(key)); - } - - for (String key : localeMap.keySet()) { - Map localeDefinition = localeMap.get(key); - if (!localeDefinition.containsKey(key + PROP_NAME) - || !localeDefinition.containsKey(key + PROP_FLAG)) { - continue; - } - - LocaleDescriptor descriptor = new LocaleDescriptor( - localeDefinition.get(key + PROP_NAME), - localeDefinition.get(key + PROP_FLAG), - localeDefinition.get(key + PROP_DEFAULT), - WebComponentUtil.getLocaleFromString(key) - ); - locales.add(descriptor); - } - } catch (Exception ex) { - LoggingUtils.logUnexpectedException(LOGGER, "Couldn't load localization", ex); - } finally { - IOUtils.closeQuietly(reader); - } + for (Resource resource : localeDescriptorResources) { + if (!resource.isReadable()) { + continue; } - Collections.sort(locales); - } catch (Exception ex) { - LoggingUtils.logUnexpectedException(LOGGER, "Couldn't load locales", ex); + try { + LOGGER.debug("Found localization descriptor {}.", new Object[]{resource.getURL()}); + locales = loadLocaleDescriptors(resource); + + break; + } catch (Exception ex) { + LoggingUtils.logUnexpectedException(LOGGER, "Couldn't load localization", ex); + } } + Collections.sort(locales); + AVAILABLE_LOCALES = Collections.unmodifiableList(locales); } @@ -246,8 +218,6 @@ public void init() { resourceSettings.setThrowExceptionOnMissingResource(false); getMarkupSettings().setStripWicketTags(true); -// getMarkupSettings().setDefaultBeforeDisabledLink(""); -// getMarkupSettings().setDefaultAfterDisabledLink(""); if (RuntimeConfigurationType.DEVELOPMENT.equals(getConfigurationType())) { getDebugSettings().setAjaxDebugModeEnabled(true); @@ -276,6 +246,55 @@ public void init() { new DescriptorLoader().loadData(this); } + private static List loadLocaleDescriptors(Resource resource) throws IOException { + List locales = new ArrayList<>(); + + Properties properties = new Properties(); + Reader reader = null; + try { + reader = new InputStreamReader(resource.getInputStream(), "utf-8"); + properties.load(reader); + + Map> localeMap = new HashMap<>(); + Set keys = (Set) properties.keySet(); + for (String key : keys) { + String[] array = key.split("\\."); + if (array.length != 2) { + continue; + } + + String locale = array[0]; + Map map = localeMap.get(locale); + if (map == null) { + map = new HashMap<>(); + localeMap.put(locale, map); + } + + map.put(key, properties.getProperty(key)); + } + + for (String key : localeMap.keySet()) { + Map localeDefinition = localeMap.get(key); + if (!localeDefinition.containsKey(key + PROP_NAME) + || !localeDefinition.containsKey(key + PROP_FLAG)) { + continue; + } + + LocaleDescriptor descriptor = new LocaleDescriptor( + localeDefinition.get(key + PROP_NAME), + localeDefinition.get(key + PROP_FLAG), + localeDefinition.get(key + PROP_DEFAULT), + WebComponentUtil.getLocaleFromString(key) + ); + locales.add(descriptor); + } + } finally { + IOUtils.closeQuietly(reader); + } + + return locales; + } + private URL buildMidpointHomeLocalizationFolderUrl() { String midpointHome = System.getProperty(WebApplicationConfiguration.MIDPOINT_HOME); @@ -437,18 +456,6 @@ public SystemConfigurationType getSystemConfigurationIfAvailable() { } } - private static class ResourceFileFilter implements FilenameFilter { - - @Override - public boolean accept(File parent, String name) { - if (name.endsWith("png") || name.endsWith("gif")) { - return true; - } - - return false; - } - } - public static MidPointApplication get() { return (MidPointApplication) WebApplication.get(); }