Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 411   Methods: 13
NCLOC: 235   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
FaultCodes.java 44.4% 83.7% 46.2% 76.2%
coverage coverage
 1    /*
 2    * Licensed to the Apache Software Foundation (ASF) under one or more
 3    * contributor license agreements. See the NOTICE file distributed with
 4    * this work for additional information regarding copyright ownership.
 5    * The ASF licenses this file to You under the Apache License, Version 2.0
 6    * (the "License"); you may not use this file except in compliance with
 7    * the License. You may obtain a copy of the License at
 8    *
 9    * http://www.apache.org/licenses/LICENSE-2.0
 10    *
 11    * Unless required by applicable law or agreed to in writing, software
 12    * distributed under the License is distributed on an "AS IS" BASIS,
 13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14    * See the License for the specific language governing permissions and
 15    * limitations under the License.
 16    *
 17    * $Id: FaultCodes.java 564427 2007-08-09 23:45:48Z vgritsenko $
 18    */
 19   
 20    package org.apache.xindice.core;
 21   
 22    import org.xmldb.api.base.ErrorCodes;
 23    import org.xmldb.api.base.XMLDBException;
 24   
 25    import java.util.HashMap;
 26    import java.util.Map;
 27   
 28    /**
 29    * FaultCodes defines the Xindice specific fault codes and associated error
 30    * messages.
 31    *
 32    * @version $Revision: 564427 $, $Date: 2007-08-09 16:45:48 -0700 (Thu, 09 Aug 2007) $
 33    */
 34    public abstract class FaultCodes {
 35   
 36    /**
 37    * Defines maximum possible fault code
 38    */
 39    public static final int MAX_CODE = 10000;
 40   
 41    private static final Map faultMessages = new HashMap();
 42   
 43    private static final Map faultCodesToErrorCodesMap = new HashMap();
 44   
 45    public static final int GEN = 0;
 46    public static final int OBJ = 100;
 47    public static final int COL = 200;
 48    public static final int IDX = 300;
 49    public static final int TRX = 400;
 50    public static final int DBE = 500;
 51    public static final int QRY = 600;
 52    public static final int SEC = 700;
 53    public static final int URI = 800;
 54    public static final int JAVA = 2000;
 55   
 56    public static final int GEN_UNKNOWN = 0;
 57    public static final int GEN_GENERAL_ERROR = 40;
 58    public static final int GEN_CRITICAL_ERROR = 70;
 59    public static final int GEN_FATAL_ERROR = 90;
 60   
 61    public static final int COL_COLLECTION_NOT_FOUND = 200;
 62    public static final int COL_DOCUMENT_NOT_FOUND = 201;
 63    public static final int COL_DUPLICATE_COLLECTION = 240;
 64    public static final int COL_NULL_RESULT = 241;
 65    public static final int COL_NO_FILER = 242;
 66    public static final int COL_NO_INDEXMANAGER = 242;
 67    public static final int COL_DOCUMENT_MALFORMED = 243;
 68    public static final int COL_CANNOT_STORE = 244;
 69    public static final int COL_CANNOT_RETRIEVE = 245;
 70    public static final int COL_COLLECTION_READ_ONLY = 246;
 71    public static final int COL_COLLECTION_CLOSED = 247;
 72    public static final int COL_DUPLICATE_RESOURCE = 248;
 73    public static final int COL_CANNOT_CREATE = 270;
 74    public static final int COL_CANNOT_DROP = 271;
 75    public static final int COL_INVALID_RESULT = 277;
 76   
 77    public static final int IDX_VALUE_NOT_FOUND = 300;
 78    public static final int IDX_INDEX_NOT_FOUND = 301;
 79    public static final int IDX_MATCHES_NOT_FOUND = 340;
 80    public static final int IDX_DUPLICATE_INDEX = 341;
 81    public static final int IDX_NOT_SUPPORTED = 370;
 82    public static final int IDX_STYLE_NOT_FOUND = 371;
 83    public static final int IDX_CORRUPTED = 372;
 84    public static final int IDX_CANNOT_CREATE = 373;
 85   
 86    public static final int TRX_DOC_LOCKED = 400;
 87    public static final int TRX_NO_CONTEXT = 440;
 88    public static final int TRX_NOT_ACTIVE = 441;
 89    public static final int TRX_NOT_SUPPORTED = 470;
 90   
 91    public static final int DBE_NO_PARENT = 500;
 92    public static final int DBE_CANNOT_DROP = 570;
 93    public static final int DBE_CANNOT_CREATE = 571;
 94    public static final int DBE_CANNOT_READ = 572;
 95   
 96    public static final int QRY_NULL_RESULT = 600;
 97    public static final int QRY_COMPILATION_ERROR = 640;
 98    public static final int QRY_PROCESSING_ERROR = 641;
 99    public static final int QRY_NOT_SUPPORTED = 670;
 100    public static final int QRY_STYLE_NOT_FOUND = 671;
 101   
 102    public static final int SEC_INVALID_USER = 770;
 103    public static final int SEC_INVALID_GROUP = 771;
 104    public static final int SEC_INVALID_ACCESS = 772;
 105    public static final int SEC_INVALID_CREDENTIALS = 773;
 106   
 107    public static final int URI_EMPTY = 800;
 108    public static final int URI_NULL = 801;
 109    public static final int URI_PARSE_ERROR = 820;
 110   
 111    public static final int JAVA_RUNTIME_ERROR = 2070;
 112   
 113   
 114  0 private FaultCodes() {
 115    }
 116   
 117    static {
 118    // General errors 0 series
 119  3 putCodeMessage(GEN_UNKNOWN, "Unknown");
 120  3 putCodeMessage(GEN_GENERAL_ERROR, "General Error");
 121  3 putCodeMessage(GEN_CRITICAL_ERROR, "Critical Error");
 122  3 putCodeMessage(GEN_FATAL_ERROR, "Fatal Error");
 123   
 124  3 addFaultCodeToErrorCodeMap(GEN_UNKNOWN, ErrorCodes.UNKNOWN_ERROR);
 125  3 addFaultCodeToErrorCodeMap(GEN_GENERAL_ERROR, ErrorCodes.UNKNOWN_ERROR);
 126  3 addFaultCodeToErrorCodeMap(GEN_CRITICAL_ERROR, ErrorCodes.UNKNOWN_ERROR);
 127  3 addFaultCodeToErrorCodeMap(GEN_FATAL_ERROR, ErrorCodes.UNKNOWN_ERROR);
 128   
 129    // Collection-related errors 200 series
 130  3 putCodeMessage(COL_COLLECTION_NOT_FOUND, "Collection Not Found");
 131  3 putCodeMessage(COL_DOCUMENT_NOT_FOUND, "Collection Document Not Found");
 132  3 putCodeMessage(COL_DUPLICATE_COLLECTION, "Collection Duplicated");
 133  3 putCodeMessage(COL_NULL_RESULT, "Collection Null Result");
 134  3 putCodeMessage(COL_NO_FILER, "Collection No Filer");
 135  3 putCodeMessage(COL_NO_INDEXMANAGER, "Collection No IndexManager");
 136  3 putCodeMessage(COL_DOCUMENT_MALFORMED, "Collection Document Malformed");
 137  3 putCodeMessage(COL_CANNOT_STORE, "Collection Cannot Store");
 138  3 putCodeMessage(COL_CANNOT_RETRIEVE, "Collection Cannot Retrieve");
 139  3 putCodeMessage(COL_COLLECTION_READ_ONLY, "Collection Read-only");
 140  3 putCodeMessage(COL_COLLECTION_CLOSED, "Collection Closed");
 141  3 putCodeMessage(COL_DUPLICATE_RESOURCE, "Resource Duplicated");
 142  3 putCodeMessage(COL_CANNOT_CREATE, "Collection Cannot Create");
 143  3 putCodeMessage(COL_CANNOT_DROP, "Collection Cannot Drop");
 144  3 putCodeMessage(COL_INVALID_RESULT, "Collection Invalid Result");
 145   
 146  3 addFaultCodeToErrorCodeMap(COL_COLLECTION_NOT_FOUND, ErrorCodes.NO_SUCH_COLLECTION);
 147  3 addFaultCodeToErrorCodeMap(COL_DOCUMENT_NOT_FOUND, ErrorCodes.NO_SUCH_RESOURCE);
 148  3 addFaultCodeToErrorCodeMap(COL_DUPLICATE_COLLECTION, ErrorCodes.INVALID_COLLECTION);
 149  3 addFaultCodeToErrorCodeMap(COL_NULL_RESULT, ErrorCodes.INVALID_COLLECTION);
 150  3 addFaultCodeToErrorCodeMap(COL_NO_FILER, ErrorCodes.INVALID_COLLECTION);
 151  3 addFaultCodeToErrorCodeMap(COL_NO_INDEXMANAGER, ErrorCodes.INVALID_COLLECTION);
 152  3 addFaultCodeToErrorCodeMap(COL_DOCUMENT_MALFORMED, ErrorCodes.INVALID_RESOURCE);
 153  3 addFaultCodeToErrorCodeMap(COL_CANNOT_STORE, ErrorCodes.VENDOR_ERROR);
 154  3 addFaultCodeToErrorCodeMap(COL_CANNOT_RETRIEVE, ErrorCodes.VENDOR_ERROR);
 155  3 addFaultCodeToErrorCodeMap(COL_COLLECTION_READ_ONLY, ErrorCodes.VENDOR_ERROR);
 156  3 addFaultCodeToErrorCodeMap(COL_COLLECTION_CLOSED, ErrorCodes.COLLECTION_CLOSED);
 157  3 addFaultCodeToErrorCodeMap(COL_CANNOT_CREATE, ErrorCodes.VENDOR_ERROR);
 158  3 addFaultCodeToErrorCodeMap(COL_CANNOT_DROP, ErrorCodes.VENDOR_ERROR);
 159  3 addFaultCodeToErrorCodeMap(COL_INVALID_RESULT, ErrorCodes.VENDOR_ERROR);
 160   
 161    // Index-related errors 300 series
 162  3 putCodeMessage(IDX_VALUE_NOT_FOUND, "Index Value Not Found");
 163  3 putCodeMessage(IDX_INDEX_NOT_FOUND, "Index Not Found");
 164  3 putCodeMessage(IDX_MATCHES_NOT_FOUND, "Index Matches Not Found");
 165  3 putCodeMessage(IDX_DUPLICATE_INDEX, "Index Duplicate Index");
 166  3 putCodeMessage(IDX_NOT_SUPPORTED, "Index Not Supported");
 167  3 putCodeMessage(IDX_STYLE_NOT_FOUND, "Index Style Not Found");
 168  3 putCodeMessage(IDX_CORRUPTED, "Index Corrupted");
 169  3 putCodeMessage(IDX_CANNOT_CREATE, "Index Cannot Create");
 170   
 171  3 addFaultCodeToErrorCodeMap(IDX_VALUE_NOT_FOUND, ErrorCodes.INVALID_RESOURCE);
 172  3 addFaultCodeToErrorCodeMap(IDX_INDEX_NOT_FOUND, ErrorCodes.NO_SUCH_RESOURCE);
 173  3 addFaultCodeToErrorCodeMap(IDX_MATCHES_NOT_FOUND, ErrorCodes.VENDOR_ERROR);
 174  3 addFaultCodeToErrorCodeMap(IDX_DUPLICATE_INDEX, ErrorCodes.INVALID_RESOURCE);
 175  3 addFaultCodeToErrorCodeMap(IDX_NOT_SUPPORTED, ErrorCodes.NOT_IMPLEMENTED);
 176  3 addFaultCodeToErrorCodeMap(IDX_STYLE_NOT_FOUND, ErrorCodes.NO_SUCH_RESOURCE);
 177  3 addFaultCodeToErrorCodeMap(IDX_CORRUPTED, ErrorCodes.INVALID_RESOURCE);
 178  3 addFaultCodeToErrorCodeMap(IDX_CANNOT_CREATE, ErrorCodes.VENDOR_ERROR);
 179   
 180    // Transaction-related errors 400 series
 181  3 putCodeMessage(TRX_DOC_LOCKED, "Transaction Document Locked");
 182  3 putCodeMessage(TRX_NO_CONTEXT, "Transaction No Context");
 183  3 putCodeMessage(TRX_NOT_ACTIVE, "Transaction Not Active");
 184  3 putCodeMessage(TRX_NOT_SUPPORTED, "Transaction Not Supported");
 185   
 186  3 addFaultCodeToErrorCodeMap(TRX_DOC_LOCKED, ErrorCodes.VENDOR_ERROR);
 187  3 addFaultCodeToErrorCodeMap(TRX_NO_CONTEXT, ErrorCodes.INVALID_RESOURCE);
 188  3 addFaultCodeToErrorCodeMap(TRX_NOT_ACTIVE, ErrorCodes.VENDOR_ERROR);
 189  3 addFaultCodeToErrorCodeMap(TRX_NOT_SUPPORTED, ErrorCodes.VENDOR_ERROR);
 190   
 191    // Database-related errors 500 series
 192  3 putCodeMessage(DBE_NO_PARENT, "Database No Parent");
 193  3 putCodeMessage(DBE_CANNOT_DROP, "Database Cannot Drop");
 194  3 putCodeMessage(DBE_CANNOT_CREATE, "Database Cannot Create");
 195  3 putCodeMessage(DBE_CANNOT_READ, "Database Cannot Read");
 196   
 197  3 addFaultCodeToErrorCodeMap(DBE_NO_PARENT, ErrorCodes.INVALID_DATABASE);
 198  3 addFaultCodeToErrorCodeMap(DBE_CANNOT_DROP, ErrorCodes.VENDOR_ERROR);
 199  3 addFaultCodeToErrorCodeMap(DBE_CANNOT_CREATE, ErrorCodes.VENDOR_ERROR);
 200  3 addFaultCodeToErrorCodeMap(DBE_CANNOT_READ, ErrorCodes.VENDOR_ERROR);
 201   
 202    // Query-related errors 600 series
 203  3 putCodeMessage(QRY_NULL_RESULT, "Query Null Result");
 204  3 putCodeMessage(QRY_COMPILATION_ERROR, "Query Compilation Error");
 205  3 putCodeMessage(QRY_PROCESSING_ERROR, "Query Processing Error");
 206  3 putCodeMessage(QRY_NOT_SUPPORTED, "Query Not Supported");
 207  3 putCodeMessage(QRY_STYLE_NOT_FOUND, "Query Style Not Found");
 208   
 209  3 addFaultCodeToErrorCodeMap(QRY_NULL_RESULT, ErrorCodes.VENDOR_ERROR);
 210  3 addFaultCodeToErrorCodeMap(QRY_COMPILATION_ERROR, ErrorCodes.VENDOR_ERROR);
 211  3 addFaultCodeToErrorCodeMap(QRY_PROCESSING_ERROR, ErrorCodes.VENDOR_ERROR);
 212  3 addFaultCodeToErrorCodeMap(QRY_NOT_SUPPORTED, ErrorCodes.VENDOR_ERROR);
 213  3 addFaultCodeToErrorCodeMap(QRY_STYLE_NOT_FOUND, ErrorCodes.VENDOR_ERROR);
 214   
 215    // Security-related errors 700 series
 216  3 putCodeMessage(SEC_INVALID_USER, "Security Invalid User");
 217  3 putCodeMessage(SEC_INVALID_GROUP, "Security Invalid Group");
 218  3 putCodeMessage(SEC_INVALID_ACCESS, "Security Invalid Access");
 219  3 putCodeMessage(SEC_INVALID_CREDENTIALS, "Security Invalid Credentials");
 220   
 221  3 addFaultCodeToErrorCodeMap(SEC_INVALID_USER, ErrorCodes.PERMISSION_DENIED);
 222  3 addFaultCodeToErrorCodeMap(SEC_INVALID_GROUP, ErrorCodes.PERMISSION_DENIED);
 223  3 addFaultCodeToErrorCodeMap(SEC_INVALID_ACCESS, ErrorCodes.PERMISSION_DENIED);
 224  3 addFaultCodeToErrorCodeMap(SEC_INVALID_CREDENTIALS, ErrorCodes.PERMISSION_DENIED);
 225   
 226    // URI-related errors 800 series
 227  3 putCodeMessage(URI_EMPTY, "URI Empty");
 228  3 putCodeMessage(URI_NULL, "URI Null");
 229  3 putCodeMessage(URI_PARSE_ERROR, "URI Parse Error");
 230   
 231  3 addFaultCodeToErrorCodeMap(URI_EMPTY, ErrorCodes.INVALID_URI);
 232  3 addFaultCodeToErrorCodeMap(URI_NULL, ErrorCodes.INVALID_URI);
 233  3 addFaultCodeToErrorCodeMap(URI_PARSE_ERROR, ErrorCodes.INVALID_URI);
 234   
 235    // Java-related errors 2000 series
 236  3 putCodeMessage(JAVA_RUNTIME_ERROR, "Java Runtime Error");
 237    }
 238   
 239  144 private static void putCodeMessage(int code, String message) {
 240  144 faultMessages.put(new Integer(code), message);
 241    }
 242   
 243  138 private static void addFaultCodeToErrorCodeMap(int code, int errCode) {
 244  138 faultCodesToErrorCodesMap.put(new Integer(code), new Integer(errCode));
 245    }
 246   
 247    /**
 248    * getErrorCode returns the associated XML:DB error code for the
 249    * specified Xindice fault code
 250    *
 251    * @param faultCode The Xindice fault code
 252    * @return the associated XML:DB error code
 253    */
 254  0 public static int getErrorCode(int faultCode) {
 255  0 Object err = faultCodesToErrorCodesMap.get(new Integer(faultCode));
 256  0 if (err == null) {
 257  0 return ErrorCodes.UNKNOWN_ERROR;
 258    } else {
 259  0 return ((Integer) err).intValue();
 260    }
 261    }
 262   
 263    /**
 264    * getMessage returns a textual form for the specified fault code.
 265    *
 266    * @param code The Fault Code
 267    * @return It's textual form
 268    */
 269  0 public static String getMessage(int code) {
 270  0 String msg = (String) faultMessages.get(new Integer(code));
 271  0 return msg != null ? msg : "";
 272    }
 273   
 274    /**
 275    * createXMLDBException creates an XMLDBException instance based
 276    * on the specified Exception. If the Exception is a DBException,
 277    * it will extract any important information from it (like fault
 278    * codes and messages)
 279    *
 280    * @param e The Exception to use
 281    * @return An XMLDBException instance
 282    */
 283  42 public static XMLDBException createXMLDBException(Exception e) {
 284  42 return createXMLDBException(null, e);
 285    }
 286   
 287    /**
 288    * createXMLDBException creates an XMLDBException instance based
 289    * on the specified Exception. If the Exception is a DBException,
 290    * it will extract any important information from it (like fault
 291    * codes and messages)
 292    *
 293    * @param e The Exception to use
 294    * @param message The Message to use
 295    * @return An XMLDBException instance
 296    */
 297  42 public static XMLDBException createXMLDBException(String message, Exception e) {
 298   
 299  42 int faultCode = e instanceof RuntimeException ? JAVA_RUNTIME_ERROR : GEN_UNKNOWN;
 300  42 return createXMLDBException(faultCode, message, e);
 301    }
 302   
 303  46 public static XMLDBException createXMLDBException(int faultCode, String message, Exception e) {
 304  46 return createXMLDBException(ErrorCodes.VENDOR_ERROR, faultCode, message, e);
 305    }
 306   
 307    /**
 308    * Creates an XMLDBException instance based
 309    * on the specified fault code, message, and Exception. If the Exception is a DBException,
 310    * it will extract any important information from it (like fault
 311    * codes and messages). If the exception is an instance of XindiceException,
 312    * the exception class name and message of any Throwable(s) available
 313    * via XindiceException.getCause will also be incorporated
 314    * into the message. If a chain of wrapped XindiceExceptions is available,
 315    * the chain will be followed incorporating the class name and message
 316    * at each level.
 317    *
 318    * @param errorCode error code for the XMLDBException
 319    * @param faultCode the fault code for the created exception
 320    * @param message message for the XMLDBException, or null to use message from e
 321    * @param e The Exception to use
 322    * @return An XMLDBException instance
 323    */
 324  58 public static XMLDBException createXMLDBException(int errorCode, int faultCode, String message, Exception e) {
 325  58 if (e instanceof XMLDBException) {
 326  27 return (XMLDBException) e;
 327    }
 328   
 329  31 if (e instanceof DBException) {
 330  28 faultCode = ((DBException) e).faultCode;
 331    }
 332   
 333  31 if (message == null) {
 334  15 message = e.getMessage();
 335    } else {
 336  16 message += ": " + e.getMessage();
 337    }
 338   
 339    // TODO: Mapping FaultCodes.COL_DOCUMENT_NOT_FOUND -> ErrorCodes.NO_SUCH_RESOURCE etc
 340   
 341  31 return new XMLDBException(errorCode, faultCode, message, e);
 342    }
 343   
 344    /**
 345    * getFaultCodeType examines the provided exception to determine
 346    * the general fault code that is associated with it. General
 347    * fault codes are reduced from actual fault codes to be one of
 348    * the GEN_ prefixed fault code values.
 349    *
 350    * @param e The Exception to examine
 351    * @return The General Fault Code
 352    */
 353  0 public static int getFaultCodeType(Exception e) {
 354  0 int code = 0;
 355  0 if (e instanceof DBException) {
 356  0 code = ((DBException) e).faultCode;
 357    }
 358   
 359    // Strip it to the General series
 360  0 code = code % 100;
 361    // Narrow to a General value
 362  0 code = code - (code % 10);
 363  0 return code;
 364    }
 365   
 366    /**
 367    * getFaultCodeSeries examines the provided exception to
 368    * determine the fault code series that is associated with it.
 369    * Series are reduced from actual fault codes to be one of
 370    * the fault code prefixes (ex: COL, DB, SEC).
 371    *
 372    * @param e The Exception to examine
 373    * @return The Fault Code Series
 374    */
 375  0 public static int getFaultCodeSeries(Exception e) {
 376  0 int code = 0;
 377  0 if (e instanceof DBException) {
 378  0 code = ((DBException) e).faultCode;
 379    }
 380   
 381    // Strip it to the series
 382  0 code = code - (code % 100);
 383  0 return code;
 384    }
 385   
 386    /**
 387    * getFaultCode examines the provided exception to determine
 388    * the fault code that is associated with it.
 389    *
 390    * @param e The Exception to examine
 391    * @return The Fault Code
 392    */
 393  0 public static int getFaultCode(Exception e) {
 394  0 if (e instanceof DBException) {
 395  0 return ((DBException) e).faultCode;
 396    } else {
 397  0 return 0;
 398    }
 399    }
 400   
 401    /**
 402    * getFaultMessage examines the provided exception to determine
 403    * the fault message that is associated with it.
 404    *
 405    * @param e The Exception to examine
 406    * @return The Fault Message
 407    */
 408  0 public static String getFaultMessage(Exception e) {
 409  0 return getMessage(getFaultCode(e));
 410    }
 411    }