Net/Z3950/UDDI/Config.pm untested

filter: Statementstmt 0.0% 0 / 72 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 120 CC 11 SCAR 22.4
CC
11
Cov
0%
_substitute_constants
20.0
_annotate_content
12.0
property
12.0
CRAP 9.4
1 package Net::Z3950::UDDI::Config;
2 use strict;
3 use warnings;
4  
5 use YAML;
6  
7 =head1 NAME
8  
9 Net::Z3950::UDDI::Config - Configuration for z2uddi
10  
11 =head1 SYNOPSIS
12  
13 use Net::Z3950::UDDI::Config;
14 $config = new Net::Z3950::UDDI::Config($uddi, $configFile, $constHash);
15  
16 =head1 DESCRIPTION
17  
18 This module parses configuration files for the C<Net::Z3950::UDDI>
19 module that implements the guts of C<z2uddi>, the Z39.50-to-UDDI
20 gateway.
21  
22 =head1 METHODS
23  
24 =head2 new()
25  
26 $config = new Net::Z3950::UDDI::Config($uddi, $configFile, $constHash);
27 use Data::Dumper; print Dumper($config);
28  
29 Reads the configuration file named by C<$configFile> and parses it on
30 behalf of the UDDI object C<$uddi>, returning a
31 Net::Z3950::UDDI::Config object representing its contents. Constants
32 referred to in the configuration file are substituted with their
33 values as specifed in the hash referenced by C<$constHash>.
34  
35 The returned object is a pretty-much self-documenting rendition of the
36 configuration file, as you will see if you print it out using
37 C<Data::Dumper>.
38  
39 Throws an exception if an error occurs.
40  
41 =cut
42  
43 sub new {
44 my $class = shift();
45 my($uddi, $file, $const) = @_;
46  
47 my $yaml = YAML::LoadFile($file);
48 my $this = bless {
49 uddi => $uddi,
50 file => $file,
51 contents => $yaml,
52 timestamp => time(),
53 }, $class;
54  
55 $this->_substitute_constants($yaml, $const);
56 $this->_annotate_content($yaml);
57 $this->_validate_config($yaml);
58  
59 return $this;
60 }
61  
62  
63 # PRIVATE to new()
64 sub _substitute_constants {
65 my $this = shift();
66 my($val, $const) = @_;
67  
68 my $ref = ref $val;
69 if (!$ref) {
70 my $res = "";
71 while ($val =~ s/(.*?)\$\((.*?)\)//) {
72 my $s = $const->{$2};
73 $this->_throw(100, "no such parameter '$2'") if !defined $s;
74 $res .= $1 . $s;
75 }
76 $_[0] = $res . $val;
77 } elsif ($ref eq "HASH") {
78 foreach my $key (keys %$val) {
79 $this->_substitute_constants($val->{$key}, $const);
80 }
81 } else {
82 $this->_throw(100, "non-hash reference '$ref'");
83 }
84 }
85  
86  
87 # PRIVATE to new()
88 sub _annotate_content {
89 my $this = shift();
90 my($yaml) = @_;
91  
92 bless $yaml, "Net::Z3950::UDDI::Config::Content";
93 _maybe_bless($yaml->{zparams}, "ZParams");
94 my $params = $yaml->{params};
95 _maybe_bless($params, "Params");
96 my $dbs = $yaml->{databases};
97 _maybe_bless($dbs, "Databases");
98  
99 foreach my $dbname (keys %$dbs) {
100 my $db = $dbs->{$dbname};
101 bless $db, "Net::Z3950::UDDI::Config::Database";
102 my $dbparams = $db->{params};
103 $dbparams = $db->{params} = {} if !defined $dbparams;
104 bless $dbparams, "Net::Z3950::UDDI::Config::Params";
105 $dbparams->{"*parent"} = $params;
106  
107 my $inherit = $db->{"inherit-from"};
108 if (defined $inherit) {
109 $db->{"*parent"} = $dbs->{$inherit}
110 or $this->_throw(100,
111 "database '$dbname' inherits from non-existent '$inherit'");
112 }
113 }
114 }
115  
116  
117 # PRIVATE to _annotate_content()
118 sub _maybe_bless {
119 my($thing, $class) = @_;
120 bless $thing, "Net::Z3950::UDDI::Config::$class" if defined $thing;
121 }
122  
123  
124 # PRIVATE to new()
125 sub _validate_config {
126 my $this = shift();
127 my($yaml) = @_;
128  
129 ### To be done when necessary. This could be done
130 # programmatically but it would be nicer to find a way to apply
131 # a Relax-NG schema to a YAML document. That's a non-trivial
132 # job in itself, though, and not urgent.
133 }
134  
135  
136 # Delegate
137 sub _throw {
138 my $this = shift();
139 return $this->{uddi}->_throw(@_);
140 }
141  
142  
143 =head2 file(), timestamp()
144  
145 $fileName = $config->file()
146 $time = $config->timestamp()
147  
148 Accessor methods: C<file()> returns the name of the file that was
149 parsed to generate the configuration object; C<timestamp()> returns
150 the time (in seconds since the epoch) that the configuration was
151 compiled.
152  
153 =cut
154  
155 sub file { shift()->{name} }
156 sub timestamp { shift()->{timestamp} }
157  
158  
159 =head2 Fetching Database and Parameter-Block Properties
160  
161 The configuration information returned from the contructor is a
162 self-describing structure reflecting the contents of the configuration
163 file, which can mostly be traversed by simple hash-accessing. Two of
164 the substructures are special, however: database blocks and parameter
165 blocks. Both of these may have parents, from which they inherit
166 values that they do not define themselves. All database-specific
167 parameter blocks (of type C<Net::Z3950::UDDI::Config::Params>) inherit
168 values from the top-level parameter block; and each database block (of
169 type C<Net::Z3950::UDDI::Config::Database>) may nominate a parent to
170 inherit from using the C<inherit-from> property.
171  
172 In order to ensure that this inheritance works, it is necessary
173 B<always to use the C<property()> method when accessing properties
174 of these blocks>. For example:
175  
176 $url = $database->property("endpoint");
177 $handler = $params->property("soap-fault");
178  
179 =cut
180  
181 package Net::Z3950::UDDI::Config::Database;
182 our @ISA = qw(Net::Z3950::UDDI::Config::Inheritor);
183  
184 package Net::Z3950::UDDI::Config::Params;
185 our @ISA = qw(Net::Z3950::UDDI::Config::Inheritor);
186  
187  
188 package Net::Z3950::UDDI::Config::Inheritor;
189  
190  
191 # Returns the value of the property called $key in $this or an ancestor
192 sub property {
193 my $this = shift();
194 my($key) = @_;
195  
196 my $val = $this->{$key};
197 return $val if defined $val;
198 my $parent = $this->{"*parent"};
199 return $parent->property($key) if defined $parent;
200 return undef;
201 }
202  
203  
204 # Returns a hash of all key/value pairs in $this and ancestors
205 sub properties {
206 my $this = shift();
207
208 my %data;
209 my $parent = $this->{"*parent"};
210 %data = $parent->properties() if defined $parent;
211 foreach my $key (keys %$this) {
212 $data{$key} = $this->{$key} if $key !~ /^\*/;
213 }
214  
215 return %data;
216 }
217  
218  
219 =head1 CONFIGURATION FILE FORMAT
220  
221 C<z2uddi> is configured primarily by a single file (although see the
222 SRU and SRW documentation below). The file is expressed using the
223 metaformat YAML, which is a structured data language conceptually
224 somewhat similar to XML but very much easier for humans to read and
225 write. YAML is described in detail at http://yaml.org/ but very
226 briefly:
227  
228 =over 4
229  
230 =item *
231  
232 Comment lines have hash (C<#>) as their first non-whitespace
233 character, and are ignored.
234  
235 =item *
236  
237 Blank lines are ignored.
238  
239 =item *
240  
241 Data lines are of the form I<key>C<:> I<value>
242  
243 =item *
244  
245 Substructures are introduced by a line of the form I<key>C<:>. All
246 lines of a substructure are indented to the same level, which must be
247 a deeper level than the label that introduces it.
248  
249 =back
250  
251 (There is much more to YAML, but the rest of it is not needed for
252 C<z2uddi> configuration.)
253  
254 As usual, an ounce of example is worth a ton of explanation, so here
255 is a brief bibliography expressed in YAML:
256  
257 bibliography:
258 TaylorNaish2005:
259 authors: Michael P. Taylor, Darren Naish
260 year: 2005
261 title: The phylogenetic taxonomy of Diplodocoidea (Dinosauria: Sauropoda)
262  
263 Wilson2002:
264 authors: Jeffrey A. Wilson
265 year: 2002
266 title: Sauropod dinosaur phylogeny: critique and cladistic analysis
267  
268 In the C<z2uddi> configuration file, the YAML has three top-level
269 elements: C<zparams> contains Z39.50 parameters, C<params> contains
270 parameters that configure the UDDI and SOAP back-ends, and
271 C<databases> contains the configurations for individual databases.
272  
273 C<zparams> may contain three elements, all of them providing
274 information to be returned to Z39.50 client in the Init Response:
275 C<implementation-id>,
276 C<implementation-name>
277 and
278 C<implementation-version>.
279  
280 C<params> may contain any of the following:
281  
282 =over 4
283  
284 =item soap-fault
285  
286 Specifies how the C<SOAP::Lite> library should respond when it detects
287 a fault. Acceptable values are C<die> (the default), C<warn> and
288 C<ignore>.
289  
290 =item soap-debug
291  
292 Specifies what logging the C<SOAP::Lite> module should perform. he
293 value is a list of C<SOAP::Lite> tracing levels, separated by spaces
294 or commas. Recognised levels include but are not limited to:
295 C<method>,
296 C<parameters>,
297 C<fault>,
298 C<trace>,
299 C<debug>
300 and
301 C<all>
302  
303 =back
304  
305 The C<databases> element contains the databases, each named by a
306 key that is the name they will be known as in Z39.50/SRU/SRW.
307  
308 Each database may contain a human-readable C<description>; a C<params>
309 block specific to that database, whose values will override those in
310 the global C<params>; and an C<inherit-from>, which is the name of
311 another database in the same configuration. This last is useful
312 primarily for setting up debugging versions of databases, which
313 inherit all functional parameters from their parent but add more
314 verbose logging.
315  
316 Every database must specify a C<type>, which indicates the name of a
317 back-end plugin to use in fulfulling search requests on this
318 database. At present, the supported values are C<uddi> and C<soap>.
319 The interpretation of other values is dependent on the type:
320  
321 =over 4
322  
323 =item uddi
324  
325 C<endpoint> is mandatory, and specifies the URL of the UDDI endpoint
326 (duh). C<qualifier> is options, and if provided is a list of UDDI
327 find-qualifiers to attach to searches, separated by spaces or commas.
328 Any element whose name begins with C<option-> is interpreted as a UDDI
329 option, and is set into the C<UDDI::HalfDecent> object before any
330 searching or retrieval is done. Recognised options include
331 C<http-version>, C<uddi-version>, C<proxy> and C<loglabel>. Finally,
332 C<log> specifies what types of logging to use, and may contain one or
333 more logging types separated by spaces or commas. See
334 C<UDDI::HalfDecent> for details.
335  
336 =item soap
337  
338 C<proxy>, C<uri> and C<service> correspond to the same-named
339 C<SOAP::Lite> parameters and specify where the SOAP connection is made
340 to. (Maybe the names C<endpoint>, C<namespace> and C<wsdl> would be
341 better, but we'll keep it as it is in honour of C<SOAP::Lite>.
342 C<indexmap> contains one or more Z39.50 attribute-set names, each of
343 which contains the number values of one or more access points (use
344 attributes) from that attribute sets. These access points are mapped
345 onto SOAP method names: searches on the specified access points are
346 translated into the corresponding method calls.
347  
348 =back
349  
350 Anywhere in the configuration, implementation constants may be
351 referenced using the syntax C<$(>I<name>C<)>, and will be substituted
352 by their implementation-defined values. A small number of names are
353 supported, including C<$(package)>, the name of the Perl module that
354 implements the gateway, and C<$(version)>, the current version of that
355 module.
356  
357 An example configuration is provided in the file C<etc/config.yaml> in
358 the C<z2uddi> distribution: if in doubt, copy and massage this file.
359  
360 =head1 XML
361  
362 The configuration file format is based on YAML rather than XML, since
363 YAML's syntax is much cleaner, more concise and more human-readable.
364 If, however, it is considered necessary to configure C<z2uddi> using
365 XML rather than YAML, then the facilities exist to do so: an XSLT
366 stylesheet is described and provided at
367 http://yaml.org/xml.html
368 by which an XML-based configuration file can be automatically
369 transformed into the YAML version that the gateway uses. If the
370 configuration file is maintained in XML (and transformed using simple
371 C<Makefile>, perhaps,) then facilities such as XInclude may be used to
372 break the congfiguration across multiple files.
373  
374 But the YAML version is much nicer.
375  
376 =head1 SRU AND SRW
377  
378 The configuration of C<z2uddi> is expressed in terms of incoming
379 ANSI/NISO Z39.50 connections from the client. Support for the related
380 protocols SRU and SRW (which are REST-like and SOAP-based web-services
381 respectively) is provided by the GFS (Generic Front-end Server) of the
382 YAZ toolkit, which in turn is made available to Perl via the
383 C<Net::Z3950::SimpleServer> module.
384  
385 The GFS translates incoming SRU and SRW requests and reformulates them
386 as Z39.50 before passing them through the the specific server
387 implementation, in particular translating SRU's and SRW's query
388 language into Z39.50's Type-1 query. Configuration of the GFS is done
389 using an XML file, often called C<yazgfs.xml>, which is specified on
390 in the command-line invocation using the C<-f> option. This file in
391 turn nominates a CQL-to-PQF translation file which provides the
392 mappings of specific CQL indexes, relations, etc. to PQF (Prefix Query
393 Format), from which the Z39.50 Type-1 query is made. The format of
394 the GFS configuration file is described at
395 http://indexdata.com/yaz/doc/server.vhosts.tkl
396  
397 So to run C<z2uddi> in such a way that Z39.50, SRU and SRW are all
398 supported, you will need to invoke it as
399  
400 z2uddi config.yaml -f yazgfs.xml
401  
402 Where C<etc/yazgfs.xml> includes the line:
403  
404 <cql2rpn>pqf.properties</cql2rpn>
405  
406 And ensure that the three files C<config.yaml>, C<yazgfs.xml> and
407 C<pqf.properties> are all in the working directory. (There are
408 examples of all three files in the distribution's C<etc> directory.)
409 Then the server can be interrogated using SRU URLs such as
410 http://localhost:8019/gbif?version=1.1&operation=searchRetrieve&query=dc.title=geo%25&maximumRecords=1
411 http://localhost:8019/gbif?version=1.1&operation=searchRetrieve&query=dc.title=/service%20geo%25&maximumRecords=2
412  
413 =head1 SEE ALSO
414  
415 C<Net::Z3950::UDDI>
416 is the module that uses this, and the only one that would ever want
417 to, I'm sure.
418  
419 C<z2uddi> is the gateway program, built on C<Net::Z3950::UDDI>, that
420 is driven by configuration files in this format.
421  
422 http://yaml.org/ is the web-site of the YAML data-serialisation
423 language.
424  
425 =head1 AUTHOR, COPYRIGHT AND LICENSE
426  
427 As for C<Net::Z3950::UDDI>.
428  
429 =cut
430  
431 1;