Alexander Wels has uploaded a new change for review. Change subject: userportal,webadmin: update branding manager ......................................................................
userportal,webadmin: update branding manager - Moved the BrandingManager and BrandingTheme to the utils module so other components can also use it. - Made the BrandingManager a singleton and added static methods to get a particular message from the resource bundle. Change-Id: I87f5a6684701c9d5f603bf79459a3a5b6614b24f Signed-off-by: Alexander Wels <aw...@redhat.com> --- R backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/branding/BrandingManager.java R backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/branding/BrandingTheme.java R backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/branding/BrandingManagerTest.java R backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/branding/BrandingThemeTest.java R backend/manager/modules/utils/src/test/resources/org/ovirt/engine/core/utils/branding/01-test.brand/branding.properties R backend/manager/modules/utils/src/test/resources/org/ovirt/engine/core/utils/branding/01-test.brand/messages.properties R backend/manager/modules/utils/src/test/resources/org/ovirt/engine/core/utils/branding/01-test.brand/messages_fr.properties M frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/BrandingServlet.java M frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/GwtDynamicHostPageServlet.java M frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/UserPortalHostPageServlet.java M frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/WebAdminHostPageServlet.java M frontend/webadmin/modules/frontend/src/test/java/org/ovirt/engine/ui/frontend/server/gwt/AbstractGwtDynamicHostPageServletTest.java M frontend/webadmin/modules/frontend/src/test/java/org/ovirt/engine/ui/frontend/server/gwt/BrandingServletTest.java M frontend/webadmin/modules/frontend/src/test/java/org/ovirt/engine/ui/frontend/server/gwt/UserPortalHostPageServletTest.java 14 files changed, 113 insertions(+), 27 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/79/15579/1 diff --git a/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/branding/BrandingManager.java b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/branding/BrandingManager.java similarity index 69% rename from frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/branding/BrandingManager.java rename to backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/branding/BrandingManager.java index db098b5..e366da7 100644 --- a/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/branding/BrandingManager.java +++ b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/branding/BrandingManager.java @@ -1,4 +1,4 @@ -package org.ovirt.engine.ui.frontend.server.gwt.branding; +package org.ovirt.engine.core.utils.branding; import java.io.File; import java.io.FileFilter; @@ -15,6 +15,7 @@ import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.node.ObjectNode; import org.ovirt.engine.core.utils.EngineLocalConfig; +import org.ovirt.engine.core.utils.servlet.LocaleFilter; /** * This class manages the available branding themes and changeable localized messages. @@ -59,10 +60,12 @@ */ private final File brandingRootPath; + private static BrandingManager instance; + /** * Default constructor. */ - public BrandingManager() { + private BrandingManager() { this(EngineLocalConfig.getInstance().getEtcDir()); } @@ -70,8 +73,22 @@ * Constructor that takes a {@code File} object to configure the brandingRootPath. * @param etcDir A {@code File} pointing to the branding root path. */ - public BrandingManager(final File etcDir) { + private BrandingManager(final File etcDir) { brandingRootPath = new File(etcDir, BRANDING_PATH); + } + + public static BrandingManager getInstance() { + if (instance == null) { + instance = new BrandingManager(); + } + return instance; + } + + public static BrandingManager getInstance(final File etcDir) { + if (instance == null) { + instance = new BrandingManager(etcDir); + } + return instance; } /** @@ -102,13 +119,44 @@ } /** - * get a JavaScript associative array string representation of the available messages. Only 'common' messages and - * messages that have keys that start with the passed in prefix will be returned. + * Get the message associated with the passed in key. + * @param key The key to get the message for. For instance obrand.common.copy_right_notice. + * @return The associated message in the default locale. + */ + public static String getMessage(final String key) { + return getMessage(key, LocaleFilter.DEFAULT_LOCALE); + } + + /** + * Get the message associated with the passed in key. + * @param key The key to get the message for. For instance obrand.common.copy_right_notice. + * @param locale The locale to use to look up the message. + * @return The associated message in the passed in locale. + */ + public static String getMessage(final String key, final Locale locale) { + String result = null; + // key needs to start with obrand. + if (key != null && key.startsWith(BRAND_PREFIX + ".")) { + String[] splitString = key.split("\\."); + String prefix = null; + if (splitString.length >= 2) { + prefix = key.split("\\.")[1]; + } + if (prefix != null && prefix.length() > 0) { + result = getInstance().getMessageMap(prefix, locale). + get(key.substring(key.indexOf(prefix) + prefix.length() + 1)); + } + } + return result; + } + + /** + * Returns a Map of String keys and values. * @param prefix The prefix to use for getting the keys. * @param locale The locale to get the messages for. - * @return A string of format {'key':'value',...} + * @return A {@code Map} of keys and values. */ - public final String getMessages(final String prefix, final Locale locale) { + private Map<String, String> getMessageMap(final String prefix, final Locale locale) { List<BrandingTheme> messageThemes = getBrandingThemes(); // We need this map to remove potential duplicate strings from the resource bundles. Map<String, String> keyValues = new HashMap<String, String>(); @@ -126,11 +174,22 @@ } } } + return keyValues; + } + + /** + * get a JavaScript associative array string representation of the available messages. Only 'common' messages and + * messages that have keys that start with the passed in prefix will be returned. + * @param prefix The prefix to use for getting the keys. + * @param locale The locale to get the messages for. + * @return A string of format {'key':'value',...} + */ + public String getMessages(final String prefix, final Locale locale) { + Map<String, String> keyValues = getMessageMap(prefix, locale); // Turn the map into a string with the format: // {"key":"value",...} return getMessagesFromMap(keyValues); } - /** * @param keyValues The map to turn into the string. * @return A string of format {"key":"value",...} diff --git a/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/branding/BrandingTheme.java b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/branding/BrandingTheme.java similarity index 98% rename from frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/branding/BrandingTheme.java rename to backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/branding/BrandingTheme.java index 051760a..1095b6f 100644 --- a/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/branding/BrandingTheme.java +++ b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/branding/BrandingTheme.java @@ -1,4 +1,4 @@ -package org.ovirt.engine.ui.frontend.server.gwt.branding; +package org.ovirt.engine.core.utils.branding; import java.io.File; import java.io.FileInputStream; diff --git a/frontend/webadmin/modules/frontend/src/test/java/org/ovirt/engine/ui/frontend/server/gwt/branding/BrandingManagerTest.java b/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/branding/BrandingManagerTest.java similarity index 71% rename from frontend/webadmin/modules/frontend/src/test/java/org/ovirt/engine/ui/frontend/server/gwt/branding/BrandingManagerTest.java rename to backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/branding/BrandingManagerTest.java index a9b4c6e..e328453 100644 --- a/frontend/webadmin/modules/frontend/src/test/java/org/ovirt/engine/ui/frontend/server/gwt/branding/BrandingManagerTest.java +++ b/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/branding/BrandingManagerTest.java @@ -1,4 +1,4 @@ -package org.ovirt.engine.ui.frontend.server.gwt.branding; +package org.ovirt.engine.core.utils.branding; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -26,9 +26,9 @@ @Before public void setUp() throws Exception { File etcDir = new File(this.getClass().getClassLoader(). - getResource("./org/ovirt/engine/ui/frontend/server/gwt") //$NON-NLS-1$ + getResource("./org/ovirt/engine/core/utils") //$NON-NLS-1$ .getFile()); - testManager = new BrandingManager(etcDir); + testManager = BrandingManager.getInstance(etcDir); } @Test @@ -70,9 +70,36 @@ @Test public void testGetBrandingRootPath() { String rootPath = this.getClass().getClassLoader(). - getResource("./org/ovirt/engine/ui/frontend/server/gwt") //$NON-NLS-1$ + getResource("./org/ovirt/engine/core/utils/") //$NON-NLS-1$ .getFile() + "/branding"; //$NON-NLS-1$ assertEquals("Root paths don't match", new File(rootPath), testManager.getBrandingRootPath()); //$NON-NLS-1$ } + @Test + public void testGetMessageDefaultLocale() { + String testKey = "obrand.common.main_header_label"; + String result = BrandingManager.getMessage(testKey); + assertEquals("The result should be 'Main header'", "Main header", result); + } + + @Test + public void testGetMessageBadKey() { + String testKey = "obrandcommonmain_header_label"; + String result = BrandingManager.getMessage(testKey); + assertNull("The result should be null", result); + } + + @Test + public void testGetMessageNullKey() { + String testKey = null; + String result = BrandingManager.getMessage(testKey); + assertNull("The result should be null", result); + } + + @Test + public void testGetMessageFrenchLocale() { + String testKey = "obrand.common.main_header_label"; + String result = BrandingManager.getMessage(testKey, Locale.FRENCH); + assertEquals("The result should be 'Main header'", "Main header", result); + } } diff --git a/frontend/webadmin/modules/frontend/src/test/java/org/ovirt/engine/ui/frontend/server/gwt/branding/BrandingThemeTest.java b/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/branding/BrandingThemeTest.java similarity index 93% rename from frontend/webadmin/modules/frontend/src/test/java/org/ovirt/engine/ui/frontend/server/gwt/branding/BrandingThemeTest.java rename to backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/branding/BrandingThemeTest.java index 2752519..2f47214 100644 --- a/frontend/webadmin/modules/frontend/src/test/java/org/ovirt/engine/ui/frontend/server/gwt/branding/BrandingThemeTest.java +++ b/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/branding/BrandingThemeTest.java @@ -1,4 +1,4 @@ -package org.ovirt.engine.ui.frontend.server.gwt.branding; +package org.ovirt.engine.core.utils.branding; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -23,7 +23,7 @@ @Before public void setUp() throws Exception { File testThemeRootPath = new File(this.getClass().getClassLoader(). - getResource("./org/ovirt/engine/ui/frontend/server/gwt/branding") //$NON-NLS-1$ + getResource("./org/ovirt/engine/core/utils/branding") //$NON-NLS-1$ .getFile()); File testThemePath = new File(testThemeRootPath.getAbsoluteFile(), "01-test.brand"); //$NON-NLS-1$ testTheme = new BrandingTheme(testThemePath.getAbsolutePath(), diff --git a/frontend/webadmin/modules/frontend/src/test/resources/org/ovirt/engine/ui/frontend/server/gwt/branding/01-test.brand/branding.properties b/backend/manager/modules/utils/src/test/resources/org/ovirt/engine/core/utils/branding/01-test.brand/branding.properties similarity index 100% rename from frontend/webadmin/modules/frontend/src/test/resources/org/ovirt/engine/ui/frontend/server/gwt/branding/01-test.brand/branding.properties rename to backend/manager/modules/utils/src/test/resources/org/ovirt/engine/core/utils/branding/01-test.brand/branding.properties diff --git a/frontend/webadmin/modules/frontend/src/test/resources/org/ovirt/engine/ui/frontend/server/gwt/branding/01-test.brand/messages.properties b/backend/manager/modules/utils/src/test/resources/org/ovirt/engine/core/utils/branding/01-test.brand/messages.properties similarity index 100% rename from frontend/webadmin/modules/frontend/src/test/resources/org/ovirt/engine/ui/frontend/server/gwt/branding/01-test.brand/messages.properties rename to backend/manager/modules/utils/src/test/resources/org/ovirt/engine/core/utils/branding/01-test.brand/messages.properties diff --git a/frontend/webadmin/modules/frontend/src/test/resources/org/ovirt/engine/ui/frontend/server/gwt/branding/01-test.brand/messages_fr.properties b/backend/manager/modules/utils/src/test/resources/org/ovirt/engine/core/utils/branding/01-test.brand/messages_fr.properties similarity index 100% rename from frontend/webadmin/modules/frontend/src/test/resources/org/ovirt/engine/ui/frontend/server/gwt/branding/01-test.brand/messages_fr.properties rename to backend/manager/modules/utils/src/test/resources/org/ovirt/engine/core/utils/branding/01-test.brand/messages_fr.properties diff --git a/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/BrandingServlet.java b/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/BrandingServlet.java index 0996c53..0d68f73 100644 --- a/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/BrandingServlet.java +++ b/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/BrandingServlet.java @@ -9,8 +9,8 @@ import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; +import org.ovirt.engine.core.utils.branding.BrandingManager; import org.ovirt.engine.core.utils.servlet.ServletUtils; -import org.ovirt.engine.ui.frontend.server.gwt.branding.BrandingManager; /** * This class serves files from the branding themes to the browser. This @@ -42,7 +42,7 @@ @Override public void init() { - setBrandingManager(new BrandingManager()); + setBrandingManager(BrandingManager.getInstance()); } @Override diff --git a/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/GwtDynamicHostPageServlet.java b/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/GwtDynamicHostPageServlet.java index 7a4e7eb..9c6b0b0 100644 --- a/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/GwtDynamicHostPageServlet.java +++ b/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/GwtDynamicHostPageServlet.java @@ -23,10 +23,10 @@ import org.ovirt.engine.core.common.queries.VdcQueryReturnValue; import org.ovirt.engine.core.common.queries.VdcQueryType; import org.ovirt.engine.core.common.users.VdcUser; +import org.ovirt.engine.core.utils.branding.BrandingManager; +import org.ovirt.engine.core.utils.branding.BrandingTheme; +import org.ovirt.engine.core.utils.branding.BrandingTheme.ApplicationType; import org.ovirt.engine.core.utils.servlet.LocaleFilter; -import org.ovirt.engine.ui.frontend.server.gwt.branding.BrandingManager; -import org.ovirt.engine.ui.frontend.server.gwt.branding.BrandingTheme; -import org.ovirt.engine.ui.frontend.server.gwt.branding.BrandingTheme.ApplicationType; /** * Renders the HTML host page of a GWT application. @@ -96,7 +96,7 @@ @Override public void init() { this.mapper = new ObjectMapper(); - setBrandingManager(new BrandingManager()); + setBrandingManager(BrandingManager.getInstance()); } @Override diff --git a/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/UserPortalHostPageServlet.java b/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/UserPortalHostPageServlet.java index 56d9973..0db9534 100644 --- a/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/UserPortalHostPageServlet.java +++ b/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/UserPortalHostPageServlet.java @@ -1,6 +1,6 @@ package org.ovirt.engine.ui.frontend.server.gwt; -import org.ovirt.engine.ui.frontend.server.gwt.branding.BrandingTheme.ApplicationType; +import org.ovirt.engine.core.utils.branding.BrandingTheme.ApplicationType; /** * UserPortal GWT application host page servlet. diff --git a/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/WebAdminHostPageServlet.java b/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/WebAdminHostPageServlet.java index ed5d512..3927b71 100644 --- a/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/WebAdminHostPageServlet.java +++ b/frontend/webadmin/modules/frontend/src/main/java/org/ovirt/engine/ui/frontend/server/gwt/WebAdminHostPageServlet.java @@ -17,7 +17,7 @@ import org.ovirt.engine.core.common.queries.ConfigurationValues; import org.ovirt.engine.core.common.queries.GetConfigurationValueParameters; import org.ovirt.engine.core.common.queries.VdcQueryType; -import org.ovirt.engine.ui.frontend.server.gwt.branding.BrandingTheme.ApplicationType; +import org.ovirt.engine.core.utils.branding.BrandingTheme.ApplicationType; import org.ovirt.engine.ui.frontend.server.gwt.plugin.PluginData; import org.ovirt.engine.ui.frontend.server.gwt.plugin.PluginDataManager; diff --git a/frontend/webadmin/modules/frontend/src/test/java/org/ovirt/engine/ui/frontend/server/gwt/AbstractGwtDynamicHostPageServletTest.java b/frontend/webadmin/modules/frontend/src/test/java/org/ovirt/engine/ui/frontend/server/gwt/AbstractGwtDynamicHostPageServletTest.java index fcc8178..eda1927 100644 --- a/frontend/webadmin/modules/frontend/src/test/java/org/ovirt/engine/ui/frontend/server/gwt/AbstractGwtDynamicHostPageServletTest.java +++ b/frontend/webadmin/modules/frontend/src/test/java/org/ovirt/engine/ui/frontend/server/gwt/AbstractGwtDynamicHostPageServletTest.java @@ -38,8 +38,8 @@ import org.ovirt.engine.core.common.queries.VdcQueryType; import org.ovirt.engine.core.common.users.VdcUser; import org.ovirt.engine.core.compat.Guid; -import org.ovirt.engine.ui.frontend.server.gwt.branding.BrandingManager; -import org.ovirt.engine.ui.frontend.server.gwt.branding.BrandingTheme; +import org.ovirt.engine.core.utils.branding.BrandingManager; +import org.ovirt.engine.core.utils.branding.BrandingTheme; public abstract class AbstractGwtDynamicHostPageServletTest<T extends GwtDynamicHostPageServlet> { diff --git a/frontend/webadmin/modules/frontend/src/test/java/org/ovirt/engine/ui/frontend/server/gwt/BrandingServletTest.java b/frontend/webadmin/modules/frontend/src/test/java/org/ovirt/engine/ui/frontend/server/gwt/BrandingServletTest.java index e6dd177..40d05b0 100644 --- a/frontend/webadmin/modules/frontend/src/test/java/org/ovirt/engine/ui/frontend/server/gwt/BrandingServletTest.java +++ b/frontend/webadmin/modules/frontend/src/test/java/org/ovirt/engine/ui/frontend/server/gwt/BrandingServletTest.java @@ -21,7 +21,7 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; -import org.ovirt.engine.ui.frontend.server.gwt.branding.BrandingManager; +import org.ovirt.engine.core.utils.branding.BrandingManager; @RunWith(MockitoJUnitRunner.class) public class BrandingServletTest { diff --git a/frontend/webadmin/modules/frontend/src/test/java/org/ovirt/engine/ui/frontend/server/gwt/UserPortalHostPageServletTest.java b/frontend/webadmin/modules/frontend/src/test/java/org/ovirt/engine/ui/frontend/server/gwt/UserPortalHostPageServletTest.java index 459e3db..5ad713e 100644 --- a/frontend/webadmin/modules/frontend/src/test/java/org/ovirt/engine/ui/frontend/server/gwt/UserPortalHostPageServletTest.java +++ b/frontend/webadmin/modules/frontend/src/test/java/org/ovirt/engine/ui/frontend/server/gwt/UserPortalHostPageServletTest.java @@ -7,7 +7,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.runners.MockitoJUnitRunner; -import org.ovirt.engine.ui.frontend.server.gwt.branding.BrandingTheme; +import org.ovirt.engine.core.utils.branding.BrandingTheme; @RunWith(MockitoJUnitRunner.class) public class UserPortalHostPageServletTest extends AbstractGwtDynamicHostPageServletTest<UserPortalHostPageServlet> { -- To view, visit http://gerrit.ovirt.org/15579 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I87f5a6684701c9d5f603bf79459a3a5b6614b24f Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Alexander Wels <aw...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches