Rewrite exception handling
[yaz4j-moved-to-github.git] / src / main / java / org / yaz4j / Connection.java
1 package org.yaz4j;
2
3 import org.yaz4j.exception.ZoomException;
4 import org.yaz4j.jni.SWIGTYPE_p_ZOOM_connection_p;
5 import org.yaz4j.jni.SWIGTYPE_p_ZOOM_query_p;
6 import org.yaz4j.jni.SWIGTYPE_p_ZOOM_resultset_p;
7 import org.yaz4j.jni.SWIGTYPE_p_ZOOM_scanset_p;
8 import org.yaz4j.jni.yaz4jlib;
9
10 public class Connection {
11     private String host;
12     private int port;
13     protected SWIGTYPE_p_ZOOM_connection_p zoomConnection;
14     //connection is initially closed
15     protected boolean closed = true;
16     private boolean disposed = false;
17
18     public enum QueryType {
19         CQLQuery, PrefixQuery
20     };
21
22     static {
23         // on Linux   'yaz4j' maps to 'libyaz4j.so' (i.e. 'lib' prefix & '.so'  extension)
24         // on Windows 'yaz4j' maps to 'yaz4j.dll'   (i.e.                '.dll' extension)
25         String libName = "yaz4j";
26         System.loadLibrary(libName);
27     }
28
29     public Connection(String host, int port) {
30         this.host = host;
31         this.port = port;
32         zoomConnection = yaz4jlib.ZOOM_connection_create(null);
33     }
34
35     public void finalize() {
36         _dispose();
37     }
38
39     public ResultSet search(String query, QueryType queryType) throws ZoomException {
40       if (closed) throw new IllegalStateException("Connection is closed.");
41       SWIGTYPE_p_ZOOM_query_p yazQuery = null;
42       if (queryType == QueryType.CQLQuery) {
43           yazQuery = yaz4jlib.ZOOM_query_create();
44           yaz4jlib.ZOOM_query_cql(yazQuery, query);
45       } else if (queryType == QueryType.PrefixQuery) {
46           yazQuery = yaz4jlib.ZOOM_query_create();
47           yaz4jlib.ZOOM_query_prefix(yazQuery, query);
48       }
49       SWIGTYPE_p_ZOOM_resultset_p yazResultSet = yaz4jlib.ZOOM_connection_search(zoomConnection, yazQuery);
50       ZoomException err = ExceptionUtil.getError(zoomConnection, host,
51         port);
52       if (err != null) {
53           yaz4jlib.ZOOM_resultset_destroy(yazResultSet);
54           yaz4jlib.ZOOM_query_destroy(yazQuery);
55           throw err;
56       }
57       return new ResultSet(yazResultSet, this);
58     }
59
60     public ScanSet scan(String query) throws ZoomException {
61       if (closed) throw new IllegalStateException("Connection is closed.");
62         SWIGTYPE_p_ZOOM_scanset_p yazScanSet = yaz4jlib.ZOOM_connection_scan(zoomConnection, query);
63         ZoomException err = ExceptionUtil.getError(zoomConnection, host, port);
64         if (err != null) {
65             yaz4jlib.ZOOM_scanset_destroy(yazScanSet);
66             throw err;
67         }
68         ScanSet scanSet = new ScanSet(yazScanSet, this);
69         return scanSet;
70     }
71
72     /**
73      * Initiates the connection
74      */
75     public void connect() throws ZoomException {
76       yaz4jlib.ZOOM_connection_connect(zoomConnection, host, port);
77       ZoomException err = ExceptionUtil.getError(zoomConnection, host, port);
78       if (err != null) throw err;
79       closed = false;
80     }
81
82     /**
83      * Closes the connection.
84      */
85     public void close() {
86       yaz4jlib.ZOOM_connection_close(zoomConnection);
87       closed = true;
88     }
89
90     /**
91      * Write option with a given name.
92      * @param name option name
93      * @param value option value
94      * @return connection (self) for chainability
95      */
96     public Connection option(String name, String value) {
97       yaz4jlib.ZOOM_connection_option_set(zoomConnection, name, value);
98       return this;
99     }
100
101     /**
102      * Read option with a given name
103      * @param name option name
104      * @return option value
105      */
106     public String option(String name) {
107       return yaz4jlib.ZOOM_connection_option_get(zoomConnection, name);
108     }
109
110     public String getSyntax() {
111         return option("preferredRecordSyntax");
112     }
113
114     public void setSyntax(String value) {
115         option("preferredRecordSyntax", value);
116     }
117
118     public String getDatabaseName() {
119         return option("databaseName");
120     }
121
122     public void setDatabaseName(String value) {
123         option("databaseName", value);
124     }
125
126     public String getUsername() {
127         return option("user");
128     }
129
130     public void setUsername(String value) {
131         option("user", value);
132     }
133
134     public String getPassword() {
135         return option("password");
136     }
137
138     public void setPassword(String value) {
139         option("password", value);
140     }
141
142     /**
143      * INTERNAL, GC-ONLY
144      */
145     void _dispose() {
146         if (!disposed) {
147           yaz4jlib.ZOOM_connection_destroy(zoomConnection);
148           zoomConnection = null;
149           disposed = true;
150         }
151     }
152 }