From 84d949cc09de9e2689b76541caeae524ab3b1f1e Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Tue, 23 Jul 2013 11:31:34 -0400 Subject: [PATCH] LP1204123 opensrf.settings reload file option opensrf.settings API calls opensrf.settings.host_config.get opensrf.settings.default_config.get now sport an additional "reload" boolean parameter. When set, the settings file (opensrf.xml) will be reloaded from the file system to pick up any changes which have occurred since the last load/reload. Signed-off-by: Bill Erickson Signed-off-by: Jason Stephenson --- src/perl/lib/OpenSRF/Application/Settings.pm | 13 ++++-- src/perl/lib/OpenSRF/Utils/SettingsClient.pm | 37 ++++++--------- src/perl/lib/OpenSRF/Utils/SettingsParser.pm | 49 +++++++++----------- 3 files changed, 44 insertions(+), 55 deletions(-) diff --git a/src/perl/lib/OpenSRF/Application/Settings.pm b/src/perl/lib/OpenSRF/Application/Settings.pm index 66d9f32..f46fb4e 100644 --- a/src/perl/lib/OpenSRF/Application/Settings.pm +++ b/src/perl/lib/OpenSRF/Application/Settings.pm @@ -11,15 +11,17 @@ sub child_exit { __PACKAGE__->register_method( method => 'get_host_config', api_name => 'opensrf.settings.host_config.get' ); sub get_host_config { - my( $self, $client, $host ) = @_; + my( $self, $client, $host, $reload ) = @_; my $parser = OpenSRF::Utils::SettingsParser->new(); + $parser->reload if $reload; return $parser->get_server_config($host); } __PACKAGE__->register_method( method => 'get_default_config', api_name => 'opensrf.settings.default_config.get' ); sub get_default_config { - my( $self, $client ) = @_; + my( $self, $client, $reload ) = @_; my $parser = OpenSRF::Utils::SettingsParser->new(); + $parser->reload if $reload; return $parser->get_default_config(); } @@ -33,9 +35,10 @@ __PACKAGE__->register_method( api_name => 'opensrf.settings.xpath.get.raw' ); sub xpath_get { - my($self, $client, $xpath) = @_; - warn "*************** Received XPATH $xpath\n"; - return OpenSRF::Utils::SettingsParser->new()->_get_all( $xpath ); + my($self, $client, $xpath, $reload) = @_; + my $parser = OpenSRF::Utils::SettingsParser->new(); + $parser->reload if $reload; + return $parser->_get_all( $xpath ); } diff --git a/src/perl/lib/OpenSRF/Utils/SettingsClient.pm b/src/perl/lib/OpenSRF/Utils/SettingsClient.pm index ab936f3..aaec612 100644 --- a/src/perl/lib/OpenSRF/Utils/SettingsClient.pm +++ b/src/perl/lib/OpenSRF/Utils/SettingsClient.pm @@ -13,12 +13,6 @@ sub new {return bless({},shift());} my $session; $host_config = undef; -my $we_cache = 1; -sub set_cache { - my($self, $val) = @_; - if(defined($val)) { $we_cache = $val; } -} - sub has_config { if($host_config) { return 1; } return 0; @@ -30,20 +24,7 @@ sub has_config { sub config_value { my($self,@keys) = @_; - - my $bsconfig = OpenSRF::Utils::Config->current; - die "No bootstrap config exists. Have you bootstrapped?\n" unless $bsconfig; - my $host = $bsconfig->env->hostname; - - if($we_cache) { - if(!$host_config) { grab_host_config($host); } - } else { - grab_host_config($host); - } - - if(!$host_config) { - throw OpenSRF::EX::Config ("Unable to retrieve host config for $host" ); - } + $self->grab_host_config unless $host_config; my $hash = $host_config; @@ -69,24 +50,27 @@ sub config_value { # XXX make smarter and more robust... sub grab_host_config { + my $self = shift; + my $reload = shift; - my $host = shift; + my $bsconfig = OpenSRF::Utils::Config->current; + die "No bootstrap config exists. Have you bootstrapped?\n" unless $bsconfig; + my $host = $bsconfig->env->hostname; $session = OpenSRF::AppSession->create( "opensrf.settings" ) unless $session; - my $bsconfig = OpenSRF::Utils::Config->current; my $resp; my $req; try { if( ! ($session->connect()) ) {die "Settings Connect timed out\n";} - $req = $session->request( "opensrf.settings.host_config.get", $host ); + $req = $session->request( "opensrf.settings.host_config.get", $host, $reload); $resp = $req->recv( timeout => 10 ); } catch OpenSRF::EX with { if( ! ($session->connect()) ) {die "Settings Connect timed out\n";} - $req = $session->request( "opensrf.settings.default_config.get" ); + $req = $session->request( "opensrf.settings.default_config.get", $reload ); $resp = $req->recv( timeout => 10 ); } catch Error with { @@ -112,6 +96,11 @@ sub grab_host_config { } $host_config = $resp->content(); + + if(!$host_config) { + throw OpenSRF::EX::Config ("Unable to retrieve host config for $host" ); + } + $req->finish(); $session->disconnect(); $session->finish; diff --git a/src/perl/lib/OpenSRF/Utils/SettingsParser.pm b/src/perl/lib/OpenSRF/Utils/SettingsParser.pm index dd504d6..8d5e5fe 100644 --- a/src/perl/lib/OpenSRF/Utils/SettingsParser.pm +++ b/src/perl/lib/OpenSRF/Utils/SettingsParser.pm @@ -2,34 +2,46 @@ use strict; use warnings; package OpenSRF::Utils::SettingsParser; use OpenSRF::Utils::Config; use OpenSRF::EX qw(:try); - - - +use OpenSRF::Utils::Logger; use XML::LibXML; +# logger is not yet exported when this mod is loaded +my $logger = 'OpenSRF::Utils::Logger'; + sub DESTROY{} -our $log = 'OpenSRF::Utils::Logger'; -my $parser; my $doc; +my $settings_file; # /path/to/opensrf.xml sub new { return bless({},shift()); } +# reload the configuration file +sub reload { + my $self = shift; + $logger->info("settings parser reloading '$settings_file'"); + $self->initialize; +} + # returns 0 if the config file could not be found or if there is a parse error # returns 1 if successful sub initialize { + my ($self, $filename) = @_; - my ($self,$bootstrap_config) = @_; - return 0 unless($self and $bootstrap_config); + $settings_file = $filename if $filename; + return 0 unless $settings_file; - $parser = XML::LibXML->new(); + my $parser = XML::LibXML->new(); $parser->keep_blanks(0); + + my $err; try { - $doc = $parser->parse_file( $bootstrap_config ); + $doc = $parser->parse_file( $settings_file ); } catch Error with { - return 0; + $err = shift; + $logger->error("Error parsing $settings_file : $err"); }; - return 1; + + return $err ? 0 : 1; } sub _get { _get_overlay(@_) } @@ -150,19 +162,4 @@ sub get_default_config { return $self->_get( $xpath ); } -sub get_bootstrap_config { - my( $self ) = @_; - my $xpath = "/opensrf/bootstrap"; - return $self->_get( $xpath ); -} - -sub get_router_config { - my( $self, $router ) = @_; - my $xpath = "/opensrf/routers/$router"; - return $self->_get($xpath ); -} - - - - 1; -- 2.43.2