Java Doc for KeyedMessage.java in  » Portal » jetspeed-2.1.3 » org » apache » jetspeed » i18n » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » jetspeed 2.1.3 » org.apache.jetspeed.i18n 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   org.apache.jetspeed.i18n.KeyedMessage

KeyedMessage
public class KeyedMessage implements Serializable(Code)
KeyedMessage provides an automatically derived i18n message key based on its static instance definition and can be used as comparable constant too.

Purpose

With a KeyedMessage a named constant message (format) can be statically defined which automatically translate themselves for a specific locale using an automatically derived ResourceBundle or even a specified one.

Key derivation

Because KeyedMessages are created with a default message (format), even if no ResourceBundle or its key is defined or can't be found, message translation is still possible.

A KeyedMessage automatically derives the ResourceBundle lookup key from its (statically defined) instance field name using the following format:

  <containingClass.name>.<staticInstanceField.name>

The containingClass is derived at construction time by analyzing the StackTraceElements of a thrown exception. This requires the instance to be defined as a public static field!

At first access, the key is resolved by inspecting the derived containingClass for the declared field defining this instance.

If the KeyedMessage instance wasn't defined as public static field, the key can't be resolved and message translation using a ResourceBundle won't be possible. Translation using the default message will still work though. Furthermore, this instance can't be used as comparable named constant as the KeyedMessage.equals(Object) method will always return false in this case.

Default ResourceBundle name derivation

When the key of a KeyedMessage is resolved, the default ResourceBundle name for message translation is retrieved from the defined public static String field named KeyedMessage.KEYED_MESSAGE_BUNDLE_FIELD_NAME "KEYED_MESSAGE_BUNDLE" defined in its containingClass or one of its superClasses or interfaces.

If this field cannot be found, the fully qualified name of the containingClass is used.

ResourceBundle names are cached in a Map for each containingClass and only derived for the first KeyedMessage defined in a containingClass.

Again: only resolved instances can use a ResourceBundle for message translation.

Default Locale lookup

When a message is translated without a specified Locale, CurrentLocale.get is used to determine the default Locale for the current Thread.

In Jetspeed, the LocalizationValve initializes the CurrentLocale on each request. KeyedMessages accessed within the context of an Jetspeed request therefore will always be translated using the current user Locale with the KeyedMessage.getMessage() or KeyedMessage.toString() methods.

Default ResourceBundle lookup

If a message translation is done using the default ResourceBundle name the ResourceBundle is retrieved using the ClassLoader of the containingClass. This means the bundle(s) must be provided in the same context as from where the containingClass is loaded. Usually (and preferably), this will be from the shared classpath of the webserver.

MessageFormat parameters

MessageFormat patterns can also be used for a KeyedMessage.
With the KeyedMessage.create(Object[]) method a specialized copy of a KeyedMessage instance can be created containing the arguments to be used during message translation.

This new copy remains equals(Object) to its source and can still be used for named constant comparison.

For simplified usage, three KeyedMessage.create(Object) , KeyedMessage.create(Object,Object) and KeyedMessage.create(Object,Object,Object) methods are provided which delegate to KeyedMessage.create(Object[]) with their argument(s) transformed into an Object array.

Extending KeyedMessage

An statically defined KeyedMessage can be used as a "simple" named constant.
If additional metadata is required like some kind of status, level or type indication, the KeyedMessage class can easily be extended by providing a specialized version of the KeyedMessage.create(KeyedMessage,Object[]) copy factory.

Usage

KeyedMessage has been used to replace the hardcoded SecurityException String constants.
The ResourceBundle name used is defined by JetspeedException.KEYED_MESSAGE_BUNDLE which is the superClass of SecurityException .

For a different ResourceBundle to be used for SecurityException messages a KEYED_MESSAGE_BUNDLE field can be defined in SecurityException too, overriding the one in JetspeedException .

