KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > instance > Localizer


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.instance;
24
25 import com.sun.enterprise.util.i18n.StringManager;
26 import com.sun.enterprise.config.ConfigException;
27
28 /**
29  * The purpose of this class is to create localized ConfigException
30  * objects conveniently. The code is here instead of in ConfigException itself
31  * to avoid passing in a StringManager reference everytime a ConfigException
32  * is thrown -- and the concommitant increase in the number of constructors.
33  * To use this class you create one instance for each calling class so that
34  * the correct properties file is chosen automatically.
35  *
36  * Note that StringManager only uses the *package* name to locate the properties file.
37  * Also note that only this package can access this class. Thus the StringManager
38  * can be shared by all callers of this class.
39  *
40  * @author Byron Nevins
41  */

42
43 class Localizer
44 {
45     private Localizer()
46     {
47     }
48
49     ///////////////////////////////////////////////////////////////////////////
50

51     static String JavaDoc getValue(ExceptionType type)
52     {
53         String JavaDoc key = type.getString();
54         
55         if(localStrings == null)
56             return key;
57         
58         try
59         {
60             return localStrings.getStringWithDefault(key, key);
61         }
62         catch(Exception JavaDoc e)
63         {
64             return key;
65         }
66     }
67
68     ///////////////////////////////////////////////////////////////////////////
69

70     static String JavaDoc getValue(ExceptionType type, String JavaDoc arg1of1)
71     {
72         return getValue(type, new Object JavaDoc[] { arg1of1 });
73     }
74
75     ///////////////////////////////////////////////////////////////////////////
76

77     static String JavaDoc getValue(ExceptionType type, int arg1of1)
78     {
79         return getValue(type, new Object JavaDoc[] { new Integer JavaDoc(arg1of1) });
80     }
81
82     ///////////////////////////////////////////////////////////////////////////
83

84     static String JavaDoc getValue(ExceptionType type, Object JavaDoc[] objs)
85     {
86         if(objs == null || objs.length <= 0)
87             return getValue(type);
88
89         String JavaDoc key = type.getString();
90         
91         if(localStrings == null)
92             return key;
93         
94         try
95         {
96             return localStrings.getStringWithDefault(key, key, objs);
97         }
98         catch(Exception JavaDoc e)
99         {
100             return key;
101         }
102     }
103
104     ///////////////////////////////////////////////////////////////////////////
105

106     private static StringManager localStrings;
107     
108     static
109     {
110         try
111         {
112             localStrings = StringManager.getManager(Localizer.class.getPackage().getName());
113         }
114         catch(Exception JavaDoc e)
115         {
116             //StringManager has already logged the problem...
117
localStrings = null;
118         }
119     }
120 }
121
Popular Tags