|
|
||||
|
|
Yaz4J is a wrapper library over the client-specific parts of YAZ, a C-based Z39.50 toolkit, and allows you to use the ZOOM API directly from Java. Initial version of Yaz4j has been written by Rob Styles from Talis and the project is now developed and maintained at Index Data. ZOOM is a relatively straightforward API and with a few lines of code you can write a basic application that can establish connection to a Z39.50 server. Here we will try to build a very simple HTTP-to-Z3950 gateway using yaz4j and the Java Servlet technology. COMPILING AND INSTALLING YAZ4JYaz4j is still an experimental piece of software and as such is not distributed via Index Data's public Debian Apt repository and there is no Windows build (yet) either. While it is possible to use the pre-built Linux binaries, users of other OSes will have to compile yaz4j from source. No need to worry (yet) - the process of compiling yaz4j is quite simple and we will be up and running in no time :). As a prerequisite, to complete th build process you will need JDK, Maven, Swig and Yaz (development package) installed on your machine. On Debian/Ubuntu you can get those easily via apt:
The Yaz4j's source code can be checked-out out from our Git repository, and assuming you have Git installed on your machine you can do that with:
The compilation of both native and Java source code is controlled by Maven2, to build the library, invoke the following commands:
That's it. If the build has completed successfully you end up with two files: os-independent jar archive with Java ZOOM API classes (yaz4j/any/target/yaz4j-any-VERSION.jar) and os-dependent shared library (yaz4j/linux/target/libyaz4j.so or yaz4j/win32/target/yaz4j.dll) that contains all necessary JNI "glue" to make the native calls possible from Java. If we were writing a command line Java application, like any other external Java library, yaz4j-any-VERSION.jar would have to be placed on your application classpath and the native, shared library would have to be added to your system shared library path (LD_LIBRARY_PATH on linux, PATH on Windows) or specified as a Java system property (namely the java.library.path) just before your application is executed:
SETTING UP THE DEVELOPMENT ENVIRONMENTSetting up a development/runtime environment for a web (servlet) application is
a bit more complicated. First, you are not invoking the JVM directly, but the
servlet container (e.g Tomcat) run-script is doing that for you. At this
point the shared library (so or dll) has to be placed on the servlet container's
shared libraries load path. Unless your library is deployed to the standard
system location for shared libs (
on Windows (though no Windows build is yet provided)
That's one way of doing it, another would be to alter the standard set of
arguments passed to the JVM before the Tomcat starts and add
With the shared library installed we need to install the pure-Java yaz4j-any*jar
with ZOOM API classes by placing it in Tomcat's WRITING A SERVLET-BASED GATEWAYWith your servlet environment set up all that is left is to write the actual application (peanuts :)). At Index Data we use Maven for managing builds of our Java software components but Maven is also a great tool for quickly starting up a project. To generate a skeleton for our webapp use the Maven archetype plugin:
This will generate a basic webapp project structure: |-- pom.xml `-- src |-- main | |-- java | | `-- com | | `-- indexdata | | `-- zgate | `-- webapp | |-- WEB-INF | | `-- web.xml | `-- index.jsp `-- test `-- java `-- com `-- indexdata `-- zgate Maven has already added basic JEE APIs for web development as the project
dependencies, we need to do the same for yaz4j, so edit the <dependency> <groupId>org.yaz4j</groupId> <artifactId>yaz4j-any</artifactId> <version>VERSION</version> <scope>provided</scope> </dependency> It's crucial that the scope of this dependency is set to The implementation of our simple gateway will be contained in a single servlet -
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String zurl = request.getParameter("zurl"); if (zurl == null || zurl.isEmpty()) { response.sendError(400, "Missing parameter 'zurl'"); return; } String query = request.getParameter("query"); if (query == null || query.isEmpty()) { response.sendError(400, "Missing parameter 'query'"); return; } String syntax = request.getParameter("syntax"); if (syntax == null || syntax.isEmpty()) { response.sendError(400, "Missing parameter 'syntax'"); return; } int maxrecs=10; if (request.getParameter("maxrecs") != null && !request.getParameter("maxrecs").isEmpty()) { try { maxrecs = Integer.parseInt(request.getParameter("maxrecs")); } catch (NumberFormatException nfe) { response.sendError(400, "Malformed parameter 'maxrecs'"); return; } } response.getWriter().println("SEARCH PARAMETERS"); response.getWriter().println("zurl: " + zurl); response.getWriter().println("query: " + query); response.getWriter().println("syntax: " + syntax); response.getWriter().println("maxrecs: " + maxrecs); response.getWriter().println(); Connection con = new Connection(zurl, 0); con.setSyntax(syntax); try { con.connect(); ResultSet set = con.search(query, Connection.QueryType.PrefixQuery); response.getWriter().println("Showing "+maxrecs+" of "+set.getSize()); response.getWriter().println(); for(int i=0; i<set.getSize() && i<maxrecs; i++) { Record rec = set.getRecord(i); response.getWriter().print(rec.render()); } } catch (ZoomException ze) { throw new ServletException(ze); } finally { con.close(); } } With the code in-place we can try to compile the project:
If all is OK, the next step is to register our servlet and map it to an URL in src/main/webapp/WEB-INF/web.xml: <servlet> <servlet-name>ZgateServlet</servlet-name> <servlet-class>com.indexdata.zgate.ZgateServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ZgateServlet</servlet-name> <url-pattern>/zgate</url-pattern> </servlet-mapping> On top of that, we will also make sure that our servlet is automatically triggered when accessing the root path of our application: <welcome-file-list> <welcome-file>zgate</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list> Now we are ready to build our webapp:
The resulting .war archive is located under http://localhost:8080/zgate/?zurl=z3950.loc.gov:7090/voyager... That's it! You just build yourself a HTTP-to-Z3950 gateway! Just be careful
with exposing it to the outside world - it's not very secure and could be
easily exploited. The source code and the gateway's Maven project is available
in the Yaz4j's Git repository under |
|
||
|
|
||||
| Copyright Index Data LLC 2010 | ||||
Thanks for the yaz4j! It's a
Thanks for the yaz4j! It's a great! But i have a one question: how about extended services z39.50 (for CRUD operations in zebra)?
Windows build
Hello,
I tried to follow your instructions on a windows machine, but unfortunately it seems that there is a directory (win32) missing to perform the build. I just copied the linux directory and renamed it, but of course that did not work without adapting the pom.xml for windows. At the moment it is complaining that the yaz-config executable is missing, which does not seem to exist on windows.
Do you have any hints on how to do that? Or do you already have built on the windows platform and can provide me with binaries?
Thanks in advance for any help!
Best Regards,
Christoph
Hi Christoph, As mentioned at
Hi Christoph,
As mentioned at the end of the article we do not provide Windows build files or binaries for yaz4j at this point. Main reason for that is that we generally do not use Windows for development or deployment of our software and currently yaz4j is being used in an UNIX-only (Linux, MacOSX) environment. We are always open for enhancement requests, however, at the moment we can only introduce Windows support as a result of sponsored development.
Yours,
Jakub Skoczen
Index Data
Hi Jakub, thank you for your
Hi Jakub,
thank you for your reply.
With a lot of ugly hacks (unfortunately I know nothing about maven) I managed to build the .dll under windows using mingw.
I even manged to build it again with Visual Studio using the generated source. If you are interested, I can send you the pom.xml I used to build under windows (and the binary), however since I don't know much about maven it is full of hacks and hardcoded paths.
I now tried the library and used it to do a lot of searches with one connection. However, after about 100 requests I get an exception "TooManyResultSetsCreated". Therefore, I have to close the connection and open it again. Is that expected behavior, or is there a way to "close" a resultset, so that I can create another search?
Best Regards,
Christoph
Yes, please send the patch to
Yes, please send the patch to jakub AT indexdata.dk and maybe I'll manage to get it to the next release.
TooManyResultSetsCreated is a Bib1 diagnostic returned by the server to indicate that you have exhausted the number of result sets allowed by the server. By default YAZ will create a new server-side result set for every client-side one. One way to avoid it is to keep only one "server-side" result set, and you can do that by "naming" it:
con.option("setname", "default")
Name "default" is highly recommended as some server do not support name result sets at all (but they will work with "default").
Now, if you need to keep more server-side result sets alive you need to figure out the number supported by the server and set the "setname" option (to whatever) on the connection object just before you execute search() and then keep on overwriting old result sets as you need to create new ones.
btw, for any further question please send them to the yaz-mailing list http://lists.indexdata.dk/cgi-bin/mailman/listinfo/yazlist
import
import org.yaz4j.Connection;
import org.yaz4j.Record;
import org.yaz4j.ResultSet;
import org.yaz4j.exception.ZoomException;
public class Main {
public static void main(String[] args) {
Connection con = new Connection("z3950.loc.gov:7090/voyager", 0);
try {
con.setSyntax("rusmarc");
con.connect();
ResultSet set = con.search("@attr 1=7 0253333490", Connection.QueryType.PrefixQuery);
Record rec = set.getRecord(0);
System.out.print(rec.render());
} catch (ZoomException ze) {
//fail(ze.getMessage());
} finally {
con.close();
}
}
This code crushed java, bc type of records rusmarc. epic fail :(
Bug 3115
Why repeating this program, Sergey? I have already filed this as a bug and informed you Dec 2009. The bug is still open: NOT fixed.
http://bugzilla.indexdata.dk/show_bug.cgi?id=3115
Looks like the LC server just doesn't support RUSMARC
Sergios,
I don't think this is YAZ4J's fault, or indeed the fault of any of the client-side software. Using zoomsh to invoke the underlying ZOOM-C functions directly, we can verify that the LC server just plain doesn't support the RUSMARC record syntax:
mike@xeno:~$ zoomsh
ZOOM>open z3950.loc.gov:7090/voyager
ZOOM>set preferredRecordSyntax rusmarc
ZOOM>find @attr 1=7 0253333490
z3950.loc.gov:7090/voyager: 1 hits
ZOOM>show 0
z3950.loc.gov:7090/voyager error: Record not available in requested syntax (Bib-1:238) 1.2.840.10003.5.28
ZOOM>
I know that lc does not
I know that lc does not support rusmarc. In this case, it should throw an exception on that record type is not supported, but java destroyed. This problem exists with any z server, including zebra
Please be more explicit about what happens
What do you mean by saying that "java destroyed"? Surely you're not saying that your Java implementation's JVM crashes? (If so, that is a bug in Java itself.) What is actually printed when you run your program?
ubuntu 9.10 (32) (new
ubuntu 9.10 (32) (new installations) running on vmware 7. jdk, swig, yaz etc installed from apt-get.
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0xb7675f93, pid=20362, tid=3067304816
#
# JRE version: 6.0_15-b03
# Java VM: Java HotSpot(TM) Client VM (14.1-b02 mixed mode, sharing linux-x86 )
# Problematic frame:
# C [libc.so.6+0x72f93] strlen+0x33
#
# An error report file with more information is saved as:
# /home/sergios/NetBeansProjects/JavaApplication1/hs_err_pid20362.log
#
# If you would like to submit a bug report, please visit:
# http://java.sun.com/webapps/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
sorry, forget send listing of
sorry, forget send listing of log file
http://paste.bradleygill.com/?paste_id=32015
i'll take a look at it
i'll take a look at it