001: package org.claros.commons.exception;
002:
003: import java.io.ByteArrayOutputStream;
004: import java.io.IOException;
005: import java.io.PrintStream;
006:
007: import javax.servlet.ServletException;
008:
009: import org.apache.commons.configuration.Configuration;
010: import org.apache.commons.configuration.PropertiesConfiguration;
011: import org.apache.commons.logging.Log;
012: import org.apache.commons.logging.LogFactory;
013: import org.claros.commons.configuration.Paths;
014: import org.claros.commons.configuration.PropertyFile;
015: import org.claros.commons.utility.Utility;
016:
017: /**
018: * @author Umut Gokbayrak
019: */
020: public class ClarosBaseException extends ServletException {
021: private static final long serialVersionUID = -2268010989574881830L;
022: private static Log log = LogFactory
023: .getLog(ClarosBaseException.class);
024: private String errorKey;
025: private String userMessage;
026: private String systemMessage;
027: private Exception nestedException;
028: private static Configuration msgs;
029:
030: public ClarosBaseException() {
031: super ();
032: }
033:
034: public ClarosBaseException(String errorKey) {
035: this .errorKey = errorKey;
036: this .userMessage = msgs.getString(this .errorKey);
037: log.error("An exception is created. Message: " + userMessage);
038: }
039:
040: public ClarosBaseException(Exception nestedException,
041: String errorKey) {
042: this .nestedException = nestedException;
043:
044: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
045: PrintStream out = new PrintStream(bOut);
046: this .nestedException.printStackTrace(out);
047: this .systemMessage = bOut.toString();
048: try {
049: bOut.close();
050: } catch (IOException e) {
051: }
052: this .errorKey = errorKey;
053: this .userMessage = msgs.getString(errorKey);
054: log.error("An exception is created. Message: " + userMessage);
055: }
056:
057: public ClarosBaseException(Exception nestedException) {
058: this .nestedException = nestedException;
059:
060: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
061: PrintStream out = new PrintStream(bOut);
062: this .nestedException.printStackTrace(out);
063: this .systemMessage = bOut.toString();
064: log.error("An exception is created. System Message: "
065: + this .systemMessage);
066: try {
067: bOut.close();
068: } catch (IOException e) {
069: }
070: }
071:
072: static {
073: try {
074: String configPath = "/config/config.xml";
075: Configuration configXml = PropertyFile
076: .getConfiguration(configPath);
077: String resourceType = configXml
078: .getString("error-handling.resource-type");
079: if (resourceType != null) {
080: if (resourceType.equals("FILE")) {
081: log
082: .debug("Error messages are read from FILE resource");
083: String resourceFile = configXml
084: .getString("error-handling.resource-path");
085: if (resourceFile != null) {
086: if (resourceFile.indexOf("%respath%") >= 0) {
087: resourceFile = Utility
088: .replaceAllOccurances(resourceFile,
089: "%respath%", Paths
090: .getResFolder());
091: }
092: log.debug("Error messages are read from path: "
093: + resourceFile);
094: msgs = new PropertiesConfiguration(resourceFile);
095: log
096: .debug("Error messages file initialized successfully");
097: }
098: } else if (resourceType.equals("DB")) {
099: // TODO: not implemented yet
100: }
101: } else {
102: // log.fatal("Could not initialize error messages file. Check the config-error statements in config.xml file. resource-type can not be read.");
103: }
104: } catch (Exception e) {
105: // log.fatal("Could not initialize error messages file. Check the config-error statements in config.xml file", e);
106: }
107: }
108:
109: public String getErrorKey() {
110: return errorKey;
111: }
112:
113: public void setErrorKey(String errorKey) {
114: this .errorKey = errorKey;
115: }
116:
117: public String getUserMessage() {
118: return userMessage;
119: }
120:
121: public void setUserMessage(String userMessage) {
122: this .userMessage = userMessage;
123: }
124:
125: public String getSystemMessage() {
126: return systemMessage;
127: }
128:
129: public void setSystemMessage(String systemMessage) {
130: this.systemMessage = systemMessage;
131: }
132:
133: }
|