Multi-threaded DLL (do not use libcmt.lib)
[yaz4j-moved-to-github.git] / src / main / java / org / yaz4j / Package.java
index c5671b3..1af9c01 100644 (file)
@@ -1,55 +1,77 @@
 package org.yaz4j;
 
+import org.yaz4j.exception.ZoomException;
 import org.yaz4j.jni.SWIGTYPE_p_ZOOM_package_p;
 import org.yaz4j.jni.yaz4jlib;
 
+/**
+ * Allows to perform an extended-service operation.
+ * 
+ * Once created, a package is configured by means of options, then the package 
+ * is send and the result is inspected (again, by means of options).
+ * 
+ * @see org.yaz4j.ConnectionExtended#getPackage(java.lang.String)
+ * 
+ * @author jakub
+ */
 public class Package {
-    private SWIGTYPE_p_ZOOM_package_p pack;
-    private ConnectionExtended connection;
-    private String type;
-
-    Package(SWIGTYPE_p_ZOOM_package_p pack, ConnectionExtended connection, String type) {
-        this.type = type;
-        this.connection = connection;
-        this.pack = pack;
-    }
+  //for GC ref count
+  private ConnectionExtended conn;
+  private SWIGTYPE_p_ZOOM_package_p pack;
+  private final String type;
 
-    public void finalize() {
-        _dispose();
-    }
+  Package(SWIGTYPE_p_ZOOM_package_p pack, ConnectionExtended conn, String type) {
+    if (type == null)
+      throw new NullPointerException("type cannot be null");
+    this.type = type;
+    this.pack = pack;
+    this.conn = conn;
+  }
 
-    /**
-     * Write option for a specified key
-     * @param key option name
-     * @param value option value
-     * @return package (self) for chainability
-     */
-    public Package option(String key, String value) {
-      yaz4jlib.ZOOM_package_option_set(pack, key, value);
-      return this;
-    }
+  public void finalize() {
+    _dispose();
+  }
 
-    /**
-     * Read option for a specified key.
-     * @param key option name
-     * @return option value
-     */
-    public String option(String key) {
-      return yaz4jlib.ZOOM_package_option_get(pack, key);
-    }
+  /**
+   * Write option for a specified key
+   * @param key option name
+   * @param value option value
+   * @return package (self) for chainability
+   */
+  public Package option(String key, String value) {
+    if (key == null)
+      throw new NullPointerException("option name cannot be null");
+    yaz4jlib.ZOOM_package_option_set(pack, key, value);
+    return this;
+  }
+
+  /**
+   * Read option for a specified key.
+   * @param key option name
+   * @return option value
+   */
+  public String option(String key) {
+    if (key == null)
+      throw new NullPointerException("option name cannot be null");
+    return yaz4jlib.ZOOM_package_option_get(pack, key);
+  }
 
-    /**
-     * Send the package.
-     */
-    public void send() {
-        yaz4jlib.ZOOM_package_send(pack, type);
+  /**
+   * Send the package.
+   */
+  public void send() throws ZoomException {
+    yaz4jlib.ZOOM_package_send(pack, type);
+    ZoomException e = conn.getZoomException();
+    if (e != null) {
+      throw e;
     }
+  }
 
-    void _dispose() {
-        if (pack != null) {
-            yaz4jlib.ZOOM_package_destroy(pack);
-            connection = null;
-            pack = null;
-        }
+  void _dispose() {
+    if (pack != null) {
+      yaz4jlib.ZOOM_package_destroy(pack);
+      pack = null;
+      conn = null;
     }
+  }
 }