f54c48238c63ab5eaedd4bda370e656220651eeb
[yaz4j-moved-to-github.git] / README.md
1 INTRODUCTION
2 ============
3
4 Yaz4j is a Java binding for the high-level, client-side portion of the YAZ
5 toolkit known as the ZOOM API. With Yaz4j you can program clients for YAZ-
6 supported protocols like Z39.50, SRU/W and Solr. Yaz4j includes a native
7 component and supports Windows, Linux and OSX.
8
9 Yaz4j is covered by the [Revised BSD](http://www.indexdata.com/licences/revised-bsd) license. That should be same as the the [BSD 3 Clause License](http://opensource.org/licenses/BSD-3-Clause).
10
11 INSTALLATION
12 ============
13
14 Index Data provides ready to use yaz4j RPMs for CentOS 5 and 6, available from
15 our public YUM repository. On Windows yaz4j can be installed with the YAZ
16 [installer][4] (select _Windows_ in the _Downloads_ section on the right)  Those methods are the simplest ways to get yaz4j up and running
17 on the particular platforms and are highly recommended.
18
19
20 Index Data YUM repository (CentOS)
21 ----------------------------------
22
23 Yaz4j with it's runtime and compilation dependencies are provided through
24 Index Data's YUM repository, the repository is enabled by placing the following
25 contents:
26
27     [indexdata-main]
28     name=Index Data Main Repository
29     baseurl=http://ftp.indexdata.com/pub/yum/centos/6/main/$basearch
30     failovermethod=priority
31     gpgcheck=1
32     gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-indexdata
33     enabled=1
34     priority=1
35
36 under `/etc/yum.repos.d/indexdata.repo`.
37
38 With the above repository enabled, yaz4j can be simply installed with:
39
40     yum install yaz4j
41
42 The installation can be verified by running a provided command-line test 
43 program, which executes a search against a public Index Data Z39.50 test server:
44
45     java -jar /usr/share/java/yaz4j.jar
46
47
48 YAZ Installer (Windows)
49 -----------------------
50
51 YAZ Windows installer can be downloaded from [here][4] (select _Windows_ in 
52 the _Downloads_ section on the right). Yaz4j is bundled with
53 the installer: just make sure that during the installation yaz4j box is checked.
54 It is also recommended to check the box for updating the `PATH` environment
55 variable with a path to yaz binaries and DLLs. 
56
57 After installation yaz4j can be tested with (Java runtime environment be [installed][1] separately):
58
59     java -jar C:\Program Files\YAZ\java\yaz4j.jar
60
61 All native libraries and binaries are installed to `C:\Program Files\YAZ\bin\`
62 while the `yaz4j.jar` is installed to `C:\Program Files\YAZ\java`. 
63   
64 `C:\Program Files\YAZ\` is the default YAZ location on a 64-bit Windows and 
65 it is assumed for `yaz-path` in the yaz4j build process.
66
67 COMPILATION FROM SOURCE
68 =======================
69
70 Checking out the source code
71 ----------------------------
72
73 Yaz4j can be checked out from Index Data's Git repository:
74
75     git clone git://git.indexdata.com/yaz4j
76
77 It's recommended to build the latest tagged version (see tags with `git tag`),
78 e.g:
79
80     git checkout v1.5 -b v1.5
81
82
83 Compilation on CentOS/RHEL
84 --------------------------
85
86 Compilation requires [JDK][1], [Maven][2], [Swig][3] and [YAZ][4] development 
87 packages installed.
88
89 Installing build dependencies can be done through the package manager specific 
90 for the distribution (subject to availability). For CentOS 5/6 (YUM) JDK and Swig RPMs can be installed  with:
91
92     yum install java-1.7.0-openjdk-devel swig
93
94 YAZ development package needs to be installed from Index Data's YUM 
95 repository (see the INSTALLATION section on how to enable the YUM repo):
96
97     yum install libyaz5-devel
98
99 Maven is not part of CentOS so a binary distribution needs to be downloaded
100 from Maven [website][2] and installed manually. Refer to Maven's website for
101 details. In case Index Data's YUM repo is enabled, Maven 3 can also be 
102 installed with:
103
104     yum install maven3-indexdata
105
106 in which case the Maven program is called `mvn-id` rather than `mvn`.
107
108 With all dependencies in place you can continue the yaz4j compilation with:
109
110     cd yaz4j
111     mvn install
112
113 Which will also run tests that open a connection to Index Data's public Z39.50
114 server.
115
116 Notice that `yaz-config` binary must be on the `PATH` (this is assured when 
117 `libyaz5-devel` package is installed), if it isn't e.g if a local YAZ (source) 
118 installation is used, the binary location can be specified with:
119
120     mvn -Dyaz.config=/path/to/yaz-config install
121
122 The compiled jar file ends up in `any/target/yaz4j.jar` while the native library
123 in `unix/target/libyaz4j.so`.
124
125 Compilation on generic Unix
126 ---------------------------
127
128 You will need the JDK, Maven, Swig and YAZ development packages.
129 Consult your package manager on how to install those. For compilation
130 of YAZ please consult YAZ [manual][8].
131
132 If yaz-config is in your `PATH`, the following command should suffice:
133
134     mvn install
135
136 If yaz-config is not in your `PATH`, you'll have to tell where YAZ is located:
137
138     mvn -Dyaz.config=/path/to/yaz-config install
139
140 Windows
141 -------
142
143 Besides the exact same requirements as in the Unix case (JDK, Maven, Swig,
144 YAZ), you will need the Windows [SDK][5] installed (which in turn requires
145 .NET Framework 4) to compile yaz4j. Again it's much easier to use the
146 YAZ Installer. Git must be installed to checkout yaz4j source code.
147
148 Use the command prompt provided with the Windows SDK, navigate to the yaz4j
149 source directory and run:
150
151     mvn install
152
153 Default 64-bit YAZ installer location, that is`C:\Program Files\YAZ\`,
154 is assumed for the `yaz.path` property. Nothing is assumed for `swig`, 
155 so you either need to specify an absolute path or update the `PATH` 
156 environment variable to include the directory containing 
157 `swig.exe`. Both can be specified with:
158
159     mvn -Dyaz.path=/path/to/yaz/installdir -Dswig=/path/to/swig/binary install
160
161 The compiled jar file ends up in `any/target/yaz4j.jar` while the native library
162 in `win32/target/libyaz4j.dll`.
163
164
165 YAZ4J AND A SERVLET CONTAINER
166 =============================
167
168 If you are coding a web application that uses yaz4j there's a couple of things
169 to keep in mind. First, you are not invoking the JVM directly, but the servlet
170 container (e.g Tomcat) run/init script is doing that for you and that's the 
171 place to configure any environment settings (e.g the `PATH`). Second, yaz4j 
172 includes static initializers to load the native part and can't be packaged 
173 along with the webapp as that would break on consecutive redeployments. It must
174 be deployed to the servlet container common classloader, similarly to JDBC 
175 drivers.
176
177 For convenience, `yaz4j-tomcat6` RPM is provided in the ID's YUM repo which will
178 set up the default CentOS-provided Tomcat 6 with yaz4j automatically:
179
180     sudo yum install yaz4j-tomcat6
181
182 Linux (CentOS)
183 --------------
184
185 In the case when yaz4j is installed through the RPM (Index Data's YUM repo) the
186 native libraries are placed in the standard system locations (/usr/lib/.. etc)
187 and are readily available to all applications, including Tomcat. The only other
188 thing to configure is to add yaz4j.jar (the pure Java component) to Tomcat's
189 common class loader (further down).
190
191 In the case when yaz4j is built from source or for some other reason the native
192 parts are not present in the standard system library locations, they need to be
193 placed on the servlet container's shared libraries load path. One way
194 to do this in Tomcat (assuming Tomcat is run from a tarball rather than RPM) is
195 by editing  (create it if it does not exist) the `CATALINA_HOME/bin/setenv.sh`
196 script and putting the following lines in there:
197
198     LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/libyaz4j.so
199     export LD_LIBRARY_PATH
200
201 Notice, that this has to be done for all native libraries that `yaz4j.so`
202 depends on, e.g `libyaz5.so` and so on, unless they are already on the default 
203 system library paths (e.g when installed from RPMs).
204
205 If Tomcat is started by a custom init scripts similar operation needs to be
206 performed there.
207
208 The pure Java yaz4j.jar must be added to Tomcat's common classloader so that
209 it becomes available to all deployed webapps and is loaded only once. There
210 are a couple ways to do that.
211
212 One (employed by `yaz4j-tomcat6` RPM) is to place (or symlink) `yaz4j.jar` to
213 to `CATALINA_BASE/lib` (`CATALINA_BASE` is `/usr/share/tomcat6` when Tomcat is
214 installed from RPMs on CentOS):
215
216     ln -s /path/to/yaz4j.jar /usr/share/tomcat6/lib
217
218 and restart Tomcat.
219
220 Another option is to edit the file `catalina.properties` shipped with Tomcat
221 (and located in `CATALINA_BASE/conf/` e.g `/etc/tomcat6/`on RPM-packaged Tomcat)
222 and extend the `common.loader=` property with the following:
223
224     common.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar,/path/to/yaz4j.jar
225
226 again restarting Tomcat afterwards.
227
228 Please consult Tomcat documentation for more information on [classloading][6].
229
230 Windows
231 -------
232
233 On Windows Tomcat will most likely be run from the binary distribution which
234 includes `CATALINA_BASE/bin/setenv.bat` for the purpose of setting up the 
235 environment. Unless, you have installed yaz4j.dll through the YAZ Installer and 
236 checked the option to update the global `PATH` and include all native YAZ and
237 yaz4j components or you have set up the global `PATH` on your own, edit 
238 `setenv.bat` with the following:
239
240     set PATH=%PATH;X:\path\to\yaz\bin;X:\path\to\yaz4j.dll
241
242 The `X:\path\to\yaz\bin` is `C:\Program Files\YAZ\bin` when the installer was
243 used and includes also yaz4j.dll.
244
245 In case Tomcat start up does not execute `setenv.sh`, e.g when custom startup
246 script is used, please include similar steps there.
247
248 To deploy `yaz4j.jar` you must edit `catalina.properties` files, see the Linux 
249 section for details. Again, when the YAZ installer is used `yaz4j.jar` is 
250 located at `C:\Program Files\YAZ\java\yaz4j.jar` by default.
251
252
253 Deploy a test app
254 -----------------
255
256 Under `yaz4j/examples` you'll find a small webapp called `zgate`. This can be 
257 deployed to Tomcat to test the yaz4j installation. To do so:
258
259     cd yaz4j/examples/zgate
260     mvn install
261     cp target/zgate.war CATALINA_BASE/webapps
262
263 (substitute / with \\ and copy as necessary under Windows!)
264
265 If successful you can run the application with a URL as follows:
266
267 [http://localhost:8080/zgate/?zurl=z3950.indexdata.com/marc&query=computer&syntax=usmarc](http://localhost:8080/zgate/?zurl=z3950.indexdata.com/marc&query=computer&syntax=usmarc)
268
269 You should see results from the Index Data's test z3950 server.
270
271 PREPARING A DEVELOPMENT ENVIRONMENT
272 ===================================
273
274 Maven
275 -----
276
277 If you are using maven to build your application you can include Index Data's
278 maven repository and include yaz4j as a dependency in your jar or war project:
279
280 Index Data's Maven repository (put under `<repositories/>` in pom.xml):
281
282     <repository>
283       <id>id-maven-repo</id>
284       <name>Indexdata Maven Repository</name>
285       <url>http://maven.indexdata.com/</url>
286     </repository>
287
288 Yaz4j API dependency (put under `<dependencies/>` in pom.xml):
289
290     <dependency>
291       <groupId>org.yaz4j</groupId>
292       <artifactId>yaz4j-any</artifactId>
293       <version>VERSION</version>
294       <scope>provided</scope>
295     </dependency>
296
297
298 It's crucial that the scope of this dependency is set to `provided` for web
299 application type projects, otherwise the library would end up packaged in 
300 the .war archive and we wouldn't want that.
301
302 Yaz4j includes a trivial HTTP to z3590 gateway under `examples/zgate` that shows
303 best how to use yaz4j in a servlet. There's also a blog entry on building the
304 gateway [here][7]
305
306
307 [1]: http://www.oracle.com/technetwork/java/javase/downloads/index.html "JDK"
308
309 [2]: http://maven.apache.org/download.cgi "Maven"
310
311 [3]: http://www.swig.org/download.html "Swig"
312
313 [4]: http://www.indexdata.com/yaz "YAZ"
314
315 [5]: http://www.microsoft.com/en-us/download/details.aspx?id=8279 "Windows SDK"
316
317 [6]: https://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html "Tomcat 6 class loading"
318
319 [7]: http://www.indexdata.com/blog/2010/02/building-simple-http-z3950-gateway-using-yaz4j-and-tomcat "Building a simple HTTP-to-Z39.50 gateway using Yaz4j and Tomcat"
320
321 [8]: http://www.indexdata.com/yaz/doc/installation.html "YAZ Compilation"