View Javadoc

1   /*
2    * Copyright 2007-2013 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  abstract class AbstractTextHandler
31  {
32    // ********************************* Fields *********************************
33  
34    // --- constants ------------------------------------------------------------
35  
36    // --- members --------------------------------------------------------------
37  
38    /**
39     * The localization key to fetch messages from message bundles. If this value
40     * is not set, the {@link String} representation of the provided code instance
41     * is used.
42     */
43    protected final String resourceKey;
44  
45    /**
46     * The fully qualified base name of the bundle to use.
47     * <p>
48     * This bundle base name must never be <code>null</code>.
49     * </p>
50     */
51    protected final String bundleBaseName;
52  
53    // ****************************** Initializer *******************************
54  
55    // ****************************** Constructors ******************************
56  
57    /**
58     * Default constructor.
59     *
60     * @param bundleBaseName the fully qualified base name of the bundle to use.
61     * @param resourceKey the localization key to fetch messages from message
62     *          bundles.
63     */
64    protected AbstractTextHandler(final String bundleBaseName,
65        final String resourceKey)
66    {
67      this.bundleBaseName = bundleBaseName;
68      this.resourceKey = resourceKey;
69    }
70  
71    // ****************************** Inner Classes *****************************
72  
73    // ********************************* Methods ********************************
74  
75    // --- init -----------------------------------------------------------------
76  
77    // --- get&set --------------------------------------------------------------
78  
79    // --- business -------------------------------------------------------------
80  
81    /**
82     * Returns the structured and localized message for the given locale,
83     * returning each part of information that is provided.
84     *
85     * @param bean the bean to construct the localized message.
86     * @param locale the locale for which the message is requested.
87     * @param loader the loader to read the message bundle.
88     * @return returns the localized message of this exception.
89     */
90    public abstract String getMessage(Object bean, Locale locale,
91        ClassLoader loader);
92  
93    protected final String internalGetLocalizedMessage(final Object throwable,
94        final String keyPrefix, final Locale locale,
95        final MessageType messageType, final ClassLoader loader) // ,
96    {
97      try
98      {
99        return fetchLocalizedMessage(throwable, keyPrefix, locale, messageType,
100           loader);
101     }
102     catch (final MissingResourceException e)
103     {
104       return null;
105     }
106   }
107 
108   protected final String fetchLocalizedMessage(final Object bean,
109       final String keyPrefix, final Locale locale,
110       final MessageType messageType, final ClassLoader loader)
111     throws MissingResourceException
112   {
113     final BundleProvider bundleProvider =
114         I18nExceptionContextManager.getBundleProvider(loader);
115     final ResourceBundle bundle =
116         bundleProvider.getBundle(locale, loader, bundleBaseName);
117     final MessageComposer messageComposer =
118         I18nExceptionContextManager.getMessageComposer(loader);
119     final String message =
120         messageComposer.composeMessage(bean, locale, bundle, keyPrefix,
121             messageType);
122     return message;
123   }
124 
125   /**
126    * Returns the localized message for the given locale.
127    *
128    * @param bean the throwable to construct the localized message.
129    * @param keyPrefix the prefix of the key to load from the bundle.
130    * @param locale the locale for which the message is requested.
131    * @param messageType the type of message to localize.
132    * @param loader the loader to read the message bundle.
133    * @return returns the localized message of this exception.
134    */
135   protected String getLocalizedMessage(final Object bean,
136       final String keyPrefix, final Locale locale,
137       final MessageType messageType, final ClassLoader loader)
138   {
139     try
140     {
141       return fetchLocalizedMessage(bean, keyPrefix, locale, messageType,
142           loader);
143     }
144     catch (final MissingResourceException e)
145     {
146       return keyPrefix + '.' + messageType;
147     }
148   }
149 
150   // --- object basics --------------------------------------------------------
151 
152 }