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