author | dfuchs |
Mon, 02 Sep 2013 18:28:50 +0200 | |
changeset 19795 | 6c628e165476 |
parent 16098 | 9001e536ab4e |
child 20745 | 3d2e35965c56 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
7803
diff
changeset
|
2 |
* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.util.logging; |
|
16098 | 27 |
import java.util.ArrayList; |
28 |
import java.util.HashMap; |
|
29 |
import java.util.List; |
|
19795
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
30 |
import java.util.Locale; |
16098 | 31 |
import java.util.Map; |
2 | 32 |
import java.util.ResourceBundle; |
33 |
||
34 |
/** |
|
35 |
* The Level class defines a set of standard logging levels that |
|
36 |
* can be used to control logging output. The logging Level objects |
|
37 |
* are ordered and are specified by ordered integers. Enabling logging |
|
38 |
* at a given level also enables logging at all higher levels. |
|
39 |
* <p> |
|
40 |
* Clients should normally use the predefined Level constants such |
|
41 |
* as Level.SEVERE. |
|
42 |
* <p> |
|
43 |
* The levels in descending order are: |
|
44 |
* <ul> |
|
45 |
* <li>SEVERE (highest value) |
|
46 |
* <li>WARNING |
|
47 |
* <li>INFO |
|
48 |
* <li>CONFIG |
|
49 |
* <li>FINE |
|
50 |
* <li>FINER |
|
51 |
* <li>FINEST (lowest value) |
|
52 |
* </ul> |
|
53 |
* In addition there is a level OFF that can be used to turn |
|
54 |
* off logging, and a level ALL that can be used to enable |
|
55 |
* logging of all messages. |
|
56 |
* <p> |
|
57 |
* It is possible for third parties to define additional logging |
|
58 |
* levels by subclassing Level. In such cases subclasses should |
|
59 |
* take care to chose unique integer level values and to ensure that |
|
60 |
* they maintain the Object uniqueness property across serialization |
|
61 |
* by defining a suitable readResolve method. |
|
62 |
* |
|
63 |
* @since 1.4 |
|
64 |
*/ |
|
65 |
||
66 |
public class Level implements java.io.Serializable { |
|
19795
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
67 |
private static final String defaultBundle = "sun.util.logging.resources.logging"; |
2 | 68 |
|
69 |
/** |
|
70 |
* @serial The non-localized name of the level. |
|
71 |
*/ |
|
72 |
private final String name; |
|
73 |
||
74 |
/** |
|
75 |
* @serial The integer value of the level. |
|
76 |
*/ |
|
77 |
private final int value; |
|
78 |
||
79 |
/** |
|
80 |
* @serial The resource bundle name to be used in localizing the level name. |
|
81 |
*/ |
|
82 |
private final String resourceBundleName; |
|
83 |
||
16098 | 84 |
// localized level name |
19795
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
85 |
private transient String localizedLevelName; |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
86 |
private transient Locale cachedLocale; |
16098 | 87 |
|
2 | 88 |
/** |
89 |
* OFF is a special level that can be used to turn off logging. |
|
90 |
* This level is initialized to <CODE>Integer.MAX_VALUE</CODE>. |
|
91 |
*/ |
|
92 |
public static final Level OFF = new Level("OFF",Integer.MAX_VALUE, defaultBundle); |
|
93 |
||
94 |
/** |
|
95 |
* SEVERE is a message level indicating a serious failure. |
|
96 |
* <p> |
|
97 |
* In general SEVERE messages should describe events that are |
|
98 |
* of considerable importance and which will prevent normal |
|
99 |
* program execution. They should be reasonably intelligible |
|
100 |
* to end users and to system administrators. |
|
101 |
* This level is initialized to <CODE>1000</CODE>. |
|
102 |
*/ |
|
103 |
public static final Level SEVERE = new Level("SEVERE",1000, defaultBundle); |
|
104 |
||
105 |
/** |
|
106 |
* WARNING is a message level indicating a potential problem. |
|
107 |
* <p> |
|
108 |
* In general WARNING messages should describe events that will |
|
109 |
* be of interest to end users or system managers, or which |
|
110 |
* indicate potential problems. |
|
111 |
* This level is initialized to <CODE>900</CODE>. |
|
112 |
*/ |
|
113 |
public static final Level WARNING = new Level("WARNING", 900, defaultBundle); |
|
114 |
||
115 |
/** |
|
116 |
* INFO is a message level for informational messages. |
|
117 |
* <p> |
|
118 |
* Typically INFO messages will be written to the console |
|
119 |
* or its equivalent. So the INFO level should only be |
|
120 |
* used for reasonably significant messages that will |
|
3853 | 121 |
* make sense to end users and system administrators. |
2 | 122 |
* This level is initialized to <CODE>800</CODE>. |
123 |
*/ |
|
124 |
public static final Level INFO = new Level("INFO", 800, defaultBundle); |
|
125 |
||
126 |
/** |
|
127 |
* CONFIG is a message level for static configuration messages. |
|
128 |
* <p> |
|
129 |
* CONFIG messages are intended to provide a variety of static |
|
130 |
* configuration information, to assist in debugging problems |
|
131 |
* that may be associated with particular configurations. |
|
132 |
* For example, CONFIG message might include the CPU type, |
|
133 |
* the graphics depth, the GUI look-and-feel, etc. |
|
134 |
* This level is initialized to <CODE>700</CODE>. |
|
135 |
*/ |
|
136 |
public static final Level CONFIG = new Level("CONFIG", 700, defaultBundle); |
|
137 |
||
138 |
/** |
|
139 |
* FINE is a message level providing tracing information. |
|
140 |
* <p> |
|
141 |
* All of FINE, FINER, and FINEST are intended for relatively |
|
142 |
* detailed tracing. The exact meaning of the three levels will |
|
143 |
* vary between subsystems, but in general, FINEST should be used |
|
144 |
* for the most voluminous detailed output, FINER for somewhat |
|
145 |
* less detailed output, and FINE for the lowest volume (and |
|
146 |
* most important) messages. |
|
147 |
* <p> |
|
148 |
* In general the FINE level should be used for information |
|
149 |
* that will be broadly interesting to developers who do not have |
|
150 |
* a specialized interest in the specific subsystem. |
|
151 |
* <p> |
|
152 |
* FINE messages might include things like minor (recoverable) |
|
153 |
* failures. Issues indicating potential performance problems |
|
154 |
* are also worth logging as FINE. |
|
155 |
* This level is initialized to <CODE>500</CODE>. |
|
156 |
*/ |
|
157 |
public static final Level FINE = new Level("FINE", 500, defaultBundle); |
|
158 |
||
159 |
/** |
|
160 |
* FINER indicates a fairly detailed tracing message. |
|
161 |
* By default logging calls for entering, returning, or throwing |
|
162 |
* an exception are traced at this level. |
|
163 |
* This level is initialized to <CODE>400</CODE>. |
|
164 |
*/ |
|
165 |
public static final Level FINER = new Level("FINER", 400, defaultBundle); |
|
166 |
||
167 |
/** |
|
168 |
* FINEST indicates a highly detailed tracing message. |
|
169 |
* This level is initialized to <CODE>300</CODE>. |
|
170 |
*/ |
|
171 |
public static final Level FINEST = new Level("FINEST", 300, defaultBundle); |
|
172 |
||
173 |
/** |
|
174 |
* ALL indicates that all messages should be logged. |
|
175 |
* This level is initialized to <CODE>Integer.MIN_VALUE</CODE>. |
|
176 |
*/ |
|
177 |
public static final Level ALL = new Level("ALL", Integer.MIN_VALUE, defaultBundle); |
|
178 |
||
179 |
/** |
|
180 |
* Create a named Level with a given integer value. |
|
181 |
* <p> |
|
182 |
* Note that this constructor is "protected" to allow subclassing. |
|
183 |
* In general clients of logging should use one of the constant Level |
|
184 |
* objects such as SEVERE or FINEST. However, if clients need to |
|
185 |
* add new logging levels, they may subclass Level and define new |
|
186 |
* constants. |
|
187 |
* @param name the name of the Level, for example "SEVERE". |
|
188 |
* @param value an integer value for the level. |
|
189 |
* @throws NullPointerException if the name is null |
|
190 |
*/ |
|
191 |
protected Level(String name, int value) { |
|
192 |
this(name, value, null); |
|
193 |
} |
|
194 |
||
195 |
/** |
|
196 |
* Create a named Level with a given integer value and a |
|
197 |
* given localization resource name. |
|
198 |
* <p> |
|
199 |
* @param name the name of the Level, for example "SEVERE". |
|
200 |
* @param value an integer value for the level. |
|
201 |
* @param resourceBundleName name of a resource bundle to use in |
|
202 |
* localizing the given name. If the resourceBundleName is null |
|
203 |
* or an empty string, it is ignored. |
|
204 |
* @throws NullPointerException if the name is null |
|
205 |
*/ |
|
206 |
protected Level(String name, int value, String resourceBundleName) { |
|
207 |
if (name == null) { |
|
208 |
throw new NullPointerException(); |
|
209 |
} |
|
210 |
this.name = name; |
|
211 |
this.value = value; |
|
212 |
this.resourceBundleName = resourceBundleName; |
|
16098 | 213 |
this.localizedLevelName = resourceBundleName == null ? name : null; |
19795
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
214 |
this.cachedLocale = null; |
16098 | 215 |
KnownLevel.add(this); |
2 | 216 |
} |
217 |
||
218 |
/** |
|
219 |
* Return the level's localization resource bundle name, or |
|
220 |
* null if no localization bundle is defined. |
|
221 |
* |
|
222 |
* @return localization resource bundle name |
|
223 |
*/ |
|
224 |
public String getResourceBundleName() { |
|
225 |
return resourceBundleName; |
|
226 |
} |
|
227 |
||
228 |
/** |
|
229 |
* Return the non-localized string name of the Level. |
|
230 |
* |
|
231 |
* @return non-localized name |
|
232 |
*/ |
|
233 |
public String getName() { |
|
234 |
return name; |
|
235 |
} |
|
236 |
||
237 |
/** |
|
238 |
* Return the localized string name of the Level, for |
|
239 |
* the current default locale. |
|
240 |
* <p> |
|
241 |
* If no localization information is available, the |
|
242 |
* non-localized name is returned. |
|
243 |
* |
|
244 |
* @return localized name |
|
245 |
*/ |
|
246 |
public String getLocalizedName() { |
|
16098 | 247 |
return getLocalizedLevelName(); |
248 |
} |
|
249 |
||
250 |
// package-private getLevelName() is used by the implementation |
|
251 |
// instead of getName() to avoid calling the subclass's version |
|
252 |
final String getLevelName() { |
|
253 |
return this.name; |
|
254 |
} |
|
255 |
||
19795
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
256 |
private String computeLocalizedLevelName(Locale newLocale) { |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
257 |
ResourceBundle rb = ResourceBundle.getBundle(resourceBundleName, newLocale); |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
258 |
final String localizedName = rb.getString(name); |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
259 |
|
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
260 |
final boolean isDefaultBundle = defaultBundle.equals(resourceBundleName); |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
261 |
if (!isDefaultBundle) return localizedName; |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
262 |
|
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
263 |
// This is a trick to determine whether the name has been translated |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
264 |
// or not. If it has not been translated, we need to use Locale.ROOT |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
265 |
// when calling toUpperCase(). |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
266 |
final Locale rbLocale = rb.getLocale(); |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
267 |
final Locale locale = |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
268 |
Locale.ROOT.equals(rbLocale) |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
269 |
|| name.equals(localizedName.toUpperCase(Locale.ROOT)) |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
270 |
? Locale.ROOT : rbLocale; |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
271 |
|
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
272 |
// ALL CAPS in a resource bundle's message indicates no translation |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
273 |
// needed per Oracle translation guideline. To workaround this |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
274 |
// in Oracle JDK implementation, convert the localized level name |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
275 |
// to uppercase for compatibility reason. |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
276 |
return Locale.ROOT.equals(locale) ? name : localizedName.toUpperCase(locale); |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
277 |
} |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
278 |
|
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
279 |
// Avoid looking up the localizedLevelName twice if we already |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
280 |
// have it. |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
281 |
final String getCachedLocalizedLevelName() { |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
282 |
|
16098 | 283 |
if (localizedLevelName != null) { |
19795
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
284 |
if (cachedLocale != null) { |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
285 |
if (cachedLocale.equals(Locale.getDefault())) { |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
286 |
// OK: our cached value was looked up with the same |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
287 |
// locale. We can use it. |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
288 |
return localizedLevelName; |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
289 |
} |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
290 |
} |
16098 | 291 |
} |
292 |
||
19795
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
293 |
if (resourceBundleName == null) { |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
294 |
// No resource bundle: just use the name. |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
295 |
return name; |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
296 |
} |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
297 |
|
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
298 |
// We need to compute the localized name. |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
299 |
// Either because it's the first time, or because our cached |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
300 |
// value is for a different locale. Just return null. |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
301 |
return null; |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
302 |
} |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
303 |
|
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
304 |
final synchronized String getLocalizedLevelName() { |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
305 |
|
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
306 |
// See if we have a cached localized name |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
307 |
final String cachedLocalizedName = getCachedLocalizedLevelName(); |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
308 |
if (cachedLocalizedName != null) { |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
309 |
return cachedLocalizedName; |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
310 |
} |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
311 |
|
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
312 |
// No cached localized name or cache invalid. |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
313 |
// Need to compute the localized name. |
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
314 |
final Locale newLocale = Locale.getDefault(); |
2 | 315 |
try { |
19795
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
316 |
localizedLevelName = computeLocalizedLevelName(newLocale); |
2 | 317 |
} catch (Exception ex) { |
16098 | 318 |
localizedLevelName = name; |
319 |
} |
|
19795
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
320 |
cachedLocale = newLocale; |
16098 | 321 |
return localizedLevelName; |
322 |
} |
|
323 |
||
324 |
// Returns a mirrored Level object that matches the given name as |
|
325 |
// specified in the Level.parse method. Returns null if not found. |
|
326 |
// |
|
327 |
// It returns the same Level object as the one returned by Level.parse |
|
328 |
// method if the given name is a non-localized name or integer. |
|
329 |
// |
|
330 |
// If the name is a localized name, findLevel and parse method may |
|
331 |
// return a different level value if there is a custom Level subclass |
|
332 |
// that overrides Level.getLocalizedName() to return a different string |
|
333 |
// than what's returned by the default implementation. |
|
334 |
// |
|
335 |
static Level findLevel(String name) { |
|
336 |
if (name == null) { |
|
337 |
throw new NullPointerException(); |
|
2 | 338 |
} |
16098 | 339 |
|
340 |
KnownLevel level; |
|
341 |
||
342 |
// Look for a known Level with the given non-localized name. |
|
343 |
level = KnownLevel.findByName(name); |
|
344 |
if (level != null) { |
|
345 |
return level.mirroredLevel; |
|
346 |
} |
|
347 |
||
348 |
// Now, check if the given name is an integer. If so, |
|
349 |
// first look for a Level with the given value and then |
|
350 |
// if necessary create one. |
|
351 |
try { |
|
352 |
int x = Integer.parseInt(name); |
|
353 |
level = KnownLevel.findByValue(x); |
|
354 |
if (level == null) { |
|
355 |
// add new Level |
|
356 |
Level levelObject = new Level(name, x); |
|
357 |
level = KnownLevel.findByValue(x); |
|
358 |
} |
|
359 |
return level.mirroredLevel; |
|
360 |
} catch (NumberFormatException ex) { |
|
361 |
// Not an integer. |
|
362 |
// Drop through. |
|
363 |
} |
|
364 |
||
365 |
level = KnownLevel.findByLocalizedLevelName(name); |
|
366 |
if (level != null) { |
|
367 |
return level.mirroredLevel; |
|
368 |
} |
|
369 |
||
370 |
return null; |
|
2 | 371 |
} |
372 |
||
373 |
/** |
|
3853 | 374 |
* Returns a string representation of this Level. |
375 |
* |
|
2 | 376 |
* @return the non-localized name of the Level, for example "INFO". |
377 |
*/ |
|
19795
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
378 |
@Override |
2 | 379 |
public final String toString() { |
380 |
return name; |
|
381 |
} |
|
382 |
||
383 |
/** |
|
384 |
* Get the integer value for this level. This integer value |
|
385 |
* can be used for efficient ordering comparisons between |
|
386 |
* Level objects. |
|
387 |
* @return the integer value for this level. |
|
388 |
*/ |
|
389 |
public final int intValue() { |
|
390 |
return value; |
|
391 |
} |
|
392 |
||
393 |
private static final long serialVersionUID = -8176160795706313070L; |
|
394 |
||
395 |
// Serialization magic to prevent "doppelgangers". |
|
396 |
// This is a performance optimization. |
|
397 |
private Object readResolve() { |
|
16098 | 398 |
KnownLevel o = KnownLevel.matches(this); |
399 |
if (o != null) { |
|
400 |
return o.levelObject; |
|
2 | 401 |
} |
16098 | 402 |
|
403 |
// Woops. Whoever sent us this object knows |
|
404 |
// about a new log level. Add it to our list. |
|
405 |
Level level = new Level(this.name, this.value, this.resourceBundleName); |
|
406 |
return level; |
|
2 | 407 |
} |
408 |
||
409 |
/** |
|
410 |
* Parse a level name string into a Level. |
|
411 |
* <p> |
|
412 |
* The argument string may consist of either a level name |
|
413 |
* or an integer value. |
|
414 |
* <p> |
|
415 |
* For example: |
|
416 |
* <ul> |
|
417 |
* <li> "SEVERE" |
|
418 |
* <li> "1000" |
|
419 |
* </ul> |
|
16098 | 420 |
* |
2 | 421 |
* @param name string to be parsed |
422 |
* @throws NullPointerException if the name is null |
|
423 |
* @throws IllegalArgumentException if the value is not valid. |
|
424 |
* Valid values are integers between <CODE>Integer.MIN_VALUE</CODE> |
|
425 |
* and <CODE>Integer.MAX_VALUE</CODE>, and all known level names. |
|
3853 | 426 |
* Known names are the levels defined by this class (e.g., <CODE>FINE</CODE>, |
2 | 427 |
* <CODE>FINER</CODE>, <CODE>FINEST</CODE>), or created by this class with |
428 |
* appropriate package access, or new levels defined or created |
|
429 |
* by subclasses. |
|
430 |
* |
|
431 |
* @return The parsed value. Passing an integer that corresponds to a known name |
|
3853 | 432 |
* (e.g., 700) will return the associated name (e.g., <CODE>CONFIG</CODE>). |
433 |
* Passing an integer that does not (e.g., 1) will return a new level name |
|
2 | 434 |
* initialized to that value. |
435 |
*/ |
|
436 |
public static synchronized Level parse(String name) throws IllegalArgumentException { |
|
437 |
// Check that name is not null. |
|
438 |
name.length(); |
|
439 |
||
16098 | 440 |
KnownLevel level; |
441 |
||
2 | 442 |
// Look for a known Level with the given non-localized name. |
16098 | 443 |
level = KnownLevel.findByName(name); |
444 |
if (level != null) { |
|
445 |
return level.levelObject; |
|
2 | 446 |
} |
447 |
||
448 |
// Now, check if the given name is an integer. If so, |
|
449 |
// first look for a Level with the given value and then |
|
450 |
// if necessary create one. |
|
451 |
try { |
|
452 |
int x = Integer.parseInt(name); |
|
16098 | 453 |
level = KnownLevel.findByValue(x); |
454 |
if (level == null) { |
|
455 |
// add new Level |
|
456 |
Level levelObject = new Level(name, x); |
|
457 |
level = KnownLevel.findByValue(x); |
|
2 | 458 |
} |
16098 | 459 |
return level.levelObject; |
2 | 460 |
} catch (NumberFormatException ex) { |
461 |
// Not an integer. |
|
462 |
// Drop through. |
|
463 |
} |
|
464 |
||
465 |
// Finally, look for a known level with the given localized name, |
|
466 |
// in the current default locale. |
|
467 |
// This is relatively expensive, but not excessively so. |
|
16098 | 468 |
level = KnownLevel.findByLocalizedName(name); |
469 |
if (level != null) { |
|
470 |
return level.levelObject; |
|
2 | 471 |
} |
472 |
||
473 |
// OK, we've tried everything and failed |
|
474 |
throw new IllegalArgumentException("Bad level \"" + name + "\""); |
|
475 |
} |
|
476 |
||
477 |
/** |
|
478 |
* Compare two objects for value equality. |
|
479 |
* @return true if and only if the two objects have the same level value. |
|
480 |
*/ |
|
19795
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
481 |
@Override |
2 | 482 |
public boolean equals(Object ox) { |
483 |
try { |
|
484 |
Level lx = (Level)ox; |
|
485 |
return (lx.value == this.value); |
|
486 |
} catch (Exception ex) { |
|
487 |
return false; |
|
488 |
} |
|
489 |
} |
|
490 |
||
491 |
/** |
|
492 |
* Generate a hashcode. |
|
493 |
* @return a hashcode based on the level value |
|
494 |
*/ |
|
19795
6c628e165476
8016127: NLS: logging.properties translatability recommendation
dfuchs
parents:
16098
diff
changeset
|
495 |
@Override |
2 | 496 |
public int hashCode() { |
497 |
return this.value; |
|
498 |
} |
|
16098 | 499 |
|
500 |
// KnownLevel class maintains the global list of all known levels. |
|
501 |
// The API allows multiple custom Level instances of the same name/value |
|
502 |
// be created. This class provides convenient methods to find a level |
|
503 |
// by a given name, by a given value, or by a given localized name. |
|
504 |
// |
|
505 |
// KnownLevel wraps the following Level objects: |
|
506 |
// 1. levelObject: standard Level object or custom Level object |
|
507 |
// 2. mirroredLevel: Level object representing the level specified in the |
|
508 |
// logging configuration. |
|
509 |
// |
|
510 |
// Level.getName, Level.getLocalizedName, Level.getResourceBundleName methods |
|
511 |
// are non-final but the name and resource bundle name are parameters to |
|
512 |
// the Level constructor. Use the mirroredLevel object instead of the |
|
513 |
// levelObject to prevent the logging framework to execute foreign code |
|
514 |
// implemented by untrusted Level subclass. |
|
515 |
// |
|
516 |
// Implementation Notes: |
|
517 |
// If Level.getName, Level.getLocalizedName, Level.getResourceBundleName methods |
|
518 |
// were final, the following KnownLevel implementation can be removed. |
|
519 |
// Future API change should take this into consideration. |
|
520 |
static final class KnownLevel { |
|
521 |
private static Map<String, List<KnownLevel>> nameToLevels = new HashMap<>(); |
|
522 |
private static Map<Integer, List<KnownLevel>> intToLevels = new HashMap<>(); |
|
523 |
final Level levelObject; // instance of Level class or Level subclass |
|
524 |
final Level mirroredLevel; // instance of Level class |
|
525 |
KnownLevel(Level l) { |
|
526 |
this.levelObject = l; |
|
527 |
if (l.getClass() == Level.class) { |
|
528 |
this.mirroredLevel = l; |
|
529 |
} else { |
|
530 |
this.mirroredLevel = new Level(l.name, l.value, l.resourceBundleName); |
|
531 |
} |
|
532 |
} |
|
533 |
||
534 |
static synchronized void add(Level l) { |
|
535 |
// the mirroredLevel object is always added to the list |
|
536 |
// before the custom Level instance |
|
537 |
KnownLevel o = new KnownLevel(l); |
|
538 |
List<KnownLevel> list = nameToLevels.get(l.name); |
|
539 |
if (list == null) { |
|
540 |
list = new ArrayList<>(); |
|
541 |
nameToLevels.put(l.name, list); |
|
542 |
} |
|
543 |
list.add(o); |
|
544 |
||
545 |
list = intToLevels.get(l.value); |
|
546 |
if (list == null) { |
|
547 |
list = new ArrayList<>(); |
|
548 |
intToLevels.put(l.value, list); |
|
549 |
} |
|
550 |
list.add(o); |
|
551 |
} |
|
552 |
||
553 |
// Returns a KnownLevel with the given non-localized name. |
|
554 |
static synchronized KnownLevel findByName(String name) { |
|
555 |
List<KnownLevel> list = nameToLevels.get(name); |
|
556 |
if (list != null) { |
|
557 |
return list.get(0); |
|
558 |
} |
|
559 |
return null; |
|
560 |
} |
|
561 |
||
562 |
// Returns a KnownLevel with the given value. |
|
563 |
static synchronized KnownLevel findByValue(int value) { |
|
564 |
List<KnownLevel> list = intToLevels.get(value); |
|
565 |
if (list != null) { |
|
566 |
return list.get(0); |
|
567 |
} |
|
568 |
return null; |
|
569 |
} |
|
570 |
||
571 |
// Returns a KnownLevel with the given localized name matching |
|
572 |
// by calling the Level.getLocalizedLevelName() method (i.e. found |
|
573 |
// from the resourceBundle associated with the Level object). |
|
574 |
// This method does not call Level.getLocalizedName() that may |
|
575 |
// be overridden in a subclass implementation |
|
576 |
static synchronized KnownLevel findByLocalizedLevelName(String name) { |
|
577 |
for (List<KnownLevel> levels : nameToLevels.values()) { |
|
578 |
for (KnownLevel l : levels) { |
|
579 |
String lname = l.levelObject.getLocalizedLevelName(); |
|
580 |
if (name.equals(lname)) { |
|
581 |
return l; |
|
582 |
} |
|
583 |
} |
|
584 |
} |
|
585 |
return null; |
|
586 |
} |
|
587 |
||
588 |
// Returns a KnownLevel with the given localized name matching |
|
589 |
// by calling the Level.getLocalizedName() method |
|
590 |
static synchronized KnownLevel findByLocalizedName(String name) { |
|
591 |
for (List<KnownLevel> levels : nameToLevels.values()) { |
|
592 |
for (KnownLevel l : levels) { |
|
593 |
String lname = l.levelObject.getLocalizedName(); |
|
594 |
if (name.equals(lname)) { |
|
595 |
return l; |
|
596 |
} |
|
597 |
} |
|
598 |
} |
|
599 |
return null; |
|
600 |
} |
|
601 |
||
602 |
static synchronized KnownLevel matches(Level l) { |
|
603 |
List<KnownLevel> list = nameToLevels.get(l.name); |
|
604 |
if (list != null) { |
|
605 |
for (KnownLevel level : list) { |
|
606 |
Level other = level.mirroredLevel; |
|
607 |
if (l.value == other.value && |
|
608 |
(l.resourceBundleName == other.resourceBundleName || |
|
609 |
(l.resourceBundleName != null && |
|
610 |
l.resourceBundleName.equals(other.resourceBundleName)))) { |
|
611 |
return level; |
|
612 |
} |
|
613 |
} |
|
614 |
} |
|
615 |
return null; |
|
616 |
} |
|
617 |
} |
|
618 |
||
2 | 619 |
} |