9 |
## ------------------------------------------------------------------------ |
## ------------------------------------------------------------------------ |
10 |
## |
## |
11 |
## $Log$ |
## $Log$ |
12 |
|
## Revision 1.20 2003/06/25 22:51:51 joko |
13 |
|
## + sub get_locator_type & Co. |
14 |
|
## |
15 |
|
## Revision 1.19 2003/02/18 19:22:11 joko |
16 |
|
## + fixed logging |
17 |
|
## |
18 |
|
## Revision 1.18 2003/01/30 22:12:17 joko |
19 |
|
## - removed/refactored old code: ->Data::Storage::Handler::Tangram|DBI |
20 |
|
## |
21 |
|
## Revision 1.17 2003/01/30 21:42:22 joko |
22 |
|
## + minor update: renamed method |
23 |
|
## |
24 |
|
## Revision 1.16 2003/01/20 16:52:13 joko |
25 |
|
## + now using 'DesignPattern::Object' to create a new 'Data::Storage::Handler::Xyz' on demand - before we did this in a hand-rolled fashion |
26 |
|
## |
27 |
## Revision 1.15 2003/01/19 03:12:59 joko |
## Revision 1.15 2003/01/19 03:12:59 joko |
28 |
## + modified header |
## + modified header |
29 |
## - removed pod-documentation - now in 'Storage.pod' |
## - removed pod-documentation - now in 'Storage.pod' |
96 |
use strict; |
use strict; |
97 |
use warnings; |
use warnings; |
98 |
|
|
|
use Data::Storage::Locator; |
|
99 |
use Data::Dumper; |
use Data::Dumper; |
100 |
|
# FIXME: wipe out! |
|
# TODO: wipe out! |
|
101 |
use DBI; |
use DBI; |
102 |
|
|
103 |
|
use Data::Storage::Locator; |
104 |
|
use DesignPattern::Object; |
105 |
|
|
106 |
|
|
107 |
# TODO: actually implement level (integrate with Log::Dispatch) |
# TODO: actually implement level (integrate with Log::Dispatch) |
108 |
my $TRACELEVEL = 0; |
my $TRACELEVEL = 0; |
109 |
|
|
117 |
|
|
118 |
my $arg_locator = shift; |
my $arg_locator = shift; |
119 |
my $arg_options = shift; |
my $arg_options = shift; |
120 |
|
#my @args = @_; |
121 |
|
#@args ||= (); |
122 |
|
|
123 |
if (!$arg_locator) { |
if (!$arg_locator) { |
124 |
$logger->critical( __PACKAGE__ . "->new: No locator passed in!" ); |
$logger->critical( __PACKAGE__ . "->new: No locator passed in!" ); |
125 |
return; |
return; |
126 |
} |
} |
127 |
|
|
128 |
|
#print Dumper($arg_locator); |
129 |
|
|
130 |
#my $self = { STORAGEHANDLE => undef, @_ }; |
#my $self = { STORAGEHANDLE => undef, @_ }; |
131 |
my $self = { STORAGEHANDLE => undef, locator => $arg_locator, options => $arg_options }; |
my $self = { STORAGEHANDLE => undef, locator => $arg_locator, options => $arg_options }; |
132 |
#$logger->debug( __PACKAGE__ . "[$self->{locator}->{type}]" . "->new(@_)" ); |
#$logger->debug( __PACKAGE__ . "[$self->{locator}->{type}]" . "->new(@_)" ); |
133 |
$logger->debug( __PACKAGE__ . "[$arg_locator->{type}]" . "->new(@_)" ); |
$logger->debug( __PACKAGE__ . "[$arg_locator->{type}]" . "->new()" ); |
134 |
return bless $self, $class; |
return bless $self, $class; |
135 |
} |
} |
136 |
|
|
152 |
my $method = $AUTOLOAD; |
my $method = $AUTOLOAD; |
153 |
$method =~ s/^.*:://; |
$method =~ s/^.*:://; |
154 |
|
|
155 |
|
#print __PACKAGE__, "\n"; |
156 |
|
#print $method, "\n"; |
157 |
|
#print $self->{locator}, "\n"; |
158 |
|
|
159 |
|
my $locator_type = $self->get_locator_type(); |
160 |
|
|
161 |
# advanced logging of AUTOLOAD calls ... |
# advanced logging of AUTOLOAD calls ... |
162 |
# ... nice but do it only when TRACING (TODO) is enabled |
# ... nice but do it only when TRACING (TODO) is enabled |
|
if ($TRACELEVEL) { |
|
163 |
my $logstring = ""; |
my $logstring = ""; |
164 |
$logstring .= __PACKAGE__ . "[$self->{locator}->{type}]" . "->" . $method; |
$logstring .= __PACKAGE__ . "[$locator_type]" . "->" . $method; |
165 |
#print "count: ", $#_, "\n"; |
#print "count: ", $#_, "\n"; |
166 |
#$logstring .= Dumper(@_) if ($#_ != -1); |
#$logstring .= Dumper(@_) if ($#_ != -1); |
167 |
my $tabcount = int( (80 - length($logstring)) / 10 ); |
my $tabcount = int( (80 - length($logstring)) / 10 ); |
169 |
# TODO: only ok if logstring doesn't contain |
# TODO: only ok if logstring doesn't contain |
170 |
# e.g. "Data::Storage[Tangram]->insert(SystemEvent=HASH(0x5c0034c)) (AUTOLOAD)" |
# e.g. "Data::Storage[Tangram]->insert(SystemEvent=HASH(0x5c0034c)) (AUTOLOAD)" |
171 |
# but that would be _way_ too specific as long as we don't have an abstract handler for this ;) |
# but that would be _way_ too specific as long as we don't have an abstract handler for this ;) |
172 |
|
if ($TRACELEVEL) { |
173 |
$logger->debug( $logstring ); |
$logger->debug( $logstring ); |
174 |
#print join('; ', @_); |
#print join('; ', @_); |
175 |
} |
} |
176 |
|
|
177 |
# filtering AUTOLOAD calls and first-time-touch of the actual storage impl |
# filtering AUTOLOAD calls and first-time-touch of the actual storage impl |
178 |
if ($self->_filter_AUTOLOAD($method)) { |
if ($self->_filter_AUTOLOAD($method)) { |
179 |
#print "_accessStorage\n"; |
#print "=== FILTER!", "\n"; |
180 |
$self->_accessStorage(); |
$self->_accessStorageHandle(); |
181 |
return $self->{STORAGEHANDLE}->$method(@_); |
if ($self->{STORAGEHANDLE}) { |
182 |
|
return $self->{STORAGEHANDLE}->$method(@_); |
183 |
|
} else { |
184 |
|
my $msg = __PACKAGE__ . "->AUTOLOAD: ERROR: " . $logstring; |
185 |
|
$logger->critical( $msg ) if $logger; |
186 |
|
print STDERR $msg if not $logger; |
187 |
|
return; |
188 |
|
} |
189 |
} |
} |
190 |
|
|
191 |
} |
} |
202 |
} |
} |
203 |
|
|
204 |
|
|
205 |
sub normalizeArgs { |
sub _accessStorageHandle { |
|
my %args = @_; |
|
|
if (!$args{dsn} && $args{meta}{dsn}) { |
|
|
$args{dsn} = $args{meta}{dsn}; |
|
|
} |
|
|
my @result = %args; |
|
|
return @result; |
|
|
} |
|
|
|
|
|
sub _accessStorage { |
|
206 |
my $self = shift; |
my $self = shift; |
207 |
# TODO: to some tracelevel! |
# TODO: to some tracelevel! |
208 |
|
#print "=========== _accessStorage", "\n"; |
209 |
if ($TRACELEVEL) { |
if ($TRACELEVEL) { |
210 |
$logger->debug( __PACKAGE__ . "[$self->{locator}->{type}]" . "->_accessStorage()" ); |
$logger->debug( __PACKAGE__ . "[$self->{locator}->{type}]" . "->_accessStorageHandle()" ); |
211 |
} |
} |
212 |
if (!$self->{STORAGEHANDLE}) { |
if (!$self->{STORAGEHANDLE}) { |
213 |
$self->_createStorageHandle(); |
return $self->_createStorageHandle(); |
214 |
} |
} |
215 |
} |
} |
216 |
|
|
217 |
sub _createStorageHandle { |
sub _createStorageHandle1 { |
218 |
my $self = shift; |
my $self = shift; |
219 |
my $type = $self->{locator}->{type}; |
my $type = $self->{locator}->{type}; |
220 |
$logger->debug( __PACKAGE__ . "[$type]" . "->_createStorageHandle()" ); |
$logger->debug( __PACKAGE__ . "[$type]" . "->_createStorageHandle()" ); |
240 |
|
|
241 |
} |
} |
242 |
|
|
243 |
|
sub _createStorageHandle { |
244 |
|
my $self = shift; |
245 |
|
|
246 |
|
# 2003-06-18: protection against storagehandles w/o locators |
247 |
|
return if not defined $self->{locator}; |
248 |
|
|
249 |
|
my $type = $self->get_locator_type(); |
250 |
|
$logger->debug( __PACKAGE__ . "[$type]" . "->_createStorageHandle()" ); |
251 |
|
|
252 |
|
#print Dumper($self); |
253 |
|
#exit; |
254 |
|
|
255 |
|
my $pkg = "Data::Storage::Handler::" . $type . ""; |
256 |
|
|
257 |
|
# try to load perl module at runtime |
258 |
|
=pod |
259 |
|
my $evalstr = "use $pkg;"; |
260 |
|
eval($evalstr); |
261 |
|
if ($@) { |
262 |
|
$logger->error( __PACKAGE__ . "[$type]" . "->_createStorageHandle(): $@" ); |
263 |
|
return; |
264 |
|
} |
265 |
|
|
266 |
|
# build up some additional arguments to pass on |
267 |
|
#my @args = %{$self->{locator}}; |
268 |
|
my @args = (); |
269 |
|
|
270 |
|
# - create new storage handle object |
271 |
|
# - propagate arguments to handler |
272 |
|
# - pass locator by reference to be able to store status- or meta-information in it |
273 |
|
$self->{STORAGEHANDLE} = $pkg->new( locator => $self->{locator}, @args ); |
274 |
|
=cut |
275 |
|
|
276 |
|
$self->{STORAGEHANDLE} = DesignPattern::Object->fromPackage( $pkg, locator => $self->{locator} ); |
277 |
|
return 1; |
278 |
|
|
279 |
|
} |
280 |
|
|
281 |
sub addLogDispatchHandler { |
sub addLogDispatchHandler { |
282 |
|
|
283 |
my $self = shift; |
my $self = shift; |
313 |
$logger->remove($name); |
$logger->remove($name); |
314 |
} |
} |
315 |
|
|
316 |
sub getDbName { |
sub get_locator_type { |
|
my $self = shift; |
|
|
my $dsn = $self->{locator}->{dbi}->{dsn}; |
|
|
$dsn =~ m/database=(.+?);/; |
|
|
my $database_name = $1; |
|
|
return $database_name; |
|
|
} |
|
|
|
|
|
sub testAvailability { |
|
317 |
my $self = shift; |
my $self = shift; |
318 |
my $status = $self->testDsn(); |
my $locator_type = ''; |
319 |
$self->{locator}->{status}->{available} = $status; |
$locator_type = $self->{locator}->{type} if defined $self->{locator}; |
320 |
return $status; |
return $locator_type; |
|
} |
|
|
|
|
|
sub isConnected { |
|
|
my $self = shift; |
|
|
# TODO: REVIEW! |
|
|
return 1 if $self->{STORAGEHANDLE}; |
|
|
} |
|
|
|
|
|
sub testDsn { |
|
|
my $self = shift; |
|
|
my $dsn = $self->{locator}->{dbi}->{dsn}; |
|
|
my $result; |
|
|
if ( my $dbh = DBI->connect($dsn, '', '', { |
|
|
PrintError => 0, |
|
|
} ) ) { |
|
|
|
|
|
# TODO: REVIEW |
|
|
$dbh->disconnect(); |
|
|
|
|
|
return 1; |
|
|
} else { |
|
|
$logger->warning( __PACKAGE__ . "[$self->{locator}->{type}]" . "->testDsn(): " . "DBI-error: " . $DBI::errstr ); |
|
|
} |
|
321 |
} |
} |
322 |
|
|
323 |
1; |
1; |