From: Jakub Skoczen Date: Wed, 24 Feb 2010 11:41:34 +0000 (+0100) Subject: Rewrite exception handling X-Git-Tag: v1.2~6 X-Git-Url: http://jsfdemo.indexdata.com/cgi-bin?a=commitdiff_plain;h=95873c80a22cbcb609b5a0260d624359500dcf19;p=yaz4j-moved-to-github.git Rewrite exception handling --- diff --git a/src/main/java/org/yaz4j/Connection.java b/src/main/java/org/yaz4j/Connection.java index 075cfa2..3f8dc25 100644 --- a/src/main/java/org/yaz4j/Connection.java +++ b/src/main/java/org/yaz4j/Connection.java @@ -1,19 +1,11 @@ package org.yaz4j; -import org.yaz4j.exception.ZoomImplementationException; -import org.yaz4j.exception.ConnectionTimeoutException; -import org.yaz4j.exception.InitRejectedException; -import org.yaz4j.exception.Bib1Exception; -import org.yaz4j.exception.InvalidQueryException; -import org.yaz4j.exception.Bib1Diagnostic; -import org.yaz4j.exception.ConnectionUnavailableException; import org.yaz4j.exception.ZoomException; import org.yaz4j.jni.SWIGTYPE_p_ZOOM_connection_p; import org.yaz4j.jni.SWIGTYPE_p_ZOOM_query_p; import org.yaz4j.jni.SWIGTYPE_p_ZOOM_resultset_p; import org.yaz4j.jni.SWIGTYPE_p_ZOOM_scanset_p; import org.yaz4j.jni.yaz4jlib; -import org.yaz4j.jni.yaz4jlibConstants; public class Connection { private String host; @@ -31,13 +23,7 @@ public class Connection { // on Linux 'yaz4j' maps to 'libyaz4j.so' (i.e. 'lib' prefix & '.so' extension) // on Windows 'yaz4j' maps to 'yaz4j.dll' (i.e. '.dll' extension) String libName = "yaz4j"; - try { - // System.err.println( "Loading library '"+ System.mapLibraryName( libName ) + "'" ); - System.loadLibrary(libName); - } catch (AbstractMethodError e) { - System.err.println("Fatal Error: Failed to load library '" + System.mapLibraryName(libName) + "'"); - e.printStackTrace(); - } + System.loadLibrary(libName); } public Connection(String host, int port) { @@ -52,44 +38,33 @@ public class Connection { public ResultSet search(String query, QueryType queryType) throws ZoomException { if (closed) throw new IllegalStateException("Connection is closed."); - SWIGTYPE_p_ZOOM_query_p yazQuery = yaz4jlib.ZOOM_query_create(); - ResultSet resultSet = null; - - try { - if (queryType == QueryType.CQLQuery) { - yaz4jlib.ZOOM_query_cql(yazQuery, query); - } else if (queryType == QueryType.PrefixQuery) { - yaz4jlib.ZOOM_query_prefix(yazQuery, query); - } else { - throw new InvalidQueryException("queryType"); - } - - SWIGTYPE_p_ZOOM_resultset_p yazResultSet = yaz4jlib.ZOOM_connection_search(zoomConnection, yazQuery); - - int errorCode = yaz4jlib.ZOOM_connection_errcode(zoomConnection); - if (errorCode != yaz4jlib.ZOOM_ERROR_NONE) { - yaz4jlib.ZOOM_resultset_destroy(yazResultSet); - } - checkErrorCodeAndThrow(errorCode); - - resultSet = new ResultSet(yazResultSet, this); - } finally { - yaz4jlib.ZOOM_query_destroy(yazQuery); // deallocate yazQuery also when exceptions - yazQuery = null; - } - return resultSet; + SWIGTYPE_p_ZOOM_query_p yazQuery = null; + if (queryType == QueryType.CQLQuery) { + yazQuery = yaz4jlib.ZOOM_query_create(); + yaz4jlib.ZOOM_query_cql(yazQuery, query); + } else if (queryType == QueryType.PrefixQuery) { + yazQuery = yaz4jlib.ZOOM_query_create(); + yaz4jlib.ZOOM_query_prefix(yazQuery, query); + } + SWIGTYPE_p_ZOOM_resultset_p yazResultSet = yaz4jlib.ZOOM_connection_search(zoomConnection, yazQuery); + ZoomException err = ExceptionUtil.getError(zoomConnection, host, + port); + if (err != null) { + yaz4jlib.ZOOM_resultset_destroy(yazResultSet); + yaz4jlib.ZOOM_query_destroy(yazQuery); + throw err; + } + return new ResultSet(yazResultSet, this); } public ScanSet scan(String query) throws ZoomException { if (closed) throw new IllegalStateException("Connection is closed."); SWIGTYPE_p_ZOOM_scanset_p yazScanSet = yaz4jlib.ZOOM_connection_scan(zoomConnection, query); - - int errorCode = yaz4jlib.ZOOM_connection_errcode(zoomConnection); - if (errorCode != yaz4jlib.ZOOM_ERROR_NONE) { + ZoomException err = ExceptionUtil.getError(zoomConnection, host, port); + if (err != null) { yaz4jlib.ZOOM_scanset_destroy(yazScanSet); + throw err; } - checkErrorCodeAndThrow(errorCode); - ScanSet scanSet = new ScanSet(yazScanSet, this); return scanSet; } @@ -99,8 +74,8 @@ public class Connection { */ public void connect() throws ZoomException { yaz4jlib.ZOOM_connection_connect(zoomConnection, host, port); - int errorCode = yaz4jlib.ZOOM_connection_errcode(zoomConnection); - checkErrorCodeAndThrow(errorCode); + ZoomException err = ExceptionUtil.getError(zoomConnection, host, port); + if (err != null) throw err; closed = false; } @@ -112,32 +87,6 @@ public class Connection { closed = true; } - private void checkErrorCodeAndThrow(int errorCode) throws ZoomException { - String message; - - if (errorCode == yaz4jlibConstants.ZOOM_ERROR_NONE) { - return; - } else if (errorCode == yaz4jlib.ZOOM_ERROR_CONNECT) { - message = String.format("Connection could not be made to %s:%d", host, port); - throw new ConnectionUnavailableException(message); - } else if (errorCode == yaz4jlib.ZOOM_ERROR_INVALID_QUERY) { - message = String.format("The query requested is not valid or not supported"); - throw new InvalidQueryException(message); - } else if (errorCode == yaz4jlib.ZOOM_ERROR_INIT) { - message = String.format("Server %s:%d rejected our init request", host, port); - throw new InitRejectedException(message); - } else if (errorCode == yaz4jlib.ZOOM_ERROR_TIMEOUT) { - message = String.format("Server %s:%d timed out handling our request", host, port); - throw new ConnectionTimeoutException(message); - } else if ((errorCode == yaz4jlib.ZOOM_ERROR_MEMORY) || (errorCode == yaz4jlib.ZOOM_ERROR_ENCODE) || (errorCode == yaz4jlib.ZOOM_ERROR_DECODE) || (errorCode == yaz4jlib.ZOOM_ERROR_CONNECTION_LOST) || (errorCode == yaz4jlib.ZOOM_ERROR_INTERNAL) || (errorCode == yaz4jlib.ZOOM_ERROR_UNSUPPORTED_PROTOCOL) || (errorCode == yaz4jlib.ZOOM_ERROR_UNSUPPORTED_QUERY)) { - message = yaz4jlib.ZOOM_connection_errmsg(zoomConnection); - throw new ZoomImplementationException("A fatal error occurred in Yaz: " + errorCode + " - " + message); - } else { - String errMsgBib1 = "Bib1Exception: Error Code = " + errorCode + " (" + Bib1Diagnostic.getError(errorCode) + ")"; - throw new Bib1Exception(errMsgBib1); - } - } - /** * Write option with a given name. * @param name option name diff --git a/src/main/java/org/yaz4j/ExceptionUtil.java b/src/main/java/org/yaz4j/ExceptionUtil.java new file mode 100644 index 0000000..8eb223b --- /dev/null +++ b/src/main/java/org/yaz4j/ExceptionUtil.java @@ -0,0 +1,58 @@ +/* + * Copyright (c) 1995-2010, Index Data + * All rights reserved. + * See the file LICENSE for details. + */ +package org.yaz4j; + +import org.yaz4j.exception.*; +import org.yaz4j.jni.SWIGTYPE_p_ZOOM_connection_p; +import org.yaz4j.jni.yaz4jlib; +import org.yaz4j.jni.yaz4jlibConstants; + +/** + * Utility class to map ZOOM errors into ZoomExceptions + * @author jakub + */ +class ExceptionUtil { + + static ZoomException getError(SWIGTYPE_p_ZOOM_connection_p zoomConnection, String host, int port) { + int errorCode = yaz4jlib.ZOOM_connection_errcode(zoomConnection); + String message; + switch (errorCode) { + case yaz4jlibConstants.ZOOM_ERROR_NONE: + return null; + case yaz4jlib.ZOOM_ERROR_CONNECT: + message = String.format("Connection could not be made to %s:%d", host, + port); + return new ConnectionUnavailableException(message); + case yaz4jlib.ZOOM_ERROR_INVALID_QUERY: + message = String.format( + "The query requested is not valid or not supported"); + return new InvalidQueryException(message); + case yaz4jlib.ZOOM_ERROR_INIT: + message = String.format("Server %s:%d rejected our init request", host, + port); + return new InitRejectedException(message); + case yaz4jlib.ZOOM_ERROR_TIMEOUT: + message = String.format("Server %s:%d timed out handling our request", + host, port); + return new ConnectionTimeoutException(message); + case yaz4jlib.ZOOM_ERROR_MEMORY: + case yaz4jlib.ZOOM_ERROR_ENCODE: + case yaz4jlib.ZOOM_ERROR_DECODE: + case yaz4jlib.ZOOM_ERROR_CONNECTION_LOST: + case yaz4jlib.ZOOM_ERROR_INTERNAL: + case yaz4jlib.ZOOM_ERROR_UNSUPPORTED_PROTOCOL: + case yaz4jlib.ZOOM_ERROR_UNSUPPORTED_QUERY: + message = yaz4jlib.ZOOM_connection_errmsg(zoomConnection); + return new ZoomImplementationException("A fatal error occurred in Yaz: " + + errorCode + " - " + message); + default: + String errMsgBib1 = "Bib1Exception: Error Code = " + errorCode + " (" + + Bib1Diagnostic.getError(errorCode) + ")"; + return new Bib1Exception(errMsgBib1); + } + } + +} \ No newline at end of file