3 import org.yaz4j.exception.ZoomImplementationException;
4 import org.yaz4j.exception.ConnectionTimeoutException;
5 import org.yaz4j.exception.InitRejectedException;
6 import org.yaz4j.exception.Bib1Exception;
7 import org.yaz4j.exception.InvalidQueryException;
8 import org.yaz4j.exception.Bib1Diagnostic;
9 import org.yaz4j.exception.ConnectionUnavailableException;
10 import org.yaz4j.exception.ZoomException;
11 import org.yaz4j.jni.SWIGTYPE_p_ZOOM_connection_p;
12 import org.yaz4j.jni.SWIGTYPE_p_ZOOM_query_p;
13 import org.yaz4j.jni.SWIGTYPE_p_ZOOM_resultset_p;
14 import org.yaz4j.jni.SWIGTYPE_p_ZOOM_scanset_p;
15 import org.yaz4j.jni.SWIGTYPE_p_ZOOM_options_p;
16 import org.yaz4j.jni.yaz4jlib;
17 import org.yaz4j.jni.yaz4jlibConstants;
19 public class Connection {
22 protected SWIGTYPE_p_ZOOM_connection_p zoomConnection;
23 //connection is initially closed
24 protected boolean closed = true;
25 private boolean disposed = false;
27 public enum QueryType {
32 // on Linux 'yaz4j' maps to 'libyaz4j.so' (i.e. 'lib' prefix & '.so' extension)
33 // on Windows 'yaz4j' maps to 'yaz4j.dll' (i.e. '.dll' extension)
34 String libName = "yaz4j";
36 // System.err.println( "Loading library '"+ System.mapLibraryName( libName ) + "'" );
37 System.loadLibrary(libName);
38 } catch (AbstractMethodError e) {
39 System.err.println("Fatal Error: Failed to load library '" + System.mapLibraryName(libName) + "'");
44 public Connection(String host, int port) {
47 zoomConnection = yaz4jlib.ZOOM_connection_create(null);
50 public void finalize() {
54 public ResultSet search(String query, QueryType queryType) throws ZoomException {
55 if (closed) throw new IllegalStateException("Connection is closed.");
56 SWIGTYPE_p_ZOOM_query_p yazQuery = yaz4jlib.ZOOM_query_create();
57 ResultSet resultSet = null;
60 if (queryType == QueryType.CQLQuery) {
61 yaz4jlib.ZOOM_query_cql(yazQuery, query);
62 } else if (queryType == QueryType.PrefixQuery) {
63 yaz4jlib.ZOOM_query_prefix(yazQuery, query);
65 throw new InvalidQueryException("queryType");
68 SWIGTYPE_p_ZOOM_resultset_p yazResultSet = yaz4jlib.ZOOM_connection_search(zoomConnection, yazQuery);
70 int errorCode = yaz4jlib.ZOOM_connection_errcode(zoomConnection);
71 if (errorCode != yaz4jlib.ZOOM_ERROR_NONE) {
72 yaz4jlib.ZOOM_resultset_destroy(yazResultSet);
74 checkErrorCodeAndThrow(errorCode);
76 resultSet = new ResultSet(yazResultSet, this);
78 yaz4jlib.ZOOM_query_destroy(yazQuery); // deallocate yazQuery also when exceptions
84 public ScanSet scan(String query) throws ZoomException {
85 if (closed) throw new IllegalStateException("Connection is closed.");
86 SWIGTYPE_p_ZOOM_scanset_p yazScanSet = yaz4jlib.ZOOM_connection_scan(zoomConnection, query);
88 int errorCode = yaz4jlib.ZOOM_connection_errcode(zoomConnection);
89 if (errorCode != yaz4jlib.ZOOM_ERROR_NONE) {
90 yaz4jlib.ZOOM_scanset_destroy(yazScanSet);
92 checkErrorCodeAndThrow(errorCode);
94 ScanSet scanSet = new ScanSet(yazScanSet, this);
99 * Initiates the connection
101 public void connect() throws ZoomException {
102 yaz4jlib.ZOOM_connection_connect(zoomConnection, host, port);
103 int errorCode = yaz4jlib.ZOOM_connection_errcode(zoomConnection);
104 checkErrorCodeAndThrow(errorCode);
109 * Closes the connection.
111 public void close() {
112 yaz4jlib.ZOOM_connection_close(zoomConnection);
116 private void checkErrorCodeAndThrow(int errorCode) throws ZoomException {
119 if (errorCode == yaz4jlibConstants.ZOOM_ERROR_NONE) {
121 } else if (errorCode == yaz4jlib.ZOOM_ERROR_CONNECT) {
122 message = String.format("Connection could not be made to %s:%d", host, port);
123 throw new ConnectionUnavailableException(message);
124 } else if (errorCode == yaz4jlib.ZOOM_ERROR_INVALID_QUERY) {
125 message = String.format("The query requested is not valid or not supported");
126 throw new InvalidQueryException(message);
127 } else if (errorCode == yaz4jlib.ZOOM_ERROR_INIT) {
128 message = String.format("Server %s:%d rejected our init request", host, port);
129 throw new InitRejectedException(message);
130 } else if (errorCode == yaz4jlib.ZOOM_ERROR_TIMEOUT) {
131 message = String.format("Server %s:%d timed out handling our request", host, port);
132 throw new ConnectionTimeoutException(message);
133 } 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)) {
134 message = yaz4jlib.ZOOM_connection_errmsg(zoomConnection);
135 throw new ZoomImplementationException("A fatal error occurred in Yaz: " + errorCode + " - " + message);
137 String errMsgBib1 = "Bib1Exception: Error Code = " + errorCode + " (" + Bib1Diagnostic.getError(errorCode) + ")";
138 throw new Bib1Exception(errMsgBib1);
143 * Write option with a given name.
144 * @param name option name
145 * @param value option value
146 * @return connection (self) for chainability
148 public Connection option(String name, String value) {
149 yaz4jlib.ZOOM_connection_option_set(zoomConnection, name, value);
154 * Read option with a given name
155 * @param name option name
156 * @return option value
158 public String option(String name) {
159 return yaz4jlib.ZOOM_connection_option_get(zoomConnection, name);
162 public String getSyntax() {
163 return option("preferredRecordSyntax");
166 public void setSyntax(String value) {
167 option("preferredRecordSyntax", value);
170 public String getDatabaseName() {
171 return option("databaseName");
174 public void setDatabaseName(String value) {
175 option("databaseName", value);
178 public String getUsername() {
179 return option("user");
182 public void setUsername(String value) {
183 option("user", value);
186 public String getPassword() {
187 return option("password");
190 public void setPassword(String value) {
191 option("password", value);
199 yaz4jlib.ZOOM_connection_destroy(zoomConnection);
200 zoomConnection = null;