3 import org.yaz4j.jni.SWIGTYPE_p_ZOOM_connection_p;
4 import org.yaz4j.jni.SWIGTYPE_p_ZOOM_query_p;
5 import org.yaz4j.jni.SWIGTYPE_p_ZOOM_resultset_p;
6 import org.yaz4j.jni.SWIGTYPE_p_ZOOM_scanset_p;
7 import org.yaz4j.jni.SWIGTYPE_p_p_char;
8 import org.yaz4j.jni.yaz4jlib;
9 import org.yaz4j.jni.yaz4jlibConstants;
11 public class Connection
15 private ConnectionOptionsCollection options = null ;
16 protected SWIGTYPE_p_ZOOM_connection_p zoomConnection = null ;
17 private boolean connected = false;
18 private boolean disposed = false;
22 // on Linux 'yaz4j' maps to 'libyaz4j.so' (i.e. 'lib' prefix & '.so' extension)
23 // on Windows 'yaz4j' maps to 'yaz4j.dll' (i.e. '.dll' extension)
24 String libName = "yaz4j" ;
27 // System.err.println( "Loading library '"+ System.mapLibraryName( libName ) + "'" );
28 System.loadLibrary( libName );
32 System.err.println( "Fatal Error: Failed to load library '" + System.mapLibraryName( libName ) + "'");
37 public Connection(String host, int port)
42 options = new ConnectionOptionsCollection();
43 zoomConnection = yaz4jlib.ZOOM_connection_create(options.zoomOptions);
45 SWIGTYPE_p_p_char cp = null;
46 SWIGTYPE_p_p_char addinfo = null ;
47 int errorCode = yaz4jlib.ZOOM_connection_error(zoomConnection, cp, addinfo);
48 CheckErrorCodeAndThrow(errorCode);
51 public void finalize()
56 private void CheckErrorCodeAndThrow(int errorCode)
60 if( errorCode == yaz4jlibConstants.ZOOM_ERROR_NONE )
64 else if( errorCode == yaz4jlib.ZOOM_ERROR_CONNECT )
66 message = String.format("Connection could not be made to %s:%d", host, port);
67 throw new ConnectionUnavailableException(message);
69 else if( errorCode == yaz4jlib.ZOOM_ERROR_INVALID_QUERY )
71 message = String.format("The query requested is not valid or not supported");
72 throw new InvalidQueryException(message);
74 else if( errorCode == yaz4jlib.ZOOM_ERROR_INIT )
76 message = String.format("Server %s:%d rejected our init request", host, port);
77 throw new InitRejectedException(message);
79 else if( errorCode == yaz4jlib.ZOOM_ERROR_TIMEOUT )
81 message = String.format("Server %s:%d timed out handling our request", host, port);
82 throw new ConnectionTimeoutException(message);
84 else if( ( errorCode == yaz4jlib.ZOOM_ERROR_MEMORY )
85 || ( errorCode == yaz4jlib.ZOOM_ERROR_ENCODE )
86 || ( errorCode == yaz4jlib.ZOOM_ERROR_DECODE )
87 || ( errorCode == yaz4jlib.ZOOM_ERROR_CONNECTION_LOST )
88 || ( errorCode == yaz4jlib.ZOOM_ERROR_INTERNAL )
89 || ( errorCode == yaz4jlib.ZOOM_ERROR_UNSUPPORTED_PROTOCOL )
90 || ( errorCode == yaz4jlib.ZOOM_ERROR_UNSUPPORTED_QUERY ) )
92 message = yaz4jlib.ZOOM_connection_errmsg(zoomConnection);
93 throw new ZoomImplementationException("A fatal error occurred in Yaz: " + errorCode + " - " + message);
97 String errMsgBib1 = "Bib1Exception: Error Code = " + errorCode + " (" + Bib1Diagnostic.GetError(errorCode) + ")" ;
98 throw new Bib1Exception( errMsgBib1 );
102 private enum QueryType { CQLQuery, PrefixQuery };
104 public ResultSet Search(PrefixQuery query)
106 return Search( query.getQueryString(), QueryType.PrefixQuery);
109 public ResultSet Search(CQLQuery query)
111 return Search( query.getQueryString(), QueryType.CQLQuery);
114 private ResultSet Search(String query, QueryType queryType)
118 SWIGTYPE_p_ZOOM_query_p yazQuery = yaz4jlib.ZOOM_query_create();
119 ResultSet resultSet = null;
123 if( queryType == QueryType.CQLQuery )
124 yaz4jlib.ZOOM_query_cql(yazQuery, query);
125 else if( queryType == QueryType.PrefixQuery )
126 yaz4jlib.ZOOM_query_prefix(yazQuery, query);
128 throw new InvalidQueryException("queryType");
130 SWIGTYPE_p_ZOOM_resultset_p yazResultSet = yaz4jlib.ZOOM_connection_search(zoomConnection, yazQuery);
132 int errorCode = yaz4jlib.ZOOM_connection_errcode( zoomConnection );
133 if (errorCode != yaz4jlib.ZOOM_ERROR_NONE)
135 yaz4jlib.ZOOM_resultset_destroy(yazResultSet);
137 CheckErrorCodeAndThrow(errorCode);
139 resultSet = new ResultSet(yazResultSet, zoomConnection);
143 yaz4jlib.ZOOM_query_destroy(yazQuery); // deallocate yazQuery also when exceptions
149 public ScanSet Scan(String query)
152 SWIGTYPE_p_ZOOM_scanset_p yazScanSet = yaz4jlib.ZOOM_connection_scan(zoomConnection, query);
154 int errorCode = yaz4jlib.ZOOM_connection_errcode(zoomConnection);
155 if( errorCode != yaz4jlib.ZOOM_ERROR_NONE )
157 yaz4jlib.ZOOM_scanset_destroy(yazScanSet);
159 CheckErrorCodeAndThrow(errorCode);
161 ScanSet scanSet = new ScanSet(yazScanSet, this);
165 public ConnectionOptionsCollection getOptions()
170 protected void EnsureConnected()
176 public void Connect()
178 yaz4jlib.ZOOM_connection_connect( zoomConnection, host, port);
179 int errorCode = yaz4jlib.ZOOM_connection_errcode(zoomConnection);
180 CheckErrorCodeAndThrow(errorCode);
184 public void Dispose()
188 yaz4jlib.ZOOM_connection_destroy(zoomConnection);
189 zoomConnection = null;
194 public String getSyntax()
196 return options.get("preferredRecordSyntax");
199 public void setSyntax( String value)
201 options.set("preferredRecordSyntax", value ) ;
204 public String getDatabaseName()
206 return options.get("databaseName");
209 public void setDatabaseName( String value )
211 options.set("databaseName", value);
214 public String getUsername()
216 return options.get("user");
219 public void setUsername( String value )
221 options.set("user", value);
224 public String getPassword()
226 return options.get("password");
229 public void setPassword( String value )
231 options.set("password", value);