You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by ep...@apache.org on 2003/10/14 11:01:54 UTC
cvs commit: jakarta-turbine-fulcrum/localization/src/test/org/apache/fulcrum/localization LocalizationFacadeTest.java
epugh 2003/10/14 02:01:54
Modified: localization/src/java/org/apache/fulcrum/localization
DefaultLocalizationService.java
Added: localization/src/java/org/apache/fulcrum/localization
Localization.java
localization/xdocs changes.xml
localization/src/test/org/apache/fulcrum/localization
LocalizationFacadeTest.java
Log:
Added the "Localication" class which provides a facade to the underlying service.
Revision Changes Path
1.2 +85 -27 jakarta-turbine-fulcrum/localization/src/java/org/apache/fulcrum/localization/DefaultLocalizationService.java
Index: DefaultLocalizationService.java
===================================================================
RCS file: /home/cvs/jakarta-turbine-fulcrum/localization/src/java/org/apache/fulcrum/localization/DefaultLocalizationService.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DefaultLocalizationService.java 16 Aug 2003 02:58:50 -0000 1.1
+++ DefaultLocalizationService.java 14 Oct 2003 09:01:54 -0000 1.2
@@ -58,15 +58,19 @@
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
+
import javax.servlet.http.HttpServletRequest;
-import org.apache.commons.lang.StringUtils;
-import org.apache.log4j.Category;
+
import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.thread.ThreadSafe;
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
/**
* <p>This class is the single point of access to all localization
* resources. It caches different ResourceBundles for different
@@ -101,6 +105,8 @@
extends AbstractLogEnabled
implements LocalizationService, Configurable, Initializable, ThreadSafe
{
+ /** The log. */
+ private static Log log = LogFactory.getLog(DefaultLocalizationService.class);
/** Key Prefix for our bundles */
private static final String BUNDLES = "bundles";
/**
@@ -130,10 +136,7 @@
private String defaultLanguage;
/** The name of the default country to use. */
private String defaultCountry = null;
- /**
- * Log4J logging category.
- */
- private Category category = Category.getInstance(getClass().getName());
+
/**
* Creates a new instance.
*/
@@ -145,10 +148,22 @@
{
Locale jvmDefault = Locale.getDefault();
System.out.println("Using parameters");
- defaultLanguage = conf.getAttribute("locale-default-language", jvmDefault.getLanguage()).trim();
- defaultCountry = conf.getAttribute("locale-default-country", jvmDefault.getCountry()).trim();
+ defaultLanguage =
+ conf
+ .getAttribute(
+ "locale-default-language",
+ jvmDefault.getLanguage())
+ .trim();
+ defaultCountry =
+ conf
+ .getAttribute("locale-default-country", jvmDefault.getCountry())
+ .trim();
// FIXME! need to add bundle names
- getLogger().info("initialized lang=" + defaultLanguage + " country=" + defaultCountry);
+ getLogger().info(
+ "initialized lang="
+ + defaultLanguage
+ + " country="
+ + defaultCountry);
final Configuration bundles = conf.getChild(BUNDLES, false);
if (bundles != null)
{
@@ -159,7 +174,7 @@
String key = nameVal[i].getName();
String val = nameVal[i].getValue();
getLogger().debug("Registered bundle " + val);
- bundleName[i] = val;
+ bundleName[i] = val;
}
initBundleNames(bundleName);
}
@@ -169,8 +184,13 @@
*/
public void initialize() throws Exception
{
- // initBundleNames(null);
+ // initBundleNames(null);
defaultLocale = new Locale(defaultLanguage, defaultCountry);
+ Localization.setLocalizationService(this);
+ if (log.isInfoEnabled())
+ {
+ log.info("OSWorkflow Service is Initialized now..");
+ }
}
/**
* Initialize list of default bundle names.
@@ -192,7 +212,12 @@
// Prepend "default" bundle name.
String[] array = new String[intBundleNames.length + 1];
array[0] = defaultBundleName;
- System.arraycopy(intBundleNames, 0, array, 1, intBundleNames.length);
+ System.arraycopy(
+ intBundleNames,
+ 0,
+ array,
+ 1,
+ intBundleNames.length);
bundleNames = array;
}
}
@@ -296,7 +321,8 @@
public ResourceBundle getBundle(String bundleName, Locale locale)
{
// Assure usable inputs.
- bundleName = (bundleName == null ? getDefaultBundleName() : bundleName.trim());
+ bundleName =
+ (bundleName == null ? getDefaultBundleName() : bundleName.trim());
if (locale == null)
{
locale = getLocale((String) null);
@@ -328,13 +354,22 @@
*
* @exception MissingResourceException Bundle not found.
*/
- private synchronized ResourceBundle cacheBundle(String bundleName, Locale locale) throws MissingResourceException
+ private synchronized ResourceBundle cacheBundle(
+ String bundleName,
+ Locale locale)
+ throws MissingResourceException
{
HashMap bundlesByLocale = (HashMap) bundles.get(bundleName);
- ResourceBundle rb = (bundlesByLocale == null ? null : (ResourceBundle) bundlesByLocale.get(locale));
+ ResourceBundle rb =
+ (bundlesByLocale == null
+ ? null
+ : (ResourceBundle) bundlesByLocale.get(locale));
if (rb == null)
{
- bundlesByLocale = (bundlesByLocale == null ? new HashMap(3) : new HashMap(bundlesByLocale));
+ bundlesByLocale =
+ (bundlesByLocale == null
+ ? new HashMap(3)
+ : new HashMap(bundlesByLocale));
try
{
rb = ResourceBundle.getBundle(bundleName, locale);
@@ -373,26 +408,34 @@
* <p>Since we're really just guessing at possible bundles to use,
* we don't ever throw <code>MissingResourceException</code>.</p>
*/
- private ResourceBundle findBundleByLocale(String bundleName, Locale locale, Map bundlesByLocale)
+ private ResourceBundle findBundleByLocale(
+ String bundleName,
+ Locale locale,
+ Map bundlesByLocale)
{
ResourceBundle rb = null;
- if (!StringUtils.isNotEmpty(locale.getCountry()) && defaultLanguage.equals(locale.getLanguage()))
+ if (!StringUtils.isNotEmpty(locale.getCountry())
+ && defaultLanguage.equals(locale.getLanguage()))
{
/*
category.debug("Requested language '" + locale.getLanguage() +
"' matches default: Attempting to guess bundle " +
"using default country '" + defaultCountry + '\'');
*/
- Locale withDefaultCountry = new Locale(locale.getLanguage(), defaultCountry);
+ Locale withDefaultCountry =
+ new Locale(locale.getLanguage(), defaultCountry);
rb = (ResourceBundle) bundlesByLocale.get(withDefaultCountry);
if (rb == null)
{
rb = getBundleIgnoreException(bundleName, withDefaultCountry);
}
}
- else if (!StringUtils.isNotEmpty(locale.getLanguage()) && defaultCountry.equals(locale.getCountry()))
+ else if (
+ !StringUtils.isNotEmpty(locale.getLanguage())
+ && defaultCountry.equals(locale.getCountry()))
{
- Locale withDefaultLanguage = new Locale(defaultLanguage, locale.getCountry());
+ Locale withDefaultLanguage =
+ new Locale(defaultLanguage, locale.getCountry());
rb = (ResourceBundle) bundlesByLocale.get(withDefaultLanguage);
if (rb == null)
{
@@ -411,7 +454,9 @@
* returning <code>null</code> instead of throwing
* <code>MissingResourceException</code>.
*/
- private final ResourceBundle getBundleIgnoreException(String bundleName, Locale locale)
+ private final ResourceBundle getBundleIgnoreException(
+ String bundleName,
+ Locale locale)
{
try
{
@@ -519,7 +564,7 @@
+ loc
+ ", key="
+ key;
- category.debug(mesg);
+ log.debug(mesg);
// Text not found in requested or default bundles.
throw new MissingResourceException(mesg, bundleName, key);
}
@@ -547,14 +592,23 @@
/**
* @see org.apache.fulcrum.localization.LocalizationService#format(String, Locale, String, Object)
*/
- public String format(String bundleName, Locale locale, String key, Object arg1)
+ public String format(
+ String bundleName,
+ Locale locale,
+ String key,
+ Object arg1)
{
return format(bundleName, locale, key, new Object[] { arg1 });
}
/**
* @see org.apache.fulcrum.localization.LocalizationService#format(String, Locale, String, Object, Object)
*/
- public String format(String bundleName, Locale locale, String key, Object arg1, Object arg2)
+ public String format(
+ String bundleName,
+ Locale locale,
+ String key,
+ Object arg1,
+ Object arg2)
{
return format(bundleName, locale, key, new Object[] { arg1, arg2 });
}
@@ -567,7 +621,11 @@
* @return Localized, formatted text identified by
* <code>key</code>.
*/
- public String format(String bundleName, Locale locale, String key, Object[] args)
+ public String format(
+ String bundleName,
+ Locale locale,
+ String key,
+ Object[] args)
{
if (locale == null)
{
1.1 jakarta-turbine-fulcrum/localization/src/java/org/apache/fulcrum/localization/Localization.java
Index: Localization.java
===================================================================
package org.apache.fulcrum.localization;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Turbine" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Turbine", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.util.Locale;
import java.util.ResourceBundle;
import javax.servlet.http.HttpServletRequest;
/**
* Wrapper around the TurbineLocalization Service that makes it easy
* to grab something from the service and make the code cleaner.
*
* <p>
*
* Instead of first lookup up the component, and then doing the query,
* you can use it directly instead.
* <br>
* <strong>Note: The component must be looked up first, or this wrapper
* won't have a localization service yet.</strong>
*
* Now you only need to type:
*
* <br>
*
* Localization.getString(str)
*
* @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
* @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
* * @author <a href="mailto:epugh@upstate.com">epugh@upstate.com</a>
* @version $Id: Localization.java,v 1.1 2003/10/14 09:01:54 epugh Exp $
*/
public class Localization
{
/** Static instance of the LocalizationService. */
private static LocalizationService localizationService;
/**
* Return whether the localization service has been initialized
* or not.
* @return
*/
public static boolean isInitialized(){
return (!(localizationService==null));
}
/**
* Utility method for accessing the service
* implementation
*
* @return a LocalizationService implementation instance
*/
private static LocalizationService getService()
{
if(localizationService==null){
throw new RuntimeException("Localization Service has not been started yet.");
}
return localizationService;
}
static void setLocalizationService(LocalizationService service)
{
localizationService = service;
}
/**
* Fetches the localized text from the specified bundle, ignoring
* any default bundles.
*
* @see LocalizationService#getString(String, Locale, String)
*/
public static String getString(String bundleName, Locale locale,
String key)
{
return getService().getString(bundleName, locale, key);
}
/**
* Pulls a string out of the LocalizationService with the default
* locale values of what is defined in the
* TurbineResources.properties file for the
* locale.default.language and locale.default.country property
* values. If those cannot be found, then the JVM defaults are
* used.
*
* @param key Name of string.
* @return A localized String.
*/
public static String getString(String key)
{
return getService().getString(null, null, key);
}
/**
* @param key Name of the text to retrieve.
* @param locale Locale to get text for.
* @return Localized text.
*/
public static String getString(String key, Locale locale)
{
return getService().getString(null, locale, key);
}
/**
* Pulls a string out of the LocalizationService and attempts to
* determine the Locale by the Accept-Language header. If that
* header is not present, it will fall back to using the locale
* values of what is defined in the TurbineResources.properties
* file for the locale.default.language and locale.default.country
* property values. If those cannot be found, then the JVM
* defaults are used.
*
* @param req HttpServletRequest information.
* @param key Name of string.
* @return A localized String.
*/
public static String getString(String key, HttpServletRequest req)
{
return getService().getString(null, getLocale(req), key);
}
/**
* Convenience method that pulls a localized string off the
* LocalizationService using the default ResourceBundle name
* defined in the TurbineResources.properties file and the
* specified language name in ISO format.
*
* @param key Name of string.
* @param lang Desired language for the localized string.
* @return A localized string.
*/
public static String getString(String key, String lang)
{
return getString(getDefaultBundleName(), new Locale(lang, ""), key);
}
/**
* Convenience method to get a ResourceBundle based on name.
*
* @param bundleName Name of bundle.
* @return A localized ResourceBundle.
*/
public static ResourceBundle getBundle(String bundleName)
{
return getService().getBundle(bundleName);
}
/**
* Convenience method to get a ResourceBundle based on name and
* HTTP Accept-Language header.
*
* @param bundleName Name of bundle.
* @param languageHeader A String with the language header.
* @return A localized ResourceBundle.
*/
public static ResourceBundle getBundle(String bundleName,
String languageHeader)
{
return getService().getBundle(bundleName, languageHeader);
}
/**
* Convenience method to get a ResourceBundle based on name and
* HTTP Accept-Language header in HttpServletRequessetLocalizationServicem req HttpServletRequest.
* @return A localized ResourceBundle.
*/
public static ResourceBundle getBundle(HttpServletRequest req)
{
return getService().getBundle(req);
}
/**
* Convenience method to get a ResourceBundle based on name and
* HTTP Accept-Language header in HttpServletRequest.
*
* @param bundleName Name of bundle.
* @param req HttpServletRequest.
* @return A localized ResourceBundle.
*/
public static ResourceBundle getBundle(String bundleName,
HttpServletRequest req)
{
return getService().getBundle(bundleName, req);
}
/**
* Convenience method to get a ResourceBundle based on name and
* Locale.
*
* @param bundleName Name of bundle.
* @param locale A Locale.
* @return A localized ResourceBundle.
*/
public static ResourceBundle getBundle(String bundleName, Locale locale)
{
return getService().getBundle(bundleName, locale);
}
/**
* This method sets the name of the default bundle.
*
* @param defaultBundle Name of default bundle.
*/
public static void setBundle(String defaultBundle)
{
getService().setBundle(defaultBundle);
}
/**
* Attempts to pull the <code>Accept-Language</code> header out of
* the HttpServletRequest object and then parse it. If the header
* is not present, it will return a null Locale.
*
* @param req HttpServletRequest.
* @return A Locale.
*/
public static Locale getLocale(HttpServletRequest req)
{
return getService().getLocale(req);
}
/**
* This method parses the <code>Accept-Language</code> header and
* attempts to create a Locale out of it.
*
* @param languageHeader A String with the language header.
* @return A Locale.
*/
public static Locale getLocale(String languageHeader)
{
return getService().getLocale(languageHeader);
}
/**
* @see org.apache.turbine.services.localization.LocalizationService#getDefaultBundle()
*/
public static String getDefaultBundleName()
{
return getService().getDefaultBundleName();
}
}
1.1 jakarta-turbine-fulcrum/localization/xdocs/changes.xml
Index: changes.xml
===================================================================
<?xml version="1.0"?>
<document>
<properties>
<title>Fulcrum Localization</title>
<author email="epugh@upstate.com">Eric Pugh</author>
</properties>
<body>
<release version="in cvs" date="">
<action dev="epugh" type="add">
New Localization class that is a facade to the underlying service.
</action>
</release>
</body>
</document>
1.1 jakarta-turbine-fulcrum/localization/src/test/org/apache/fulcrum/localization/LocalizationFacadeTest.java
Index: LocalizationFacadeTest.java
===================================================================
package org.apache.fulcrum.localization;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Jetspeed" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache" or
* "Apache Jetspeed", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
// Cactus and Junit imports
import java.util.Locale;
import junit.awtui.TestRunner;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.fulcrum.testcontainer.BaseUnitTest;
/**
* Test the facade class for LocalizationService.
*
* @author <a href="epugh@upstate.com">Eric Pugh</a>
* @version $Id: LocalizationFacadeTest.java,v 1.1 2003/10/14 09:01:54 epugh Exp $
*/
public class LocalizationFacadeTest extends BaseUnitTest
{
/**
* Defines the testcase name for JUnit.
*
* @param name the testcase's name.
*/
public LocalizationFacadeTest(String name)
{
super(name);
}
/**
* Start the tests.
*
* @param args the arguments. Not used
*/
public static void main(String args[])
{
TestRunner.main(new String[] { LocalizationFacadeTest.class.getName()});
}
/**
* Creates the test suite.
*
* @return a test suite (<code>TestSuite</code>) that includes all methods
* starting with "test"
*/
public static Test suite()
{
// All methods starting with "test" will be executed in the test suite.
return new TestSuite(LocalizationFacadeTest.class);
}
public void testFacadeNotConfigured() throws Exception
{
assertFalse(Localization.isInitialized());
try
{
Localization.getString("bill");
}
catch (RuntimeException re)
{
//good;
}
}
public void testFacadeConfigured() throws Exception
{
// this.lookup causes the service to be configured.
this.lookup(LocalizationService.ROLE);
assertTrue(Localization.isInitialized());
String s = Localization.getString(null, new Locale("ko", "KR"), "key4");
assertEquals(
"Unable to retrieve localized text for locale: default",
s,
"value4");
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org