Author: nbelaevski
Date: 2010-08-30 08:32:48 -0400 (Mon, 30 Aug 2010)
New Revision: 19010
Added:
branches/RFPL-434/core/commons/src/test/java/org/richfaces/l10n/
branches/RFPL-434/core/commons/src/test/java/org/richfaces/l10n/BundleLoaderAppMessages.java
branches/RFPL-434/core/commons/src/test/java/org/richfaces/l10n/BundleLoaderCoreMessages.java
branches/RFPL-434/core/commons/src/test/java/org/richfaces/l10n/BundleLoaderTest.java
branches/RFPL-434/core/commons/src/test/resources/
branches/RFPL-434/core/commons/src/test/resources/org/
branches/RFPL-434/core/commons/src/test/resources/org/richfaces/
branches/RFPL-434/core/commons/src/test/resources/org/richfaces/l10n/
branches/RFPL-434/core/commons/src/test/resources/org/richfaces/l10n/AppMessages_by_BY.properties
branches/RFPL-434/core/commons/src/test/resources/org/richfaces/l10n/AppMessages_en_US.properties
branches/RFPL-434/core/commons/src/test/resources/org/richfaces/l10n/BundleLoaderMessages_en_US.properties
branches/RFPL-434/core/commons/src/test/resources/org/richfaces/l10n/BundleLoaderMessages_ru_RU.properties
Modified:
branches/RFPL-434/core/commons/src/main/java/org/richfaces/l10n/BundleLoader.java
branches/RFPL-434/core/commons/src/main/java/org/richfaces/l10n/MessageBundle.java
branches/RFPL-434/core/commons/src/main/java/org/richfaces/l10n/MessageFactory.java
branches/RFPL-434/core/commons/src/main/java/org/richfaces/l10n/MessageInterpolator.java
Log:
RFPL-434
Modified:
branches/RFPL-434/core/commons/src/main/java/org/richfaces/l10n/BundleLoader.java
===================================================================
---
branches/RFPL-434/core/commons/src/main/java/org/richfaces/l10n/BundleLoader.java 2010-08-30
07:58:57 UTC (rev 19009)
+++
branches/RFPL-434/core/commons/src/main/java/org/richfaces/l10n/BundleLoader.java 2010-08-30
12:32:48 UTC (rev 19010)
@@ -25,91 +25,16 @@
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
import javax.faces.application.Application;
-import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
-import com.google.common.base.Function;
-import com.google.common.collect.MapMaker;
-
/**
* @author Nick Belaevski
*
*/
public class BundleLoader {
- private static final class BundleKey {
-
- private Locale locale;
-
- private String baseName;
-
- public BundleKey(String baseName, Locale locale) {
- super();
- this.baseName = baseName;
- this.locale = locale;
- }
-
- public String getBaseName() {
- return baseName;
- }
-
- public Locale getLocale() {
- return locale;
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((baseName == null) ? 0 : baseName.hashCode());
- result = prime * result + ((locale == null) ? 0 : locale.hashCode());
- return result;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- BundleKey other = (BundleKey) obj;
- if (baseName == null) {
- if (other.baseName != null) {
- return false;
- }
- } else if (!baseName.equals(other.baseName)) {
- return false;
- }
- if (locale == null) {
- if (other.locale != null) {
- return false;
- }
- } else if (!locale.equals(other.locale)) {
- return false;
- }
- return true;
- }
-
- }
-
- private static ConcurrentMap<ClassLoader, ConcurrentMap<BundleKey,
ResourceBundle>> bundlesCache = new MapMaker()
- .weakKeys().makeComputingMap(new Function<ClassLoader,
ConcurrentMap<BundleKey, ResourceBundle>>() {
-
- public ConcurrentMap<BundleKey, ResourceBundle> apply(ClassLoader from)
{
- return new ConcurrentHashMap<BundleKey, ResourceBundle>();
- };
-
- });
-
private ClassLoader getClassLoader() {
return Thread.currentThread().getContextClassLoader();
}
@@ -126,39 +51,14 @@
return bundleAnnotation;
}
- private Locale detectLocale(FacesContext context) {
- UIViewRoot viewRoot = context.getViewRoot();
- if (viewRoot != null && viewRoot.getLocale() != null) {
- return viewRoot.getLocale();
- }
-
- return null;
- }
-
- protected ResourceBundle getOrCreateResourceBundle(BundleKey bundleKey) throws
MissingResourceException {
- ClassLoader loader = getClassLoader();
- ConcurrentMap<BundleKey, ResourceBundle> bundles =
bundlesCache.get(loader);
- ResourceBundle bundle = bundles.get(bundleKey);
-
- if (bundle == null) {
- bundle = ResourceBundle.getBundle(bundleKey.getBaseName(),
bundleKey.getLocale(), loader);
- bundles.put(bundleKey, bundle);
- }
-
- return bundle;
- }
-
public ResourceBundle getBundle(Enum<?> messageKey, Locale locale) throws
MissingResourceException,
IllegalArgumentException {
MessageBundle bundleAnnotation = asMessageBundle(messageKey);
- BundleKey bundleKey = new BundleKey(bundleAnnotation.baseName(), locale);
-
- ResourceBundle bundle = getOrCreateResourceBundle(bundleKey);
-
- return bundle;
+
+ return ResourceBundle.getBundle(bundleAnnotation.baseName(), locale,
getClassLoader());
}
- public ResourceBundle getApplicationBundle(FacesContext facesContext, Enum<?>
messageKey, boolean useDefaultLocale)
+ public ResourceBundle getApplicationBundle(FacesContext facesContext, Enum<?>
messageKey, Locale locale)
throws MissingResourceException, IllegalArgumentException {
MessageBundle messageBundle = asMessageBundle(messageKey);
@@ -179,19 +79,6 @@
getClass().getName(), messageKey.toString());
}
- Locale locale;
- if (!useDefaultLocale) {
- locale = detectLocale(facesContext);
- } else {
- locale = Locale.getDefault();
- }
-
- if (locale == null) {
- throw new MissingResourceException("Cannot read locale from
application",
- getClass().getName(), messageKey.toString());
- }
-
- BundleKey bundleKey = new BundleKey(application.getMessageBundle(), locale);
- return getOrCreateResourceBundle(bundleKey);
+ return ResourceBundle.getBundle(application.getMessageBundle(), locale,
getClassLoader());
}
}
Modified:
branches/RFPL-434/core/commons/src/main/java/org/richfaces/l10n/MessageBundle.java
===================================================================
---
branches/RFPL-434/core/commons/src/main/java/org/richfaces/l10n/MessageBundle.java 2010-08-30
07:58:57 UTC (rev 19009)
+++
branches/RFPL-434/core/commons/src/main/java/org/richfaces/l10n/MessageBundle.java 2010-08-30
12:32:48 UTC (rev 19010)
@@ -36,6 +36,6 @@
public String baseName();
- public boolean useApplicationBundle();
+ public boolean useApplicationBundle() default false;
}
Modified:
branches/RFPL-434/core/commons/src/main/java/org/richfaces/l10n/MessageFactory.java
===================================================================
---
branches/RFPL-434/core/commons/src/main/java/org/richfaces/l10n/MessageFactory.java 2010-08-30
07:58:57 UTC (rev 19009)
+++
branches/RFPL-434/core/commons/src/main/java/org/richfaces/l10n/MessageFactory.java 2010-08-30
12:32:48 UTC (rev 19010)
@@ -37,47 +37,6 @@
*/
public class MessageFactory {
- private enum BundleLoaderInvoker {
- application {
- @Override
- public ResourceBundle getBundle(BundleLoader bundleLoader, FacesContext
context, Enum<?> messageKey,
- Locale locale) throws MissingResourceException {
-
- return bundleLoader.getApplicationBundle(context, messageKey, false);
- }
- },
-
- annotation {
- @Override
- public ResourceBundle getBundle(BundleLoader bundleLoader, FacesContext
context, Enum<?> messageKey,
- Locale locale) throws MissingResourceException {
-
- return bundleLoader.getBundle(messageKey, locale);
- }
- },
-
- applicationDefaultLocale {
- @Override
- public ResourceBundle getBundle(BundleLoader bundleLoader, FacesContext
context, Enum<?> messageKey,
- Locale locale) throws MissingResourceException {
-
- return bundleLoader.getApplicationBundle(context, messageKey, true);
- }
- },
-
- anotationDefaultLocale {
- @Override
- public ResourceBundle getBundle(BundleLoader bundleLoader, FacesContext
context, Enum<?> messageKey,
- Locale locale) throws MissingResourceException {
-
- return bundleLoader.getBundle(messageKey, Locale.getDefault());
- }
- };
-
- public abstract ResourceBundle getBundle(BundleLoader bundleLoader, FacesContext
context, Enum<?> messageKey,
- Locale locale) throws MissingResourceException;
- }
-
private BundleLoader bundleLoader;
public MessageFactory(BundleLoader bundleLoader) {
@@ -85,29 +44,46 @@
this.bundleLoader = bundleLoader;
}
+ private Locale detectLocale(FacesContext context) {
+ UIViewRoot viewRoot = context.getViewRoot();
+ if (viewRoot != null && viewRoot.getLocale() != null) {
+ return viewRoot.getLocale();
+ }
+
+ return null;
+ }
+
public FacesMessage createMessage(FacesContext facesContext, Enum<?>
messageKey, Object... args) {
return createMessage(facesContext, FacesMessage.SEVERITY_INFO, messageKey,
args);
}
public FacesMessage createMessage(FacesContext facesContext, Severity severity,
Enum<?> messageKey, Object... args) {
- Locale locale;
+ if (facesContext == null) {
+ throw new NullPointerException("context");
+ }
+
+ if (severity == null) {
+ throw new NullPointerException("severity");
+ }
+
+ if (messageKey == null) {
+ throw new NullPointerException("messageKey");
+ }
+
FacesMessage result = null;
- if (facesContext != null) {
- UIViewRoot viewRoot = facesContext.getViewRoot();
-
- if (viewRoot != null) {
- locale = viewRoot.getLocale();
-
- if (locale != null) {
- result = createMessage(facesContext, severity, locale, messageKey,
args);
- }
- }
+ Locale locale = detectLocale(facesContext);
+ if (locale != null) {
+ result = createMessage(facesContext, severity, locale, messageKey, args);
}
if (result == null) {
- locale = Locale.getDefault();
- result = createMessage(facesContext, severity, locale, messageKey, args);
+ Locale defaultLocale = Locale.getDefault();
+
+ if (!defaultLocale.equals(locale)) {
+ result = createMessage(facesContext, severity, defaultLocale, messageKey,
args);
+ }
+
}
return result;
@@ -127,20 +103,28 @@
String summary = null;
String detail = null;
- for (BundleLoaderInvoker invoker : BundleLoaderInvoker.values()) {
+ try {
+ ResourceBundle bundle = bundleLoader.getApplicationBundle(context,
messageKey, locale);
+ summary = bundle.getString(messageId);
+ detail = bundle.getString(messageId + "_detail");
+ } catch (MissingResourceException e) {
+ // do nothing
+ }
+
+ if (summary == null) {
try {
- ResourceBundle bundle = invoker.getBundle(bundleLoader, context,
messageKey, locale);
+ ResourceBundle bundle = bundleLoader.getBundle(messageKey, locale);
summary = bundle.getString(messageId);
detail = bundle.getString(messageId + "_detail");
} catch (MissingResourceException e) {
// do nothing
}
-
- if (summary != null) {
- break;
- }
}
+ if (summary == null) {
+ return null;
+ }
+
String formattedSummary = MessageFormat.format(summary, args);
String formattedDetail = null;
Modified:
branches/RFPL-434/core/commons/src/main/java/org/richfaces/l10n/MessageInterpolator.java
===================================================================
---
branches/RFPL-434/core/commons/src/main/java/org/richfaces/l10n/MessageInterpolator.java 2010-08-30
07:58:57 UTC (rev 19009)
+++
branches/RFPL-434/core/commons/src/main/java/org/richfaces/l10n/MessageInterpolator.java 2010-08-30
12:32:48 UTC (rev 19010)
@@ -52,35 +52,46 @@
return key.toString();
}
- protected String getPattern(Locale locale, Enum<?> key) throws
MissingResourceException {
+ protected String getPattern(Locale locale, Enum<?> key) {
String messageKey = getMessageKey(key);
- try {
- ResourceBundle bundle = bundleLoader.getBundle(key, locale);
- String messagePattern = bundle.getString(messageKey);
+
+ if (checkApplicationBundle) {
+ FacesContext facesContext = FacesContext.getCurrentInstance();
- return messagePattern;
- } catch (MissingResourceException e) {
- if (checkApplicationBundle) {
- FacesContext facesContext = FacesContext.getCurrentInstance();
+ if (facesContext != null) {
try {
- ResourceBundle bundle =
bundleLoader.getApplicationBundle(facesContext, key, false);
+ ResourceBundle bundle =
bundleLoader.getApplicationBundle(facesContext, key, locale);
return bundle.getString(messageKey);
} catch (MissingResourceException e1) {
- ResourceBundle bundle =
bundleLoader.getApplicationBundle(facesContext, key, true);
- return bundle.getString(messageKey);
+ //do nothing
}
- } else {
- throw e;
}
}
+
+ try {
+ ResourceBundle bundle = bundleLoader.getBundle(key, locale);
+ return bundle.getString(messageKey);
+ } catch (MissingResourceException e) {
+ //do nothing
+ }
+
+ return null;
}
public String interpolate(Locale locale, Enum<?> key, Object... args) throws
InterpolationException {
- try {
- String messagePattern = getPattern(locale, key);
+ String messagePattern = getPattern(locale, key);
+
+ if (messagePattern == null) {
+ Locale defaultLocale = Locale.getDefault();
+ if (!defaultLocale.equals(locale)) {
+ messagePattern = getPattern(defaultLocale, key);
+ }
+ }
+
+ if (messagePattern != null) {
return MessageFormat.format(messagePattern, args);
- } catch (MissingResourceException e) {
- throw new InterpolationException(e).initMessageKey(key.toString());
+ } else {
+ throw new InterpolationException().initMessageKey(key.toString());
}
}
Added:
branches/RFPL-434/core/commons/src/test/java/org/richfaces/l10n/BundleLoaderAppMessages.java
===================================================================
---
branches/RFPL-434/core/commons/src/test/java/org/richfaces/l10n/BundleLoaderAppMessages.java
(rev 0)
+++
branches/RFPL-434/core/commons/src/test/java/org/richfaces/l10n/BundleLoaderAppMessages.java 2010-08-30
12:32:48 UTC (rev 19010)
@@ -0,0 +1,33 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.richfaces.l10n;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+@MessageBundle(baseName = "org.richfaces.l10n.BundleLoaderMessages",
useApplicationBundle = true)
+public enum BundleLoaderAppMessages {
+
+ message
+
+}
Added:
branches/RFPL-434/core/commons/src/test/java/org/richfaces/l10n/BundleLoaderCoreMessages.java
===================================================================
---
branches/RFPL-434/core/commons/src/test/java/org/richfaces/l10n/BundleLoaderCoreMessages.java
(rev 0)
+++
branches/RFPL-434/core/commons/src/test/java/org/richfaces/l10n/BundleLoaderCoreMessages.java 2010-08-30
12:32:48 UTC (rev 19010)
@@ -0,0 +1,33 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.richfaces.l10n;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+@MessageBundle(baseName = "org.richfaces.l10n.BundleLoaderMessages")
+public enum BundleLoaderCoreMessages {
+
+ message
+
+}
Added:
branches/RFPL-434/core/commons/src/test/java/org/richfaces/l10n/BundleLoaderTest.java
===================================================================
--- branches/RFPL-434/core/commons/src/test/java/org/richfaces/l10n/BundleLoaderTest.java
(rev 0)
+++
branches/RFPL-434/core/commons/src/test/java/org/richfaces/l10n/BundleLoaderTest.java 2010-08-30
12:32:48 UTC (rev 19010)
@@ -0,0 +1,104 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, Red Hat, Inc. and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.richfaces.l10n;
+
+import static org.easymock.EasyMock.expect;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import java.util.Locale;
+import java.util.MissingResourceException;
+
+import org.jboss.test.faces.mock.MockFacesEnvironment;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @author Nick Belaevski
+ *
+ */
+public class BundleLoaderTest {
+
+ private MockFacesEnvironment facesEnvironment;
+
+ private BundleLoader bundleLoader;
+
+ @Before
+ public void setUp() throws Exception {
+ Locale.setDefault(Locale.ENGLISH);
+
+ bundleLoader = new BundleLoader();
+
+ facesEnvironment = MockFacesEnvironment.createEnvironment().withApplication();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ bundleLoader = null;
+
+ facesEnvironment.verify();
+
+ facesEnvironment = null;
+ }
+
+ @Test
+ public void testGetMessageBundle() throws Exception {
+ facesEnvironment.replay();
+
+ assertEquals("Hello",
bundleLoader.getBundle(BundleLoaderCoreMessages.message,
Locale.US).getString("message"));
+ assertEquals("Zdravstvujte",
bundleLoader.getBundle(BundleLoaderCoreMessages.message, new Locale("ru",
"RU"))
+ .getString("message"));
+
+ try {
+ bundleLoader.getBundle(BundleLoaderCoreMessages.message, new
Locale("by", "BY"));
+ fail();
+ } catch (MissingResourceException e) {
+ // ok
+ }
+
+ }
+
+ @Test
+ public void testGetApplicationBundle() throws Exception {
+
expect(facesEnvironment.getApplication().getMessageBundle()).andStubReturn("org.richfaces.l10n.AppMessages");
+
+ facesEnvironment.replay();
+
+ assertEquals(
+ "Welcome to app",
+ bundleLoader.getApplicationBundle(facesEnvironment.getFacesContext(),
BundleLoaderAppMessages.message,
+ Locale.US).getString("message"));
+ assertEquals(
+ "Dobro pozhalovat'",
+ bundleLoader.getApplicationBundle(facesEnvironment.getFacesContext(),
BundleLoaderAppMessages.message,
+ new Locale("by",
"BY")).getString("message"));
+
+ try {
+ bundleLoader.getApplicationBundle(facesEnvironment.getFacesContext(),
BundleLoaderAppMessages.message,
+ new Locale("ru", "RU"));
+ fail();
+ } catch (MissingResourceException e) {
+ // ok
+ }
+ }
+}
Added:
branches/RFPL-434/core/commons/src/test/resources/org/richfaces/l10n/AppMessages_by_BY.properties
===================================================================
---
branches/RFPL-434/core/commons/src/test/resources/org/richfaces/l10n/AppMessages_by_BY.properties
(rev 0)
+++
branches/RFPL-434/core/commons/src/test/resources/org/richfaces/l10n/AppMessages_by_BY.properties 2010-08-30
12:32:48 UTC (rev 19010)
@@ -0,0 +1 @@
+message=Dobro pozhalovat'
\ No newline at end of file
Added:
branches/RFPL-434/core/commons/src/test/resources/org/richfaces/l10n/AppMessages_en_US.properties
===================================================================
---
branches/RFPL-434/core/commons/src/test/resources/org/richfaces/l10n/AppMessages_en_US.properties
(rev 0)
+++
branches/RFPL-434/core/commons/src/test/resources/org/richfaces/l10n/AppMessages_en_US.properties 2010-08-30
12:32:48 UTC (rev 19010)
@@ -0,0 +1 @@
+message=Welcome to app
\ No newline at end of file
Added:
branches/RFPL-434/core/commons/src/test/resources/org/richfaces/l10n/BundleLoaderMessages_en_US.properties
===================================================================
---
branches/RFPL-434/core/commons/src/test/resources/org/richfaces/l10n/BundleLoaderMessages_en_US.properties
(rev 0)
+++
branches/RFPL-434/core/commons/src/test/resources/org/richfaces/l10n/BundleLoaderMessages_en_US.properties 2010-08-30
12:32:48 UTC (rev 19010)
@@ -0,0 +1 @@
+message=Hello
\ No newline at end of file
Added:
branches/RFPL-434/core/commons/src/test/resources/org/richfaces/l10n/BundleLoaderMessages_ru_RU.properties
===================================================================
---
branches/RFPL-434/core/commons/src/test/resources/org/richfaces/l10n/BundleLoaderMessages_ru_RU.properties
(rev 0)
+++
branches/RFPL-434/core/commons/src/test/resources/org/richfaces/l10n/BundleLoaderMessages_ru_RU.properties 2010-08-30
12:32:48 UTC (rev 19010)
@@ -0,0 +1 @@
+message=Zdravstvujte
\ No newline at end of file