3 # This file is part of Stem.
4 # Copyright (C) 1999, 2000, 2001 Stem Systems, Inc.
6 # Stem is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # Stem is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Stem; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 # For a license to use the Stem under conditions other than those
21 # described here, to purchase support for this software, or to purchase a
22 # commercial warranty contract, please contact Stem Systems at:
24 # Stem Systems, Inc. 781-643-7504
25 # 79 Everett St. info@stemsystems.com
36 use Stem::Trace 'log' => 'stem_status', 'sub' => 'TraceStatus' ;
37 use Stem::Trace 'log' => 'stem_error' , 'sub' => 'TraceError' ;
39 Stem::Route::register_class( __PACKAGE__, 'conf' ) ;
41 my @conf_paths = split ':', $Env{ 'conf_path' } || '' ;
42 if ( my $add_conf_path = $Env{ 'add_conf_path' } ) {
44 push @conf_paths, split( ':', $add_conf_path ) ;
53 This is the full path of the configuration file.
60 This is the Hub that this configuration will be sent to.
65 # this does not construct anything. just loads a conf file locally or remotely
69 my( $class ) = shift ;
71 my $self = Stem::Class::parse_args( $attr_spec, @_ ) ;
72 return $self unless ref $self ;
74 if ( my $to_hub = $self->{'to_hub'} ) {
76 my $conf_data = load_conf_file( $self->{'path'} ) ;
78 return $conf_data unless ref $conf_data ;
80 my $msg = Stem::Msg->new(
82 'to_cell' => __PACKAGE__,
83 'from_cell' => __PACKAGE__,
94 my $err = load_conf_file( $self->{'path'}, 1 ) ;
96 TraceError $err if $err ;
106 my( $self, $msg ) = @_ ;
108 my $data = $msg->data() ;
112 push( @conf_names, @{$data} ) if ref $data eq 'ARRAY' ;
113 push( @conf_names, ${$data} ) if ref $data eq 'SCALAR' ;
115 my $err = load_confs( @conf_names ) ;
117 TraceError $err if $err ;
119 return $err if $err ;
126 my( $self, $msg ) = @_ ;
128 my $err = configure( $msg->data() ) ;
130 TraceError $err if $err ;
132 return $err if $err ;
139 my( $conf_path, $do_conf ) = @_ ;
141 -r $conf_path or return "$conf_path can't be read: $!" ;
143 my $conf_data = Stem::Util::load_file( $conf_path ) ;
145 return "Stem::Conf load error:\n$conf_data" unless ref $conf_data ;
147 return $conf_data unless $do_conf ;
149 my $conf_err = configure( $conf_data ) ;
151 return <<ERR if $conf_err ;
152 Configuration error in '$conf_path'
156 # TraceStatus "$conf_path configuration loaded." ;
164 my ( @conf_names ) = @_ ;
167 foreach my $conf_name ( @conf_names ) {
169 $conf_name =~ s/\.stem$// ;
171 for my $path ( @conf_paths ) {
173 my $conf_path = "$path/$conf_name.stem" ;
175 next unless -e $conf_path ;
177 my $err = load_conf_file( $conf_path, 1 ) ;
179 return $err if $err ;
184 local( $" ) = "\n\t" ;
187 Can't find config file '$conf_name.stem' in these directories:
199 my ( $conf_list_ref ) = @_ ;
202 my @notify_done; # list of objects/packages to call config_done on
204 foreach my $conf_ref ( @{$conf_list_ref} ) {
208 if ( ref $conf_ref eq 'HASH' ) {
210 %conf = %{$conf_ref} ;
212 elsif ( ref $conf_ref eq 'ARRAY' ) {
214 %conf = @{$conf_ref} ;
217 return "config entry is not an HASH or ARRAY ref\n" .
218 Dumper($conf_ref). "\n" ;
221 unless ( $class = $conf{'class'} ) {
223 return "Missing class entry in conf\n" .
224 Dumper($conf_ref) . "\n" ;
227 # get the config name for registration
229 my $reg_name = $conf{'name'} || '' ;
233 unless ( %{"::${class}"} ) {
235 my $module = $class ;
236 $module =~ s{::}{/}g ;
241 my $err = eval { require $module } ;
243 return <<ERR if $err && $err !~ /^1/ ;
244 Configure error FOO in Cell '$reg_name' from class '$class' FOO
250 if ( $@ =~ /Can't locate $module/ ) {
252 # this could be a subclass so try to load the parent class
254 next if $module =~ s{/\w+\.pm$}{.pm} ;
257 "Conf: can't find module for class $class" ;
260 return "eval $@\n" if $@ ;
265 # if arguments, call the method or new to get a possible object
267 if ( my $args_ref = $conf{'args'} ) {
271 if ( ref $args_ref eq 'HASH' ) {
273 @args = %{$args_ref} ;
275 elsif ( ref $args_ref eq 'ARRAY' ) {
277 @args = @{$args_ref} ;
281 "args entry is not an HASH or ARRAY ref\n" .
282 Dumper($args_ref). "\n" ;
285 my $method = $conf{'method'} || 'new' ;
288 # register if we have an object
290 #print "NAME: $reg_name\n" ;
292 if ( my $obj = $class->$method(
293 'reg_name' => $reg_name,
296 return <<ERR unless ref $obj ;
297 Configure error in Cell '$reg_name' from class '$class'
301 # register the object by the conf name or the class
303 my $err = Stem::Route::register_cell(
305 $reg_name || $class ) ;
307 return $err if $err ;
308 push @notify_done, $obj if $obj->can('config_done');
313 # or else register the class if we have a name
315 my $err = Stem::Route::register_class( $class, $reg_name ) ;
317 return $err if $err ;
318 push @notify_done, $class if $class->can('config_done');
321 foreach my $class (@notify_done) {
322 $class->config_done();