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 import sun.reflect.generics.reflectiveObjects.NotImplementedException;
13 public class Connection
17 private ConnectionOptionsCollection options = null ;
18 protected SWIGTYPE_p_ZOOM_connection_p zoomConnection = null ;
19 private boolean connected = false;
20 private boolean disposed = false;
24 // on Linux 'yaz4j' maps to 'libyaz4j.so' (i.e. 'lib' prefix & '.so' extension)
25 // on Windows 'yaz4j' maps to 'yaz4j.dll' (i.e. '.dll' extension)
26 String libName = "yaz4j" ;
29 // System.err.println( "Loading library '"+ System.mapLibraryName( libName ) + "'" );
30 System.loadLibrary( libName );
34 System.err.println( "Fatal Error: Failed to load library '" + System.mapLibraryName( libName ) + "'");
39 public Connection(String host, int port)
44 options = new ConnectionOptionsCollection();
45 zoomConnection = yaz4jlib.ZOOM_connection_create(options.zoomOptions);
47 SWIGTYPE_p_p_char cp = null;
48 SWIGTYPE_p_p_char addinfo = null ;
49 int errorCode = yaz4jlib.ZOOM_connection_error(zoomConnection, cp, addinfo);
50 CheckErrorCodeAndThrow(errorCode);
53 public void finalize()
58 private void CheckErrorCodeAndThrow(int errorCode)
62 if( errorCode == yaz4jlibConstants.ZOOM_ERROR_NONE )
66 else if( errorCode == yaz4jlib.ZOOM_ERROR_CONNECT )
68 message = String.format("Connection could not be made to %s:%d", host, port);
69 throw new ConnectionUnavailableException(message);
71 else if( errorCode == yaz4jlib.ZOOM_ERROR_INVALID_QUERY )
73 message = String.format("The query requested is not valid or not supported");
74 throw new InvalidQueryException(message);
76 else if( errorCode == yaz4jlib.ZOOM_ERROR_INIT )
78 message = String.format("Server %s:%d rejected our init request", host, port);
79 throw new InitRejectedException(message);
81 else if( errorCode == yaz4jlib.ZOOM_ERROR_TIMEOUT )
83 message = String.format("Server %s:%d timed out handling our request", host, port);
84 throw new ConnectionTimeoutException(message);
86 else if( ( errorCode == yaz4jlib.ZOOM_ERROR_MEMORY )
87 || ( errorCode == yaz4jlib.ZOOM_ERROR_ENCODE )
88 || ( errorCode == yaz4jlib.ZOOM_ERROR_DECODE )
89 || ( errorCode == yaz4jlib.ZOOM_ERROR_CONNECTION_LOST )
90 || ( errorCode == yaz4jlib.ZOOM_ERROR_INTERNAL )
91 || ( errorCode == yaz4jlib.ZOOM_ERROR_UNSUPPORTED_PROTOCOL )
92 || ( errorCode == yaz4jlib.ZOOM_ERROR_UNSUPPORTED_QUERY ) )
94 message = yaz4jlib.ZOOM_connection_errmsg(zoomConnection);
95 throw new ZoomImplementationException("A fatal error occurred in Yaz: " + errorCode + " - " + message);
99 String errMsgBib1 = "Bib1Exception: Error Code = " + errorCode + " (" + Bib1Diagnostic.GetError(errorCode) + ")" ;
100 throw new Bib1Exception( errMsgBib1 );
104 private enum QueryType { CQLQuery, PrefixQuery };
106 public ResultSet Search(PrefixQuery query)
108 return Search( query.getQueryString(), QueryType.PrefixQuery);
111 public ResultSet Search(CQLQuery query)
113 return Search( query.getQueryString(), QueryType.CQLQuery);
116 private ResultSet Search(String query, QueryType queryType)
120 SWIGTYPE_p_ZOOM_query_p yazQuery = yaz4jlib.ZOOM_query_create();
121 ResultSet resultSet = null;
125 if( queryType == QueryType.CQLQuery )
126 yaz4jlib.ZOOM_query_cql(yazQuery, query);
127 else if( queryType == QueryType.PrefixQuery )
128 yaz4jlib.ZOOM_query_prefix(yazQuery, query);
130 throw new NotImplementedException();
132 SWIGTYPE_p_ZOOM_resultset_p yazResultSet = yaz4jlib.ZOOM_connection_search(zoomConnection, yazQuery);
134 int errorCode = yaz4jlib.ZOOM_connection_errcode( zoomConnection );
135 if (errorCode != yaz4jlib.ZOOM_ERROR_NONE)
137 yaz4jlib.ZOOM_resultset_destroy(yazResultSet);
139 CheckErrorCodeAndThrow(errorCode);
141 resultSet = new ResultSet(yazResultSet, zoomConnection);
145 yaz4jlib.ZOOM_query_destroy(yazQuery); // deallocate yazQuery also when exceptions
151 public ScanSet Scan(String query)
154 SWIGTYPE_p_ZOOM_scanset_p yazScanSet = yaz4jlib.ZOOM_connection_scan(zoomConnection, query);
156 int errorCode = yaz4jlib.ZOOM_connection_errcode(zoomConnection);
157 if( errorCode != yaz4jlib.ZOOM_ERROR_NONE )
159 yaz4jlib.ZOOM_scanset_destroy(yazScanSet);
161 CheckErrorCodeAndThrow(errorCode);
163 ScanSet scanSet = new ScanSet(yazScanSet, this);
167 public ConnectionOptionsCollection getOptions()
172 protected void EnsureConnected()
178 public void Connect()
180 yaz4jlib.ZOOM_connection_connect( zoomConnection, host, port);
181 int errorCode = yaz4jlib.ZOOM_connection_errcode(zoomConnection);
182 CheckErrorCodeAndThrow(errorCode);
186 public void Dispose()
190 yaz4jlib.ZOOM_connection_destroy(zoomConnection);
191 zoomConnection = null;
196 public String getSyntax()
198 return options.get("preferredRecordSyntax");
201 public void setSyntax( String value)
203 options.set("preferredRecordSyntax", value ) ;
206 public String getDatabaseName()
208 return options.get("databaseName");
211 public void setDatabaseName( String value )
213 options.set("databaseName", value);
216 public String getUsername()
218 return options.get("user");
221 public void setUsername( String value )
223 options.set("user", value);
226 public String getPassword()
228 return options.get("password");
231 public void setPassword( String value )
233 options.set("password", value);