1 /* 2 * Copyright 2007-2011 smartics, Kronseder & Reiner GmbH 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package de.smartics.exceptions.i18n.message; 17 18 import java.util.Locale; 19 import java.util.MissingResourceException; 20 import java.util.ResourceBundle; 21 22 import de.smartics.exceptions.i18n.I18nExceptionContextManager; 23 import de.smartics.exceptions.i18n.MessageComposer; 24 import de.smartics.messages.core.BundleProvider; 25 26 /** 27 * Base implementation of text handlers that format the messages provided for an 28 * exception. 29 * 30 * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a> 31 * @version $Revision:591 $ 32 */ 33 abstract class AbstractTextHandler 34 { 35 // ********************************* Fields ********************************* 36 37 // --- constants ------------------------------------------------------------ 38 39 // --- members -------------------------------------------------------------- 40 41 /** 42 * The localization key to fetch messages from message bundles. If this value 43 * is not set, the {@link String} representation of the provided code instance 44 * is used. 45 */ 46 protected final String resourceKey; 47 48 /** 49 * The fully qualified base name of the bundle to use. 50 * <p> 51 * This bundle base name must never be <code>null</code>. 52 */ 53 protected final String bundleBaseName; 54 55 // ****************************** Initializer ******************************* 56 57 // ****************************** Constructors ****************************** 58 59 /** 60 * Default constructor. 61 * 62 * @param bundleBaseName the fully qualified base name of the bundle to use. 63 * @param resourceKey the localization key to fetch messages from message 64 * bundles. 65 */ 66 protected AbstractTextHandler(final String bundleBaseName, 67 final String resourceKey) 68 { 69 this.bundleBaseName = bundleBaseName; 70 this.resourceKey = resourceKey; 71 } 72 73 // ****************************** Inner Classes ***************************** 74 75 // ********************************* Methods ******************************** 76 77 // --- init ----------------------------------------------------------------- 78 79 // --- get&set -------------------------------------------------------------- 80 81 // --- business ------------------------------------------------------------- 82 83 /** 84 * Returns the structured and localized message for the given locale, 85 * returning each part of information that is provided. 86 * 87 * @param throwable the throwable to construct the localized message. 88 * @param locale the locale for which the message is requested. 89 * @param loader the loader to read the message bundle. 90 * @return returns the localized message of this exception. 91 */ 92 public abstract String getMessage(Throwable throwable, Locale locale, 93 ClassLoader loader); 94 95 protected final String internalGetLocalizedMessage(final Throwable throwable, 96 final String keyPrefix, final Locale locale, 97 final MessageType messageType, final ClassLoader loader) // , 98 { 99 try 100 { 101 return fetchLocalizedMessage(throwable, keyPrefix, locale, messageType, 102 loader); 103 } 104 catch (final MissingResourceException e) 105 { 106 return null; 107 } 108 } 109 110 protected final String fetchLocalizedMessage(final Throwable throwable, 111 final String keyPrefix, final Locale locale, 112 final MessageType messageType, final ClassLoader loader) 113 throws MissingResourceException 114 { 115 final BundleProvider bundleProvider = 116 I18nExceptionContextManager.getBundleProvider(loader); 117 final ResourceBundle bundle = 118 bundleProvider.getBundle(locale, loader, bundleBaseName); 119 final MessageComposer messageComposer = 120 I18nExceptionContextManager.getMessageComposer(loader); 121 final String message = 122 messageComposer.composeMessage(throwable, locale, bundle, keyPrefix, 123 messageType); 124 return message; 125 } 126 127 /** 128 * Returns the localized message for the given locale. 129 * 130 * @param throwable the throwable to construct the localized message. 131 * @param keyPrefix the prefix of the key to load from the bundle. 132 * @param locale the locale for which the message is requested. 133 * @param messageType the type of message to localize. 134 * @param loader the loader to read the message bundle. 135 * @return returns the localized message of this exception. 136 */ 137 protected String getLocalizedMessage(final Throwable throwable, 138 final String keyPrefix, final Locale locale, 139 final MessageType messageType, final ClassLoader loader) 140 { 141 try 142 { 143 return fetchLocalizedMessage(throwable, keyPrefix, locale, messageType, 144 loader); 145 } 146 catch (final MissingResourceException e) 147 { 148 return keyPrefix + '.' + messageType; 149 } 150 } 151 152 // --- object basics -------------------------------------------------------- 153 154 }