NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

/*
* System Name: Exception Handling Component
*
* Class Name: BaseException
*
* Purpose: (1) Create a BaseException class to be extended by BusinessException
* and TechnicalException classes. (2) Provide get/set methods to the calling
* application to access the property attributes of the base class. (3) Convert
* the data from the exception class into a XML String format, and return the
* same to the calling application. The calling application also has the option
* of getting the Stack Trace of the exception occurred.
*
*/

package com.marutisuzuki.platform.exception.core;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import com.marutisuzuki.platform.exception.constants.ExceptionHandlingConstants;
import com.marutisuzuki.platform.exception.util.ExceptionHelper;
import com.marutisuzuki.platform.exception.util.ExceptionUtil;

/**
* BaseException is the base class for providing the framework for checked exception handling. It provides the facility
* to access its properties and also get a XML output of the exception occurred.This class is not exposed to the calling
* application, and can only be accessed by either the BusinessException or TechnicalException classes which in turn
* need to be extended by application specific exceptions.
*
* @version 1.0
* @author Nagarro
*/

public class BaseException extends Exception {

// Type to distinguish exceptions
private static final long serialVersionUID = Long.MAX_VALUE;
// Attributes to store exception details
private final String code;
private Integer severityLevel;
private final Date dateTime;
private String systemName;
private final String className;
private final String methodName;
private String message;
private final Throwable originalException;
private HashMap<String, Object> objectAttribNameAndValue;
private String remark;
private String userName;
private Object[] messageArguments;

// HashMaps to store the data of exception object for XML formatting
private final HashMap<String, Object> xmlData = new HashMap<String, Object>(
10);

/**
*
* This constructor, will be used to wrap exceptions when throwing
* exceptions across layers.
*
* @Method Name: BaseException()
* @version 1.0
* @param code
* String - Mandatory
* @param className
* String - Mandatory
* @param methodName
* String - Mandatory
* @param bException
* com.marutisuzuki.platform.exception.core.BaseException
*
* @throws InitializationException
* com.marutisuzuki.platform.exception.core.InitializationException
*/
protected BaseException(String code, String className, String methodName,
BaseException bException) throws InitializationException {
super();
// Validate the mandatory parameters
ExceptionHelper.validateMandatory(code, className, methodName,
ExceptionHandlingConstants.BASE_EXCEPTION_CLASS_NAME,
ExceptionHandlingConstants.BASE_EXCEPTION_WRAPPER_CONSTR_NAME,
ExceptionHelper.getInputParam(code, className, methodName),
bException);

// Set the mandatory attributes
this.code = code;
this.className = className;
this.methodName = methodName;

// set the optional attributes
this.originalException = bException;
this.dateTime = new Date();
setDateTimeInXml(dateTime);
this.severityLevel = null;
this.systemName = null;
this.userName = null;
this.remark = null;
this.objectAttribNameAndValue = null;
this.message = null;

}

// this method would return the string containing the stack trace for custom exceptions.
public StringBuilder printDetailStackTrace(){
StringBuilder exceptionBuilder = new StringBuilder();
exceptionBuilder.append(customStackTrace(this));
printDetailStackTrace(exceptionBuilder, getOriginalException());
//printStringStackTrace(exceptionBuilder.toString());
return exceptionBuilder;
}
private void printDetailStackTrace(final StringBuilder exceptionBuilder, final Throwable exception){
if(exception != null){
if(BaseException.class.isInstance(exception)){
exceptionBuilder.append(customStackTrace(exception));
printDetailStackTrace(exceptionBuilder, ((BaseException)exception).getOriginalException());
}else {
exceptionBuilder.append(customStackTrace(exception));
if(exception.getCause() != null){
printDetailStackTrace(exceptionBuilder, exception.getCause());
}

}
}
}
private void printStringStackTrace(final String exceptionString){
printStackTrace();
PrintStream s = System.err;
synchronized (s) {
s.println("tat " + exceptionString);
}
}
private String customStackTrace(final Throwable ex){
String exceptionString = stackTraceToString(ex);
String result=ex.toString();
if(result!=null){
if(ex instanceof BaseException){
BaseException baseEx = (BaseException)ex;
result = result.concat(": "+baseEx.getCode()+" "+baseEx.getMethodName());
}
result = result.concat("n");
result = result.concat(exceptionString);
}
return result;

}

private String stackTraceToString(final Throwable e) {
StringBuilder sb = new StringBuilder();
for (StackTraceElement element : e.getStackTrace()) {
sb.append(element.toString());
sb.append("n");
}
return sb.toString();
}



/**
* This method is used to print multi level stack trace, for base exceptions, without custom code and method information
* Then it integrates with java standard print stack trace method. It doesnot return a string & prints to console mostly.
* This is used mostly for printing short info for very large nested stack traces. For detailed, ExceptionUtil.getFromBucket is used
*
* Update: As of Java Exc. Framework version 1.3.1, this method is now deprecated and use of printDetailStackTrace is recommended.
*
*/
@Deprecated
public void printShortStackTrace() {

StackTraceElement stackTrace[] = null;
boolean flag = true;
BaseException exception = this;
BaseException prevException = null;
while (exception.getOriginalException() != null) {
prevException = exception;
if (!(exception.getOriginalException() instanceof BaseException)) {
stackTrace = exception.getOriginalException().getStackTrace();
flag = false;
break;
} else {
exception = (BaseException) exception.getOriginalException();
}
}

if (flag) {
if (prevException != null) {

stackTrace = prevException.getOriginalException()
.getStackTrace();
} else {
stackTrace = exception.getStackTrace();
}
}

this.setStackTrace(stackTrace);
Throwable throwable = new Throwable(this);
throwable.setStackTrace(stackTrace);
throwable.printStackTrace();
}


/**
*
* This constructor, will be used to wrap exceptions when throwing
* exceptions across layers.
*
* @Method Name: BaseException()
* @version 1.0
* @param code
* String - Mandatory
* @param className
* String - Mandatory
* @param methodName
* String - Mandatory
* @param treException
* com.marutisuzuki.platform.exception.core.TechnicalRuntimeException
*
* @throws InitializationException
* com.marutisuzuki.platform.exception.core.InitializationException
*/
protected BaseException(String code, String className, String methodName,
TechnicalRuntimeException treException)
throws InitializationException {
super();
// Validate the mandatory parameters
ExceptionHelper.validateMandatory(code, className, methodName,
ExceptionHandlingConstants.BASE_EXCEPTION_CLASS_NAME,
ExceptionHandlingConstants.BASE_EXCEPTION_CONSTR_NAME,
ExceptionHelper.getInputParam(code, className, methodName),
treException);

// Set the mandatory attributes
this.code = code;
this.className = className;
this.methodName = methodName;

// set the optional attributes
this.originalException = treException;
this.dateTime = new Date();
setDateTimeInXml(dateTime);
this.severityLevel = null;
this.systemName = null;
this.userName = null;
this.remark = null;
this.message = null;
this.objectAttribNameAndValue = null;


}

/**
* This constructor, with mandatory arguments, is declared as protected and
* is to be used by the constructors of the sub classes BusinessException
* and TechnicalException. It sets the mandatory attributes in class level
* variables and also validates if any of the mandatory parameters is
* blank/null.
*
* @Method Name: BaseException()
* @version 1.0
* @param code
* java.lang.String - Mandatory
* @param className
* java.lang.String - Mandatory
* @param methodName
* java.lang.String - Mandatory
* @throws InitializationException
* java.lang.InitializationException
*/
protected BaseException(String code, String className, String methodName)
throws InitializationException {
super();
// Validate the mandatory parameters
ExceptionHelper.validateMandatory(code, className, methodName,
ExceptionHandlingConstants.BASE_EXCEPTION_CLASS_NAME,
ExceptionHandlingConstants.BASE_EXCEPTION_CONSTR_NAME,
ExceptionHelper.getInputParam(code, className, methodName),
null);

// Set the mandatory attributes
this.code = code;
this.className = className;
this.methodName = methodName;

// set the optional attributes
this.dateTime = new Date();
setDateTimeInXml(dateTime);
this.severityLevel = null;
this.systemName = null;
this.userName = null;
this.originalException = null;
this.remark = null;
this.message = null;
this.objectAttribNameAndValue = null;


}

/**
* This constructor, with all the arguments, is declared as protected and is
* to be used by the constructors of the sub classes BusinessException and
* TechnicalException. It sets the mandatory attributes in class level
* variables and also validates if any of the mandatory parameters is
* blank/null
*
* @Method Name: BaseException()
* @version 1.0
* @param code
* java.lang.String - Mandatory
* @param className
* java.lang.String - Mandatory
* @param methodName
* java.lang.String - Mandatory
* @param originalException
* java.lang.Throwable
* @throws InitializationException
* java.lang.InitializationException
*/
protected BaseException(String code, String className, String methodName,
Throwable originalException) throws InitializationException {
super();

// Validate the mandatory parameters
ExceptionHelper
.validateMandatory(
code,
className,
methodName,
ExceptionHandlingConstants.BASE_EXCEPTION_CLASS_NAME,
ExceptionHandlingConstants.BASE_EXCEPTION_OVERLOADED_CONSTR_NAME,
ExceptionHelper.getInputParam(code, className,
methodName), originalException);

// Set the mandatory attributes

this.code = code;
this.dateTime = new Date();
this.setDateTimeInXml(dateTime);
this.className = className;
this.methodName = methodName;
this.originalException = originalException;

this.severityLevel = null;
this.systemName = null;
this.userName = null;
this.remark = null;
this.message = null;
this.objectAttribNameAndValue = null;
}

/**
* This method returns the information stored in the BaseException object in
* XML String format
*
* @param topLevelCode
* String
* @param topLevelMessage
* String
* @param topLevelMessageArguments
* Object[]
* @param locale
* java.util.Locale
* @return String
*/
public String toXMLString(String topLevelCode, String topLevelMessage,
Object[] topLevelMessageArguments, Locale locale) {
String xmlString = null;
ExceptionHelper.populateXmlData(code, className, methodName,
originalException, severityLevel, systemName, userName,
message, remark, objectAttribNameAndValue, this.xmlData,
originalException);
boolean isStackTraceRequired = ExceptionUtil.checkStackTraceRequired();

// Convert the data from HashMap into XML String
xmlString = ExceptionHelper.formatXML(xmlData, isStackTraceRequired,
originalException, getType(), message, code, messageArguments,
true, true, topLevelCode, topLevelMessage,
topLevelMessageArguments, locale, true);

// Return the Exception data converted into XML String format
return xmlString;
}

/**
* This method returns the information stored in the BaseException object in
* XML String format
*
* @param topLevelCode
* String
* @param topLevelMessage
* String
* @param topLevelMessageArguments
* Object[]
* @param locale
* java.util.Locale
* @return String
*/
public String toXMLString(String topLevelCode,
Object[] topLevelMessageArguments, Locale locale) {
String xmlString = null;
ExceptionHelper.populateXmlData(code, className, methodName,
originalException, severityLevel, systemName, userName,
message, remark, objectAttribNameAndValue, this.xmlData,
originalException);
boolean isStackTraceRequired = ExceptionUtil.checkStackTraceRequired();

// Convert the data from HashMap into XML String
xmlString = ExceptionHelper.formatXML(xmlData, isStackTraceRequired,
originalException, getType(), message, code, messageArguments,
true, true, topLevelCode, null, topLevelMessageArguments,
locale, true);

// Return the Exception data converted into XML String format
return xmlString;
}

/**
* This method returns the information stored in the BaseException object in
* XML String format
*
* @param topLevelCode
* String
* @return String
*/
public String toXMLString(String topLevelCode) {
return toXMLString(topLevelCode, null, null, null);
}

/**
* This method returns the information stored in the BaseException object in
* XML String format
*
*
* @return String
*/
public String toXMLString() {
return toXMLString(null);
}

/**
* This method returns the information stored in the BaseException object in
* XML String format
*
* @param topLevelCode
* String
* @param topLevelMessage
* String
* @param topLevelMessageArguments
* Object[]
* @return String
*/
public String toXMLString(String topLevelCode, String topLevelMessage,
Object[] topLevelMessageArguments) {
return toXMLString(topLevelCode, topLevelMessage,
topLevelMessageArguments, null);
}

/**
* @return Returns the className.
*/
public String getClassName() {
return this.className;
}

/**
* @return Returns the code.
*/
public String getCode() {
return this.code;
}

/**
* @return Returns the dateTime.
*/

public Date getDateTime() {
return this.dateTime;
}

/**
* Current date/time being set
*/
private void setDateTimeInXml(Date dtTime) {

// Reset the HashMap
this.xmlData.put(ExceptionHandlingConstants.DATETIME,
ExceptionUtil.formatDateTime(dtTime));

}

/**
* @param locale
* java.util.Locale
* @return Returns the message. If locale is passed as null, locale is
* picked from the system properties.
*/
public String getMessage(Locale locale) {
String mesg = null;
if (null != this.message) {
mesg = this.message;
} else if (this.message == null && null != getCode()) {
mesg = ExceptionHelper.getMessage(null, getCode(),
messageArguments, locale);
}
return mesg;
}

/**
* @return Returns the methodName.
*/
public String getMethodName() {
return this.methodName;
}

/**
* @return Returns the objectAttribNameAndValue.
*/
public HashMap<String, Object> getObjectAttribNameAndValue() {
return this.objectAttribNameAndValue;
}

/**
* @return Returns the originalException.
*/
public Throwable getOriginalException() {
return this.originalException;
}

/**
* @return Returns the remark.
*/
public String getRemark() {
return this.remark;
}

/**
* @return Returns the severityLevel.
*/
public Integer getSeverityLevel() {
return this.severityLevel;
}

/**
* @return Returns the systemName.
*/
public String getSystemName() {
return this.systemName;
}

/**
*
* @return String type of exception
*/
public String getType() {
return getClassName();
}

/**
* @return String userName
*/
public String getUserName() {
return userName;
}

/**
* @return the xmlData
*/
public HashMap<String, Object> getXmlData() {
return xmlData;
}

/**
* @return arguments
*/
public Object[] getMessageArguments() {
return messageArguments;
}

/**
* @param arguments
* setting message parameters to construct dynamic message
*/
public void setMessageArguments(Object[] messageArguments) {
this.messageArguments = messageArguments;
}

/**
* This method sets optional fields
*
* @param severityLevel
* @param message
* @param objectAttribNameAndValue
* @param remark
*/
public void setOptionalFields(Integer severityLevel, String message,
HashMap<String, Object> objectAttribNameAndValue, String remark) {
// set the optional attributes
this.severityLevel = severityLevel;
this.message = message;
this.remark = remark;
this.objectAttribNameAndValue = objectAttribNameAndValue;

}

/**
* @param systemName
* @param userName
*/
public void setSystemAndUserName(String systemName, String userName) {
this.systemName = systemName;
this.userName = userName;
}

}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.