blob: 460bcd2ce2d48e670f07e2a4dc749e41ef29ae67 [file] [log] [blame]
/**
*
* Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*/
package org.eclipse.osbp.xtext.messagedsl.common;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.eclipse.osbp.ui.api.metadata.IDSLMetadataService;
import org.eclipse.osbp.ui.api.user.IUser;
import org.eclipse.osbp.xtext.messagedsl.listener.MessageBroker;
import org.eclipse.persistence.exceptions.DatabaseException;
import org.eclipse.persistence.exceptions.EclipseLinkException;
import org.eclipse.persistence.internal.databaseaccess.DatabaseCall;
import org.eclipse.persistence.queries.Call;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Message extends Exception{
private static final long serialVersionUID = 8857086767567996406L;
private String fLogFormat;
private String fShowFormat;
private Object[] fParams;
private String fLogMessage;
private String fShowMessage;
private final Logger fLogger;
private static Map<Class<? extends IMessageCategory>, Logger> sLogger = new HashMap<>();
private static Logger getLogger(IMessageCategory category) {
return getLogger(category.getClass());
}
private static Logger getLogger(Class<? extends IMessageCategory> categoryClass) {
if (!sLogger.containsKey(categoryClass)) {
sLogger.put(categoryClass, LoggerFactory.getLogger(categoryClass));
}
return sLogger.get(categoryClass);
}
public Message(String logFormat, String showFormat, Object... params) {
this(null, logFormat, showFormat, params);
}
public Message(Logger logger, String logFormat, String showFormat, Object... params) {
super();
fLogger = logger;
fLogFormat = logFormat;
fShowFormat = showFormat;
fParams = params;
}
private void prepareMessage(IDSLMetadataService dslMetadataService, IUser user) {
Map<String, String> replaces = new HashMap<>();
String variable = null;
if (fParams != null) {
for (Object param : fParams) {
if (variable != null) {
try {
if (param instanceof Boolean) {
replaces.put("%%" + variable + "%%", param.toString());
} else if (param instanceof Class<?>) {
replaces.put("%%" + variable + ".canonicalName%%", ((Class<?>) param).getCanonicalName());
replaces.put("%%" + variable + ".simpleName%%", ((Class<?>) param).getSimpleName());
} else if (param instanceof Double) {
replaces.put("%%" + variable + "%%", param.toString());
} else if (param instanceof Exception) {
if (param instanceof Throwable) {
causedBy((Throwable) param);
}
replaces.put("%%" + variable + ".canonicalName%%", ((Exception) param).getClass().getCanonicalName());
replaces.put("%%" + variable + ".simpleName%%", ((Exception) param).getClass().getSimpleName());
replaces.put("%%" + variable + ".localizedMessage%%", ((Exception) param).getLocalizedMessage());
replaces.put("%%" + variable + ".localizedDetailMessage%%", getLocalizedDetailMessage((Exception) param));
replaces.put("%%" + variable + ".localizedSimpleDetailMessage%%",
getLocalizedSimpleDetailMessage((Exception) param));
replaces.put("%%" + variable + ".stackTrace%%", ExceptionUtils.getStackTrace((Exception) param));
} else if (param instanceof Integer) {
replaces.put("%%" + variable + "%%", param.toString());
} else if (param instanceof String) {
replaces.put("%%" + variable + "%%", param.toString());
} else if (param instanceof Object) {
replaces.put("%%" + variable + ".canonicalName%%", ((Object) param).getClass().getCanonicalName());
replaces.put("%%" + variable + ".simpleName%%", ((Object) param).getClass().getSimpleName());
} else if (param != null) {
try {
replaces.put("%%" + variable + "%%", param.toString());
} catch (Exception e) {
replaces.put("%%" + variable + "%%", "<" + variable + ":unserializable>");
}
}
} catch (Exception e) {
System.err.println(e.getLocalizedMessage());
}
variable = null;
} else if (param instanceof Throwable) {
causedBy((Throwable) param);
} else if (param instanceof String) {
variable = (String) param;
} else {
System.err.println("error handling params"); // NOSONAR
}
}
}
if (dslMetadataService != null) {
fLogMessage = fLogFormat;
fShowMessage = dslMetadataService.translate(user.getLocale().toLanguageTag(), fShowFormat);
} else {
fLogMessage = fLogFormat;
fShowMessage = fShowFormat;
}
for (Entry<String, String> entrySet : replaces.entrySet()) {
fLogMessage = fLogMessage.replaceAll(entrySet.getKey(), entrySet.getValue());
fShowMessage = fShowMessage.replaceAll(entrySet.getKey(), entrySet.getValue());
}
}
public String getLoggerName() {
if (fLogger != null) {
return fLogger.getName();
} else {
return IMessageCategory.class.getCanonicalName();
}
}
@Override
public String getLocalizedMessage() {
return getLogMessage();
}
@Override
public String getMessage() {
return getLogMessage();
}
public String getLocalizedDetailMessage(Exception ex) {
ExceptionMessageHelper exceptionHelper = getExceptionHelper(ex, new ExceptionMessageHelper());
StringBuilder strBuf = new StringBuilder();
if (exceptionHelper.getErrorCode() > 1) {
strBuf.append("Error code: ");
strBuf.append(exceptionHelper.getErrorCode());
}
if (!exceptionHelper.getExceptionTree().isEmpty()) {
strBuf.append("\n");
strBuf.append("Exception tree: ");
strBuf.append(exceptionHelper.getExceptionTree());
}
if (!exceptionHelper.getDetailMessage().isEmpty()) {
strBuf.append("\n");
strBuf.append("Detail message: ");
strBuf.append(exceptionHelper.getDetailMessage());
}
if (!exceptionHelper.getCall().isEmpty()) {
strBuf.append("\n");
strBuf.append("Call: ");
strBuf.append(exceptionHelper.getCall());
}
if (strBuf.toString().isEmpty()) {
strBuf.append("Undefined Error!");
}
return strBuf.toString();
}
public String getLocalizedSimpleDetailMessage(Exception ex) {
ExceptionMessageHelper exceptionHelper = getExceptionHelper(ex, new ExceptionMessageHelper());
StringBuilder strBuf = new StringBuilder();
if (!exceptionHelper.getDetailMessage().isEmpty()) {
strBuf.append(exceptionHelper.getDetailMessage());
}
if (!exceptionHelper.getCall().isEmpty()) {
strBuf.append("\n");
strBuf.append(exceptionHelper.getCall());
}
if (strBuf.toString().isEmpty()) {
strBuf.append("Undefined Error!");
}
return strBuf.toString();
}
private ExceptionMessageHelper getExceptionHelper(Throwable thrble, ExceptionMessageHelper exceptionHelper) {
Throwable cause = thrble.getCause();
String exceptionTree = exceptionHelper.getExceptionTree();
exceptionTree = exceptionTree.concat(thrble.getClass().getSimpleName());
if ((cause != null) && !thrble.equals(cause)) {
exceptionHelper.setExceptionTree(exceptionTree.concat("->"));
exceptionHelper = getExceptionHelper(cause, exceptionHelper);
} else {
exceptionHelper.setExceptionTree(exceptionTree);
exceptionHelper.setDetailMessage(((Exception) thrble).getMessage());
}
if (thrble instanceof DatabaseException) {
Call call = ((DatabaseException) thrble).getCall();
if (call instanceof DatabaseCall) {
exceptionHelper.setCall(((DatabaseCall) call).getSQLString());
}
}
if (thrble instanceof EclipseLinkException) {
exceptionHelper.setErrorCode(((EclipseLinkException) thrble).getErrorCode());
}
return exceptionHelper;
}
public String getLogMessage() {
prepareMessage(null, null);
return fLogMessage;
}
public String getShowMessage(IDSLMetadataService dslMetadataService, IUser user) {
prepareMessage(dslMetadataService, user);
return fShowMessage;
}
public Message causedBy(Throwable causedBy) {
try {
initCause(causedBy);
} catch (Exception e) {
System.err.println(e.getLocalizedMessage()); // NOSONAR
}
return this;
}
public Message showInfo() {
MessageBroker.instance().showInfo(this);
return this;
}
public Message showWarn() {
MessageBroker.instance().showWarn(this);
return this;
}
public Message showError() {
MessageBroker.instance().showError(this);
return this;
}
public Message logTrace() {
if ((fLogger != null) && fLogger.isTraceEnabled()) {
fLogger.trace(getLogMessage());
}
MessageBroker.instance().logTrace(this);
return this;
}
public Message logDebug() {
if ((fLogger != null) && fLogger.isDebugEnabled()) {
fLogger.debug(getLogMessage());
}
MessageBroker.instance().logDebug(this);
return this;
}
public Message logInfo() {
if ((fLogger != null) && fLogger.isInfoEnabled()) {
fLogger.info(getLogMessage());
}
MessageBroker.instance().logInfo(this);
return this;
}
public Message logWarn() {
if ((fLogger != null) && fLogger.isWarnEnabled()) {
fLogger.warn(getLogMessage());
}
MessageBroker.instance().logWarn(this);
return this;
}
public Message logError() {
if ((fLogger != null) && fLogger.isErrorEnabled()) {
fLogger.error(getLogMessage());
}
MessageBroker.instance().logError(this);
return this;
}
public void throwit() throws Throwable {
throw this;
}
private class ExceptionMessageHelper {
private String exceptionTree = "";
private String call = "";
private String detailMessage = "";
private int errorCode = 0;
public String getExceptionTree() {
return exceptionTree;
}
public void setExceptionTree(String exceptionTree) {
this.exceptionTree = exceptionTree;
}
public String getCall() {
return call;
}
public void setCall(String call) {
this.call = call;
}
public int getErrorCode() {
return errorCode;
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
public String getDetailMessage() {
return detailMessage;
}
public void setDetailMessage(String detailMessage) {
this.detailMessage = detailMessage;
}
}
}