2009-07-21 04:39:18 -04:00
|
|
|
/*
|
|
|
|
* DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
|
|
|
|
* Copyright (C) 2009 Mickael Guessant
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
2009-04-17 05:20:11 -04:00
|
|
|
package davmail;
|
|
|
|
|
2009-04-27 19:03:58 -04:00
|
|
|
import davmail.exception.DavMailException;
|
|
|
|
|
2009-04-17 05:20:11 -04:00
|
|
|
import java.text.MessageFormat;
|
2009-04-25 09:13:48 -04:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Locale;
|
2009-04-17 05:20:11 -04:00
|
|
|
import java.util.ResourceBundle;
|
2009-10-12 16:27:50 -04:00
|
|
|
import java.io.Serializable;
|
2009-04-17 05:20:11 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internationalization message.
|
|
|
|
*/
|
2009-10-12 16:27:50 -04:00
|
|
|
public class BundleMessage implements Serializable {
|
2009-09-09 17:48:55 -04:00
|
|
|
/**
|
2009-10-12 16:27:50 -04:00
|
|
|
* Root locale to get english messages for logging.
|
2009-09-09 17:48:55 -04:00
|
|
|
*/
|
2009-04-29 17:02:06 -04:00
|
|
|
public static final Locale ROOT_LOCALE = new Locale("", "");
|
2009-04-25 09:13:48 -04:00
|
|
|
protected static final String MESSAGE_BUNDLE_NAME = "davmailmessages";
|
2009-04-17 05:20:11 -04:00
|
|
|
protected final String key;
|
|
|
|
private final Object[] arguments;
|
|
|
|
|
2009-09-09 17:48:55 -04:00
|
|
|
/**
|
|
|
|
* Internationalization message.
|
|
|
|
*
|
|
|
|
* @param key message key in resource bundle
|
|
|
|
* @param arguments message values
|
|
|
|
*/
|
2009-04-25 09:13:48 -04:00
|
|
|
public BundleMessage(String key, Object... arguments) {
|
2009-04-17 05:20:11 -04:00
|
|
|
this.key = key;
|
|
|
|
this.arguments = arguments;
|
|
|
|
}
|
|
|
|
|
2009-09-09 17:48:55 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Format message with the default locale.
|
|
|
|
*
|
|
|
|
* @return formatted message
|
|
|
|
*/
|
2009-04-29 17:02:06 -04:00
|
|
|
public String format() {
|
|
|
|
return format(null);
|
|
|
|
}
|
|
|
|
|
2009-09-09 17:48:55 -04:00
|
|
|
/**
|
|
|
|
* Format message with the given locale.
|
|
|
|
*
|
|
|
|
* @param locale resource bundle locale
|
|
|
|
* @return formatted message
|
|
|
|
*/
|
2009-04-29 17:02:06 -04:00
|
|
|
public String format(Locale locale) {
|
|
|
|
return BundleMessage.format(locale, key, arguments);
|
|
|
|
}
|
|
|
|
|
2009-09-09 17:48:55 -04:00
|
|
|
/**
|
|
|
|
* Format message for logging (with the root locale).
|
|
|
|
* Log file should remain in english
|
|
|
|
*
|
|
|
|
* @return log formatted message
|
|
|
|
*/
|
2009-04-29 17:02:06 -04:00
|
|
|
public String formatLog() {
|
|
|
|
return format(ROOT_LOCALE);
|
|
|
|
}
|
|
|
|
|
2009-09-09 17:48:55 -04:00
|
|
|
/**
|
|
|
|
* Format message for logging (with the root locale).
|
|
|
|
* Log file should remain in english
|
|
|
|
*
|
|
|
|
* @return log formatted message
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return formatLog();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get bundle for the given locale.
|
|
|
|
* Load the properties file for the given locale in a resource bundle
|
|
|
|
*
|
|
|
|
* @param locale resource bundle locale
|
|
|
|
* @return resource bundle
|
|
|
|
*/
|
2009-04-29 17:02:06 -04:00
|
|
|
protected static ResourceBundle getBundle(Locale locale) {
|
2009-04-25 09:13:48 -04:00
|
|
|
if (locale == null) {
|
|
|
|
return ResourceBundle.getBundle(MESSAGE_BUNDLE_NAME);
|
|
|
|
} else {
|
|
|
|
return ResourceBundle.getBundle(MESSAGE_BUNDLE_NAME, locale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-09 17:48:55 -04:00
|
|
|
/**
|
|
|
|
* Get formatted message for message key and values with the default locale.
|
|
|
|
*
|
|
|
|
* @param key message key in resource bundle
|
|
|
|
* @param arguments message values
|
|
|
|
* @return formatted message
|
|
|
|
*/
|
2009-04-29 17:02:06 -04:00
|
|
|
public static String format(String key, Object... arguments) {
|
|
|
|
return format(null, key, arguments);
|
2009-04-25 09:13:48 -04:00
|
|
|
}
|
|
|
|
|
2009-09-09 17:48:55 -04:00
|
|
|
/**
|
|
|
|
* Get formatted message for message key and values with the given locale.
|
|
|
|
*
|
|
|
|
* @param locale resource bundle locale
|
|
|
|
* @param key message key in resource bundle
|
|
|
|
* @param arguments message values
|
|
|
|
* @return formatted message
|
|
|
|
*/
|
2009-04-29 17:02:06 -04:00
|
|
|
public static String format(Locale locale, String key, Object... arguments) {
|
2009-04-25 09:13:48 -04:00
|
|
|
Object[] formattedArguments = null;
|
|
|
|
if (arguments != null) {
|
|
|
|
formattedArguments = new Object[arguments.length];
|
|
|
|
for (int i = 0; i < arguments.length; i++) {
|
|
|
|
if (arguments[i] instanceof BundleMessage) {
|
|
|
|
formattedArguments[i] = ((BundleMessage) arguments[i]).format(locale);
|
|
|
|
} else if (arguments[i] instanceof BundleMessageList) {
|
|
|
|
StringBuilder buffer = new StringBuilder();
|
2009-04-29 17:02:06 -04:00
|
|
|
for (BundleMessage bundleMessage : (BundleMessageList) arguments[i]) {
|
2009-04-25 09:13:48 -04:00
|
|
|
buffer.append(bundleMessage.format(locale));
|
|
|
|
}
|
|
|
|
formattedArguments[i] = buffer.toString();
|
2009-04-27 19:03:58 -04:00
|
|
|
} else if (arguments[i] instanceof DavMailException) {
|
2009-04-29 17:02:06 -04:00
|
|
|
formattedArguments[i] = ((DavMailException) arguments[i]).getMessage(locale);
|
2009-04-27 19:03:58 -04:00
|
|
|
} else if (arguments[i] instanceof Throwable) {
|
2009-04-29 17:02:06 -04:00
|
|
|
formattedArguments[i] = ((Throwable) arguments[i]).getMessage();
|
2009-04-27 19:03:58 -04:00
|
|
|
if (formattedArguments[i] == null) {
|
|
|
|
formattedArguments[i] = arguments[i].toString();
|
|
|
|
}
|
2009-04-25 09:13:48 -04:00
|
|
|
} else {
|
|
|
|
formattedArguments[i] = arguments[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return MessageFormat.format(getBundle(locale).getString(key), formattedArguments);
|
2009-04-17 05:20:11 -04:00
|
|
|
}
|
|
|
|
|
2009-09-09 17:48:55 -04:00
|
|
|
/**
|
|
|
|
* Get formatted log message for message key and values.
|
|
|
|
* Use the root locale
|
|
|
|
*
|
|
|
|
* @param key message key in resource bundle
|
|
|
|
* @param arguments message values
|
|
|
|
* @return formatted message
|
|
|
|
*/
|
2009-04-29 17:02:06 -04:00
|
|
|
public static String formatLog(String key, Object... arguments) {
|
|
|
|
return format(ROOT_LOCALE, key, arguments);
|
2009-04-17 06:32:11 -04:00
|
|
|
}
|
|
|
|
|
2009-09-09 17:48:55 -04:00
|
|
|
/**
|
|
|
|
* Get formatted error message for bundle message and exception for logging.
|
|
|
|
* Use the root locale
|
|
|
|
*
|
|
|
|
* @param message bundle message
|
|
|
|
* @param e exception
|
|
|
|
* @return formatted message
|
|
|
|
*/
|
2009-04-29 17:02:06 -04:00
|
|
|
public static String getExceptionLogMessage(BundleMessage message, Exception e) {
|
|
|
|
return getExceptionMessage(message, e, ROOT_LOCALE);
|
2009-04-27 19:03:58 -04:00
|
|
|
}
|
|
|
|
|
2009-09-09 17:48:55 -04:00
|
|
|
/**
|
|
|
|
* Get formatted error message for bundle message and exception with default locale.
|
|
|
|
*
|
|
|
|
* @param message bundle message
|
|
|
|
* @param e exception
|
|
|
|
* @return formatted message
|
|
|
|
*/
|
2009-04-29 17:02:06 -04:00
|
|
|
public static String getExceptionMessage(BundleMessage message, Exception e) {
|
|
|
|
return getExceptionMessage(message, e, null);
|
2009-04-27 19:03:58 -04:00
|
|
|
}
|
|
|
|
|
2009-09-09 17:48:55 -04:00
|
|
|
/**
|
|
|
|
* Get formatted error message for bundle message and exception with given locale.
|
|
|
|
*
|
|
|
|
* @param message bundle message
|
|
|
|
* @param e exception
|
|
|
|
* @param locale bundle locale
|
|
|
|
* @return formatted message
|
|
|
|
*/
|
2009-04-29 17:02:06 -04:00
|
|
|
public static String getExceptionMessage(BundleMessage message, Exception e, Locale locale) {
|
|
|
|
StringBuilder buffer = new StringBuilder();
|
|
|
|
if (message != null) {
|
|
|
|
buffer.append(message.format(locale)).append(' ');
|
|
|
|
}
|
|
|
|
if (e instanceof DavMailException) {
|
|
|
|
buffer.append(((DavMailException) e).getMessage(locale));
|
|
|
|
} else if (e.getMessage() != null) {
|
|
|
|
buffer.append(e.getMessage());
|
|
|
|
} else {
|
|
|
|
buffer.append(e.toString());
|
|
|
|
}
|
|
|
|
return buffer.toString();
|
|
|
|
}
|
|
|
|
|
2009-09-09 17:48:55 -04:00
|
|
|
/**
|
|
|
|
* Typed bundle message collection
|
|
|
|
*/
|
2009-04-29 17:02:06 -04:00
|
|
|
public static class BundleMessageList extends ArrayList<BundleMessage> {
|
|
|
|
}
|
2009-04-17 05:20:11 -04:00
|
|
|
}
|