]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/perl/lib/OpenSRF/Utils/SettingsClient.pm
remove executable bit from module files that don't need it
[OpenSRF.git] / src / perl / lib / 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 sub has_config {
23         if($host_config) { return 1; }
24         return 0;
25 }
26
27
28 # ------------------------------------
29 # utility method for grabbing config info
30 sub config_value {
31         my($self,@keys) = @_;
32
33
34         my $bsconfig = OpenSRF::Utils::Config->current;
35         die "No bootstrap config exists.  Have you bootstrapped?\n" unless $bsconfig;
36         my $host = $bsconfig->env->hostname;
37
38         if($we_cache) {
39                 if(!$host_config) { grab_host_config($host); }
40         } else {
41                 grab_host_config($host);
42         }
43
44         if(!$host_config) {
45                 throw OpenSRF::EX::Config ("Unable to retrieve host config for $host" );
46         }
47
48         my $hash = $host_config;
49
50         # XXX TO DO, check local config 'version', 
51         # call out to settings server when necessary....
52         try {
53                 for my $key (@keys) {
54                         if(!ref($hash) eq 'HASH'){
55                                 return undef;
56                         }
57                         $hash = $hash->{$key};
58                 }
59
60         } catch Error with {
61                 my $e = shift;
62                 throw OpenSRF::EX::Config ("No Config information for @keys : $e : $@");
63         };
64
65         return $hash;
66
67 }
68
69
70 # XXX make smarter and more robust...
71 sub grab_host_config {
72
73         my $host = shift;
74
75         $session = OpenSRF::AppSession->create( "opensrf.settings" ) unless $session;
76         my $bsconfig = OpenSRF::Utils::Config->current;
77
78         my $resp;
79         my $req;
80         try {
81
82                 if( ! ($session->connect()) ) {die "Settings Connect timed out\n";}
83                 $req = $session->request( "opensrf.settings.host_config.get", $host );
84                 $resp = $req->recv( timeout => 10 );
85
86         } catch OpenSRF::EX with {
87
88                 if( ! ($session->connect()) ) {die "Settings Connect timed out\n";}
89                 $req = $session->request( "opensrf.settings.default_config.get" );
90                 $resp = $req->recv( timeout => 10 );
91
92         } catch Error with {
93
94                 my $e = shift;
95                 warn "Connection to Settings Failed  $e : $@ ***\n";
96                 die $e;
97
98         } otherwise {
99
100                 my $e = shift;
101                 warn "Settings Retrieval Failed  $e : $@ ***\n";
102                 die $e;
103         };
104
105         if(!$resp) {
106                 warn "No Response from settings server...going to sleep\n";
107                 sleep;
108         }
109
110         if( $resp && UNIVERSAL::isa( $resp, "OpenSRF::EX" ) ) {
111                 throw $resp;
112         }
113
114         $host_config = $resp->content();
115         $req->finish();
116         $session->disconnect();
117         $session->finish;
118         $session->kill_me();
119 }
120
121
122
123 1;