PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/hsqldb19b3/org/hsqldb_voltpatches/Error.java

https://github.com/vkhoroshko/voltdb
Java | 262 lines | 104 code | 46 blank | 112 comment | 12 complexity | a36144110d98d208577c9961b09c296e MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. /* Copyright (c) 2001-2009, The HSQL Development Group
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright notice, this
  8. * list of conditions and the following disclaimer.
  9. *
  10. * Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. *
  14. * Neither the name of the HSQL Development Group nor the names of its
  15. * contributors may be used to endorse or promote products derived from this
  16. * software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
  22. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. package org.hsqldb_voltpatches;
  31. import org.hsqldb_voltpatches.lib.StringUtil;
  32. import org.hsqldb_voltpatches.resources.BundleHandler;
  33. import org.hsqldb_voltpatches.result.Result;
  34. /**
  35. * Contains static factory methods to produce instances of HsqlException
  36. *
  37. * @author Loic Lefevre
  38. * @author Fred Toussi (fredt@users dot sourceforge.net)
  39. * @version 1.9.0
  40. * @since 1.9.0
  41. */
  42. public class Error {
  43. //
  44. public static boolean TRACE = false;
  45. public static boolean TRACESYSTEMOUT = false;
  46. //
  47. private static final String errPropsName = "sql-state-messages";
  48. private static final int bundleHandle =
  49. BundleHandler.getBundleHandle(errPropsName, null);
  50. private static final String MESSAGE_TAG = "$$";
  51. private static final int SQL_STATE_DIGITS = 5;
  52. private static final int SQL_CODE_DIGITS = 4;
  53. private static final int ERROR_CODE_BASE = 11;
  54. public static RuntimeException runtimeError(int code, String add) {
  55. HsqlException e = error(code, add);
  56. return new RuntimeException(e.getMessage());
  57. }
  58. public static HsqlException error(int code, String add) {
  59. String s = getMessage(code);
  60. if (add != null) {
  61. s += ": " + add.toString();
  62. }
  63. return new HsqlException(s.substring(SQL_STATE_DIGITS + 1),
  64. s.substring(0, SQL_STATE_DIGITS), -code);
  65. }
  66. public static HsqlException error(int code) {
  67. return error(code, 0, null);
  68. }
  69. public static HsqlException error(int code, Throwable t) {
  70. String message = getMessage(code, 0, null);
  71. return new HsqlException(t, message.substring(0, SQL_STATE_DIGITS),
  72. -code);
  73. }
  74. /**
  75. * Compose error message by inserting the strings in the add parameters
  76. * in placeholders within the error message. The message string contains
  77. * $$ markers for each context variable. Context variables are supplied in
  78. * the add parameters.
  79. *
  80. * @param code main error code
  81. * @param subCode sub error code (if 0 => no subMessage!)
  82. * @param add optional parameters
  83. *
  84. * @return an <code>HsqlException</code>
  85. */
  86. public static HsqlException error(int code, int subCode,
  87. final Object[] add) {
  88. String message = getMessage(code, subCode, add);
  89. int sqlCode = subCode < ERROR_CODE_BASE ? code
  90. : subCode;
  91. return new HsqlException(message.substring(SQL_STATE_DIGITS + 1),
  92. message.substring(0, SQL_STATE_DIGITS),
  93. -sqlCode);
  94. }
  95. public static HsqlException error(int code, int code2) {
  96. return error(code, getMessage(code2));
  97. }
  98. /**
  99. * Compose error
  100. * in placeholders within the error message. The message string contains
  101. * $$ markers for each context variable. Context variables are supplied in
  102. * the add parameters.
  103. *
  104. * @see HsqlException#HsqlException(String, String, int)
  105. * @return an <code>HsqlException</code>
  106. */
  107. public static HsqlException error(String message, String sqlState, int i) {
  108. return new HsqlException(message, sqlState, i);
  109. }
  110. /**
  111. * Compose error message by inserting the strings in the add variables
  112. * in placeholders within the error message. The message string contains
  113. * $$ markers for each context variable. Context variables are supplied in
  114. * the add parameter. (by Loic Lefevre)
  115. *
  116. * @param message message string
  117. * @param add optional parameters
  118. *
  119. * @return an <code>HsqlException</code>
  120. */
  121. private static String insertStrings(String message, Object[] add) {
  122. StringBuffer sb = new StringBuffer(message.length() + 32);
  123. int lastIndex = 0;
  124. int escIndex = message.length();
  125. // removed test: i < add.length
  126. // because if mainErrorMessage is equal to "blabla $$"
  127. // then the statement escIndex = mainErrorMessage.length();
  128. // is never reached! ???
  129. for (int i = 0; i < add.length; i++) {
  130. escIndex = message.indexOf(MESSAGE_TAG, lastIndex);
  131. if (escIndex == -1) {
  132. break;
  133. }
  134. sb.append(message.substring(lastIndex, escIndex));
  135. sb.append(add[i] == null ? "null exception message"
  136. : add[i].toString());
  137. lastIndex = escIndex + MESSAGE_TAG.length();
  138. }
  139. escIndex = message.length();
  140. sb.append(message.substring(lastIndex, escIndex));
  141. return sb.toString();
  142. }
  143. /**
  144. * Returns the error message given the error code.<br/>
  145. * This method is be used when throwing exception other
  146. * than <code>HsqlException</code>.
  147. *
  148. * @param errorCode the error code associated to the error message
  149. * @return the error message associated with the error code
  150. */
  151. public static String getMessage(final int errorCode) {
  152. return getMessage(errorCode, 0, null);
  153. }
  154. /**
  155. * Returns the error SQL STATE sting given the error code.<br/>
  156. * This method is be used when throwing exception based on other exceptions.
  157. *
  158. * @param errorCode the error code associated to the error message
  159. * @return the error message associated with the error code
  160. */
  161. public static String getStateString(final int errorCode) {
  162. return getMessage(errorCode, 0, null).substring(0, SQL_STATE_DIGITS);
  163. }
  164. /**
  165. * Returns the error message given the error code.<br/> This method is used
  166. * when throwing exception other than <code>HsqlException</code>.
  167. *
  168. * @param code the code for the error message
  169. * @param subCode the code for the addon message
  170. * @param add value(s) to use to replace the placeholer(s)
  171. * @return the error message associated with the error code
  172. */
  173. public static String getMessage(final int code, int subCode,
  174. final Object[] add) {
  175. String message = getResourceString(code);
  176. if (subCode != 0) {
  177. message += getResourceString(subCode);
  178. }
  179. if (add != null) {
  180. message = insertStrings(message, add);
  181. }
  182. return message;
  183. }
  184. private static String getResourceString(int code) {
  185. String key = StringUtil.toZeroPaddedString(code, SQL_CODE_DIGITS,
  186. SQL_CODE_DIGITS);
  187. return BundleHandler.getString(bundleHandle, key);
  188. }
  189. public static HsqlException error(final Result result) {
  190. return new HsqlException(result);
  191. }
  192. /**
  193. * Used to print messages to System.out
  194. *
  195. *
  196. * @param message message to print
  197. */
  198. public static void printSystemOut(String message) {
  199. if (TRACESYSTEMOUT) {
  200. System.out.println(message);
  201. }
  202. }
  203. /**
  204. * Used to print messages to System.out
  205. *
  206. *
  207. * @param message1 message to print
  208. * @param message2 message to print
  209. */
  210. public static void printSystemOut(String message1, long message2) {
  211. if (TRACESYSTEMOUT) {
  212. System.out.print(message1);
  213. System.out.println(message2);
  214. }
  215. }
  216. }