Example:

 public class JetspeedException extends Exception {
 public static final String KEYED_MESSAGE_BUNDLE = "org.apache.jetspeed.exception.JetspeedExceptionMessages";
 ...
 public String getMessage() {
 if ( keyedMessage != null ) {
 return keyedMessage.getMessage(); // translated using current Locale and default ResourceBundle
 }
 return super.getMessage();
 }
 }
 public class SecurityException extends JetspeedException {
 public static final KeyedMessage USER_DOES_NOT_EXIST = new KeyedMessage("The user {0} does not exist.");
 ...
 }
 // resource file: org.apache.jetspeed.exception.JetspeedExceptionMessages_nl.properties
 org.apache.jetspeed.security.SecurityException.USER_DOES_NOT_EXIST = De gebruiker {0} bestaat niet.
 ...
 public class UserManagerImpl implements UserManager {
 public User getUser(String username) throws SecurityException {
 ...
 if (null == userPrincipal) { 
 throw new SecurityException(SecurityException.USER_DOES_NOT_EXIST.create(username));
 }
 ...
 }
 ...
 }
 // example get User
 try {
 User user = userManager.getUser(userName);
 } catch (SecurityException sex) {
 if ( SecurityException.USER_DOES_NOT_EXISTS.equals(sex.getKeyedMessage()) {
 // handle USER_DOES_NOT_EXISTS error
 }
 }    
 

author:
   Ate Douma
version:
   $Id: KeyedMessage.java 516448 2007-03-09 16:25:47Z ate $


Field Summary
final public static  StringKEYED_MESSAGE_BUNDLE_FIELD_NAME
     Static String Field name searched for in the class defining a KeyedMessage containing the default resource bundle to use for translation.

Constructor Summary
protected  KeyedMessage(KeyedMessage source, Object[] arguments)
     Constructs a derived KeyedMessage from another KeyedMessage to provide additional message format arguments.
public  KeyedMessage(String message)
     Constructs a new KeyedMessage which will dynamically derive its own KeyedMessage.getKey() .

Method Summary
protected  KeyedMessagecreate(KeyedMessage source, Object[] arguments)
    
public  KeyedMessagecreate(Object[] arguments)
     Creates a derived KeyedMessage from this instance to provide additional message format arguments.
public  KeyedMessagecreate(Object o)
    
public  KeyedMessagecreate(Object o1, Object o2)
    
public  KeyedMessagecreate(Object o1, Object o2, Object o3)
    
public  booleanequals(Object otherObject)
    
public  ObjectgetArgument(int index)
    
public  Object[]getArguments()
    
public  ResourceBundlegetBundle(Locale locale)
     Loads and returns a Locale specific default ResourceBundle for this instance.
public  ResourceBundlegetBundle()
     Loads and returns the default ResourceBundle for this instance using the CurrentLocale.get current Locale .
final public  StringgetKey()
     Dynamically derived key based on the definingClass name, postfixed with the static field name of this instance.
public  StringgetMessage()
    
public  StringgetMessage(ResourceBundle bundle)
    
public  StringgetMessage(Locale locale)
    
public  StringtoString()
    

Field Detail
KEYED_MESSAGE_BUNDLE_FIELD_NAME
final public static String KEYED_MESSAGE_BUNDLE_FIELD_NAME(Code)
Static String Field name searched for in the class defining a KeyedMessage containing the default resource bundle to use for translation.
Note: this Field is looked up using definingClass.getField thus it may also be defined in a superclass or interface of the definingClass.




Constructor Detail
KeyedMessage
protected KeyedMessage(KeyedMessage source, Object[] arguments)(Code)
Constructs a derived KeyedMessage from another KeyedMessage to provide additional message format arguments.
See Also:   KeyedMessage.create(Object[])
Parameters:
  source - the KeyedMessage to derive this instance from
Parameters:
  arguments - this instance specific message format arguments



KeyedMessage
public KeyedMessage(String message)(Code)
Constructs a new KeyedMessage which will dynamically derive its own KeyedMessage.getKey() .
Parameters:
  message - the default message used when the KeyedMessage.getKey()could not be found in the default or aspecified resource bundle.




Method Detail
create
protected KeyedMessage create(KeyedMessage source, Object[] arguments)(Code)
Extendable KeyedMessage factory
Parameters:
  source - the source to copy from
Parameters:
  arguments - the optional message format arguments copied instance with new arguments set



create
public KeyedMessage create(Object[] arguments)(Code)
Creates a derived KeyedMessage from this instance to provide additional message format arguments.
The new instance will be KeyedMessage.equals(Object) to this instance with only different arguments.

Note: the argument objects should be lightweight types and preferably Serializable instances
Parameters:
  arguments - The derived instance specific message format arguments derived KeyedMessage KeyedMessage.equals(Object) equalto this with its own message format arguments



create
public KeyedMessage create(Object o)(Code)
Simplied version of KeyedMessage.create(Object[]) with only one argument
Parameters:
  single - message format argument
See Also:   KeyedMessage.create(Object[]) derived KeyedMessage KeyedMessage.equals(Object) equalto this with its own message format argument



create
public KeyedMessage create(Object o1, Object o2)(Code)
Simplied version of KeyedMessage.create(Object[]) with only two arguments
Parameters:
  single - message format argument
See Also:   KeyedMessage.create(Object[]) derived KeyedMessage KeyedMessage.equals(Object) equalto this with its own message format arguments



create
public KeyedMessage create(Object o1, Object o2, Object o3)(Code)
Simplied version of KeyedMessage.create(Object[]) with only three arguments
Parameters:
  single - message format argument
See Also:   KeyedMessage.create(Object[]) derived KeyedMessage KeyedMessage.equals(Object) equalto this with its own message format arguments



equals
public boolean equals(Object otherObject)(Code)

Parameters:
  otherObject - KeyedMessage instance to compare with true only if otherObject is a KeyedMessage create(Object[]) derivedfrom this instance (or visaversa) and (thus both are) KeyedMessage.resolve() resolved.
See Also:   KeyedMessage.create(Object[])
See Also:   KeyedMessage.resolve()



getArgument
public Object getArgument(int index)(Code)

Parameters:
  index - argument number an argument defined for this KeyedMessage.create(Object[]) derivedinstance



getArguments
public Object[] getArguments()(Code)
the arguments defined for this KeyedMessage.create(Object[]) derivedinstance
See Also:   KeyedMessage.create(Object[])



getBundle
public ResourceBundle getBundle(Locale locale)(Code)
Loads and returns a Locale specific default ResourceBundle for this instance.
If this instance couldn't be KeyedMessage.resolve() resolved or the bundle couldn't be loadednull will be returned.
The ResourceBundle will be loaded using the KeyedMessage.containingClass its ClassLoader.
Parameters:
  locale - the Locale to lookup the locale specific default ResourceBundle a Locale specific default ResourceBundle



getBundle
public ResourceBundle getBundle()(Code)
Loads and returns the default ResourceBundle for this instance using the CurrentLocale.get current Locale .
See Also:   KeyedMessage.getBundle(Locale)
See Also:   CurrentLocale the default ResourceBundle for the current Locale



getKey
final public String getKey()(Code)
Dynamically derived key based on the definingClass name, postfixed with the static field name of this instance.

Format:
  <containingClass.name>.<staticInstanceField.name>

If this instance couldn't be resolved, generic value UNRESOLVED_KEY will have been set. derived key



getMessage
public String getMessage()(Code)
formatted message using the default ResourceBundle using the CurrentLocale current Locale.
See Also:   KeyedMessage.getBundle()



getMessage
public String getMessage(ResourceBundle bundle)(Code)

Parameters:
  bundle - a specific ResourceBundle defining this instance KeyedMessage.getKey() key formatted message using a specific ResourceBundle.



getMessage
public String getMessage(Locale locale)(Code)

Parameters:
  locale - a specific Locale formatted message using the default ResourceBundle using a specific Locale.



toString
public String toString()(Code)
formatted message using the default ResourceBundle using the CurrentLocale current Locale.
See Also:   KeyedMessage.getMessage()



Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.