| 1 |
joko |
1.15 |
## ------------------------------------------------------------------------ |
| 2 |
|
|
## |
| 3 |
joko |
1.19 |
## $Id: Storage.pm,v 1.18 2003/01/30 22:12:17 joko Exp $ |
| 4 |
joko |
1.15 |
## |
| 5 |
|
|
## Copyright (c) 2002 Andreas Motl <andreas.motl@ilo.de> |
| 6 |
|
|
## |
| 7 |
|
|
## See COPYRIGHT section in pod text below for usage and distribution rights. |
| 8 |
|
|
## |
| 9 |
|
|
## ------------------------------------------------------------------------ |
| 10 |
|
|
## |
| 11 |
|
|
## $Log: Storage.pm,v $ |
| 12 |
joko |
1.19 |
## Revision 1.18 2003/01/30 22:12:17 joko |
| 13 |
|
|
## - removed/refactored old code: ->Data::Storage::Handler::Tangram|DBI |
| 14 |
|
|
## |
| 15 |
joko |
1.18 |
## Revision 1.17 2003/01/30 21:42:22 joko |
| 16 |
|
|
## + minor update: renamed method |
| 17 |
|
|
## |
| 18 |
joko |
1.17 |
## Revision 1.16 2003/01/20 16:52:13 joko |
| 19 |
|
|
## + now using 'DesignPattern::Object' to create a new 'Data::Storage::Handler::Xyz' on demand - before we did this in a hand-rolled fashion |
| 20 |
|
|
## |
| 21 |
joko |
1.16 |
## Revision 1.15 2003/01/19 03:12:59 joko |
| 22 |
|
|
## + modified header |
| 23 |
|
|
## - removed pod-documentation - now in 'Storage.pod' |
| 24 |
|
|
## |
| 25 |
joko |
1.15 |
## Revision 1.14 2002/12/19 16:27:59 joko |
| 26 |
|
|
## - moved 'sub dropDb' to Data::Storage::Handler::DBI |
| 27 |
|
|
## |
| 28 |
|
|
## Revision 1.13 2002/12/17 21:54:12 joko |
| 29 |
|
|
## + feature when using Tangram: |
| 30 |
|
|
## + what? each object created should delivered with a globally(!?) unique identifier (GUID) besides the native tangram object id (OID) |
| 31 |
|
|
## + patched Tangram::Storage (jonen) |
| 32 |
|
|
## + enhanced Data::Storage::Schema::Tangram (joko) |
| 33 |
|
|
## + enhanced Data::Storage::Handler::Tangram 'sub getObjectByGuid' (jonen) |
| 34 |
|
|
## + how? |
| 35 |
|
|
## + each concrete (non-abstract) class gets injected with an additional field/property called 'guid' - this is done (dynamically) on schema level |
| 36 |
|
|
## + this property ('guid') gets filled on object creation/insertion from 'sub Tangram::Storage::_insert' using Data::UUID from CPAN |
| 37 |
|
|
## + (as for now) this property can get accessed by calling 'getObjectByGuid' on the already known storage-handle used throughout the application |
| 38 |
|
|
## |
| 39 |
|
|
## Revision 1.12 2002/12/12 02:50:15 joko |
| 40 |
|
|
## + this now (unfortunately) needs DBI for some helper functions |
| 41 |
|
|
## + TODO: these have to be refactored to another scope! (soon!) |
| 42 |
|
|
## |
| 43 |
|
|
## Revision 1.11 2002/12/11 06:53:19 joko |
| 44 |
|
|
## + updated pod |
| 45 |
|
|
## |
| 46 |
|
|
## Revision 1.10 2002/12/07 03:37:23 joko |
| 47 |
|
|
## + updated pod |
| 48 |
|
|
## |
| 49 |
|
|
## Revision 1.9 2002/12/01 22:15:45 joko |
| 50 |
|
|
## - sub createDb: moved to handler |
| 51 |
|
|
## |
| 52 |
|
|
## Revision 1.8 2002/11/29 04:48:23 joko |
| 53 |
|
|
## + updated pod |
| 54 |
|
|
## |
| 55 |
|
|
## Revision 1.7 2002/11/17 06:07:18 joko |
| 56 |
|
|
## + creating the handler is easier than proposed first - for now :-) |
| 57 |
|
|
## + sub testAvailability |
| 58 |
|
|
## |
| 59 |
|
|
## Revision 1.6 2002/11/09 01:04:58 joko |
| 60 |
|
|
## + updated pod |
| 61 |
|
|
## |
| 62 |
|
|
## Revision 1.5 2002/10/29 19:24:18 joko |
| 63 |
|
|
## - reduced logging |
| 64 |
|
|
## + added some pod |
| 65 |
|
|
## |
| 66 |
|
|
## Revision 1.4 2002/10/27 18:35:07 joko |
| 67 |
|
|
## + added pod |
| 68 |
|
|
## |
| 69 |
|
|
## Revision 1.3 2002/10/25 11:40:37 joko |
| 70 |
|
|
## + enhanced robustness |
| 71 |
|
|
## + more logging for debug-levels |
| 72 |
|
|
## + sub dropDb |
| 73 |
|
|
## |
| 74 |
|
|
## Revision 1.2 2002/10/17 00:04:29 joko |
| 75 |
|
|
## + sub createDb |
| 76 |
|
|
## + sub isConnected |
| 77 |
|
|
## + bugfixes regarding "deep recursion" stuff |
| 78 |
|
|
## |
| 79 |
|
|
## Revision 1.1 2002/10/10 03:43:12 cvsjoko |
| 80 |
|
|
## + new |
| 81 |
|
|
## ------------------------------------------------------------------------ |
| 82 |
cvsjoko |
1.1 |
|
| 83 |
joko |
1.3 |
|
| 84 |
joko |
1.4 |
BEGIN { |
| 85 |
joko |
1.15 |
$Data::Storage::VERSION = 0.03; |
| 86 |
joko |
1.4 |
} |
| 87 |
|
|
|
| 88 |
cvsjoko |
1.1 |
package Data::Storage; |
| 89 |
|
|
|
| 90 |
|
|
use strict; |
| 91 |
|
|
use warnings; |
| 92 |
|
|
|
| 93 |
joko |
1.16 |
use Data::Dumper; |
| 94 |
|
|
# FIXME: wipe out! |
| 95 |
|
|
use DBI; |
| 96 |
|
|
|
| 97 |
cvsjoko |
1.1 |
use Data::Storage::Locator; |
| 98 |
joko |
1.16 |
use DesignPattern::Object; |
| 99 |
cvsjoko |
1.1 |
|
| 100 |
joko |
1.12 |
|
| 101 |
joko |
1.6 |
# TODO: actually implement level (integrate with Log::Dispatch) |
| 102 |
joko |
1.5 |
my $TRACELEVEL = 0; |
| 103 |
|
|
|
| 104 |
cvsjoko |
1.1 |
# get logger instance |
| 105 |
|
|
my $logger = Log::Dispatch::Config->instance; |
| 106 |
|
|
|
| 107 |
|
|
sub new { |
| 108 |
|
|
my $invocant = shift; |
| 109 |
|
|
my $class = ref($invocant) || $invocant; |
| 110 |
|
|
#my @args = normalizeArgs(@_); |
| 111 |
joko |
1.7 |
|
| 112 |
cvsjoko |
1.1 |
my $arg_locator = shift; |
| 113 |
|
|
my $arg_options = shift; |
| 114 |
joko |
1.19 |
#my @args = @_; |
| 115 |
|
|
#@args ||= (); |
| 116 |
joko |
1.7 |
|
| 117 |
joko |
1.15 |
if (!$arg_locator) { |
| 118 |
|
|
$logger->critical( __PACKAGE__ . "->new: No locator passed in!" ); |
| 119 |
|
|
return; |
| 120 |
|
|
} |
| 121 |
joko |
1.19 |
|
| 122 |
|
|
#print Dumper($arg_locator); |
| 123 |
joko |
1.15 |
|
| 124 |
cvsjoko |
1.1 |
#my $self = { STORAGEHANDLE => undef, @_ }; |
| 125 |
|
|
my $self = { STORAGEHANDLE => undef, locator => $arg_locator, options => $arg_options }; |
| 126 |
joko |
1.7 |
#$logger->debug( __PACKAGE__ . "[$self->{locator}->{type}]" . "->new(@_)" ); |
| 127 |
joko |
1.19 |
$logger->debug( __PACKAGE__ . "[$arg_locator->{type}]" . "->new()" ); |
| 128 |
cvsjoko |
1.1 |
return bless $self, $class; |
| 129 |
|
|
} |
| 130 |
|
|
|
| 131 |
|
|
sub AUTOLOAD { |
| 132 |
|
|
|
| 133 |
joko |
1.2 |
# since this is a core function acting as dispatcher to $self->{STORAGEHANDLE}, |
| 134 |
|
|
# some sophisticated handling and filtering is needed to avoid things like |
| 135 |
|
|
# - Deep recursion on subroutine "Data::Storage::AUTOLOAD" |
| 136 |
|
|
# - Deep recursion on subroutine "Data::Storage::Handler::Abstract::AUTOLOAD" |
| 137 |
|
|
# - Deep recursion on anonymous subroutine at [...] |
| 138 |
joko |
1.8 |
# we also might filter log messages caused by logging to itself in "advanced logging of AUTOLOAD calls" |
| 139 |
joko |
1.2 |
|
| 140 |
cvsjoko |
1.1 |
my $self = shift; |
| 141 |
|
|
our $AUTOLOAD; |
| 142 |
|
|
|
| 143 |
|
|
# ->DESTROY would - if not declared - trigger an AUTOLOAD also |
| 144 |
|
|
return if $AUTOLOAD =~ m/::DESTROY$/; |
| 145 |
|
|
|
| 146 |
|
|
my $method = $AUTOLOAD; |
| 147 |
|
|
$method =~ s/^.*:://; |
| 148 |
|
|
|
| 149 |
joko |
1.5 |
# advanced logging of AUTOLOAD calls ... |
| 150 |
|
|
# ... nice but do it only when TRACING (TODO) is enabled |
| 151 |
|
|
my $logstring = ""; |
| 152 |
|
|
$logstring .= __PACKAGE__ . "[$self->{locator}->{type}]" . "->" . $method; |
| 153 |
|
|
#print "count: ", $#_, "\n"; |
| 154 |
|
|
#$logstring .= Dumper(@_) if ($#_ != -1); |
| 155 |
|
|
my $tabcount = int( (80 - length($logstring)) / 10 ); |
| 156 |
|
|
$logstring .= "\t" x $tabcount . "(AUTOLOAD)"; |
| 157 |
|
|
# TODO: only ok if logstring doesn't contain |
| 158 |
|
|
# e.g. "Data::Storage[Tangram]->insert(SystemEvent=HASH(0x5c0034c)) (AUTOLOAD)" |
| 159 |
joko |
1.8 |
# but that would be _way_ too specific as long as we don't have an abstract handler for this ;) |
| 160 |
joko |
1.16 |
if ($TRACELEVEL) { |
| 161 |
joko |
1.5 |
$logger->debug( $logstring ); |
| 162 |
joko |
1.7 |
#print join('; ', @_); |
| 163 |
joko |
1.5 |
} |
| 164 |
|
|
|
| 165 |
joko |
1.8 |
# filtering AUTOLOAD calls and first-time-touch of the actual storage impl |
| 166 |
cvsjoko |
1.1 |
if ($self->_filter_AUTOLOAD($method)) { |
| 167 |
joko |
1.17 |
#print "=== FILTER!", "\n"; |
| 168 |
|
|
$self->_accessStorageHandle(); |
| 169 |
joko |
1.16 |
if ($self->{STORAGEHANDLE}) { |
| 170 |
|
|
return $self->{STORAGEHANDLE}->$method(@_); |
| 171 |
|
|
} else { |
| 172 |
|
|
$logger->critical( __PACKAGE__ . "->AUTOLOAD: ERROR: " . $logstring ); |
| 173 |
|
|
return; |
| 174 |
|
|
} |
| 175 |
cvsjoko |
1.1 |
} |
| 176 |
|
|
|
| 177 |
|
|
} |
| 178 |
|
|
|
| 179 |
|
|
sub _filter_AUTOLOAD { |
| 180 |
|
|
my $self = shift; |
| 181 |
|
|
my $method = shift; |
| 182 |
|
|
if ($self->{options}->{protected}) { |
| 183 |
|
|
if ($method eq 'disconnect') { |
| 184 |
|
|
return; |
| 185 |
|
|
} |
| 186 |
|
|
} |
| 187 |
|
|
return 1; |
| 188 |
|
|
} |
| 189 |
|
|
|
| 190 |
|
|
|
| 191 |
joko |
1.17 |
sub _accessStorageHandle { |
| 192 |
cvsjoko |
1.1 |
my $self = shift; |
| 193 |
|
|
# TODO: to some tracelevel! |
| 194 |
joko |
1.17 |
#print "=========== _accessStorage", "\n"; |
| 195 |
joko |
1.5 |
if ($TRACELEVEL) { |
| 196 |
joko |
1.17 |
$logger->debug( __PACKAGE__ . "[$self->{locator}->{type}]" . "->_accessStorageHandle()" ); |
| 197 |
joko |
1.5 |
} |
| 198 |
cvsjoko |
1.1 |
if (!$self->{STORAGEHANDLE}) { |
| 199 |
joko |
1.16 |
return $self->_createStorageHandle(); |
| 200 |
|
|
} |
| 201 |
|
|
} |
| 202 |
|
|
|
| 203 |
|
|
sub _createStorageHandle1 { |
| 204 |
|
|
my $self = shift; |
| 205 |
|
|
my $type = $self->{locator}->{type}; |
| 206 |
|
|
$logger->debug( __PACKAGE__ . "[$type]" . "->_createStorageHandle()" ); |
| 207 |
|
|
|
| 208 |
|
|
my $pkg = "Data::Storage::Handler::" . $type . ""; |
| 209 |
|
|
|
| 210 |
|
|
# try to load perl module at runtime |
| 211 |
|
|
my $evalstr = "use $pkg;"; |
| 212 |
|
|
eval($evalstr); |
| 213 |
|
|
if ($@) { |
| 214 |
|
|
$logger->error( __PACKAGE__ . "[$type]" . "->_createStorageHandle(): $@" ); |
| 215 |
|
|
return; |
| 216 |
cvsjoko |
1.1 |
} |
| 217 |
joko |
1.16 |
|
| 218 |
|
|
# build up some additional arguments to pass on |
| 219 |
|
|
#my @args = %{$self->{locator}}; |
| 220 |
|
|
my @args = (); |
| 221 |
|
|
|
| 222 |
|
|
# - create new storage handle object |
| 223 |
|
|
# - propagate arguments to handler |
| 224 |
|
|
# - pass locator by reference to be able to store status- or meta-information in it |
| 225 |
|
|
$self->{STORAGEHANDLE} = $pkg->new( locator => $self->{locator}, @args ); |
| 226 |
|
|
|
| 227 |
cvsjoko |
1.1 |
} |
| 228 |
|
|
|
| 229 |
|
|
sub _createStorageHandle { |
| 230 |
|
|
my $self = shift; |
| 231 |
|
|
my $type = $self->{locator}->{type}; |
| 232 |
|
|
$logger->debug( __PACKAGE__ . "[$type]" . "->_createStorageHandle()" ); |
| 233 |
|
|
|
| 234 |
joko |
1.16 |
#print Dumper($self); |
| 235 |
|
|
#exit; |
| 236 |
|
|
|
| 237 |
cvsjoko |
1.1 |
my $pkg = "Data::Storage::Handler::" . $type . ""; |
| 238 |
|
|
|
| 239 |
joko |
1.7 |
# try to load perl module at runtime |
| 240 |
joko |
1.16 |
=pod |
| 241 |
joko |
1.7 |
my $evalstr = "use $pkg;"; |
| 242 |
|
|
eval($evalstr); |
| 243 |
|
|
if ($@) { |
| 244 |
|
|
$logger->error( __PACKAGE__ . "[$type]" . "->_createStorageHandle(): $@" ); |
| 245 |
|
|
return; |
| 246 |
cvsjoko |
1.1 |
} |
| 247 |
joko |
1.7 |
|
| 248 |
|
|
# build up some additional arguments to pass on |
| 249 |
|
|
#my @args = %{$self->{locator}}; |
| 250 |
|
|
my @args = (); |
| 251 |
|
|
|
| 252 |
joko |
1.8 |
# - create new storage handle object |
| 253 |
|
|
# - propagate arguments to handler |
| 254 |
|
|
# - pass locator by reference to be able to store status- or meta-information in it |
| 255 |
joko |
1.7 |
$self->{STORAGEHANDLE} = $pkg->new( locator => $self->{locator}, @args ); |
| 256 |
joko |
1.16 |
=cut |
| 257 |
|
|
|
| 258 |
|
|
$self->{STORAGEHANDLE} = DesignPattern::Object->fromPackage( $pkg, locator => $self->{locator} ); |
| 259 |
|
|
return 1; |
| 260 |
joko |
1.3 |
|
| 261 |
cvsjoko |
1.1 |
} |
| 262 |
|
|
|
| 263 |
|
|
sub addLogDispatchHandler { |
| 264 |
|
|
|
| 265 |
|
|
my $self = shift; |
| 266 |
|
|
my $name = shift; |
| 267 |
|
|
my $package = shift; |
| 268 |
joko |
1.3 |
my $logger1 = shift; |
| 269 |
cvsjoko |
1.1 |
my $objectCreator = shift; |
| 270 |
|
|
|
| 271 |
|
|
#$logger->add( Log::Dispatch::Tangram->new( name => $name, |
| 272 |
|
|
$logger->add( $package->new( name => $name, |
| 273 |
|
|
#min_level => 'debug', |
| 274 |
|
|
min_level => 'info', |
| 275 |
|
|
storage => $self, |
| 276 |
|
|
objectCreator => $objectCreator, |
| 277 |
|
|
fields => { |
| 278 |
|
|
message => 'usermsg', |
| 279 |
|
|
timestamp => 'stamp', |
| 280 |
|
|
level => 'level', |
| 281 |
|
|
name => 'code', |
| 282 |
|
|
}, |
| 283 |
|
|
filter_patterns => [ '->insert\(SystemEvent=' ], |
| 284 |
|
|
#filter_patterns => [ 'SystemEvent' ], |
| 285 |
|
|
|
| 286 |
|
|
#format => '[%d] [%p] %m%n', |
| 287 |
|
|
) ); |
| 288 |
|
|
|
| 289 |
|
|
} |
| 290 |
|
|
|
| 291 |
|
|
sub removeLogDispatchHandler { |
| 292 |
joko |
1.8 |
my $self = shift; |
| 293 |
|
|
my $name = shift; |
| 294 |
|
|
#my $logger = shift; |
| 295 |
|
|
$logger->remove($name); |
| 296 |
cvsjoko |
1.1 |
} |
| 297 |
|
|
|
| 298 |
|
|
|
| 299 |
joko |
1.4 |
1; |
| 300 |
|
|
__END__ |