X-Git-Url: http://git.indexdata.com/?p=yaz4j-moved-to-github.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fyaz4j%2FConnection.java;h=4accb996dafae17ca3dbba79a0b07ff600fa09bf;hp=0587ec78a8d356d54180b993e55f7d48a790ae39;hb=8c7413fbaf0870975229e8e101f79096b5dfe6f0;hpb=26241aa1428575d8e72c678c0b1322aeab780ff4 diff --git a/src/main/java/org/yaz4j/Connection.java b/src/main/java/org/yaz4j/Connection.java index 0587ec7..4accb99 100644 --- a/src/main/java/org/yaz4j/Connection.java +++ b/src/main/java/org/yaz4j/Connection.java @@ -1,204 +1,308 @@ 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.SWIGTYPE_p_ZOOM_options_p; import org.yaz4j.jni.yaz4jlib; -import org.yaz4j.jni.yaz4jlibConstants; +/** + * Class representing an on-going communication with an IR server. + * + * Creating an instance of this class does not automatically connect (e.g open + * a socket) to the remote server as the programmer may want to specify options + * on the object before establishing the actual connection. + * + * The work-flow for synchronous (the only addressed) operation when using this + * class should be as follows (in pseudocode): + * + *
+ *
+ * try {
+ *  c = new Connection(...)
+ *  //possibly set some options
+ *  c.connect //establishes connection
+ *  c.search //or other operation
+ *  //possibly retrieve records
+ * catch (ZoomException e) {
+ *  //handle any protocol- or network-level errors
+ * } finally {
+ *  c.close //close the socket
+ * }
+ *
+ * 
+ * @see YAZ ZOOM Connection + * @author jakub + */ public class Connection { - private String host; - private int port; - protected SWIGTYPE_p_ZOOM_connection_p zoomConnection; - //connection is initially closed - protected boolean closed = true; - private boolean disposed = false; - - public enum QueryType { - CQLQuery, PrefixQuery - }; - - static { - // 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(); - } - } - public Connection(String host, int port) { - this.host = host; - this.port = port; - zoomConnection = yaz4jlib.ZOOM_connection_create(null); - } + private String host; + private int port; + protected SWIGTYPE_p_ZOOM_connection_p zoomConnection; + //connection is initially closed + protected boolean closed = true; + private boolean disposed = false; - public void finalize() { - _dispose(); - } + public enum QueryType { - 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; - } + CQLQuery, PrefixQuery + }; - 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); + static { + // 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"; + System.loadLibrary(libName); + } - int errorCode = yaz4jlib.ZOOM_connection_errcode(zoomConnection); - if (errorCode != yaz4jlib.ZOOM_ERROR_NONE) { - yaz4jlib.ZOOM_scanset_destroy(yazScanSet); - } - checkErrorCodeAndThrow(errorCode); + /** + * Create new connection object without physically opening a connection to the + * remote server. + * @param host host name of the server + * @param port port of the server + */ + public Connection(String host, int port) { + this.host = host; + this.port = port; + zoomConnection = yaz4jlib.ZOOM_connection_create(null); + } - ScanSet scanSet = new ScanSet(yazScanSet, this); - return scanSet; - } + public void finalize() { + _dispose(); + } - /** - * Initiates the connection - */ - public void connect() throws ZoomException { - yaz4jlib.ZOOM_connection_connect(zoomConnection, host, port); - int errorCode = yaz4jlib.ZOOM_connection_errcode(zoomConnection); - checkErrorCodeAndThrow(errorCode); - closed = false; + /** + * Performs a search operation (submits the query to the server, waits for + * response and creates a new result set that allows to retrieve particular + * results) + * @deprecated Does not allow specifying sort criteria prior to search + * use {@link #search(org.yaz4j.Query) search(Query)} instead. + * @param query search query + * @param queryType type of the query (e.g RPN. CQL) + * @return result set containing records (hits) + * @throws ZoomException protocol or network-level error + */ + @Deprecated + public ResultSet search(String query, QueryType queryType) throws + ZoomException { + if (closed) { + throw new IllegalStateException("Connection is closed."); } - - /** - * Closes the connection. - */ - public void close() { - yaz4jlib.ZOOM_connection_close(zoomConnection); - closed = true; + 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); } - - 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); - } + SWIGTYPE_p_ZOOM_resultset_p yazResultSet = yaz4jlib.ZOOM_connection_search( + zoomConnection, yazQuery); + yaz4jlib.ZOOM_query_destroy(yazQuery); + ZoomException err = ExceptionUtil.getError(zoomConnection, host, + port); + if (err != null) { + yaz4jlib.ZOOM_resultset_destroy(yazResultSet); + throw err; } - + return new ResultSet(yazResultSet, this); + } + /** - * Write option with a given name. - * @param name option name - * @param value option value - * @return connection (self) for chainability - */ - public Connection option(String name, String value) { - yaz4jlib.ZOOM_connection_option_set(zoomConnection, name, value); - return this; + * Performs a search operation (submits the query to the server, waits for + * response and creates a new result set that allows to retrieve particular + * results). Sort criteria may be specified prior to the search, directly + * on the query object. + * @param query search query of any type supported by YAZ. + * @return result set containing records (hits) + * @throws ZoomException protocol or network-level error + */ + public ResultSet search(Query query) throws ZoomException { + if (closed) { + throw new IllegalStateException("Connection is closed."); } + SWIGTYPE_p_ZOOM_resultset_p yazResultSet = yaz4jlib.ZOOM_connection_search( + zoomConnection, query.query); + ZoomException err = ExceptionUtil.getError(zoomConnection, host, + port); + if (err != null) { + yaz4jlib.ZOOM_resultset_destroy(yazResultSet); + throw err; + } + return new ResultSet(yazResultSet, this); + } + /** + * Performs a scan operation (obtains a list of candidate search terms against + * a particular access point). + * @deprecated Only allows PQF scan queries, use {@link #scan(org.yaz4j.Query) scan(Query)} instead + * @param query query for scanning + * @return a scan set with the terms + * @throws ZoomException a protocol or network-level error + */ + @Deprecated + 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); + ZoomException err = ExceptionUtil.getError(zoomConnection, host, port); + if (err != null) { + yaz4jlib.ZOOM_scanset_destroy(yazScanSet); + throw err; + } + ScanSet scanSet = new ScanSet(yazScanSet, this); + return scanSet; + } + /** - * Read option with a given name - * @param name option name - * @return option value - */ - public String option(String name) { - return yaz4jlib.ZOOM_connection_option_get(zoomConnection, name); + * Performs a scan operation (obtains a list of candidate search terms against + * a particular access point). Allows to use both CQL and PQF for Scan. + * @see ZOOM-API Scan + * @param query scan query of type supported by YAZ + * @return a scan set with the terms + * @throws ZoomException a protocol or network-level error + */ + public ScanSet scan(Query query) throws ZoomException { + if (closed) { + throw new IllegalStateException("Connection is closed."); } - - public String getSyntax() { - return option("preferredRecordSyntax"); + SWIGTYPE_p_ZOOM_scanset_p yazScanSet = yaz4jlib.ZOOM_connection_scan1( + zoomConnection, query.query); + ZoomException err = ExceptionUtil.getError(zoomConnection, host, port); + if (err != null) { + yaz4jlib.ZOOM_scanset_destroy(yazScanSet); + throw err; } + ScanSet scanSet = new ScanSet(yazScanSet, this); + return scanSet; + } - public void setSyntax(String value) { - option("preferredRecordSyntax", value); + /** + * Establishes a connection to the remote server. + * @throws ZoomException any (possibly network-level) errors that may occurr + */ + public void connect() throws ZoomException { + yaz4jlib.ZOOM_connection_connect(zoomConnection, host, port); + ZoomException err = ExceptionUtil.getError(zoomConnection, host, port); + if (err != null) { + throw err; } + closed = false; + } - public String getDatabaseName() { - return option("databaseName"); - } + /** + * Closes the connection. + */ + public void close() { + yaz4jlib.ZOOM_connection_close(zoomConnection); + closed = true; + } + + /** + * Return exception type from current connection + * + * @return null if no error + */ + ZoomException getZoomException() { + ZoomException err = ExceptionUtil.getError(zoomConnection, host, port); + return err; + } - public void setDatabaseName(String value) { - option("databaseName", value); - } + /** + * Write option with a given name. + * @param name option name + * @param value option value + * @return connection (self) for chainability + */ + public Connection option(String name, String value) { + yaz4jlib.ZOOM_connection_option_set(zoomConnection, name, value); + return this; + } - public String getUsername() { - return option("user"); - } + /** + * Read option with a given name + * @param name option name + * @return option value + */ + public String option(String name) { + return yaz4jlib.ZOOM_connection_option_get(zoomConnection, name); + } - public void setUsername(String value) { - option("user", value); - } + /** + * Same as option("preferredRecordSyntax") + * @return value of preferred record syntax + */ + public String getSyntax() { + return option("preferredRecordSyntax"); + } - public String getPassword() { - return option("password"); - } + /** + * Same as option("preferredRecordSyntax", value) + * @param value value of preferred record syntax + */ + public void setSyntax(String value) { + option("preferredRecordSyntax", value); + } - public void setPassword(String value) { - option("password", value); - } + /** + * Same as option("databaseName") + * @return value of databaseName + */ + public String getDatabaseName() { + return option("databaseName"); + } - /** - * INTERNAL, GC-ONLY - */ - void _dispose() { - if (!disposed) { - yaz4jlib.ZOOM_connection_destroy(zoomConnection); - zoomConnection = null; - disposed = true; - } + /** + * Same as option("databaseName", value) + * @param value value of databaseName + */ + public void setDatabaseName(String value) { + option("databaseName", value); + } + + /** + * Same as option("user") + * @return value of user + */ + public String getUsername() { + return option("user"); + } + + /** + * Same as option("user", value) + * @param value value of user + */ + public void setUsername(String value) { + option("user", value); + } + + /** + * Same as option("password") + * @return value of password + */ + public String getPassword() { + return option("password"); + } + + /** + * Same as option("password", value) + * @param value + */ + public void setPassword(String value) { + option("password", value); + } + + /** + * INTERNAL, GC-ONLY + */ + void _dispose() { + if (!disposed) { + yaz4jlib.ZOOM_connection_destroy(zoomConnection); + zoomConnection = null; + disposed = true; } + } }