KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mysql > jdbc > Messages


1 /*
2  Copyright (C) 2002-2004 MySQL AB
3
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of version 2 of the GNU General Public License as
6  published by the Free Software Foundation.
7
8  There are special exceptions to the terms and conditions of the GPL
9  as it is applied to this software. View the full text of the
10  exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
11  software distribution.
12
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22
23
24  */

25 package com.mysql.jdbc;
26
27 import java.text.MessageFormat JavaDoc;
28 import java.util.Locale JavaDoc;
29 import java.util.MissingResourceException JavaDoc;
30 import java.util.ResourceBundle JavaDoc;
31
32 /**
33  * Support for localized messages.
34  *
35  * @author Mark Matthews
36  * @version $Id: Messages.java,v 1.1.2.2 2005/05/19 15:52:24 mmatthews Exp $
37  */

38 public class Messages {
39
40     private static final String JavaDoc BUNDLE_NAME = "com.mysql.jdbc.LocalizedErrorMessages"; //$NON-NLS-1$
41

42     private static final ResourceBundle JavaDoc RESOURCE_BUNDLE;
43
44     static {
45         ResourceBundle JavaDoc temp = null;
46
47         //
48
// Overly-pedantic here, some appserver and JVM combos don't deal
49
// well with the no-args version, others don't deal well with
50
// the three-arg version, so we need to try both :(
51
//
52

53         try {
54             temp = ResourceBundle.getBundle(BUNDLE_NAME, Locale.getDefault(),
55                     Messages.class.getClassLoader());
56         } catch (Throwable JavaDoc t) {
57             try {
58                 temp = ResourceBundle.getBundle(BUNDLE_NAME);
59             } catch (Throwable JavaDoc t2) {
60                 throw new RuntimeException JavaDoc(
61                         "Can't load resource bundle due to underlying exception "
62                                 + t.toString());
63             }
64         } finally {
65             RESOURCE_BUNDLE = temp;
66         }
67     }
68
69     /**
70      * Returns the localized message for the given message key
71      *
72      * @param key
73      * the message key
74      * @return The localized message for the key
75      */

76     public static String JavaDoc getString(String JavaDoc key) {
77         if (RESOURCE_BUNDLE == null) {
78             throw new RuntimeException JavaDoc(
79                     "Localized messages from resource bundle '" + BUNDLE_NAME
80                             + "' not loaded during initialization of driver.");
81         }
82
83         try {
84             if (key == null) {
85                 throw new IllegalArgumentException JavaDoc(
86                         "Message key can not be null");
87             }
88
89             String JavaDoc message = RESOURCE_BUNDLE.getString(key);
90
91             if (message == null) {
92                 message = "Missing error message for key '" + key + "'";
93             }
94
95             return message;
96         } catch (MissingResourceException JavaDoc e) {
97             return '!' + key + '!';
98         }
99     }
100
101     public static String JavaDoc getString(String JavaDoc key, Object JavaDoc[] args) {
102         return MessageFormat.format(getString(key), args);
103     }
104
105     /**
106      * Dis-allow construction ...
107      */

108     private Messages() {
109
110         // XXX Auto-generated constructor stub
111
}
112 }
113
Popular Tags