PageRenderTime 30ms CodeModel.GetById 15ms app.highlight 12ms RepoModel.GetById 1ms 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
  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
 31
 32package org.hsqldb_voltpatches;
 33
 34import org.hsqldb_voltpatches.lib.StringUtil;
 35import org.hsqldb_voltpatches.resources.BundleHandler;
 36import org.hsqldb_voltpatches.result.Result;
 37
 38/**
 39 * Contains static factory methods to produce instances of HsqlException
 40 *
 41 * @author Loic Lefevre
 42 * @author Fred Toussi (fredt@users dot sourceforge.net)
 43 * @version 1.9.0
 44 * @since 1.9.0
 45 */
 46public class Error {
 47
 48    //
 49    public static boolean TRACE          = false;
 50    public static boolean TRACESYSTEMOUT = false;
 51
 52    //
 53    private static final String errPropsName = "sql-state-messages";
 54    private static final int bundleHandle =
 55        BundleHandler.getBundleHandle(errPropsName, null);
 56    private static final String MESSAGE_TAG      = "$$";
 57    private static final int    SQL_STATE_DIGITS = 5;
 58    private static final int    SQL_CODE_DIGITS  = 4;
 59    private static final int    ERROR_CODE_BASE  = 11;
 60
 61    public static RuntimeException runtimeError(int code, String add) {
 62
 63        HsqlException e = error(code, add);
 64
 65        return new RuntimeException(e.getMessage());
 66    }
 67
 68    public static HsqlException error(int code, String add) {
 69
 70        String s = getMessage(code);
 71
 72        if (add != null) {
 73            s += ": " + add.toString();
 74        }
 75
 76        return new HsqlException(s.substring(SQL_STATE_DIGITS + 1),
 77                                 s.substring(0, SQL_STATE_DIGITS), -code);
 78    }
 79
 80    public static HsqlException error(int code) {
 81        return error(code, 0, null);
 82    }
 83
 84    public static HsqlException error(int code, Throwable t) {
 85
 86        String message = getMessage(code, 0, null);
 87
 88        return new HsqlException(t, message.substring(0, SQL_STATE_DIGITS),
 89                                 -code);
 90    }
 91
 92    /**
 93     * Compose error message by inserting the strings in the add parameters
 94     * in placeholders within the error message. The message string contains
 95     * $$ markers for each context variable. Context variables are supplied in
 96     * the add parameters.
 97     *
 98     * @param code      main error code
 99     * @param subCode   sub error code (if 0 => no subMessage!)
100     * @param   add     optional parameters
101     *
102     * @return an <code>HsqlException</code>
103     */
104    public static HsqlException error(int code, int subCode,
105                                      final Object[] add) {
106
107        String message = getMessage(code, subCode, add);
108        int    sqlCode = subCode < ERROR_CODE_BASE ? code
109                                                   : subCode;
110
111        return new HsqlException(message.substring(SQL_STATE_DIGITS + 1),
112                                 message.substring(0, SQL_STATE_DIGITS),
113                                 -sqlCode);
114    }
115
116    public static HsqlException error(int code, int code2) {
117        return error(code, getMessage(code2));
118    }
119
120    /**
121     * Compose error
122     * in placeholders within the error message. The message string contains
123     * $$ markers for each context variable. Context variables are supplied in
124     * the add parameters.
125     *
126     * @see HsqlException#HsqlException(String, String, int)
127     * @return an <code>HsqlException</code>
128     */
129    public static HsqlException error(String message, String sqlState, int i) {
130        return new HsqlException(message, sqlState, i);
131    }
132
133    /**
134     * Compose error message by inserting the strings in the add variables
135     * in placeholders within the error message. The message string contains
136     * $$ markers for each context variable. Context variables are supplied in
137     * the add parameter. (by Loic Lefevre)
138     *
139     * @param message  message string
140     * @param add      optional parameters
141     *
142     * @return an <code>HsqlException</code>
143     */
144    private static String insertStrings(String message, Object[] add) {
145
146        StringBuffer sb        = new StringBuffer(message.length() + 32);
147        int          lastIndex = 0;
148        int          escIndex  = message.length();
149
150        // removed test: i < add.length
151        // because if mainErrorMessage is equal to "blabla $$"
152        // then the statement escIndex = mainErrorMessage.length();
153        // is never reached!  ???
154        for (int i = 0; i < add.length; i++) {
155            escIndex = message.indexOf(MESSAGE_TAG, lastIndex);
156
157            if (escIndex == -1) {
158                break;
159            }
160
161            sb.append(message.substring(lastIndex, escIndex));
162            sb.append(add[i] == null ? "null exception message"
163                                     : add[i].toString());
164
165            lastIndex = escIndex + MESSAGE_TAG.length();
166        }
167
168        escIndex = message.length();
169
170        sb.append(message.substring(lastIndex, escIndex));
171
172        return sb.toString();
173    }
174
175    /**
176     * Returns the error message given the error code.<br/>
177     * This method is be used when throwing exception other
178     * than <code>HsqlException</code>.
179     *
180     * @param errorCode    the error code associated to the error message
181     * @return  the error message associated with the error code
182     */
183    public static String getMessage(final int errorCode) {
184        return getMessage(errorCode, 0, null);
185    }
186
187    /**
188     * Returns the error SQL STATE sting given the error code.<br/>
189     * This method is be used when throwing exception based on other exceptions.
190     *
191     * @param errorCode    the error code associated to the error message
192     * @return  the error message associated with the error code
193     */
194    public static String getStateString(final int errorCode) {
195        return getMessage(errorCode, 0, null).substring(0, SQL_STATE_DIGITS);
196    }
197
198    /**
199     * Returns the error message given the error code.<br/> This method is used
200     * when throwing exception other than <code>HsqlException</code>.
201     *
202     * @param code the code for the error message
203     * @param subCode the code for the addon message
204     * @param add value(s) to use to replace the placeholer(s)
205     * @return the error message associated with the error code
206     */
207    public static String getMessage(final int code, int subCode,
208                                    final Object[] add) {
209
210        String message = getResourceString(code);
211
212        if (subCode != 0) {
213            message += getResourceString(subCode);
214        }
215
216        if (add != null) {
217            message = insertStrings(message, add);
218        }
219
220        return message;
221    }
222
223    private static String getResourceString(int code) {
224
225        String key = StringUtil.toZeroPaddedString(code, SQL_CODE_DIGITS,
226            SQL_CODE_DIGITS);
227
228        return BundleHandler.getString(bundleHandle, key);
229    }
230
231    public static HsqlException error(final Result result) {
232        return new HsqlException(result);
233    }
234
235    /**
236     * Used to print messages to System.out
237     *
238     *
239     * @param message message to print
240     */
241    public static void printSystemOut(String message) {
242
243        if (TRACESYSTEMOUT) {
244            System.out.println(message);
245        }
246    }
247
248    /**
249     * Used to print messages to System.out
250     *
251     *
252     * @param message1 message to print
253     * @param message2 message to print
254     */
255    public static void printSystemOut(String message1, long message2) {
256
257        if (TRACESYSTEMOUT) {
258            System.out.print(message1);
259            System.out.println(message2);
260        }
261    }
262}