View Javadoc
1 package org.jdiseq.util; 2 3 import java.util.Locale; 4 import java.util.ResourceBundle; 5 6 /*** 7 * MessageUtil class which holds references to localized messages. 8 * @author Trond Andersen <trondandersen@c2i.net> 9 * @version $Id $ 10 * @since 1.0 11 */ 12 public class MessageUtil { 13 14 /*** Message instance */ 15 private static MessageUtil instance; 16 17 /*** Application's locale */ 18 private static Locale locale; 19 20 /*** Localized messages */ 21 private ResourceBundle msgResource; 22 23 /*** 24 * Initializes the class with the given locale. If this method isn't 25 * run, <code>MessageUtil</code> will use default locale. 26 * @param locale given by the application 27 */ 28 public static void initialize(Locale locale) { 29 MessageUtil.locale = locale; 30 } 31 32 /*** 33 * Initializes the {@link MessageUtil} class. 34 * @return instance of {@link MessageUtil} 35 */ 36 public static MessageUtil getInstance() { 37 if (!MessageUtil.isInitialized()) { 38 if (locale == null) { 39 locale = new Locale("en", "US"); 40 } 41 instance = new MessageUtil("messages", locale); 42 } else { 43 throw new IllegalStateException(instance.getMessage("util.message.initialize.done")); 44 } 45 return instance; 46 } 47 48 /*** 49 * Checks if the <code>MessageUtil</code> class has been initialized 50 * @return <code>true</code> if <code>MessageUtil</code> has been initialized 51 */ 52 private static boolean isInitialized() { 53 return (MessageUtil.instance != null); 54 } 55 56 /*** 57 * Constructor initializing the messages resources. 58 * @param messageFile Name of the file containing messages 59 * @param locale The locale of the application 60 */ 61 MessageUtil(final String messageFile, final Locale locale) { 62 msgResource = createBundle(messageFile, locale); 63 } 64 65 /*** 66 * Retrieved the text message for a given locale. 67 * @param messageKey key for getting the localized messages 68 * @return Localized message 69 * @precondition <code>messageKey != null</code> 70 * @precondition <code>messageKey.trim().length() > 0</code> 71 */ 72 public String getMessage(final String messageKey) { 73 assert (messageKey != null); 74 assert (messageKey.trim().length() > 0); 75 return msgResource.getString(messageKey); 76 } 77 78 /*** 79 * Initializes the bundle towards the message file 80 * @param messageFile name of the file containing the messages 81 * @param locale for the running application 82 * @return Resource having all the messages 83 */ 84 protected ResourceBundle createBundle(final String messageFile, final Locale locale) { 85 return ResourceBundle.getBundle(messageFile, locale); 86 } 87 }

This page was automatically generated by Maven