Author: craigmcc Date: Fri Sep 16 20:46:50 2005 New Revision: 289697 URL: http://svn.apache.org/viewcvs?rev=289697&view=rev Log: Make the Messages class a proper JavaBean so that it can be initialized as a managed bean if desired. Also, add a test case for it.
Added: struts/shale/trunk/core-library/src/test/org/apache/shale/util/MessagesTestCase.java Modified: struts/shale/trunk/core-library/src/java/org/apache/shale/util/Messages.java struts/shale/trunk/core-library/src/test/org/apache/shale/util/LoadBundleTestCase.java Modified: struts/shale/trunk/core-library/src/java/org/apache/shale/util/Messages.java URL: http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/java/org/apache/shale/util/Messages.java?rev=289697&r1=289696&r2=289697&view=diff ============================================================================== --- struts/shale/trunk/core-library/src/java/org/apache/shale/util/Messages.java (original) +++ struts/shale/trunk/core-library/src/java/org/apache/shale/util/Messages.java Fri Sep 16 20:46:50 2005 @@ -27,6 +27,23 @@ * <p>Utility wrapper around resource bundles that provides locale-specific * message string lookups, as well as parameter replacement services.</p> * + * <p>If desired, this class can be used to define a managed bean wrapping + * a specified resource bundle, with a declaration like this in a + * <code>faces-config.xml</code> configuration file:</p> + * <code> + * <managed-bean> + * <managed-bean-name>messages</managed-bean-name> + * <managed-bean-class> + * org.apache.shale.util.Messages + * </managed-bean-class> + * <managed-bean-scope>application</managed-bean-scope> + * <managed-property> + * <property-name>name</property-name> + * <value>com.mycompany.mypackage.Bundle</value> + * </managed-property> + * </managed-bean> + * </code> + * * $Id$ */ public class Messages { @@ -36,6 +53,18 @@ /** + * <p>Construct an initialized [EMAIL PROTECTED] Messages} wrapper. At least the + * <code>name</code> property must be initialized before the message + * retrieval public methods may be successfully utilized.</p> + */ + public Messages() { + + this(null, null); + + } + + + /** * <p>Construct a new [EMAIL PROTECTED] Messages} wrapper around the specified * resource bundle name, loaded by the default class loader.</p> * @@ -54,7 +83,8 @@ * * @param name Name of the requested <code>ResourceBundle</code> * @param cl <code>ClassLoader</code> to use for loading this - * resource bundle, or <code>null</code> for the default + * resource bundle, or <code>null</code> for the default (which + * selects the thread context class loader) */ public Messages(String name, ClassLoader cl) { @@ -75,22 +105,53 @@ /** + * <p>The default <code>Locale</code> for this server.</p> + */ + private Locale defaultLocale = Locale.getDefault(); + + + /** + * <p><code>MessageFormat</code> used to perform parameter substitution.</p> + */ + private MessageFormat format = new MessageFormat(""); + + + // -------------------------------------------------------------- Properties + + + /** * <p><code>ClassLoader</code> from which to load the specfied - * resource bundle.</p> + * resource bundle, or <code>null</code> for the thread context + * class loader.</p> */ private ClassLoader cl = null; /** - * <p>The default <code>Locale</code> for this server.</p> + * <p>Return the <code>ClassLoader</code> from which to load the + * specified resource bundle, or <code>null</code> for the thread + * context class loader.</p> */ - private Locale defaultLocale = Locale.getDefault(); + public ClassLoader getClassLoader() { + + return this.cl; + + } /** - * <p><code>MessageFormat</code> used to perform parameter substitution.</p> + * <p>Set the <code>ClassLoader</code> from which to load the + * specified resource bundle.</p> + * + * @param cl The new class loader, or <code>null</code> for the + * thread context class loader */ - private MessageFormat format = new MessageFormat(""); + public void setClassLoader(ClassLoader cl) { + + this.cl = null; + reset(); + + } /** @@ -99,6 +160,29 @@ private String name = null; + /** + * <p>Return the name of the resource bundle to be retrieved.</p> + */ + public String getName() { + + return this.name; + + } + + + /** + * <p>Set the name of the resource bunde to be retrieved.</p> + * + * @param name New name of the resource bundle to be retrieved + */ + public void setName(String name) { + + this.name = name; + reset(); + + } + + // ---------------------------------------------------------- Public Methods @@ -185,17 +269,30 @@ private ResourceBundle getBundle(Locale locale) { ResourceBundle rb = null; + ClassLoader rbcl = cl; + if (cl == null) { + cl = Thread.currentThread().getContextClassLoader(); + } synchronized (bundles) { rb = (ResourceBundle) bundles.get(locale); if (rb == null) { - if (cl == null) { - rb = ResourceBundle.getBundle(name, locale); - } else { - rb = ResourceBundle.getBundle(name, locale, cl); - } + rb = ResourceBundle.getBundle(name, locale, cl); bundles.put(locale, rb); } return rb; + } + + } + + + /** + * <p>Reset any cached <code>ResourceBundle</code> instances due to a + * change in one of the relevant properties.</p> + */ + private void reset() { + + synchronized (bundles) { + bundles.clear(); } } Modified: struts/shale/trunk/core-library/src/test/org/apache/shale/util/LoadBundleTestCase.java URL: http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/test/org/apache/shale/util/LoadBundleTestCase.java?rev=289697&r1=289696&r2=289697&view=diff ============================================================================== --- struts/shale/trunk/core-library/src/test/org/apache/shale/util/LoadBundleTestCase.java (original) +++ struts/shale/trunk/core-library/src/test/org/apache/shale/util/LoadBundleTestCase.java Fri Sep 16 20:46:50 2005 @@ -24,8 +24,7 @@ import org.apache.shale.test.base.AbstractJsfTestCase; /** - * - * @author craigmcc + * <p>Test case for <code>LoadBundle</code>.</p> */ public class LoadBundleTestCase extends AbstractJsfTestCase { Added: struts/shale/trunk/core-library/src/test/org/apache/shale/util/MessagesTestCase.java URL: http://svn.apache.org/viewcvs/struts/shale/trunk/core-library/src/test/org/apache/shale/util/MessagesTestCase.java?rev=289697&view=auto ============================================================================== --- struts/shale/trunk/core-library/src/test/org/apache/shale/util/MessagesTestCase.java (added) +++ struts/shale/trunk/core-library/src/test/org/apache/shale/util/MessagesTestCase.java Fri Sep 16 20:46:50 2005 @@ -0,0 +1,109 @@ +/* + * Copyright 2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shale.util; + +import java.util.Locale; +import javax.faces.context.FacesContext; +import junit.framework.Test; +import junit.framework.TestSuite; +import org.apache.shale.test.base.AbstractJsfTestCase; + +/** + * <p>Test case for <code>Messages</code>.</p> + */ +public class MessagesTestCase extends AbstractJsfTestCase { + + + // ------------------------------------------------------------ Constructors + + + // Construct a new instance of this test case. + public MessagesTestCase(String name) { + super(name); + } + + + // ---------------------------------------------------- Overall Test Methods + + + // Set up instance variables required by this test case. + public void setUp() { + + super.setUp(); + + // Set up the instance we will be testing + m = new Messages("org.apache.shale.util.TestBundle"); + + } + + + // Return the tests included in this test case. + public static Test suite() { + + return (new TestSuite(MessagesTestCase.class)); + + } + + + // Tear down instance variables required by this test case. + public void tearDown() { + + m = null; + super.tearDown(); + + } + + + // ------------------------------------------------------ Instance Variables + + + // The instance to be tested + Messages m = null; + + + // ------------------------------------------------------------ Test Methods + + + // Test access to the English values for this resource bundle + public void testEngish() { + + Locale l = new Locale("en", "US"); + assertEquals("English Key 1", m.getMessage("key1", l)); + assertEquals("English Key 2", m.getMessage("key2", l)); + + } + + + // Test access to the French values for this resource bundle + public void testFrench() { + + Locale l = new Locale("fr", "FR"); + assertEquals("French Key 1", m.getMessage("key1", l)); + assertEquals("French Key 2", m.getMessage("key2", l)); + + } + + + // Test a pristine instance + public void testPristine() { + + assertEquals("org.apache.shale.util.TestBundle", m.getName()); + + } + + +} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]