Dont use NotImplementedException.
[yaz4j-moved-to-github.git] / src / main / java / org / yaz4j / Connection.java
1 package org.yaz4j;
2
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;
10
11 public class Connection
12 {
13         private String host ;
14         private int port ;
15         private ConnectionOptionsCollection options = null ;
16         protected SWIGTYPE_p_ZOOM_connection_p zoomConnection = null ;
17         private boolean connected = false;
18         private boolean disposed = false;
19         
20         static
21         {
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" ;
25                 try
26                 {
27                         // System.err.println( "Loading library '"+ System.mapLibraryName( libName ) + "'" );
28                         System.loadLibrary( libName );
29                 }
30                 catch( Throwable e )
31                 {
32                         System.err.println( "Fatal Error: Failed to load library '" + System.mapLibraryName( libName ) + "'");
33                         e.printStackTrace();
34                 }               
35         }
36         
37         public Connection(String host, int port)
38         {
39                 this.host = host ;
40                 this.port = port ;
41
42                 options = new ConnectionOptionsCollection();
43                 zoomConnection = yaz4jlib.ZOOM_connection_create(options.zoomOptions);
44
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);
49         }
50         
51         public void finalize()
52         {
53                 Dispose();
54         }
55         
56         private void CheckErrorCodeAndThrow(int errorCode)
57         {
58                 String message;
59                 
60                 if( errorCode == yaz4jlibConstants.ZOOM_ERROR_NONE )
61                 {
62                         return ;
63                 }
64                 else if( errorCode == yaz4jlib.ZOOM_ERROR_CONNECT ) 
65                 {
66                         message = String.format("Connection could not be made to %s:%d", host, port);
67                         throw new ConnectionUnavailableException(message);
68                 }
69                 else if( errorCode == yaz4jlib.ZOOM_ERROR_INVALID_QUERY )
70                 {
71                         message = String.format("The query requested is not valid or not supported");
72                         throw new InvalidQueryException(message);
73                 }
74                 else if( errorCode == yaz4jlib.ZOOM_ERROR_INIT )
75                 {
76                         message = String.format("Server %s:%d rejected our init request", host, port);
77                         throw new InitRejectedException(message);                       
78                 }
79                 else if( errorCode == yaz4jlib.ZOOM_ERROR_TIMEOUT )
80                 {
81                         message = String.format("Server %s:%d timed out handling our request", host, port);
82                         throw new ConnectionTimeoutException(message);                  
83                 }
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 ) )                     
91         {
92                         message = yaz4jlib.ZOOM_connection_errmsg(zoomConnection);
93                         throw new ZoomImplementationException("A fatal error occurred in Yaz: " + errorCode + " - " + message);
94         }
95         else
96         {
97                 String errMsgBib1 = "Bib1Exception: Error Code = " + errorCode + " (" + Bib1Diagnostic.GetError(errorCode) + ")" ; 
98                         throw new Bib1Exception( errMsgBib1 );
99         }
100         }
101
102         private enum QueryType { CQLQuery, PrefixQuery };
103         
104         public ResultSet Search(PrefixQuery query)
105         {
106                 return Search( query.getQueryString(), QueryType.PrefixQuery);
107         }
108         
109         public ResultSet Search(CQLQuery query)
110         {
111                 return Search( query.getQueryString(), QueryType.CQLQuery);
112         }
113         
114         private ResultSet Search(String query, QueryType queryType)
115         {       
116                 EnsureConnected();
117
118                 SWIGTYPE_p_ZOOM_query_p yazQuery = yaz4jlib.ZOOM_query_create();
119         ResultSet resultSet = null;
120
121         try
122         {
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);
127                 else
128                         throw new InvalidQueryException("queryType");
129                 
130                 SWIGTYPE_p_ZOOM_resultset_p yazResultSet = yaz4jlib.ZOOM_connection_search(zoomConnection, yazQuery);
131             
132             int errorCode = yaz4jlib.ZOOM_connection_errcode( zoomConnection );
133             if (errorCode != yaz4jlib.ZOOM_ERROR_NONE)
134             {
135                 yaz4jlib.ZOOM_resultset_destroy(yazResultSet);
136             }
137             CheckErrorCodeAndThrow(errorCode);
138
139             resultSet = new ResultSet(yazResultSet, zoomConnection);
140         }
141         finally
142         {
143                 yaz4jlib.ZOOM_query_destroy(yazQuery); // deallocate yazQuery also when exceptions
144             yazQuery = null;
145         }
146         return resultSet;
147         }
148         
149         public ScanSet Scan(String query)
150         {
151                 EnsureConnected();
152                 SWIGTYPE_p_ZOOM_scanset_p yazScanSet = yaz4jlib.ZOOM_connection_scan(zoomConnection, query);
153
154                 int errorCode = yaz4jlib.ZOOM_connection_errcode(zoomConnection);
155                 if( errorCode != yaz4jlib.ZOOM_ERROR_NONE )
156                 {
157                         yaz4jlib.ZOOM_scanset_destroy(yazScanSet);
158                 }
159                 CheckErrorCodeAndThrow(errorCode);
160
161                 ScanSet scanSet = new ScanSet(yazScanSet, this);
162                 return scanSet;
163         }
164         
165         public ConnectionOptionsCollection getOptions()
166         {
167                 return options;
168         }
169
170         protected void EnsureConnected()
171         {
172                 if (! connected )
173                         Connect();
174         }
175         
176         public void Connect()
177         {
178                 yaz4jlib.ZOOM_connection_connect( zoomConnection, host, port);
179                 int errorCode = yaz4jlib.ZOOM_connection_errcode(zoomConnection);
180                 CheckErrorCodeAndThrow(errorCode);
181                 connected = true;
182         }
183         
184         public void Dispose()
185         {
186                 if (! disposed )
187                 {
188                         yaz4jlib.ZOOM_connection_destroy(zoomConnection);
189                         zoomConnection = null;
190                         disposed = true;
191                 }
192         }
193
194         public String getSyntax()
195         {
196                 return options.get("preferredRecordSyntax");
197         }
198         
199         public void setSyntax( String value)
200         {
201                 options.set("preferredRecordSyntax", value ) ;
202         }
203
204         public String getDatabaseName()
205         {
206                 return options.get("databaseName");
207         }
208         
209         public void setDatabaseName( String value )
210         {
211                 options.set("databaseName", value);
212         }
213
214         public String getUsername()
215         {
216                 return options.get("user");
217         }
218         
219         public void setUsername( String value )
220         {
221                 options.set("user", value);
222         }
223
224         public String getPassword()
225         {
226                 return options.get("password");
227         }
228         
229         public void setPassword( String value )
230         {
231                 options.set("password", value);
232         }
233 }