__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();
}
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 );
}
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;
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;
# 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 {
}
$host_config = $resp->content();
+
+ if(!$host_config) {
+ throw OpenSRF::EX::Config ("Unable to retrieve host config for $host" );
+ }
+
$req->finish();
$session->disconnect();
$session->finish;
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(@_) }
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;