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