
The branch, master has been updated via 2f05b5c32a36c116bc1ed8f8f53f7cf3e2716d50 (commit) from e15eefb1a0752d02f9ddf0644c3168611b450247 (commit) Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below. - Log ----------------------------------------------------------------- http://git.qos.ch/gitweb/?p=cal10n.git;a=commit;h=2f05b5c32a36c116bc1ed8f8f5... http://github.com/ceki/cal10n/commit/2f05b5c32a36c116bc1ed8f8f53f7cf3e2716d5... commit 2f05b5c32a36c116bc1ed8f8f53f7cf3e2716d50 Author: Ceki Gulcu <ceki@qos.ch> Date: Wed Sep 2 21:29:32 2009 +0200 - MessageConveyor now caches resource bundles for dramatically improved performance. - Resource bundles are reloaded upon change. diff --git a/cal10n-api/pom.xml b/cal10n-api/pom.xml index b1cca6e..c40b7c1 100644 --- a/cal10n-api/pom.xml +++ b/cal10n-api/pom.xml @@ -5,7 +5,7 @@ <parent> <groupId>ch.qos.cal10n</groupId> <artifactId>cal10n-parent</artifactId> - <version>0.6</version> + <version>0.6.5</version> </parent> <modelVersion>4.0.0</modelVersion> diff --git a/cal10n-api/src/main/java/ch/qos/cal10n/IMessageConveyor.java b/cal10n-api/src/main/java/ch/qos/cal10n/IMessageConveyor.java index 14c4275..02abc0e 100644 --- a/cal10n-api/src/main/java/ch/qos/cal10n/IMessageConveyor.java +++ b/cal10n-api/src/main/java/ch/qos/cal10n/IMessageConveyor.java @@ -55,7 +55,7 @@ public interface IMessageConveyor { * optional arguments * @return The translated/localized message */ - <E extends Enum<?>> String getMessage(E key, Object... args); + <E extends Enum<?>> String getMessage(E key, Object... args) throws MessageConveyorException; /** * Syntactic sugar for the case where the massage is contained in a @@ -73,5 +73,5 @@ public interface IMessageConveyor { * The MessageParameterObj to translate * @return translated message */ - String getMessage(MessageParameterObj mpo); + String getMessage(MessageParameterObj mpo) throws MessageConveyorException; } diff --git a/cal10n-api/src/main/java/ch/qos/cal10n/MessageConveyor.java b/cal10n-api/src/main/java/ch/qos/cal10n/MessageConveyor.java index 17069e2..4d05573 100644 --- a/cal10n-api/src/main/java/ch/qos/cal10n/MessageConveyor.java +++ b/cal10n-api/src/main/java/ch/qos/cal10n/MessageConveyor.java @@ -24,9 +24,11 @@ package ch.qos.cal10n; import java.text.MessageFormat; import java.util.Locale; -import java.util.ResourceBundle; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import ch.qos.cal10n.util.AnnotationExtractor; +import ch.qos.cal10n.util.CAL10NPropertyResourceBundle; import ch.qos.cal10n.util.PropertyResourceBundleFinder; /** @@ -40,7 +42,9 @@ import ch.qos.cal10n.util.PropertyResourceBundleFinder; */ public class MessageConveyor implements IMessageConveyor { - Locale locale; + final Locale locale; + + final Map<String, CAL10NPropertyResourceBundle> cache = new ConcurrentHashMap<String, CAL10NPropertyResourceBundle>(); /** * The {@link Locale} associated with this instance. @@ -64,20 +68,16 @@ public class MessageConveyor implements IMessageConveyor { * an enum instance used as message key * */ - public <E extends Enum<?>> String getMessage(E key, Object... args) { - String keyAsStr = key.toString(); + public <E extends Enum<?>> String getMessage(E key, Object... args) throws MessageConveyorException { - String baseName = AnnotationExtractor.getBaseName(key.getDeclaringClass()); - if (baseName == null) { - throw new IllegalArgumentException( - "Missing @BaseName annotation in enum type [" - + key.getClass().getName() + "]. See also " - + Cal10nConstants.MISSING_BN_ANNOTATION_URL); + String declararingClassName = key.getDeclaringClass().getName(); + CAL10NPropertyResourceBundle rb = cache.get(declararingClassName); + if (rb == null || rb.hasChanged()) { + rb = lookup(key); + cache.put(declararingClassName, rb); } - ResourceBundle rb = PropertyResourceBundleFinder.getBundle(this.getClass() - .getClassLoader(), baseName, locale); - + String keyAsStr = key.toString(); String value = rb.getString(keyAsStr); if (value == null) { return "No key found for " + keyAsStr; @@ -90,7 +90,26 @@ public class MessageConveyor implements IMessageConveyor { } } - public String getMessage(MessageParameterObj mpo) { + private <E extends Enum<?>> CAL10NPropertyResourceBundle lookup(E key) throws MessageConveyorException { + String baseName = AnnotationExtractor.getBaseName(key.getDeclaringClass()); + if (baseName == null) { + throw new MessageConveyorException( + "Missing @BaseName annotation in enum type [" + + key.getClass().getName() + "]. See also " + + Cal10nConstants.MISSING_BN_ANNOTATION_URL); + } + CAL10NPropertyResourceBundle rb = PropertyResourceBundleFinder.getBundle(this.getClass() + .getClassLoader(), baseName, locale); + + if(rb == null) { + throw new MessageConveyorException("Failed to locate resource bundle [" + baseName + + "] for locale [" + locale + "] for enum type [" + key.getDeclaringClass().getName() + + "]"); + } + return rb; + } + + public String getMessage(MessageParameterObj mpo) throws MessageConveyorException { if (mpo == null) { throw new IllegalArgumentException( "MessageParameterObj argumument cannot be null"); diff --git a/cal10n-api/src/main/java/ch/qos/cal10n/util/CAL10NPropertyResourceBundle.java b/cal10n-api/src/main/java/ch/qos/cal10n/MessageConveyorException.java similarity index 72% copy from cal10n-api/src/main/java/ch/qos/cal10n/util/CAL10NPropertyResourceBundle.java copy to cal10n-api/src/main/java/ch/qos/cal10n/MessageConveyorException.java index b708cee..bae5c6e 100644 --- a/cal10n-api/src/main/java/ch/qos/cal10n/util/CAL10NPropertyResourceBundle.java +++ b/cal10n-api/src/main/java/ch/qos/cal10n/MessageConveyorException.java @@ -19,21 +19,15 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package ch.qos.cal10n.util; +package ch.qos.cal10n; -import java.io.IOException; -import java.io.InputStream; -import java.util.PropertyResourceBundle; -import java.util.ResourceBundle; -public class CAL10NPropertyResourceBundle extends PropertyResourceBundle { +public class MessageConveyorException extends RuntimeException { - public CAL10NPropertyResourceBundle(InputStream is) throws IOException { - super(is); - } + private static final long serialVersionUID = 175752418665292427L; - public void setParent(ResourceBundle parent) { - super.setParent(parent); + MessageConveyorException(String msg) { + super(msg); } - + } diff --git a/cal10n-api/src/main/java/ch/qos/cal10n/util/CAL10NPropertyResourceBundle.java b/cal10n-api/src/main/java/ch/qos/cal10n/util/CAL10NPropertyResourceBundle.java index b708cee..fec35be 100644 --- a/cal10n-api/src/main/java/ch/qos/cal10n/util/CAL10NPropertyResourceBundle.java +++ b/cal10n-api/src/main/java/ch/qos/cal10n/util/CAL10NPropertyResourceBundle.java @@ -21,6 +21,7 @@ */ package ch.qos.cal10n.util; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.PropertyResourceBundle; @@ -28,12 +29,40 @@ import java.util.ResourceBundle; public class CAL10NPropertyResourceBundle extends PropertyResourceBundle { - public CAL10NPropertyResourceBundle(InputStream is) throws IOException { + static long CHECK_DELAY = 10 * 60 * 1000; // 10 minutes delay + + File hostFile; + volatile long nextCheck; + long lastModified; + + public CAL10NPropertyResourceBundle(InputStream is, File file) + throws IOException { super(is); + this.hostFile = file; + nextCheck = System.currentTimeMillis() + CHECK_DELAY; } public void setParent(ResourceBundle parent) { super.setParent(parent); } + public boolean hasChanged() { + //if the host file is unknown, no point in a check + if (hostFile == null) { + return false; + } + + long now = System.currentTimeMillis(); + if (now < nextCheck) { + return false; + } else { + nextCheck = now + CHECK_DELAY; + if (lastModified != hostFile.lastModified()) { + lastModified = hostFile.lastModified(); + return true; + } else { + return false; + } + } + } } diff --git a/cal10n-api/src/main/java/ch/qos/cal10n/util/PropertyResourceBundleFinder.java b/cal10n-api/src/main/java/ch/qos/cal10n/util/PropertyResourceBundleFinder.java index 7ecdf89..6b102f4 100644 --- a/cal10n-api/src/main/java/ch/qos/cal10n/util/PropertyResourceBundleFinder.java +++ b/cal10n-api/src/main/java/ch/qos/cal10n/util/PropertyResourceBundleFinder.java @@ -21,16 +21,16 @@ */ package ch.qos.cal10n.util; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import java.util.Locale; -import java.util.ResourceBundle; public class PropertyResourceBundleFinder { - public static ResourceBundle getBundle(ClassLoader classLoader, String baseName, + public static CAL10NPropertyResourceBundle getBundle(ClassLoader classLoader, String baseName, Locale locale) { // same as the JDK convention @@ -70,7 +70,7 @@ public class PropertyResourceBundleFinder { if (url != null) { try { InputStream in = openConnectionForUrl(url); - prb = new CAL10NPropertyResourceBundle(in); + prb = new CAL10NPropertyResourceBundle(in, urlToFile(url)); in.close(); } catch (IOException e) { } @@ -78,6 +78,21 @@ public class PropertyResourceBundleFinder { return prb; } + static File urlToFile(URL url) { + if(url.getProtocol() != "file") { + return null; + } + String path = url.getPath(); + if(path == null) + return null; + File candidate = new File(path); + if(candidate.exists()) { + return candidate; + } else { + return null; + } + } + private static String computeLanguageAndCountryCandidate(String baseName, Locale locale) { String language = locale.getLanguage(); diff --git a/cal10n-api/src/test/java/ch/qos/cal10n/sample/MessageConveyorTest.java b/cal10n-api/src/test/java/ch/qos/cal10n/sample/MessageConveyorTest.java index bdd7ba8..6d8e987 100644 --- a/cal10n-api/src/test/java/ch/qos/cal10n/sample/MessageConveyorTest.java +++ b/cal10n-api/src/test/java/ch/qos/cal10n/sample/MessageConveyorTest.java @@ -23,12 +23,14 @@ package ch.qos.cal10n.sample; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; import java.util.Locale; import org.junit.Test; import ch.qos.cal10n.MessageConveyor; +import ch.qos.cal10n.MessageConveyorException; import ch.qos.cal10n.MessageParameterObj; import ch.qos.cal10n.sample.Host.OtherColors; @@ -90,4 +92,18 @@ public class MessageConveyorTest { val = rbbmc.getMessage(mpo); assertEquals("apples are green", val); } + + @Test + public void failedRBLookup() { + + MessageConveyor mc = new MessageConveyor(Locale.CHINA); + try { + mc.getMessage(Colors.BLUE); + fail("missing exception"); + } catch (MessageConveyorException e) { + assertEquals( + "Failed to locate resource bundle [colors] for locale [zh_CN] for enum type [ch.qos.cal10n.sample.Colors]", + e.getMessage()); + } + } } diff --git a/cal10n-api/src/test/java/ch/qos/cal10n/util/PropertyResourceBundleFinderTest.java b/cal10n-api/src/test/java/ch/qos/cal10n/util/PropertyResourceBundleFinderTest.java index 323f4d2..ab02b10 100644 --- a/cal10n-api/src/test/java/ch/qos/cal10n/util/PropertyResourceBundleFinderTest.java +++ b/cal10n-api/src/test/java/ch/qos/cal10n/util/PropertyResourceBundleFinderTest.java @@ -22,8 +22,13 @@ package ch.qos.cal10n.util; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import java.io.File; import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; import java.util.Locale; import java.util.ResourceBundle; @@ -60,4 +65,22 @@ public class PropertyResourceBundleFinderTest { "foobar.sample", Locale.ENGLISH); assertEquals("A is the first letter of the alphabet", rb.getString("A")); } + + @Test + public void urlToFile() { + ClassLoader classLoader = this.getClass().getClassLoader(); + String resourceCandidate = "colors" + "_" + "en" + ".properties"; + URL url = classLoader.getResource(resourceCandidate); + assertNotNull("the problem is in this test, not the code tested", url); + + File file = PropertyResourceBundleFinder.urlToFile(url); + assertNotNull(file); + } + + @Test + public void httpUrlToFile() throws MalformedURLException { + URL url = new URL("http://www.xyz.com"); + File file = PropertyResourceBundleFinder.urlToFile(url); + assertNull(file); + } } diff --git a/cal10n-site/pom.xml b/cal10n-site/pom.xml index 380f65e..c5fefa4 100644 --- a/cal10n-site/pom.xml +++ b/cal10n-site/pom.xml @@ -5,7 +5,7 @@ <parent> <groupId>ch.qos.cal10n</groupId> <artifactId>cal10n-parent</artifactId> - <version>0.6</version> + <version>0.6.5</version> </parent> <modelVersion>4.0.0</modelVersion> diff --git a/cal10n-site/src/site/pages/news.html b/cal10n-site/src/site/pages/news.html index 488a47f..8703581 100644 --- a/cal10n-site/src/site/pages/news.html +++ b/cal10n-site/src/site/pages/news.html @@ -29,6 +29,15 @@ <hr width="80%" align="center" /> + <h3>xx of September 2009 - Release of CAL10N version 0.6.5</h3> + + <p><code>MessageConveyor</code> now caches resource bundles for + dramatically improved performance.</p> + + <p>Resource bundles are reloaded upon change.</p> + + <hr width="80%" align="center" /> + <h3>2nd of September 2009 - Release of CAL10N version 0.6</h3> <p>Fixed issue <a href="http://jira.qos.ch/browse/CAL-1">CAL-1</a> diff --git a/maven-cal10n-plugin-smoke/pom.xml b/maven-cal10n-plugin-smoke/pom.xml index 7103297..a7f6cc8 100644 --- a/maven-cal10n-plugin-smoke/pom.xml +++ b/maven-cal10n-plugin-smoke/pom.xml @@ -4,7 +4,7 @@ <parent> <groupId>ch.qos.cal10n</groupId> <artifactId>cal10n-parent</artifactId> - <version>0.6</version> + <version>0.6.5</version> </parent> <modelVersion>4.0.0</modelVersion> diff --git a/maven-cal10n-plugin/pom.xml b/maven-cal10n-plugin/pom.xml index 755b2e6..476b445 100644 --- a/maven-cal10n-plugin/pom.xml +++ b/maven-cal10n-plugin/pom.xml @@ -8,7 +8,7 @@ <parent> <artifactId>cal10n-parent</artifactId> <groupId>ch.qos.cal10n</groupId> - <version>0.6</version> + <version>0.6.5</version> </parent> <groupId>ch.qos.cal10n.plugins</groupId> diff --git a/pom.xml b/pom.xml index b10eb01..236a95b 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ <groupId>ch.qos.cal10n</groupId> <artifactId>cal10n-parent</artifactId> <packaging>pom</packaging> - <version>0.6</version> + <version>0.6.5</version> <name>Compiler assisted localization library (CAL10N) - Parent</name> <url>http://cal10n.qos.ch</url> ----------------------------------------------------------------------- Summary of changes: cal10n-api/pom.xml | 2 +- .../main/java/ch/qos/cal10n/IMessageConveyor.java | 4 +- .../main/java/ch/qos/cal10n/MessageConveyor.java | 47 ++++++++++++++------ .../ch/qos/cal10n/MessageConveyorException.java} | 18 ++++---- .../cal10n/util/CAL10NPropertyResourceBundle.java | 31 ++++++++++++- .../cal10n/util/PropertyResourceBundleFinder.java | 21 ++++++++- .../ch/qos/cal10n/sample/MessageConveyorTest.java | 16 +++++++ .../util/PropertyResourceBundleFinderTest.java | 23 ++++++++++ cal10n-site/pom.xml | 2 +- cal10n-site/src/site/pages/news.html | 9 ++++ maven-cal10n-plugin-smoke/pom.xml | 2 +- maven-cal10n-plugin/pom.xml | 2 +- pom.xml | 2 +- 13 files changed, 145 insertions(+), 34 deletions(-) copy cal10n-api/src/{test/java/ch/qos/cal10n/sample/Furnitures.java => main/java/ch/qos/cal10n/MessageConveyorException.java} (81%) hooks/post-receive -- Compiler assisted localization library