3 |
# $Id$ |
# $Id$ |
4 |
# |
# |
5 |
# $Log$ |
# $Log$ |
6 |
|
# Revision 1.9 2003/03/27 15:31:08 joko |
7 |
|
# fixes to modules regarding new namespace(s) below Data::Mungle::* |
8 |
|
# |
9 |
|
# Revision 1.8 2003/02/18 19:19:47 joko |
10 |
|
# + modified locator handling |
11 |
|
# |
12 |
|
# Revision 1.7 2003/01/30 22:21:52 joko |
13 |
|
# + changed 'connect'-behaviour back to old state |
14 |
|
# + renamed log-output (now using 'initializing' instead of 'booting' - corresponding to 'initStorage') |
15 |
|
# |
16 |
|
# Revision 1.6 2003/01/30 21:44:00 joko |
17 |
|
# + temporary fix: (FIXME) now connecting to storage on storagehandle-instantiation |
18 |
|
# |
19 |
|
# Revision 1.5 2003/01/19 02:39:57 joko |
20 |
|
# + moved 'deep_copy' from module 'libp' to module 'Data.Transform.Deep' |
21 |
|
# + preserved order for hashes '$self->{config}' and '$self->{locator}' by using Tie::IxHash |
22 |
|
# |
23 |
|
# Revision 1.4 2002/12/04 07:38:07 jonen |
24 |
|
# + deep copy |
25 |
|
# |
26 |
|
# Revision 1.3 2002/12/01 22:18:28 joko |
27 |
|
# - no interactive implicit deploy |
28 |
|
# + only test integrity if storage available |
29 |
|
# |
30 |
|
# Revision 1.2 2002/12/01 07:08:35 joko |
31 |
|
# + needs to "use Data::Storage;" |
32 |
|
# |
33 |
# Revision 1.1 2002/11/29 04:54:54 joko |
# Revision 1.1 2002/11/29 04:54:54 joko |
34 |
# + initial check-in |
# + initial check-in |
35 |
# |
# |
48 |
# get logger instance |
# get logger instance |
49 |
my $logger = Log::Dispatch::Config->instance; |
my $logger = Log::Dispatch::Config->instance; |
50 |
|
|
51 |
use libp qw( deep_copy ); |
|
52 |
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - main |
53 |
|
use Tie::IxHash; |
54 |
use Data::Dumper; |
use Data::Dumper; |
55 |
|
|
56 |
|
use Data::Storage; |
57 |
|
use Data::Storage::Locator; |
58 |
|
use Data::Mungle::Transform::Deep qw( deep_copy ); |
59 |
|
use Data::Mungle::Compare::Struct qw( isEmpty ); |
60 |
|
|
61 |
sub new { |
sub new { |
62 |
my $invocant = shift; |
my $invocant = shift; |
68 |
$logger->debug( __PACKAGE__ . "->new( @args )" ); |
$logger->debug( __PACKAGE__ . "->new( @args )" ); |
69 |
|
|
70 |
my $self = { @_ }; |
my $self = { @_ }; |
71 |
return bless $self, $class; |
bless $self, $class; |
72 |
|
|
73 |
|
# preserve order of configuration variables |
74 |
|
#$self->{config} = {}; |
75 |
|
tie %{$self->{config}}, 'Tie::IxHash'; |
76 |
|
tie %{$self->{locator}}, 'Tie::IxHash'; |
77 |
|
|
78 |
|
return $self; |
79 |
} |
} |
80 |
|
|
81 |
sub addConfig { |
sub addConfig { |
108 |
my $self = shift; |
my $self = shift; |
109 |
my $name = shift; |
my $name = shift; |
110 |
my $db_config = $self->{config}->{$name}; |
my $db_config = $self->{config}->{$name}; |
111 |
|
$db_config ||= {}; |
112 |
|
|
113 |
$logger->debug( __PACKAGE__ . "->initLocator( db_config $db_config )" ); |
$logger->debug( __PACKAGE__ . "->initLocator( name='$name' )" ); |
114 |
|
|
115 |
my $cfg_locator = {}; |
my $cfg_locator = {}; |
116 |
|
|
120 |
$cfg_locator->{$_} = $default->{$_}; |
$cfg_locator->{$_} = $default->{$_}; |
121 |
} |
} |
122 |
|
|
|
# name it |
|
|
$cfg_locator->{name} = $name; |
|
|
|
|
123 |
# merge in specific settings |
# merge in specific settings |
124 |
foreach (keys %$db_config) { |
my $specific = deep_copy($db_config); |
125 |
$cfg_locator->{$_} = $db_config->{$_}; |
foreach (keys %$specific) { |
126 |
|
$cfg_locator->{$_} = $specific->{$_}; |
127 |
} |
} |
128 |
|
|
129 |
# HACK: transfer dsn from main to dbi settings |
# HACK: transfer dsn from main to dbi settings |
135 |
# HACK: set errorhandler if dbi settings are present |
# HACK: set errorhandler if dbi settings are present |
136 |
$cfg_locator->{dbi}->{HandleError} = \&_dbErrorHandler if $cfg_locator->{dbi}; |
$cfg_locator->{dbi}->{HandleError} = \&_dbErrorHandler if $cfg_locator->{dbi}; |
137 |
|
|
138 |
|
# trace |
139 |
|
#print "locator:", "\n"; |
140 |
|
#print Dumper($cfg_locator); |
141 |
|
|
142 |
|
# check locator metadata |
143 |
|
if (isEmpty($cfg_locator)) { |
144 |
|
$logger->warning( __PACKAGE__ . "->initLocator( name='$name' ): Metadata was empty. Please check configuration." ); |
145 |
|
return; |
146 |
|
} |
147 |
|
|
148 |
|
# name it |
149 |
|
$cfg_locator->{name} = $name; |
150 |
|
|
151 |
# create new locator object |
# create new locator object |
152 |
$self->{locator}->{$name} = Data::Storage::Locator->new( $cfg_locator ); |
$self->{locator}->{$name} = Data::Storage::Locator->new( $cfg_locator ); |
153 |
|
|
156 |
sub initLocators { |
sub initLocators { |
157 |
my $self = shift; |
my $self = shift; |
158 |
foreach (keys %{$self->{config}}) { |
foreach (keys %{$self->{config}}) { |
159 |
|
#print $_, "\n"; |
160 |
$self->initLocator($_, $self->{config}->{$_}) if !/^_/; |
$self->initLocator($_, $self->{config}->{$_}) if !/^_/; |
161 |
} |
} |
162 |
#print "locs: ", Dumper($self->{locator}); |
#print "locs: ", Dumper($self->{locator}); |
173 |
my $name = shift; |
my $name = shift; |
174 |
|
|
175 |
my $locator = $self->getLocator($name); |
my $locator = $self->getLocator($name); |
176 |
$logger->info( __PACKAGE__ . " is booting storage declared by locator \"$name\"" ); |
$logger->info( __PACKAGE__ . " is initializing storage declared by locator \"$name\"" ); |
177 |
|
|
178 |
my $storage = Data::Storage->new($locator); |
my $storage = Data::Storage->new($locator); |
179 |
|
|
180 |
|
# TODO: do below (after 'testAvailability' and 'testIntegrity') again!!! |
181 |
|
#$storage->connect(); |
182 |
|
|
183 |
my $log_prefix = __PACKAGE__ . "->initStorage: "; |
my $log_prefix = __PACKAGE__ . "->initStorage: "; |
184 |
$log_prefix .= "dsn=\"$self->{locator}->{$name}->{dsn}\"" if $self->{locator}->{$name}->{dsn}; |
$log_prefix .= "dsn=\"$self->{locator}->{$name}->{dsn}\"" if $self->{locator}->{$name}->{dsn}; |
185 |
|
|
186 |
# should we test availability of the storage before using it? |
# should we test availability of the storage before using it? |
187 |
if ($locator->{test_availability}) { |
if ($locator->{test_availability}) { |
188 |
|
$locator->{status}->{availability} = $storage->testAvailability(); |
189 |
if ( !$storage->testAvailability() ) { |
if ( !$storage->testAvailability() ) { |
190 |
$logger->error( "$log_prefix is not available" ); |
$logger->error( "$log_prefix: testAvailability failed" ); |
|
return; |
|
191 |
} |
} |
192 |
} |
} |
193 |
|
|
194 |
# should we test integrity of the storage before using it? |
# should we test integrity of the storage before using it? |
195 |
if ($locator->{test_integrity}) { |
if ($locator->{status}->{availability} && $locator->{test_integrity}) { |
196 |
#return unless $storage->testIntegrity(); |
#return unless $storage->testIntegrity(); |
197 |
$locator->{status}->{integrity} = $storage->testIntegrity(); |
$locator->{status}->{integrity} = $storage->testIntegrity(); |
198 |
# actions if integrity fails |
# actions if integrity fails |
199 |
if (!$locator->{status}->{integrity}) { |
if (!$locator->{status}->{integrity}) { |
200 |
$logger->error( "testIntegrity failed on $log_prefix" ); |
$logger->error( "$log_prefix: testIntegrity failed" ); |
|
print "Try a $locator->{name}.deploySchema()? (y/n) "; |
|
|
my $res = <STDIN>; |
|
|
if ($res =~ m/y/i) { |
|
|
$storage->deploySchema(); |
|
|
} |
|
201 |
} |
} |
202 |
} |
} |
203 |
|
|
206 |
# don't connect right here, do an implicit connect on (later) usage |
# don't connect right here, do an implicit connect on (later) usage |
207 |
# maybe set ->{meta}->{connectmethod} = "connect" here |
# maybe set ->{meta}->{connectmethod} = "connect" here |
208 |
#return unless $storage->connect(); |
#return unless $storage->connect(); |
209 |
$storage->connect(); |
$storage->connect() if $locator->{status}->{integrity}; |
210 |
|
|
211 |
# should we check emptyness? |
# should we check emptyness? |
212 |
if ( $locator->{test_emptyness} && $locator->{status}->{integrity} ) { |
if ($locator->{status}->{availability} && $locator->{test_emptyness}) { |
213 |
|
#print "test empty", "\n"; |
214 |
if ( !@{$storage->getChildNodes()} ) { |
if ( !@{$storage->getChildNodes()} ) { |
215 |
$locator->{status}->{empty} = 1; |
$locator->{status}->{empty} = 1; |
216 |
$logger->warning( "$log_prefix is empty"); |
$logger->warning( "$log_prefix: Storage is empty."); |
217 |
#return; |
#return; |
218 |
} |
} |
219 |
} |
} |
220 |
|
|
221 |
# expand logging? |
# expand logging? |
222 |
if ( $locator->{logger} && $locator->{status}->{integrity} ) { |
if ( $locator->{status}->{integrity} && $locator->{logger} ) { |
223 |
# expand logging (to Tangram-Database) |
# expand logging (to Tangram-Database) |
224 |
# TODO: |
# TODO: |
225 |
# - move configuration data from this code to db_config somehow |
# - move configuration data from this code to db_config somehow |
236 |
} |
} |
237 |
|
|
238 |
#$self->{storage}->{$name} = $storage; |
#$self->{storage}->{$name} = $storage; |
239 |
|
#print "add storage: $name", "\n"; |
240 |
$self->addStorage($name, $storage); |
$self->addStorage($name, $storage); |
241 |
|
|
242 |
return 1; |
return 1; |
246 |
sub initStorages { |
sub initStorages { |
247 |
my $self = shift; |
my $self = shift; |
248 |
foreach (keys %{$self->{locator}}) { |
foreach (keys %{$self->{locator}}) { |
249 |
|
#print $_, "\n"; |
250 |
$self->initStorage($_); |
$self->initStorage($_); |
251 |
} |
} |
252 |
} |
} |