Author: etnu
Date: Fri Jul 24 23:43:35 2009
New Revision: 797692
URL: http://svn.apache.org/viewvc?rev=797692&view=rev
Log:
Small optimization: avoid fetching the same message bundle multiple times when
the base locale is not an exact match (such as "en" / "ALL"). Particularly
relevant for containers that don't use the country field at all.
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/DefaultMessageBundleFactory.java
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/DefaultMessageBundleFactoryTest.java
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/DefaultMessageBundleFactory.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/DefaultMessageBundleFactory.java?rev=797692&r1=797691&r2=797692&view=diff
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/DefaultMessageBundleFactory.java
(original)
+++
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/DefaultMessageBundleFactory.java
Fri Jul 24 23:43:35 2009
@@ -63,9 +63,31 @@
public MessageBundle getBundle(GadgetSpec spec, Locale locale, boolean
ignoreCache)
throws GadgetException {
MessageBundle exact = getBundleFor(spec, locale, ignoreCache);
- MessageBundle lang = getBundleFor(spec, new Locale(locale.getLanguage(),
"ALL"), ignoreCache);
- MessageBundle country = getBundleFor(spec, new Locale("all",
locale.getCountry()), ignoreCache);
- MessageBundle all = getBundleFor(spec, ALL_ALL, ignoreCache);
+
+ // We don't want to fetch the same bundle multiple times, so we verify
that the exact match
+ // has not already been fetched.
+ MessageBundle lang, country, all;
+
+ boolean isAllLanguage = locale.getLanguage().equalsIgnoreCase("all");
+ boolean isAllCountry = locale.getCountry().equalsIgnoreCase("ALL");
+
+ if (isAllCountry) {
+ lang = MessageBundle.EMPTY;
+ } else {
+ lang = getBundleFor(spec, new Locale(locale.getLanguage(), "ALL"),
ignoreCache);
+ }
+
+ if (isAllLanguage) {
+ country = MessageBundle.EMPTY;
+ } else {
+ country = getBundleFor(spec, new Locale("all", locale.getCountry()),
ignoreCache);
+ }
+
+ if (isAllCountry && isAllLanguage) {
+ all = MessageBundle.EMPTY;
+ } else {
+ all = getBundleFor(spec, ALL_ALL, ignoreCache);
+ }
return new MessageBundle(all, country, lang, exact);
}
Modified:
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/DefaultMessageBundleFactoryTest.java
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/DefaultMessageBundleFactoryTest.java?rev=797692&r1=797691&r2=797692&view=diff
==============================================================================
---
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/DefaultMessageBundleFactoryTest.java
(original)
+++
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/DefaultMessageBundleFactoryTest.java
Fri Jul 24 23:43:35 2009
@@ -94,6 +94,19 @@
"<Content type='html'/>" +
"</Module>";
+ private static final String ALL_EXTERNAL_SPEC
+ = "<Module>" +
+ "<ModulePrefs title='foo'>" +
+ " <Locale lang='all' country='ALL' messages='" + BUNDLE_URI + "'/>" +
+ " <Locale lang='all' country='" + LOCALE.getCountry() + "'" +
+ " messages='" + BUNDLE_URI + "'/>" +
+ " <Locale lang='" + LOCALE.getLanguage() + "' messages='" + BUNDLE_URI
+ "'/>" +
+ " <Locale lang='" + LOCALE.getLanguage() + "' country='" +
LOCALE.getCountry() + "' " +
+ " messages='" + BUNDLE_URI + "'/>" +
+ "</ModulePrefs>" +
+ "<Content type='html'/>" +
+ "</Module>";
+
private static final int MAX_AGE = 10000;
private final RequestPipeline pipeline =
EasyMock.createNiceMock(RequestPipeline.class);
@@ -103,16 +116,17 @@
private final DefaultMessageBundleFactory bundleFactory
= new DefaultMessageBundleFactory(new TestExecutorService(), pipeline,
cacheProvider, MAX_AGE);
private final GadgetSpec gadgetSpec;
+ private final GadgetSpec externalSpec;
public DefaultMessageBundleFactoryTest() {
try {
gadgetSpec = new GadgetSpec(SPEC_URI, BASIC_SPEC);
+ externalSpec = new GadgetSpec(SPEC_URI, ALL_EXTERNAL_SPEC);
} catch (GadgetException e) {
throw new RuntimeException(e);
}
}
-
@Test
public void getExactBundle() throws Exception {
HttpResponse response = new HttpResponse(BASIC_BUNDLE);
@@ -157,6 +171,46 @@
}
@Test
+ public void getExactBundleAllExternal() throws Exception {
+ HttpResponse response = new HttpResponse(BASIC_BUNDLE);
+
expect(pipeline.execute(isA(HttpRequest.class))).andReturn(response).times(4);
+
+ replay(pipeline);
+ bundleFactory.getBundle(externalSpec, LOCALE, true);
+ verify(pipeline);
+ }
+
+ @Test
+ public void getLangBundleAllExternal() throws Exception {
+ HttpResponse response = new HttpResponse(BASIC_BUNDLE);
+
expect(pipeline.execute(isA(HttpRequest.class))).andReturn(response).times(3);
+
+ replay(pipeline);
+ bundleFactory.getBundle(externalSpec, LANG_LOCALE, true);
+ verify(pipeline);
+ }
+
+ @Test
+ public void getCountryBundleAllExternal() throws Exception {
+ HttpResponse response = new HttpResponse(BASIC_BUNDLE);
+
expect(pipeline.execute(isA(HttpRequest.class))).andReturn(response).times(3);
+
+ replay(pipeline);
+ bundleFactory.getBundle(externalSpec, COUNTRY_LOCALE, true);
+ verify(pipeline);
+ }
+
+ @Test
+ public void getAllAllExternal() throws Exception {
+ HttpResponse response = new HttpResponse(BASIC_BUNDLE);
+
expect(pipeline.execute(isA(HttpRequest.class))).andReturn(response).once();
+
+ replay(pipeline);
+ bundleFactory.getBundle(externalSpec, new Locale("all", "ALL"), true);
+ verify(pipeline);
+ }
+
+ @Test
public void getBundleFromCache() throws Exception {
HttpResponse response = new HttpResponse(BASIC_BUNDLE);
expect(pipeline.execute(isA(HttpRequest.class))).andReturn(response).once();