org.eclipse.persistence.exceptions.i18n.ExceptionMessageGenerator Maven / Gradle / Ivy
Show all versions of eclipselink Show documentation
/*
* 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.
*
* 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