KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > i18n > CmsMultiMessages


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/i18n/CmsMultiMessages.java,v $
3  * Date : $Date: 2006/03/27 14:53:01 $
4  * Version: $Revision: 1.16 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.i18n;
33
34 import org.opencms.main.CmsIllegalArgumentException;
35 import org.opencms.main.CmsLog;
36
37 import java.util.ArrayList JavaDoc;
38 import java.util.Hashtable JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.Locale JavaDoc;
42 import java.util.Map JavaDoc;
43
44 import org.apache.commons.logging.Log;
45
46 /**
47  * Provides access to the localized messages for several resource bundles simultaneously.<p>
48  *
49  * Messages are cached for faster lookup. If a localized key is contained in more then one resource bundle,
50  * it will be used only from the resource bundle where it was first found in. The resource bundle order is undefined. It is therefore
51  * recommended to ensure the uniqueness of all module keys by placing a special prefix in front of all keys of a resource bundle.<p>
52  *
53  * @author Alexander Kandzior
54  * @author Michael Moossen
55  *
56  * @version $Revision: 1.16 $
57  *
58  * @since 6.0.0
59  */

60 public class CmsMultiMessages extends CmsMessages {
61
62     /** Constant for the multi bundle name. */
63     public static final String JavaDoc MULTI_BUNDLE_NAME = CmsMultiMessages.class.getName();
64
65     /** Null String value for caching of null message results. */
66     public static final String JavaDoc NULL_STRING = "null";
67
68     /** Static reference to the log. */
69     private static final Log LOG = CmsLog.getLog(CmsMultiMessages.class);
70
71     /** A cache for the messages to prevent multiple lookups in many bundles. */
72     private Map JavaDoc m_messageCache;
73
74     /** List of resource bundles from the installed modules. */
75     private List JavaDoc m_messages;
76
77     /**
78      * Constructor for creating a new messages object initialized with the given locale.<p>
79      *
80      * @param locale the locale to use for localization of the messages
81      */

82     public CmsMultiMessages(Locale JavaDoc locale) {
83
84         super();
85         // set the bundle name and the locale
86
setBundleName(CmsMultiMessages.MULTI_BUNDLE_NAME);
87         setLocale(locale);
88         // generate array for the messages
89
m_messages = new ArrayList JavaDoc();
90         // use "old" Hashtable since it is the most efficient synchronized HashMap implementation
91
m_messageCache = new Hashtable JavaDoc();
92     }
93
94     /**
95      * Adds a bundle instance to this multi message bundle.<p>
96      *
97      * The added bundle will be localized with the locale of this multi message bundle.<p>
98      *
99      * @param bundle the bundle instance to add
100      */

101     public void addBundle(I_CmsMessageBundle bundle) {
102
103         // add the localized bundle to the messages
104
addMessages(bundle.getBundle(getLocale()));
105     }
106
107     /**
108      * Adds a messages instance to this multi message bundle.<p>
109      *
110      * The messages instance should have been initialized with the same locale as this multi bundle,
111      * if not, the locale of the messages instance is automatically replaced. However, this will not work
112      * if the added messages instance is in face also of type <code>{@link CmsMultiMessages}</code>.<p>
113      *
114      * @param messages the messages instance to add
115      *
116      * @throws CmsIllegalArgumentException if the locale of the given <code>{@link CmsMultiMessages}</code> does not match the locale of this multi messages
117      */

118     public void addMessages(CmsMessages messages) throws CmsIllegalArgumentException {
119
120         Locale JavaDoc locale = messages.getLocale();
121         if (!getLocale().equals(locale)) {
122             // not the same locale, try to change the locale if this is a simple CmsMessage object
123
if (!(messages instanceof CmsMultiMessages)) {
124                 // match locale of multi bundle
125
String JavaDoc bundleName = messages.getBundleName();
126                 messages = new CmsMessages(bundleName, getLocale());
127             } else {
128                 // multi bundles with wrong locales can't be added this way
129
throw new CmsIllegalArgumentException(Messages.get().container(
130                     Messages.ERR_MULTIMSG_LOCALE_DOES_NOT_MATCH_2,
131                     messages.getLocale(),
132                     getLocale()));
133             }
134         }
135         if (!m_messages.contains(messages)) {
136             if ((m_messageCache != null) && (m_messageCache.size() > 0)) {
137                 // cache has already been used, must flush because of newly added keys
138
m_messageCache = new Hashtable JavaDoc();
139             }
140             m_messages.add(messages);
141         }
142     }
143
144     /**
145      * Adds a list a messages instances to this multi message bundle.<p>
146      *
147      * @param messages the messages instance to add
148      */

149     public void addMessages(List JavaDoc messages) {
150
151         if (messages == null) {
152             throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_MULTIMSG_EMPTY_LIST_0));
153         }
154
155         Iterator JavaDoc i = messages.iterator();
156         while (i.hasNext()) {
157             addMessages((CmsMessages)i.next());
158         }
159     }
160
161     /**
162      * Returns the list of all individual message objects in this multi message instance.<p>
163      *
164      * @return the list of all individual message objects in this multi message instance
165      */

