File Coverage

File:blib/lib/OpenSRF/Utils/SettingsClient.pm
Coverage:25.4%

linestmtbrancondsubpodtimecode
1
9
9
9
9
9
9
52
29
62
62
37
66
use strict; use warnings;
2package OpenSRF::Utils::SettingsClient;
3
9
9
9
79
35
95
use OpenSRF::Utils::SettingsParser;
4
9
9
9
102
39
71
use OpenSRF::System;
5
9
9
9
92
34
67
use OpenSRF::AppSession;
6
9
9
9
60
32
78
use OpenSRF::Utils::Config;
7
9
9
9
62
32
72
use OpenSRF::EX qw(:try);
8
9
9
9
9
69
32
87
use vars qw/$host_config/;
10
11
12
0
sub new {return bless({},shift());}
13my $session;
14$host_config = undef;
15
16my $we_cache = 1;
17sub set_cache {
18
0
        my($self, $val) = @_;
19
0
0
        if(defined($val)) { $we_cache = $val; }
20}
21
22sub has_config {
23
0
0
        if($host_config) { return 1; }
24
0
        return 0;
25}
26
27
28# ------------------------------------
29# utility method for grabbing config info
30sub config_value {
31
0
        my($self,@keys) = @_;
32
33
34
0
        my $bsconfig = OpenSRF::Utils::Config->current;
35
0
        die "No bootstrap config exists. Have you bootstrapped?\n" unless $bsconfig;
36
0
        my $host = $bsconfig->env->hostname;
37
38
0
        if($we_cache) {
39
0
0
                if(!$host_config) { grab_host_config($host); }
40        } else {
41
0
                grab_host_config($host);
42        }
43
44
0
        if(!$host_config) {
45
0
                throw OpenSRF::EX::Config ("Unable to retrieve host config for $host" );
46        }
47
48
0
        my $hash = $host_config;
49
50        # XXX TO DO, check local config 'version',
51        # call out to settings server when necessary....
52        try {
53
0
                for my $key (@keys) {
54
0
                        if(!ref($hash) eq 'HASH'){
55
0
                                return undef;
56                        }
57
0
                        $hash = $hash->{$key};
58                }
59
60        } catch Error with {
61
0
                my $e = shift;
62
0
                throw OpenSRF::EX::Config ("No Config information for @keys : $e : $@");
63
0
        };
64
65
0
        return $hash;
66
67}
68
69
70# XXX make smarter and more robust...
71sub grab_host_config {
72
73
0
        my $host = shift;
74
75
0
        $session = OpenSRF::AppSession->create( "opensrf.settings" ) unless $session;
76
0
        my $bsconfig = OpenSRF::Utils::Config->current;
77
78
0
        my $resp;
79
0
        my $req;
80        try {
81
82
0
0
                if( ! ($session->connect()) ) {die "Settings Connect timed out\n";}
83
0
                $req = $session->request( "opensrf.settings.host_config.get", $host );
84
0
                $resp = $req->recv( timeout => 10 );
85
86        } catch OpenSRF::EX with {
87
88
0
0
                if( ! ($session->connect()) ) {die "Settings Connect timed out\n";}
89
0
                $req = $session->request( "opensrf.settings.default_config.get" );
90
0
                $resp = $req->recv( timeout => 10 );
91
92        } catch Error with {
93
94
0
                my $e = shift;
95
0
                warn "Connection to Settings Failed $e : $@ ***\n";
96
0
                die $e;
97
98        } otherwise {
99
100
0
                my $e = shift;
101
0
                warn "Settings Retrieval Failed $e : $@ ***\n";
102
0
                die $e;
103
0
        };
104
105
0
        if(!$resp) {
106
0
                warn "No Response from settings server...going to sleep\n";
107
0
                sleep;
108        }
109
110
0
        if( $resp && UNIVERSAL::isa( $resp, "OpenSRF::EX" ) ) {
111
0
                throw $resp;
112        }
113
114
0
        $host_config = $resp->content();
115
0
        $req->finish();
116
0
        $session->disconnect();
117
0
        $session->finish;
118
0
        $session->kill_me();
119}
120
121
122
1231;