| 1 |
################################################ |
| 2 |
# |
| 3 |
# $Id: Container.pm,v 1.10 2003/06/06 03:26:24 joko Exp $ |
| 4 |
# |
| 5 |
# $Log: Container.pm,v $ |
| 6 |
# Revision 1.10 2003/06/06 03:26:24 joko |
| 7 |
# + sub existsStorage |
| 8 |
# |
| 9 |
# Revision 1.9 2003/03/27 15:31:08 joko |
| 10 |
# fixes to modules regarding new namespace(s) below Data::Mungle::* |
| 11 |
# |
| 12 |
# Revision 1.8 2003/02/18 19:19:47 joko |
| 13 |
# + modified locator handling |
| 14 |
# |
| 15 |
# Revision 1.7 2003/01/30 22:21:52 joko |
| 16 |
# + changed 'connect'-behaviour back to old state |
| 17 |
# + renamed log-output (now using 'initializing' instead of 'booting' - corresponding to 'initStorage') |
| 18 |
# |
| 19 |
# Revision 1.6 2003/01/30 21:44:00 joko |
| 20 |
# + temporary fix: (FIXME) now connecting to storage on storagehandle-instantiation |
| 21 |
# |
| 22 |
# Revision 1.5 2003/01/19 02:39:57 joko |
| 23 |
# + moved 'deep_copy' from module 'libp' to module 'Data.Transform.Deep' |
| 24 |
# + preserved order for hashes '$self->{config}' and '$self->{locator}' by using Tie::IxHash |
| 25 |
# |
| 26 |
# Revision 1.4 2002/12/04 07:38:07 jonen |
| 27 |
# + deep copy |
| 28 |
# |
| 29 |
# Revision 1.3 2002/12/01 22:18:28 joko |
| 30 |
# - no interactive implicit deploy |
| 31 |
# + only test integrity if storage available |
| 32 |
# |
| 33 |
# Revision 1.2 2002/12/01 07:08:35 joko |
| 34 |
# + needs to "use Data::Storage;" |
| 35 |
# |
| 36 |
# Revision 1.1 2002/11/29 04:54:54 joko |
| 37 |
# + initial check-in |
| 38 |
# |
| 39 |
# Revision 1.1 2002/11/17 06:20:41 joko |
| 40 |
# + initial check in |
| 41 |
# |
| 42 |
# |
| 43 |
################################################ |
| 44 |
|
| 45 |
|
| 46 |
package Data::Storage::Container; |
| 47 |
|
| 48 |
use strict; |
| 49 |
use warnings; |
| 50 |
|
| 51 |
# get logger instance |
| 52 |
my $logger = Log::Dispatch::Config->instance; |
| 53 |
|
| 54 |
|
| 55 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - main |
| 56 |
use Tie::IxHash; |
| 57 |
use Data::Dumper; |
| 58 |
|
| 59 |
use Data::Storage; |
| 60 |
use Data::Storage::Locator; |
| 61 |
use Data::Mungle::Transform::Deep qw( deep_copy ); |
| 62 |
use Data::Mungle::Compare::Struct qw( isEmpty ); |
| 63 |
|
| 64 |
sub new { |
| 65 |
my $invocant = shift; |
| 66 |
my $class = ref($invocant) || $invocant; |
| 67 |
|
| 68 |
# get constructor arguments |
| 69 |
my @args = (); |
| 70 |
@_ && (@args = @_); |
| 71 |
$logger->debug( __PACKAGE__ . "->new( @args )" ); |
| 72 |
|
| 73 |
my $self = { @_ }; |
| 74 |
bless $self, $class; |
| 75 |
|
| 76 |
# preserve order of configuration variables |
| 77 |
#$self->{config} = {}; |
| 78 |
tie %{$self->{config}}, 'Tie::IxHash'; |
| 79 |
tie %{$self->{locator}}, 'Tie::IxHash'; |
| 80 |
|
| 81 |
return $self; |
| 82 |
} |
| 83 |
|
| 84 |
sub addConfig { |
| 85 |
my $self = shift; |
| 86 |
my $name = shift; |
| 87 |
my $config = shift; |
| 88 |
$self->{config}->{$name} = $config; |
| 89 |
} |
| 90 |
|
| 91 |
sub addConfigByDsn { |
| 92 |
my $self = shift; |
| 93 |
my $name = shift; |
| 94 |
my $dsn = shift; |
| 95 |
|
| 96 |
# HACK: assume DBI as default type for now |
| 97 |
# TODO: guess type from dsn |
| 98 |
my $type = "DBI"; |
| 99 |
|
| 100 |
$self->addConfig($name, { type => $type, dsn => $dsn }); |
| 101 |
} |
| 102 |
|
| 103 |
sub addStorage { |
| 104 |
my $self = shift; |
| 105 |
my $name = shift; |
| 106 |
my $storage = shift; |
| 107 |
$self->{storage}->{$name} = $storage; |
| 108 |
} |
| 109 |
|
| 110 |
sub existsStorage { |
| 111 |
my $self = shift; |
| 112 |
my $name = shift; |
| 113 |
return exists $self->{storage}->{$name}; |
| 114 |
} |
| 115 |
|
| 116 |
sub initLocator { |
| 117 |
my $self = shift; |
| 118 |
my $name = shift; |
| 119 |
my $db_config = $self->{config}->{$name}; |
| 120 |
$db_config ||= {}; |
| 121 |
|
| 122 |
$logger->debug( __PACKAGE__ . "->initLocator( name='$name' )" ); |
| 123 |
|
| 124 |
my $cfg_locator = {}; |
| 125 |
|
| 126 |
# set default settings, if any |
| 127 |
my $default = deep_copy($self->{config}->{_default}); |
| 128 |
foreach (keys %$default) { |
| 129 |
$cfg_locator->{$_} = $default->{$_}; |
| 130 |
} |
| 131 |
|
| 132 |
# merge in specific settings |
| 133 |
my $specific = deep_copy($db_config); |
| 134 |
foreach (keys %$specific) { |
| 135 |
$cfg_locator->{$_} = $specific->{$_}; |
| 136 |
} |
| 137 |
|
| 138 |
# HACK: transfer dsn from main to dbi settings |
| 139 |
$cfg_locator->{dbi}->{dsn} = $cfg_locator->{dsn} if $cfg_locator->{dsn}; |
| 140 |
|
| 141 |
# HACK: set classnames empty if none given from config |
| 142 |
$cfg_locator->{classnames} = [] if !$cfg_locator->{classnames}; |
| 143 |
|
| 144 |
# HACK: set errorhandler if dbi settings are present |
| 145 |
$cfg_locator->{dbi}->{HandleError} = \&_dbErrorHandler if $cfg_locator->{dbi}; |
| 146 |
|
| 147 |
# trace |
| 148 |
#print "locator:", "\n"; |
| 149 |
#print Dumper($cfg_locator); |
| 150 |
|
| 151 |
# check locator metadata |
| 152 |
if (isEmpty($cfg_locator)) { |
| 153 |
$logger->warning( __PACKAGE__ . "->initLocator( name='$name' ): Metadata was empty. Please check configuration." ); |
| 154 |
return; |
| 155 |
} |
| 156 |
|
| 157 |
# name it |
| 158 |
$cfg_locator->{name} = $name; |
| 159 |
|
| 160 |
# create new locator object |
| 161 |
$self->{locator}->{$name} = Data::Storage::Locator->new( $cfg_locator ); |
| 162 |
|
| 163 |
} |
| 164 |
|
| 165 |
sub initLocators { |
| 166 |
my $self = shift; |
| 167 |
foreach (keys %{$self->{config}}) { |
| 168 |
#print $_, "\n"; |
| 169 |
$self->initLocator($_, $self->{config}->{$_}) if !/^_/; |
| 170 |
} |
| 171 |
#print "locs: ", Dumper($self->{locator}); |
| 172 |
} |
| 173 |
|
| 174 |
sub getLocator { |
| 175 |
my $self = shift; |
| 176 |
my $name = shift; |
| 177 |
return $self->{locator}->{$name}; |
| 178 |
} |
| 179 |
|
| 180 |
sub initStorage { |
| 181 |
my $self = shift; |
| 182 |
my $name = shift; |
| 183 |
|
| 184 |
return if $self->existsStorage($name); |
| 185 |
|
| 186 |
my $locator = $self->getLocator($name); |
| 187 |
$logger->info( __PACKAGE__ . " is initializing storage declared by locator \"$name\"" ); |
| 188 |
|
| 189 |
my $storage = Data::Storage->new($locator); |
| 190 |
|
| 191 |
# TODO: do below (after 'testAvailability' and 'testIntegrity') again!!! |
| 192 |
#$storage->connect(); |
| 193 |
|
| 194 |
$self->addStorage($name, $storage); |
| 195 |
|
| 196 |
my $log_prefix = __PACKAGE__ . "->initStorage: "; |
| 197 |
$log_prefix .= "dsn=\"$self->{locator}->{$name}->{dsn}\"" if $self->{locator}->{$name}->{dsn}; |
| 198 |
|
| 199 |
# should we test availability of the storage before using it? |
| 200 |
if ($locator->{test_availability}) { |
| 201 |
$locator->{status}->{availability} = $storage->testAvailability(); |
| 202 |
if ( !$storage->testAvailability() ) { |
| 203 |
$logger->error( "$log_prefix: testAvailability failed" ); |
| 204 |
return; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
# should we test integrity of the storage before using it? |
| 209 |
if ($locator->{status}->{availability} && $locator->{test_integrity}) { |
| 210 |
#return unless $storage->testIntegrity(); |
| 211 |
$locator->{status}->{integrity} = $storage->testIntegrity(); |
| 212 |
# actions if integrity fails |
| 213 |
if (!$locator->{status}->{integrity}) { |
| 214 |
$logger->error( "$log_prefix: testIntegrity failed" ); |
| 215 |
return; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
# try to connect... |
| 220 |
# TODO: |
| 221 |
# don't connect right here, do an implicit connect on (later) usage |
| 222 |
# maybe set ->{meta}->{connectmethod} = "connect" here |
| 223 |
#return unless $storage->connect(); |
| 224 |
$storage->connect() if $locator->{status}->{integrity}; |
| 225 |
|
| 226 |
# should we check emptyness? |
| 227 |
if ($locator->{status}->{availability} && $locator->{test_emptyness}) { |
| 228 |
#print "test empty", "\n"; |
| 229 |
if ( !@{$storage->getChildNodes()} ) { |
| 230 |
$locator->{status}->{empty} = 1; |
| 231 |
$logger->warning( "$log_prefix: Storage is empty."); |
| 232 |
return; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
# expand logging? |
| 237 |
if ( $locator->{status}->{integrity} && $locator->{logger} ) { |
| 238 |
# expand logging (to Tangram-Database) |
| 239 |
# TODO: |
| 240 |
# - move configuration data from this code to db_config somehow |
| 241 |
# - do complete handling of this stuff in Data::Storage::* also |
| 242 |
# - just leave a simple on/off-trigger here and/or |
| 243 |
# - make it optionally configurable via e.g. get/set-methods |
| 244 |
# to satisfy "manual"-mode instead of "config"-mode |
| 245 |
# - (re-)add hierarchical logging (each event may have a parent) |
| 246 |
no strict; |
| 247 |
my $creator = sub { return new SystemEvent; }; |
| 248 |
use strict; |
| 249 |
$storage->addLogDispatchHandler("Tangram11", "Log::Dispatch::Tangram", $logger, $creator); |
| 250 |
#$storage_left->addLogDispatchHandler("Tangram11", "Log::Dispatch::Tangram", $logger); |
| 251 |
} |
| 252 |
|
| 253 |
#$self->{storage}->{$name} = $storage; |
| 254 |
#print "add storage: $name", "\n"; |
| 255 |
#$self->addStorage($name, $storage); |
| 256 |
|
| 257 |
return 1; |
| 258 |
|
| 259 |
} |
| 260 |
|
| 261 |
sub initStorages { |
| 262 |
my $self = shift; |
| 263 |
foreach (keys %{$self->{locator}}) { |
| 264 |
#print $_, "\n"; |
| 265 |
$self->initStorage($_); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
sub _dbErrorHandler { |
| 270 |
my $message_db = shift; |
| 271 |
# get logger instance |
| 272 |
#my $logger = Log::Dispatch::Config->instance; |
| 273 |
#$logger->log(level => 'error', message => "Tangram DBI-Error: $message"); |
| 274 |
my $message_log = "DBI-Error: $message_db"; |
| 275 |
print STDERR $message_log, "\n"; |
| 276 |
#$logger->error( $message_log ); |
| 277 |
#$logger->log_to( name => 'file', level => 'error', message => $message_log); |
| 278 |
$logger->log_to( name => 'file', level => 'warning', message => $message_log); |
| 279 |
return 1; |
| 280 |
} |
| 281 |
|
| 282 |
1; |
| 283 |
__END__ |