Revert "Bug 15253: Add Koha::Logger based logging for SIP2"
[koha-equinox.git] / C4 / SIP / Sip / Configuration.pm
1 #
2 # parse-config: Parse an XML-format
3 # ACS configuration file and build the configuration
4 # structure.
5 #
6
7 package C4::SIP::Sip::Configuration;
8
9 use strict;
10 use warnings;
11 use XML::Simple qw(:strict);
12
13 my $parser = new XML::Simple(
14     KeyAttr => {
15         login       => '+id',
16         institution => '+id',
17         service     => '+port'
18     },
19     GroupTags => {
20         listeners    => 'service',
21         accounts     => 'login',
22         institutions => 'institution',
23     },
24     ForceArray => [ 'service', 'login', 'institution' ],
25     ValueAttr  => {
26         'error-detect' => 'enabled',
27         'min_servers'  => 'value',
28         'max_servers'  => 'value'
29     }
30 );
31
32 sub new {
33     my ( $class, $config_file ) = @_;
34     my $cfg = $parser->XMLin($config_file);
35     my %listeners;
36
37     # The key to the listeners hash is the 'port' component of the
38     # configuration, which is of the form '[host]:[port]/proto', and
39     # the 'proto' component could be upper-, lower-, or mixed-cased.
40     # Regularize it here to lower-case, and then do the same below in
41     # find_server() when building the keys to search the hash.
42
43     foreach my $service ( values %{ $cfg->{listeners} } ) {
44         $listeners{ lc $service->{port} } = $service;
45     }
46     $cfg->{listeners} = \%listeners;
47
48     return bless $cfg, $class;
49 }
50
51 sub error_detect {
52     my $self = shift;
53     return $self->{'error-detect'};
54 }
55
56 sub accounts {
57     my $self = shift;
58     return values %{ $self->{accounts} };
59 }
60
61 sub find_service {
62     my ( $self, $sockaddr, $port, $proto ) = @_;
63     my $portstr;
64     foreach my $addr ( '', '*:', "$sockaddr:", "[$sockaddr]:" ) {
65         $portstr = sprintf( "%s%s/%s", $addr, $port, lc $proto );
66         Sys::Syslog::syslog( "LOG_DEBUG",
67             "Configuration::find_service: Trying $portstr" );
68         last if ( exists( ( $self->{listeners} )->{$portstr} ) );
69     }
70     return $self->{listeners}->{$portstr};
71 }
72
73 1;