1 21 22 package org.apache.derby.iapi.error; 23 24 import org.apache.derby.iapi.reference.SQLState; 25 26 import org.apache.derby.impl.jdbc.EmbedSQLException; 27 import org.apache.derby.iapi.error.ExceptionSeverity; 28 import org.apache.derby.iapi.services.i18n.MessageService; 29 import org.apache.derby.iapi.services.sanity.SanityManager; 30 31 import java.sql.SQLException ; 32 import java.sql.SQLWarning ; 33 34 50 51 public class StandardException extends Exception 52 { 53 public static final int REPORT_DEFAULT = 0; 54 public static final int REPORT_NEVER = 1; 55 public static final int REPORT_ALWAYS = 2; 56 57 60 private Throwable nestedException; 61 private transient Object [] arguments; 62 private int severity; 63 private String textMessage; 64 private String sqlState; 65 private transient int report; 66 67 70 71 protected StandardException(String messageID) 72 { 73 this(messageID, (Throwable ) null, (Object []) null); 74 75 } 76 77 protected StandardException(String messageID, Object [] args) 78 { 79 this(messageID, (Throwable ) null, args); 80 } 81 82 protected StandardException(String messageID, Throwable t, Object [] args) 83 { 84 super(messageID); 85 86 this.severity = getSeverityFromIdentifier(messageID); 87 this.sqlState = getSQLStateFromIdentifier(messageID); 88 this.nestedException = t; 89 this.arguments = args; 90 91 if (SanityManager.DEBUG) 92 { 93 SanityManager.ASSERT(messageID != null, 94 "StandardException with no messageID"); 95 } 96 } 97 98 105 private StandardException(String sqlState, String text) 106 { 107 this(sqlState); 108 textMessage = text; 109 } 110 111 114 117 private final void setArguments(Object [] arguments) 118 { 119 this.arguments = arguments; 120 } 121 122 126 public final Object [] getArguments() 127 { 128 return arguments; 129 } 130 131 134 public final void setNestedException(Throwable nestedException) 135 { 136 this.nestedException = nestedException; 137 } 138 139 143 public final Throwable getNestedException() 144 { 145 return nestedException; 146 } 147 148 152 public final int report() { 153 return report; 154 } 155 156 159 public final void setReport(int report) { 160 this.report = report; 161 } 162 163 public final void setSeverity(int severity) { 164 this.severity = severity; 165 } 166 167 168 public final int getSeverity() { 169 return severity; 170 } 171 172 public final int getErrorCode() { 173 return severity; 174 } 175 176 182 public final String getSQLState() 183 { 184 return sqlState; 185 } 186 187 193 public static String getSQLStateFromIdentifier(String messageID) { 194 195 if (messageID.length() == 5) 196 return messageID; 197 return messageID.substring(0, 5); 198 } 199 200 203 public static int getSeverityFromIdentifier(String messageID) { 204 205 int lseverity = ExceptionSeverity.NO_APPLICABLE_SEVERITY; 206 207 switch (messageID.length()) { 208 case 5: 209 switch (messageID.charAt(0)) { 210 case '0': 211 switch (messageID.charAt(1)) { 212 case '1': 213 lseverity = ExceptionSeverity.WARNING_SEVERITY; 214 break; 215 case 'A': 216 case '7': 217 lseverity = ExceptionSeverity.STATEMENT_SEVERITY; 218 break; 219 case '8': 220 lseverity = ExceptionSeverity.SESSION_SEVERITY; 221 break; 222 } 223 break; 224 case '2': 225 case '3': 226 lseverity = ExceptionSeverity.STATEMENT_SEVERITY; 227 break; 228 case '4': 229 switch (messageID.charAt(1)) { 230 case '0': 231 lseverity = ExceptionSeverity.TRANSACTION_SEVERITY; 232 break; 233 case '2': 234 lseverity = ExceptionSeverity.STATEMENT_SEVERITY; 235 break; 236 } 237 break; 238 } 239 break; 240 241 default: 242 switch (messageID.charAt(6)) { 243 case 'M': 244 lseverity = ExceptionSeverity.SYSTEM_SEVERITY; 245 break; 246 case 'D': 247 lseverity = ExceptionSeverity.DATABASE_SEVERITY; 248 break; 249 case 'C': 250 lseverity = ExceptionSeverity.SESSION_SEVERITY; 251 break; 252 case 'T': 253 lseverity = ExceptionSeverity.TRANSACTION_SEVERITY; 254 break; 255 case 'S': 256 lseverity = ExceptionSeverity.STATEMENT_SEVERITY; 257 break; 258 case 'U': 259 lseverity = ExceptionSeverity.NO_APPLICABLE_SEVERITY; 260 break; 261 } 262 break; 263 } 264 265 return lseverity; 266 } 267 268 282 283 284 285 public static StandardException normalClose() 286 { 287 StandardException se = newException( SQLState.NORMAL_CLOSE ); 288 se.report = REPORT_NEVER; 289 return se; 290 } 291 292 293 294 public static StandardException newException(String messageID) { 295 return new StandardException(messageID); 296 } 297 public static StandardException newException(String messageID, Throwable t) { 298 return new StandardException(messageID, t, (Object []) null); 299 } 300 301 302 303 public static StandardException newException(String messageID, Object a1) { 304 Object [] oa = new Object [] {a1}; 305 return new StandardException(messageID, oa); 306 } 307 public static StandardException newException(String messageID, Throwable t, Object a1) { 308 Object [] oa = new Object [] {a1}; 309 return new StandardException(messageID, t, oa); 310 } 311 312 313 314 public static StandardException newException(String messageID, Object a1, Object a2) { 315 Object [] oa = new Object [] {a1, a2}; 316 return new StandardException(messageID, oa); 317 } 318 319 329 public static class BadMessageArgumentException extends Throwable {} 330 331 340 public static StandardException newException(String messageID, 341 Object a1, 342 Throwable t) 343 throws BadMessageArgumentException { 344 throw new BadMessageArgumentException(); 345 } 346 347 public static StandardException newException(String messageID, Throwable t, Object a1, Object a2) { 348 Object [] oa = new Object [] {a1, a2}; 349 return new StandardException(messageID, t, oa); 350 } 351 352 353 354 public static StandardException newException(String messageID, Object a1, Object a2, Object a3) { 355 Object [] oa = new Object [] {a1, a2, a3}; 356 return new StandardException(messageID, oa); 357 } 358 359 369 public static StandardException newException(String messageID, 370 Object a1, 371 Object a2, 372 Throwable t) 373 throws BadMessageArgumentException { 374 throw new BadMessageArgumentException(); 375 } 376 377 public static StandardException newException(String messageID, Throwable t, Object a1, Object a2, Object a3) { 378 Object [] oa = new Object [] {a1, a2, a3}; 379 return new StandardException(messageID, t, oa); 380 } 381 382 383 384 public static StandardException newException(String messageID, Object a1, Object a2, Object a3, Object a4) { 385 Object [] oa = new Object [] {a1, a2, a3, a4}; 386 return new StandardException(messageID, oa); 387 } 388 public static StandardException newException(String messageID, Throwable t, Object a1, Object a2, Object a3, Object a4) { 389 Object [] oa = new Object [] {a1, a2, a3, a4}; 390 return new StandardException(messageID, t, oa); 391 } 392 393 394 public static StandardException newException(String messageID, Object a1, Object a2, Object a3, Object a4, Object a5) { 395 Object [] oa = new Object [] {a1, a2, a3, a4, a5}; 396 return new StandardException(messageID, oa); 397 } 398 public static StandardException newException(String messageID, Throwable t, Object a1, Object a2, Object a3, Object a4, Object a5) { 399 Object [] oa = new Object [] {a1, a2, a3, a4, a5}; 400 return new StandardException(messageID, t, oa); 401 } 402 403 404 public static StandardException newException(String messageID, Object a1, Object a2, Object a3, Object a4, Object a5, Object a6) { 405 Object [] oa = new Object [] {a1, a2, a3, a4, a5, a6}; 406 return new StandardException(messageID, oa); 407 } 408 public static StandardException newException(String messageID, Throwable t, Object a1, Object a2, Object a3, Object a4, Object a5, Object a6) { 409 Object [] oa = new Object [] {a1, a2, a3, a4, a5, a6}; 410 return new StandardException(messageID, t, oa); 411 } 412 413 414 public static StandardException newException(String messageID, Object a1, Object a2, Object a3, Object a4, Object a5, Object a6, Object a7) { 415 Object [] oa = new Object [] {a1, a2, a3, a4, a5, a6, a7}; 416 return new StandardException(messageID, oa); 417 } 418 public static StandardException newException(String messageID, Throwable t, Object a1, Object a2, Object a3, Object a4, Object a5, Object a6, Object a7) { 419 Object [] oa = new Object [] {a1, a2, a3, a4, a5, a6, a7}; 420 return new StandardException(messageID, t, oa); 421 } 422 423 424 public static StandardException newException(String messageID, Object a1, Object a2, Object a3, Object a4, Object a5, Object a6, Object a7, Object a8) { 425 Object [] oa = new Object [] {a1, a2, a3, a4, a5, a6, a7, a8}; 426 return new StandardException(messageID, oa); 427 } 428 public static StandardException newException(String messageID, Throwable t, Object a1, Object a2, Object a3, Object a4, Object a5, Object a6, Object a7, Object a8) { 429 Object [] oa = new Object [] {a1, a2, a3, a4, a5, a6, a7, a8}; 430 return new StandardException(messageID, t, oa); 431 } 432 433 443 public static StandardException newPreLocalizedException( String MessageID, 444 Throwable t, 445 String localizedMessage) 446 { 447 StandardException se = new StandardException( MessageID, localizedMessage); 448 if( t != null) 449 se.nestedException = t; 450 return se; 451 } 452 453 public static StandardException unexpectedUserException(Throwable t) 454 { 455 462 if ((t instanceof SQLException) && 463 !(t instanceof EmbedSQLException)) 464 { 465 SQLException sqlex = (SQLException)t; 466 String state = sqlex.getSQLState(); 467 if ((state != null) && 468 (state.length() == 5) && 469 state.startsWith("38") && 470 !state.equals("38000")) 471 { 472 StandardException se = new StandardException(state, sqlex.getMessage()); 473 if (sqlex.getNextException() != null) 474 { 475 se.setNestedException(sqlex.getNextException()); 476 } 477 return se; 478 } 479 } 480 481 if (t instanceof EmbedSQLException) { 483 EmbedSQLException csqle = (EmbedSQLException) t; 484 if (csqle.isSimpleWrapper()) { 485 Throwable wrapped = csqle.getJavaException(); 486 if (wrapped instanceof StandardException) 487 return (StandardException) wrapped; 488 } 489 } 490 491 492 if (t instanceof StandardException) 494 { 495 return (StandardException) t; 496 } 497 else 498 { 499 529 String detailMessage; 530 boolean cloudscapeException = false; 531 532 if (t instanceof EmbedSQLException) { 533 detailMessage = ((EmbedSQLException) t).toString(); 534 cloudscapeException = true; 535 } 536 else { 537 detailMessage = t.getMessage(); 538 } 539 540 if (detailMessage == null) 541 { 542 detailMessage = ""; 543 } else { 544 detailMessage = detailMessage.trim(); 545 } 546 547 if (detailMessage.length() == 0) { 549 detailMessage = t.getClass().getName(); 550 } 551 else { 552 553 if (!cloudscapeException) { 554 detailMessage = t.getClass().getName() + ": " + detailMessage; 555 } 556 } 557 558 StandardException se = 559 newException(SQLState.LANG_UNEXPECTED_USER_EXCEPTION, t, detailMessage); 560 return se; 561 } 562 } 563 564 569 570 public static StandardException plainWrapException(Throwable t) { 571 572 if (t instanceof StandardException) 573 return (StandardException) t; 574 575 if (t instanceof SQLException) { 576 577 SQLException sqle = (SQLException) t; 578 579 String sqlState = sqle.getSQLState(); 580 if (sqlState != null) { 581 582 StandardException se = new StandardException(sqlState, "(" + sqle.getErrorCode() + ") " + sqle.getMessage()); 583 sqle = sqle.getNextException(); 584 if (sqle != null) 585 se.setNestedException(plainWrapException(sqle)); 586 return se; 587 } 588 } 589 590 String detailMessage = t.getMessage(); 591 592 if (detailMessage == null) 593 { 594 detailMessage = ""; 595 } else { 596 detailMessage = detailMessage.trim(); 597 } 598 599 StandardException se = 600 newException(SQLState.JAVA_EXCEPTION, t, detailMessage, t.getClass().getName()); 601 return se; 602 } 603 604 607 public static StandardException closeException() { 608 StandardException se = newException(SQLState.CLOSE_REQUEST); 609 se.setReport(REPORT_NEVER); 610 return se; 611 } 612 615 616 630 631 public String getMessage() { 632 if (textMessage == null) 633 textMessage = MessageService.getCompleteMessage(getMessageId(), getArguments()); 634 635 return textMessage; 636 } 637 638 642 public final String getMessageId() { 643 return super.getMessage(); 644 } 645 646 647 652 public String getErrorProperty(String type) { 653 return getErrorProperty(getMessageId(), type); 654 } 655 656 659 public String toString() { 660 String msg = getMessage(); 661 662 return "ERROR " + getSQLState() + ": " + msg; 663 } 664 665 668 669 private static String getErrorProperty(String messageId, String type) { 670 return MessageService.getProperty(messageId, type); 671 } 672 673 public static StandardException interrupt(InterruptedException ie) { 674 StandardException se = StandardException.newException(SQLState.CONN_INTERRUPT, ie); 675 return se; 676 } 677 680 681 public static SQLWarning newWarning(String messageId) { 682 683 return newWarningCommon( messageId, (Object []) null ); 684 685 } 686 687 public static SQLWarning newWarning(String messageId, Object a1) { 688 689 Object [] oa = new Object [] {a1}; 690 691 return newWarningCommon( messageId, oa ); 692 } 693 694 public static SQLWarning newWarning(String messageId, Object a1, Object a2) { 695 696 Object [] oa = new Object [] {a1, a2}; 697 698 return newWarningCommon( messageId, oa ); 699 } 700 701 private static SQLWarning newWarningCommon( String messageId, Object [] oa ) 702 { 703 String message = MessageService.getCompleteMessage(messageId, oa); 704 String state = StandardException.getSQLStateFromIdentifier(messageId); 705 SQLWarning sqlw = new SQLWarning (message, state, ExceptionSeverity.WARNING_SEVERITY); 706 707 return sqlw; 708 } 709 } 710 | Popular Tags |