vgritsenko 2003/08/14 21:34:54
Modified: java/src/org/apache/xindice/client/xmldb XindiceCollection.java java/src/org/apache/xindice/client/xmldb/embed CollectionImpl.java DatabaseImpl.java java/src/org/apache/xindice/core Collection.java CollectionManager.java FaultCodes.java Log: Improving exception handling: * add methods to FaultCodes * Use FaultCodes.createXMLDBException Revision Changes Path 1.13 +2 -3 xml-xindice/java/src/org/apache/xindice/client/xmldb/XindiceCollection.java Index: XindiceCollection.java =================================================================== RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/client/xmldb/XindiceCollection.java,v retrieving revision 1.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- XindiceCollection.java 13 Aug 2003 18:51:15 -0000 1.12 +++ XindiceCollection.java 15 Aug 2003 04:34:54 -0000 1.13 @@ -220,7 +220,6 @@ protected void checkOpen() throws XMLDBException { if (!isOpen()) { - throw new XMLDBException(ErrorCodes.COLLECTION_CLOSED); } } 1.22 +62 -47 xml-xindice/java/src/org/apache/xindice/client/xmldb/embed/CollectionImpl.java Index: CollectionImpl.java =================================================================== RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/client/xmldb/embed/CollectionImpl.java,v retrieving revision 1.21 retrieving revision 1.22 diff -u -r1.21 -r1.22 --- CollectionImpl.java 13 Aug 2003 18:51:15 -0000 1.21 +++ CollectionImpl.java 15 Aug 2003 04:34:54 -0000 1.22 @@ -1,5 +1,3 @@ -package org.apache.xindice.client.xmldb.embed; - /* * The Apache Software License, Version 1.1 * @@ -58,6 +56,9 @@ * * $Id$ */ + +package org.apache.xindice.client.xmldb.embed; + import java.util.Enumeration; import java.util.Hashtable; @@ -96,8 +97,8 @@ */ public class CollectionImpl extends XindiceCollection { - Database db = null; - Collection col = null; + private Database db = null; + private Collection col = null; /** * Creates new <code>CollectionImpl</code> instance representing connection @@ -114,10 +115,13 @@ try { this.col = db.getCollection(collPath); } catch (Exception e) { - throw new XMLDBException(ErrorCodes.NO_SUCH_COLLECTION, "Collection not found: " + collPath, e); + throw FaultCodes.createXMLDBException(ErrorCodes.INVALID_COLLECTION, + "Collection not available: " + collPath, e); } + if (this.col == null) { - throw new XMLDBException(ErrorCodes.NO_SUCH_COLLECTION, "Collection not found: " + collPath); + throw new XMLDBException(ErrorCodes.NO_SUCH_COLLECTION, + "Collection not found: " + collPath); } } @@ -146,17 +150,18 @@ Document doc = (Document) entry; // This should probably just pass the document. - return new XMLResourceImpl(id, id, this, ((DocumentImpl) doc).getSymbols(), ((DocumentImpl) doc).getDataBytes()); + return new XMLResourceImpl(id, id, this, + ((DocumentImpl) doc).getSymbols(), + ((DocumentImpl) doc).getDataBytes()); } else if (entry instanceof byte[]) { return new BinaryResourceImpl(id, entry); } else { - throw new XMLDBException( - ErrorCodes.UNKNOWN_RESOURCE_TYPE, - "Internal error: unexpected type " + entry.getClass().getName()); + throw new XMLDBException(ErrorCodes.UNKNOWN_RESOURCE_TYPE, + "Internal error: unexpected type " + entry.getClass().getName()); } } catch (Exception e) { - throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, "Message: " + e.getMessage(), e); + throw FaultCodes.createXMLDBException("Resource not available: " + id, e); } } @@ -177,7 +182,7 @@ try { return (int) col.getDocumentCount(); } catch (Exception e) { - throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e); + throw FaultCodes.createXMLDBException(e); } } @@ -198,7 +203,8 @@ public void storeResource(Resource res) throws XMLDBException { if (res.getContent() == null) { - throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "no resource data"); + throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, + "No resource data"); } checkOpen(); @@ -209,7 +215,8 @@ if (content instanceof byte[]) { bytes = (byte[]) content; } else { - throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "The contents of a binary resource must have type byte[]."); + throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, + "The contents of a binary resource must have type byte[]."); } try { @@ -219,8 +226,9 @@ String name = col.insertBinary(bytes).toString(); ((BinaryResourceImpl) res).setId(name); } - } catch (DBException e) { - throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e); + } catch (Exception e) { + throw FaultCodes.createXMLDBException(ErrorCodes.INVALID_RESOURCE, + "Invalid resource:" + res.getId(), e); } } else if (res instanceof XMLResource) { @@ -236,13 +244,16 @@ ((XMLResourceImpl) res).setId(name); } } else { - throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "A resource must be a document in order to be stored."); + throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, + "A resource must be a document in order to be stored."); } } catch (Exception e) { - throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e); + throw FaultCodes.createXMLDBException(ErrorCodes.INVALID_RESOURCE, + "Invalid resource:" + res.getId(), e); } } else { - throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "Only XMLResource and BinaryResource supported"); + throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, + "Only XMLResource and BinaryResource supported"); } } @@ -253,7 +264,7 @@ /* see superclass for documentation */ public String getURI() { - return "xmldb:" + DatabaseImpl.DRIVER_NAME + "://" + collPath; + return "xmldb:" + DatabaseImpl.DRIVER_NAME + "://" + getCanonicalName(); } /** @@ -271,17 +282,21 @@ public org.xmldb.api.base.Collection getChildCollection(String name) throws XMLDBException { if (name.indexOf('/') != -1) { - throw new XMLDBException(ErrorCodes.INVALID_COLLECTION); + throw new XMLDBException(ErrorCodes.INVALID_COLLECTION, + "Invalid collection: " + name); } try { - return new CollectionImpl(db, collPath + "/" + name); + return new CollectionImpl(db, getCanonicalName() + "/" + name); } catch (XMLDBException e) { if (e.errorCode == ErrorCodes.NO_SUCH_COLLECTION) { // per getChildCollection contract, return null if not found return null; } throw e; + } catch (Exception e) { + throw FaultCodes.createXMLDBException(ErrorCodes.INVALID_COLLECTION, + "Invalid collection: " + name, e); } } @@ -301,7 +316,7 @@ try { return col.createNewOID().toString(); } catch (Exception e) { - throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e); + throw FaultCodes.createXMLDBException(e); } } @@ -316,7 +331,7 @@ * <code>ErrorCodes.VENDOR_ERROR</code> for any vendor * specific errors that occur.<br /> */ - public void close() throws org.xmldb.api.base.XMLDBException { + public void close() throws XMLDBException { col = null; db.flushConfig(); } @@ -347,6 +362,8 @@ return null; } throw e; + } catch (Exception e) { + throw FaultCodes.createXMLDBException(e); } } @@ -381,7 +398,7 @@ try { col.remove(res.getId()); } catch (Exception e) { - throw new XMLDBException(ErrorCodes.NO_SUCH_RESOURCE, e); + throw FaultCodes.createXMLDBException(e); } } @@ -404,7 +421,7 @@ try { return col.listCollections(); } catch (Exception e) { - throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e); + throw FaultCodes.createXMLDBException(e); } } @@ -425,7 +442,7 @@ try { return (int) col.countCollections(); } catch (Exception e) { - throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e); + throw FaultCodes.createXMLDBException(e); } } @@ -446,7 +463,7 @@ try { return col.listDocuments(); } catch (Exception e) { - throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e); + throw FaultCodes.createXMLDBException(e); } } @@ -454,7 +471,6 @@ public ResourceSet query(String name, String queryLang, String query, Hashtable nsMap) throws XMLDBException { checkOpen(); - try { NodeSet result; if (name != null) { @@ -465,7 +481,8 @@ return new ResourceSetImpl(this, nodesetToDocument(result)); } catch (Exception e) { - throw FaultCodes.createXMLDBException(FaultCodes.QRY_PROCESSING_ERROR, "Query error: " + e.getMessage(), e); + throw FaultCodes.createXMLDBException(FaultCodes.QRY_PROCESSING_ERROR, + "Query error: " + e.getMessage(), e); } } @@ -487,7 +504,9 @@ return createCollection(name, doc); } catch (Exception e) { - throw new XMLDBException(ErrorCodes.INVALID_COLLECTION, "Cannot create child collection", e); + throw FaultCodes.createXMLDBException(ErrorCodes.INVALID_COLLECTION, + FaultCodes.GEN_UNKNOWN, + "Cannot create child collection", e); } } @@ -500,7 +519,9 @@ return getChildCollection(name); } catch (Exception e) { - throw new XMLDBException(ErrorCodes.INVALID_COLLECTION, "Cannot create child collection", e); + throw FaultCodes.createXMLDBException(ErrorCodes.INVALID_COLLECTION, + FaultCodes.GEN_UNKNOWN, + "Cannot create child collection", e); } } @@ -511,7 +532,9 @@ try { col.dropCollection(col.getCollection(childName)); } catch (Exception e) { - throw new XMLDBException(ErrorCodes.INVALID_COLLECTION, "Cannot remove child collection", e); + throw FaultCodes.createXMLDBException(ErrorCodes.INVALID_COLLECTION, + FaultCodes.GEN_UNKNOWN, + "Cannot remove child collection", e); } } @@ -565,8 +588,7 @@ int count = 0; while (ns != null && ns.hasMoreNodes()) { final Object element = ns.getNextNode(); - if (element instanceof Node) - { + if (element instanceof Node) { Node n = (Node) element; if (n.getNodeType() == Node.DOCUMENT_NODE) { @@ -578,17 +600,11 @@ } root.appendChild(doc.importNode(n, true)); - } - else if (element instanceof Boolean || element instanceof Double) - { + } else if (element instanceof Boolean || element instanceof Double) { root.appendChild(doc.createTextNode(element.toString())); - } - else if (element instanceof String) - { + } else if (element instanceof String) { root.appendChild(doc.createTextNode((String) element)); - } - else - { + } else { throw new XindiceRuntimeException("Unknown result type (" + element.getClass().getName() + "in nodeset"); } @@ -596,7 +612,6 @@ } root.setAttribute("count", Integer.toString(count)); - return doc; } 1.19 +3 -2 xml-xindice/java/src/org/apache/xindice/client/xmldb/embed/DatabaseImpl.java Index: DatabaseImpl.java =================================================================== RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/client/xmldb/embed/DatabaseImpl.java,v retrieving revision 1.18 retrieving revision 1.19 diff -u -r1.18 -r1.19 --- DatabaseImpl.java 9 Aug 2003 02:44:23 -0000 1.18 +++ DatabaseImpl.java 15 Aug 2003 04:34:54 -0000 1.19 @@ -56,6 +56,7 @@ * * CVS $Id$ */ + package org.apache.xindice.client.xmldb.embed; import org.apache.commons.logging.Log; 1.37 +6 -5 xml-xindice/java/src/org/apache/xindice/core/Collection.java Index: Collection.java =================================================================== RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/core/Collection.java,v retrieving revision 1.36 retrieving revision 1.37 diff -u -r1.36 -r1.37 --- Collection.java 15 Aug 2003 03:06:02 -0000 1.36 +++ Collection.java 15 Aug 2003 04:34:54 -0000 1.37 @@ -1326,7 +1326,8 @@ } if (!filer.deleteRecord(objKey)) { - throw new DBException(FaultCodes.COL_DOCUMENT_NOT_FOUND, "Document Does Not Exist"); + throw new DBException(FaultCodes.COL_DOCUMENT_NOT_FOUND, + "Document Does Not Exist"); } // update the meta for this collection if necessary @@ -1369,7 +1370,7 @@ throw new DBException(FaultCodes.GEN_UNKNOWN, "Mismatch type of meta data for collection " + getCanonicalName()); } - + MetaSystemCollection metacol = getMetaSystemCollection(); MetaData current = metacol.getCollectionMeta(this); current.copyDataFrom(meta); @@ -1540,7 +1541,7 @@ throw new DBException(FaultCodes.GEN_UNKNOWN, "Mismatch type of meta data for document " + getCanonicalDocumentName(id)); } - + MetaSystemCollection metacol = getMetaSystemCollection(); MetaData current = metacol.getDocumentMeta(this, id); current.copyDataFrom(meta); 1.19 +4 -3 xml-xindice/java/src/org/apache/xindice/core/CollectionManager.java Index: CollectionManager.java =================================================================== RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/core/CollectionManager.java,v retrieving revision 1.18 retrieving revision 1.19 diff -u -r1.18 -r1.19 --- CollectionManager.java 12 Aug 2003 02:57:30 -0000 1.18 +++ CollectionManager.java 15 Aug 2003 04:34:54 -0000 1.19 @@ -241,8 +241,9 @@ cm = (CollectionManager) cm.collections.get(path); } return (Collection) cm; - } else + } else { return (Collection) collections.get(path); + } } /** 1.17 +31 -9 xml-xindice/java/src/org/apache/xindice/core/FaultCodes.java Index: FaultCodes.java =================================================================== RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/core/FaultCodes.java,v retrieving revision 1.16 retrieving revision 1.17 diff -u -r1.16 -r1.17 --- FaultCodes.java 15 Aug 2003 03:08:36 -0000 1.16 +++ FaultCodes.java 15 Aug 2003 04:34:54 -0000 1.17 @@ -238,12 +238,27 @@ * @return An XMLDBException instance */ public static XMLDBException createXMLDBException(Exception e) { - if (e instanceof XMLDBException) { - return (XMLDBException) e; - } + return createXMLDBException(null, e); + } + + /** + * createXMLDBException creates an XMLDBException instance based + * on the specified Exception. If the Exception is a DBException, + * it will extract any important information from it (like fault + * codes and messages) + * + * @param e The Exception to use + * @param message The Message to use + * @return An XMLDBException instance + */ + public static XMLDBException createXMLDBException(String message, Exception e) { int faultCode = e instanceof RuntimeException ? JAVA_RUNTIME_ERROR : GEN_UNKNOWN; - return createXMLDBException(faultCode, null, e); + return createXMLDBException(faultCode, message, e); + } + + public static XMLDBException createXMLDBException(int faultCode, String message, Exception e) { + return createXMLDBException(ErrorCodes.VENDOR_ERROR, faultCode, message, e); } /** @@ -256,6 +271,7 @@ * into the message. If a chain of wrapped XindiceExceptions is available, * the chain will be followed incorporating the class name and message * at each level. + * * TODO: This should only be considered a temporary fix * TODO: until such time as the xmldb API can be refined to allow for wrapped * TODO: throwables in XMLDBException. @@ -264,7 +280,7 @@ * @param e The Exception to use * @return An XMLDBException instance */ - public static XMLDBException createXMLDBException(int faultCode, String message, Exception e) { + public static XMLDBException createXMLDBException(int errorCode, int faultCode, String message, Exception e) { if (e instanceof XMLDBException) { return (XMLDBException) e; } @@ -273,7 +289,13 @@ faultCode = ((DBException) e).faultCode; } - return new XMLDBException(ErrorCodes.VENDOR_ERROR, faultCode, message, e); + if (message == null) { + message = e.getMessage(); + } + + // TODO: Mapping FaultCodes.COL_DOCUMENT_NOT_FOUND -> ErrorCodes.NO_SUCH_RESOURCE etc + + return new XMLDBException(errorCode, faultCode, message, e); } /** @@ -312,7 +334,7 @@ if (e instanceof DBException) { code = ((DBException) e).faultCode; } - + // Strip it to the series code = code - (code % 100); return code;