]> git.evergreen-ils.org Git - working/Evergreen.git/blob - OpenSRF/src/perlmods/OpenSRF/Utils/SettingsClient.pm
now do an explicit kill me on the session after the host config is retrieved
[working/Evergreen.git] / OpenSRF / src / perlmods / OpenSRF / Utils / SettingsClient.pm
1 use strict; use warnings;
2 package OpenSRF::Utils::SettingsClient;
3 use OpenSRF::Utils::SettingsParser;
4 use OpenSRF::System;
5 use OpenSRF::AppSession;
6 use OpenSRF::Utils::Config;
7 use OpenSRF::EX qw(:try);
8
9 use vars qw/$host_config/;
10
11
12 sub new {return bless({},shift());}
13 my $session;
14 $host_config = undef;
15
16 my $we_cache = 1;
17 sub set_cache {
18         my($self, $val) = @_;
19         if(defined($val)) { $we_cache = $val; }
20 }
21
22
23 # ------------------------------------
24 # utility method for grabbing config info
25 sub config_value {
26         my($self,@keys) = @_;
27
28
29         my $bsconfig = OpenSRF::Utils::Config->current;
30         my $host = $bsconfig->env->hostname;
31
32         if($we_cache) {
33                 if(!$host_config) { grab_host_config($host); }
34         } else {
35                 grab_host_config($host);
36         }
37
38         if(!$host_config) {
39                 throw OpenSRF::EX::Config ("Unable to retrieve host config for $host" );
40         }
41
42         my $hash = $host_config;
43
44         # XXX TO DO, check local config 'version', 
45         # call out to settings server when necessary....
46         try {
47                 for my $key (@keys) {
48                         $hash = $hash->{$key};
49                 }
50
51         } catch Error with {
52                 my $e = shift;
53                 throw OpenSRF::EX::Config ("No Config information for @keys : $e : $@");
54         };
55
56         return $hash;
57
58 }
59
60
61 # XXX make smarter and more robust...
62 sub grab_host_config {
63
64         my $host = shift;
65
66         warn "Grabbing Host config for $host\n";
67         OpenSRF::System->bootstrap_client(client_name => "system_client");
68         $session = OpenSRF::AppSession->create( "opensrf.settings" ) unless $session;
69         my $bsconfig = OpenSRF::Utils::Config->current;
70
71         my $resp;
72         my $req;
73         try {
74
75                 if( ! ($session->connect()) ) {die "Settings Connect timed out\n";}
76                 $req = $session->request( "opensrf.settings.host_config.get", $host );
77                 $resp = $req->recv( timeout => 10 );
78
79         } catch OpenSRF::EX with {
80
81                 my $e = shift;
82                 warn "Connection to Settings Failed  $e : $@ ***\n";
83                 die $e;
84         };
85
86         if(!$resp) {
87                 warn "No Response from settings server...going to sleep\n";
88                 sleep;
89         }
90
91         if( $resp && UNIVERSAL::isa( $resp, "OpenSRF::EX" ) ) {
92                 throw $resp;
93         }
94
95         $host_config = $resp->content();
96         $req->finish();
97         $session->disconnect();
98         $session->finish;
99         $session->kill_me();
100 }
101
102
103
104 1;