CorbaResourceUtil.java revision 608:7e06bf1dcb09
1/*
2 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.corba.se.impl.orbutil;
27
28import java.util.ResourceBundle;
29import java.util.MissingResourceException;
30
31public class CorbaResourceUtil {
32    public static String getString(String key) {
33        if (!resourcesInitialized) {
34            initResources();
35        }
36
37        try {
38            return resources.getString(key);
39        } catch (MissingResourceException ignore) {
40        }
41        return null;
42    }
43
44    public static String getText(String key) {
45        String message = getString(key);
46        if (message == null) {
47            message = "no text found: \"" + key + "\"";
48        }
49        return message;
50    }
51
52    public static String getText(String key, int num) {
53        return getText(key, Integer.toString(num), null, null);
54    }
55
56    public static String getText(String key, String arg0) {
57        return getText(key, arg0, null, null);
58    }
59
60    public static String getText(String key, String arg0, String arg1) {
61        return getText(key, arg0, arg1, null);
62    }
63
64    public static String getText(String key,
65                                 String arg0, String arg1, String arg2)
66    {
67        String format = getString(key);
68        if (format == null) {
69            format = "no text found: key = \"" + key + "\", " +
70                "arguments = \"{0}\", \"{1}\", \"{2}\"";
71        }
72
73        String[] args = new String[3];
74        args[0] = (arg0 != null ? arg0.toString() : "null");
75        args[1] = (arg1 != null ? arg1.toString() : "null");
76        args[2] = (arg2 != null ? arg2.toString() : "null");
77
78        return java.text.MessageFormat.format(format, (Object[]) args);
79    }
80
81    private static boolean resourcesInitialized = false;
82    private static ResourceBundle resources;
83
84    private static void initResources() {
85        try {
86            resources =
87                ResourceBundle.getBundle("com.sun.corba.se.impl.orbutil.resources.sunorb");
88            resourcesInitialized = true;
89        } catch (MissingResourceException e) {
90            throw new Error("fatal: missing resource bundle: " +
91                            e.getClassName());
92        }
93    }
94
95}
96