166     public List JavaDoc getMessages() {
167
168         return m_messages;
169     }
170
171     /**
172      * @see org.opencms.i18n.CmsMessages#getString(java.lang.String)
173      */

174     public String JavaDoc getString(String JavaDoc keyName) {
175
176         return resolveKey(keyName);
177     }
178
179     /**
180      * @see org.opencms.i18n.CmsMessages#isInitialized()
181      */

182     public boolean isInitialized() {
183
184         return (m_messages != null) && !m_messages.isEmpty();
185     }
186
187     /**
188      * @see org.opencms.i18n.CmsMessages#key(java.lang.String, boolean)
189      */

190     public String JavaDoc key(String JavaDoc keyName, boolean allowNull) {
191
192         // special implementation since we uses several bundles for the messages
193
String JavaDoc result = resolveKey(keyName);
194         if ((result == null) && !allowNull) {
195             result = formatUnknownKey(keyName);
196         }
197         return result;
198     }
199
200     /**
201      * Returns the localized resource string for a given message key,
202      * checking the workplace default resources and all module bundles.<p>
203      *
204      * If the key was not found, <code>null</code> is returned.<p>
205      *
206      * @param keyName the key for the desired string
207      * @return the resource string for the given key or null if not found
208      */

209     private String JavaDoc resolveKey(String JavaDoc keyName) {
210
211         if (LOG.isDebugEnabled()) {
212             LOG.debug(Messages.get().getBundle().key(Messages.LOG_RESOLVE_MESSAGE_KEY_1, keyName));
213         }
214
215         String JavaDoc result = (String JavaDoc)m_messageCache.get(keyName);
216         if (result == NULL_STRING) {
217             // key was already checked and not found
218
return null;
219         }
220         if (result == null) {
221             // so far not in the cache
222
for (int i = 0; (result == null) && (i < m_messages.size()); i++) {
223                 try {
224                     result = ((CmsMessages)m_messages.get(i)).getString(keyName);
225                     // if no exception is thrown here we have found the result
226
} catch (CmsMessageException e) {
227                     // can usually be ignored
228
if (LOG.isDebugEnabled()) {
229                         LOG.debug(e.getMessage(), e);
230                     }
231                 }
232             }
233         } else {
234             // result was found in cache
235
if (LOG.isDebugEnabled()) {
236                 LOG.debug(Messages.get().getBundle().key(Messages.LOG_MESSAGE_KEY_FOUND_CACHED_2, keyName, result));
237             }
238             return result;
239         }
240         if (result == null) {
241             // key was not found in "regular" bundle as well as module messages
242
if (LOG.isDebugEnabled()) {
243                 LOG.debug(Messages.get().getBundle().key(Messages.LOG_MESSAGE_KEY_NOT_FOUND_1, keyName));
244             }
245             // ensure null values are also cached
246
m_messageCache.put(keyName, NULL_STRING);
247         } else {
248             // optional debug output
249
if (LOG.isDebugEnabled()) {
250                 LOG.debug(Messages.get().getBundle().key(Messages.LOG_MESSAGE_KEY_FOUND_2, keyName, result));
251             }
252             // cache the result
253
m_messageCache.put(keyName, result);
254         }
255         // return the result
256
return result;
257     }
258 }
Popular Tags