1 10 11 package org.mule.umo; 12 13 import org.apache.commons.lang.SystemUtils; 14 import org.mule.config.ExceptionHelper; 15 import org.mule.config.i18n.Message; 16 import org.mule.config.i18n.Messages; 17 import org.mule.util.StringUtils; 18 19 import java.io.PrintWriter ; 20 import java.io.StringWriter ; 21 import java.lang.reflect.InvocationTargetException ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 26 33 public abstract class UMOException extends Exception 34 { 35 private Map info = new HashMap (); 36 private int errorCode = -1; 37 private String message = null; 38 private Message i18nMessage; 39 40 43 public UMOException(Message message) 44 { 45 super(); 46 setMessage(message); 47 } 48 49 53 public UMOException(Message message, Throwable cause) 54 { 55 super((cause instanceof InvocationTargetException 56 ? ((InvocationTargetException )cause).getTargetException() : cause)); 57 setMessage(message); 58 } 59 60 public UMOException(Throwable cause) 61 { 62 super((cause instanceof InvocationTargetException 63 ? ((InvocationTargetException )cause).getTargetException() : cause)); 64 setMessage(Message.createStaticMessage(cause.getMessage() + " (" + cause.getClass().getName() + ")")); 65 initialise(); 66 } 67 68 protected UMOException() 69 { 70 super(); 71 initialise(); 72 } 73 74 protected void setMessage(Message message) 75 { 76 initialise(); 77 this.message = message.getMessage(); 78 i18nMessage = message; 79 } 80 81 protected void setMessage(String message) 82 { 83 initialise(); 84 this.message = message; 85 if (i18nMessage == null) 86 { 87 i18nMessage = Message.createStaticMessage(message); 88 } 89 } 90 91 public int getExceptionCode() 92 { 93 return errorCode; 94 } 95 96 public Message getI18nMessage() 97 { 98 return i18nMessage; 99 } 100 101 public int getMessageCode() 102 { 103 return (i18nMessage == null ? 0 : i18nMessage.getCode()); 104 } 105 106 public void addInfo(String name, Object info) 107 { 108 this.info.put(name, info); 109 } 110 111 protected void appendMessage(String s) 112 { 113 message += s; 114 } 115 116 protected void prependMessage(String s) 117 { 118 message = message + ". " + s; 119 } 120 121 protected void setExceptionCode(int code) 122 { 123 errorCode = code; 124 } 125 126 public final String getMessage() 127 { 128 return message; 129 } 130 131 protected void initialise() 132 { 133 setExceptionCode(ExceptionHelper.getErrorCode(getClass())); 134 String javadoc = ExceptionHelper.getJavaDocUrl(getClass()); 135 String doc = ExceptionHelper.getDocUrl(getClass()); 136 if (javadoc != null) 137 { 138 info.put("JavaDoc", javadoc); 140 } 141 if (doc != null) 142 { 143 info.put("Other Doc", doc); 145 } 146 } 147 148 public String getDetailedMessage() 149 { 150 UMOException e = ExceptionHelper.getRootMuleException(this); 151 if (!e.equals(this)) 152 { 153 return getMessage(); 154 } 155 StringBuffer buf = new StringBuffer (1024); 156 buf.append(SystemUtils.LINE_SEPARATOR).append(StringUtils.repeat('*', 80)).append( 157 SystemUtils.LINE_SEPARATOR); 158 buf.append("Message : ").append(message).append(SystemUtils.LINE_SEPARATOR); 159 buf.append("Type : ") 160 .append(getClass().getName()) 161 .append(SystemUtils.LINE_SEPARATOR); 162 buf.append("Code : ").append("MULE_ERROR-").append( 163 getExceptionCode() + getMessageCode()).append(SystemUtils.LINE_SEPARATOR); 164 167 Map info = ExceptionHelper.getExceptionInfo(this); 168 for (Iterator iterator = info.keySet().iterator(); iterator.hasNext();) 169 { 170 String s = (String )iterator.next(); 171 int pad = 22 - s.length(); 172 buf.append(s); 173 if (pad > 0) 174 { 175 buf.append(StringUtils.repeat(' ', pad)); 176 } 177 buf.append(": "); 178 buf.append(info.get(s)).append(SystemUtils.LINE_SEPARATOR); 179 } 180 181 buf.append(StringUtils.repeat('*', 80)).append(SystemUtils.LINE_SEPARATOR); 183 buf.append(new Message(Messages.EXCEPTION_STACK_IS)).append(SystemUtils.LINE_SEPARATOR); 184 buf.append(ExceptionHelper.getExceptionStack(this)); 185 186 buf.append(StringUtils.repeat('*', 80)).append(SystemUtils.LINE_SEPARATOR); 187 buf.append(new Message(Messages.ROOT_STACK_TRACE)).append(SystemUtils.LINE_SEPARATOR); 188 Throwable root = ExceptionHelper.getRootException(this); 189 StringWriter w = new StringWriter (); 190 PrintWriter p = new PrintWriter (w); 191 root.printStackTrace(p); 192 buf.append(w.toString()).append(SystemUtils.LINE_SEPARATOR); 193 buf.append(StringUtils.repeat('*', 80)).append(SystemUtils.LINE_SEPARATOR); 194 195 return buf.toString(); 196 } 197 198 public boolean equals(Object o) 199 { 200 if (this == o) 201 { 202 return true; 203 } 204 if (!(o instanceof UMOException)) 205 { 206 return false; 207 } 208 209 final UMOException umoException = (UMOException)o; 210 211 if (errorCode != umoException.errorCode) 212 { 213 return false; 214 } 215 if (i18nMessage != null 216 ? !i18nMessage.equals(umoException.i18nMessage) : umoException.i18nMessage != null) 217 { 218 return false; 219 } 220 if (message != null ? !message.equals(umoException.message) : umoException.message != null) 221 { 222 return false; 223 } 224 225 return true; 226 } 227 228 public int hashCode() 229 { 230 int result; 231 result = errorCode; 232 result = 29 * result + (message != null ? message.hashCode() : 0); 233 result = 29 * result + (i18nMessage != null ? i18nMessage.hashCode() : 0); 234 return result; 235 } 236 237 public Map getInfo() 238 { 239 return info; 240 } 241 } 242 | Popular Tags |