- }
-
- public ScanSet Scan(String query)
- {
- EnsureConnected();
- 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 )
- {
- yaz4jlib.ZOOM_scanset_destroy(yazScanSet);
- }
- CheckErrorCodeAndThrow(errorCode);
-
- ScanSet scanSet = new ScanSet(yazScanSet, this);
- return scanSet;
- }
-
- public ConnectionOptionsCollection getOptions()
- {
- return options;
- }
-
- protected void EnsureConnected()
- {
- if (! connected )
- Connect();
- }
-
- public void Connect()
- {
- yaz4jlib.ZOOM_connection_connect( zoomConnection, host, port);
- int errorCode = yaz4jlib.ZOOM_connection_errcode(zoomConnection);
- CheckErrorCodeAndThrow(errorCode);
- connected = true;
- }
-
- public void Dispose()
- {
- if (! disposed )
- {
- yaz4jlib.ZOOM_connection_destroy(zoomConnection);
- zoomConnection = null;
- disposed = true;
- }
- }
-
- public String getSyntax()
- {
- return options.get("preferredRecordSyntax");
- }
-
- public void setSyntax( String value)
- {
- options.set("preferredRecordSyntax", value ) ;
- }
-
- public String getDatabaseName()
- {
- return options.get("databaseName");
- }
-
- public void setDatabaseName( String value )
- {
- options.set("databaseName", value);
- }
-
- public String getUsername()
- {
- return options.get("user");
- }
-
- public void setUsername( String value )
- {
- options.set("user", value);
- }
-
- public String getPassword()
- {
- return options.get("password");
- }
-
- public void setPassword( String value )
- {
- options.set("password", value);
- }
+ }
+
+ 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) {
+ yaz4jlib.ZOOM_scanset_destroy(yazScanSet);
+ }
+ checkErrorCodeAndThrow(errorCode);
+
+ ScanSet scanSet = new ScanSet(yazScanSet, this);
+ return scanSet;
+ }
+
+ /**
+ * Initiates the connection
+ */
+ public void connect() throws ZoomException {
+ //this is temporary before ZOOM-C has proper close method, right now
+ // simply recreate connection
+ if (closed)
+ zoomConnection = yaz4jlib.ZOOM_connection_create(options);
+ yaz4jlib.ZOOM_connection_connect(zoomConnection, host, port);
+ int errorCode = yaz4jlib.ZOOM_connection_errcode(zoomConnection);
+ checkErrorCodeAndThrow(errorCode);
+ closed = false;
+ }
+
+ /**
+ * Closes the connection.
+ */
+ public void close() {
+ if (!closed) {
+ yaz4jlib.ZOOM_connection_destroy(zoomConnection);
+ zoomConnection = null;
+ 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
+ * @param value option value
+ * @return connection (self) for chainability
+ */
+ public Connection option(String name, String value) {
+ yaz4jlib.ZOOM_options_set(options, name, value);
+ return this;
+ }
+
+ /**
+ * Read option with a given name
+ * @param name option name
+ * @return option value
+ */
+ public String option(String name) {
+ return yaz4jlib.ZOOM_options_get(options, name);
+ }
+
+ public String getSyntax() {
+ return option("preferredRecordSyntax");
+ }
+
+ public void setSyntax(String value) {
+ option("preferredRecordSyntax", value);
+ }
+
+ public String getDatabaseName() {
+ return option("databaseName");
+ }
+
+ public void setDatabaseName(String value) {
+ option("databaseName", value);
+ }
+
+ public String getUsername() {
+ return option("user");
+ }
+
+ public void setUsername(String value) {
+ option("user", value);
+ }
+
+ public String getPassword() {
+ return option("password");
+ }
+
+ public void setPassword(String value) {
+ option("password", value);
+ }
+
+ /**
+ * INTERNAL, GC-ONLY
+ */
+ void _dispose() {
+ if (!disposed) {
+ close();
+ disposed = true;
+ }
+ }