/
ExceptionMessageGenerator.java
123 lines (109 loc) · 4.71 KB
/
ExceptionMessageGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* Copyright (c) 1998, 2024 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2024 IBM Corporation. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
// Contributors:
// Oracle - initial API and implementation from Oracle TopLink
// 08/29/2016 Jody Grassel
// - 500441: Eclipselink core has System.getProperty() calls that are not potentially executed under doPriv()
package org.eclipse.persistence.exceptions.i18n;
import org.eclipse.persistence.internal.helper.ConversionManager;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Vector;
/**
* INTERNAL:
* Utility class to generate exception messages using ResourceBundles.
* <p>
* Creation date: (12/7/00 10:30:38 AM)
* @author Rick Barkhouse
*/
public final class ExceptionMessageGenerator {
private final static String CR = System.lineSeparator();
private ExceptionMessageGenerator() {
// for reflection
}
/**
* Return the loader for loading the resource bundles.
*/
public static ClassLoader getLoader() {
ClassLoader loader = ExceptionMessageGenerator.class.getClassLoader();
if (loader == null) {
loader = ConversionManager.getDefaultManager().getLoader();
}
return loader;
}
/**
* Return the message for the given exception class and error number.
*/
public static String buildMessage(Class<?> exceptionClass, int errorNumber, Object[] arguments) {
String shortClassName = exceptionClass.getSimpleName();
String message = "";
ResourceBundle bundle = null;
// JDK 1.1 MessageFormat can't handle null arguments
for (int i = 0; i < arguments.length; i++) {
if (arguments[i] == null) {
arguments[i] = "null";
}
}
bundle = ResourceBundle.getBundle("org.eclipse.persistence.exceptions.i18n." + shortClassName + "Resource", Locale.getDefault(), getLoader());
try {
message = bundle.getString(String.valueOf(errorNumber));
} catch (java.util.MissingResourceException mre) {
// Found bundle, but couldn't find exception translation.
// Get the current language's NoExceptionTranslationForThisLocale message.
bundle = ResourceBundle.getBundle("org.eclipse.persistence.exceptions.i18n.ExceptionResource", Locale.getDefault(), getLoader());
String noTranslationMessage = bundle.getString("NoExceptionTranslationForThisLocale");
Object[] args = { CR };
return format(message, arguments) + format(noTranslationMessage, args);
}
return format(message, arguments);
}
/**
* Return the formatted message for the given exception class and error number.
*/
//Bug#4619864 Catch any exception during formatting and try to throw that exception. One possibility is toString() to an argument
protected static String format(String message, Object[] arguments) {
try {
return MessageFormat.format(message, arguments);
} catch (Exception ex) {
ResourceBundle bundle = null;
bundle = ResourceBundle.getBundle("org.eclipse.persistence.exceptions.i18n.ExceptionResource", Locale.getDefault(), getLoader());
String errorMessage = bundle.getString("ErrorFormattingMessage");
Vector<Object> vec = new Vector<>();
if (arguments != null) {
for (int index = 0; index < arguments.length; index++) {
try {
vec.add(arguments[index].toString());
} catch (Exception ex2) {
vec.add(ex2);
}
}
}
return MessageFormat.format(errorMessage, message, vec);
}
}
/**
* Get one of the generic headers used for the exception's toString().
* <p>
* E.g., "EXCEPTION DESCRIPTION: ", "ERROR CODE: ", etc.
*/
public static String getHeader(String headerLabel) {
ResourceBundle bundle = null;
try {
bundle = ResourceBundle.getBundle("org.eclipse.persistence.exceptions.i18n.ExceptionResource", Locale.getDefault(), getLoader());
return bundle.getString(headerLabel);
} catch (java.util.MissingResourceException mre) {
return "[" + headerLabel + "]";
}
}
}